Junk loading has some unexpected side effects for model / loading events in Laravel

I am trying to create some tests.

Here is my test class:

class ExampleTest extends TestCase { public function setUp() { parent::setUp(); Artisan::call('migrate'); $this->seed(); Auth::loginUsingId(1); } public function testActionUpdateNew() { $action = new Action(Array()); $action->save(); var_dump($action->id); Action::with('reponses','contact','user','etudiant','entreprise','etude')->findOrFail($action->id); } public function testEtudes() { $etudes=Etude::all()->toArray(); $this->assertCount(10, $etudes, "Nombre d'Γ©tudes incorrectes"); $numEtudes=count($etudes); //Buggy part $etude= Etude::create(Array()); var_dump($etude->id); $etudes=Etude::all()->toArray(); $this->assertCount(11, $etudes, "Nombre d'Γ©tudes incorrectes"); //10+1 should equal to 11 but it hasnt updated } } 

The test that fails is the second: I count the number of eloquent sketches of objects that initially have a value of 10, then add one sketch to the database (using Etude :: create ()), the object is created because $ etude-> id gives real number. However, the number of Etudes was not updated.

The problem disappears when I remove the "study" from impatient loading in Action :: with ("reponses", ...)

Here is the relation of sketches in the Action class:

 public function etude() { return $this->belongsTo('Etude'); } 

Do you have any ideas if you might experience such strange behavior like loading in laravel and how to fix it?

EDIT

I found out that a call with ("sketch") had an action to delete events recorded in the Eloquent Model:

boot Etude method:

 public static function boot() { parent::boot(); static::creating(function($etude) { var_dump("creating etude"); //This doesn't get executed even when I run Etude::create(Array()); } ); } 

So, if I add Etude :: boot () at the beginning of testEtudes, it works again. This is still weird.

Does a loaded download really affect events or a loading method? Or is the boot method not called automatically after each test?

+8
php testing phpunit laravel laravel-4
source share
1 answer

In Laravel tests, the event dispatcher is reset between each test, but the models are still only loaded once, since they live a rather independent life. This means that between each test, model listeners are erased, but never re-registered. The solution is to not use boot() to register model events, but rather, in a separate file, either the service provider or the file included in app / start / global.php (the application / events.php is shared).

+1
source share

All Articles