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?
php testing phpunit laravel laravel-4
edi9999
source share