mardi 30 mai 2017

Laravel5, PHPUnit and Date Mutators: can't use mutator in data provider

I've just noticed that we can't use date mutators in a test data provider. Example

class FooTest extends TestCase {

    /**
     * @dataProvider baz
     */
    public function testBar($baz) {
        $this->assertTrue(true);
    }

    public function baz() {
        $model = new Foo(['someDate' => \Carbon\Carbon::now());
        return [
            [$model]
        ];
    }
}

Given your model like this

class Foo extends Model {

    public function getDates() {
        return ['someDate'];
    }

}

That test will produce the following error

PHP Fatal error:  Call to a member function connection() on null in /var/www/agents/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php on line 3224

That's because in eloquent Model we have

protected function getDateFormat()
{
    return $this->dateFormat ?: $this->getConnection()->getQueryGrammar()->getDateFormat();
}

which is trying to access the connection that is not established in the test data provider.

So, how should we provide that testBar() method an input model?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2qw3oJi
via IFTTT

Aucun commentaire:

Enregistrer un commentaire