laracasts / TestDummy

Easy factories for PHP integration testing.
https://laracasts.com/lessons/whats-new-in-testdummy
MIT License
457 stars 80 forks source link

Call to undefined method Closure::format() #81

Open mikeritter opened 9 years ago

mikeritter commented 9 years ago

'date' => $faker->dateTimeBetween('-1 days','+1 months')->format('Y-m-d')

throws

Call to undefined method Closure::format()

gbrock commented 9 years ago

I ran into this too. The problem lies in the fact that the way you're declaring the variable TestDummy uses to generate is not the data itself, but rather a quick means to tell TestDummy how it should generate the data.

In order to manipulate each date directly, you need to use a closure. From the docs (example modified):

Alternatively, you may pass a closure as the second argument to the $factory method. This can be useful for situations where you need a bit more control over the values that you assign to each attribute. Here's an example:

$factory('App\MyModel', function($faker, $overrides) {
    $date = $faker->dateTimeBetween('-1 days','+1 months')->format('Y-m-d');

    return [
        'date' => $date
    ];
});
swekaj commented 9 years ago

Note, the closure doesn't accept an $overrides argument. It only accepts the $faker instance.

mikeritter commented 9 years ago

Thanks for that @gbrock . This produces a date that's the same in all entries.

gbrock commented 9 years ago

There seems to be a bug in defining closures introduced recently, covered in this issue. You could load the last working version in your composer.json:

        "laracasts/testdummy": "2.1",

...which works for the time being (thanks @h-collector).

alariva commented 8 years ago

I had the same problem and https://github.com/laracasts/TestDummy/issues/86 helped out.

Example here