@@ -9,6 +9,8 @@ public function setUp() {
99
1010 public function tearDown ()
1111 {
12+ Mockery::close ();
13+
1214 User::truncate ();
1315 Book::truncate ();
1416 Item::truncate ();
@@ -297,7 +299,15 @@ public function testEmbedsManySave()
297299 $ user = User::create (array ('name ' => 'John Doe ' ));
298300 $ address = new Address (array ('city ' => 'London ' ));
299301
302+ $ address ->setEventDispatcher ($ events = Mockery::mock ('Illuminate\Events\Dispatcher ' ));
303+ $ events ->shouldReceive ('until ' )->once ()->with ('eloquent.saving: ' .get_class ($ address ), $ address )->andReturn (true );
304+ $ events ->shouldReceive ('until ' )->once ()->with ('eloquent.creating: ' .get_class ($ address ), $ address )->andReturn (true );
305+ $ events ->shouldReceive ('fire ' )->once ()->with ('eloquent.created: ' .get_class ($ address ), $ address );
306+ $ events ->shouldReceive ('fire ' )->once ()->with ('eloquent.saved: ' .get_class ($ address ), $ address );
307+
300308 $ address = $ user ->addresses ()->save ($ address );
309+ $ address ->unsetEventDispatcher ();
310+
301311 $ this ->assertNotNull ($ user ->_addresses );
302312 $ this ->assertEquals (array ('London ' ), $ user ->addresses ->lists ('city ' ));
303313 $ this ->assertInstanceOf ('DateTime ' , $ address ->created_at );
@@ -309,8 +319,15 @@ public function testEmbedsManySave()
309319 $ user = User::find ($ user ->_id );
310320 $ this ->assertEquals (array ('London ' , 'Paris ' ), $ user ->addresses ->lists ('city ' ));
311321
322+ $ address ->setEventDispatcher ($ events = Mockery::mock ('Illuminate\Events\Dispatcher ' ));
323+ $ events ->shouldReceive ('until ' )->once ()->with ('eloquent.saving: ' .get_class ($ address ), $ address )->andReturn (true );
324+ $ events ->shouldReceive ('until ' )->once ()->with ('eloquent.updating: ' .get_class ($ address ), $ address )->andReturn (true );
325+ $ events ->shouldReceive ('fire ' )->once ()->with ('eloquent.updated: ' .get_class ($ address ), $ address );
326+ $ events ->shouldReceive ('fire ' )->once ()->with ('eloquent.saved: ' .get_class ($ address ), $ address );
327+
312328 $ address ->city = 'New York ' ;
313329 $ user ->addresses ()->save ($ address );
330+ $ address ->unsetEventDispatcher ();
314331
315332 $ this ->assertEquals (2 , count ($ user ->addresses ));
316333 $ this ->assertEquals (2 , count ($ user ->addresses ()->get ()));
@@ -403,9 +420,16 @@ public function testEmbedsManyDestroy()
403420 $ user ->addresses ()->saveMany (array (new Address (array ('city ' => 'London ' )), new Address (array ('city ' => 'Bristol ' )), new Address (array ('city ' => 'Bruxelles ' ))));
404421
405422 $ address = $ user ->addresses ->first ();
423+
424+ $ address ->setEventDispatcher ($ events = Mockery::mock ('Illuminate\Events\Dispatcher ' ));
425+ $ events ->shouldReceive ('until ' )->once ()->with ('eloquent.deleting: ' .get_class ($ address ), Mockery::mustBe ($ address ))->andReturn (true );
426+ $ events ->shouldReceive ('fire ' )->once ()->with ('eloquent.deleted: ' .get_class ($ address ), Mockery::mustBe ($ address ));
427+
406428 $ user ->addresses ()->destroy ($ address ->_id );
407429 $ this ->assertEquals (array ('Bristol ' , 'Bruxelles ' ), $ user ->addresses ->lists ('city ' ));
408430
431+ $ address ->unsetEventDispatcher ();
432+
409433 $ address = $ user ->addresses ->first ();
410434 $ user ->addresses ()->destroy ($ address );
411435 $ this ->assertEquals (array ('Bruxelles ' ), $ user ->addresses ->lists ('city ' ));
@@ -452,4 +476,62 @@ public function testEmbedsManyAliases()
452476 $ this ->assertEquals (array (), $ user ->addresses ->lists ('city ' ));
453477 }
454478
479+ public function testEmbedsManyCreatingEventReturnsFalse ()
480+ {
481+ $ user = User::create (array ('name ' => 'John Doe ' ));
482+ $ address = new Address (array ('city ' => 'London ' ));
483+
484+ $ address ->setEventDispatcher ($ events = Mockery::mock ('Illuminate\Events\Dispatcher ' ));
485+ $ events ->shouldReceive ('until ' )->once ()->with ('eloquent.saving: ' .get_class ($ address ), $ address )->andReturn (true );
486+ $ events ->shouldReceive ('until ' )->once ()->with ('eloquent.creating: ' .get_class ($ address ), $ address )->andReturn (false );
487+
488+ $ this ->assertFalse ($ user ->addresses ()->save ($ address ));
489+ $ address ->unsetEventDispatcher ();
490+ }
491+
492+ public function testEmbedsManySavingEventReturnsFalse ()
493+ {
494+ $ user = User::create (array ('name ' => 'John Doe ' ));
495+ $ address = new Address (array ('city ' => 'Paris ' ));
496+ $ address ->exists = true ;
497+
498+ $ address ->setEventDispatcher ($ events = Mockery::mock ('Illuminate\Events\Dispatcher ' ));
499+ $ events ->shouldReceive ('until ' )->once ()->with ('eloquent.saving: ' .get_class ($ address ), $ address )->andReturn (false );
500+
501+ $ this ->assertFalse ($ user ->addresses ()->save ($ address ));
502+ $ address ->unsetEventDispatcher ();
503+ }
504+
505+ public function testEmbedsManyUpdatingEventReturnsFalse ()
506+ {
507+ $ user = User::create (array ('name ' => 'John Doe ' ));
508+ $ address = new Address (array ('city ' => 'New York ' ));
509+ $ address ->exists = true ;
510+
511+ $ address ->setEventDispatcher ($ events = Mockery::mock ('Illuminate\Events\Dispatcher ' ));
512+ $ events ->shouldReceive ('until ' )->once ()->with ('eloquent.saving: ' .get_class ($ address ), $ address )->andReturn (true );
513+ $ events ->shouldReceive ('until ' )->once ()->with ('eloquent.updating: ' .get_class ($ address ), $ address )->andReturn (false );
514+
515+ $ address ->city = 'Warsaw ' ;
516+
517+ $ this ->assertFalse ($ user ->addresses ()->save ($ address ));
518+ $ address ->unsetEventDispatcher ();
519+ }
520+
521+ public function testEmbedsManyDeletingEventReturnsFalse ()
522+ {
523+ $ user = User::create (array ('name ' => 'John Doe ' ));
524+ $ user ->addresses ()->save (new Address (array ('city ' => 'New York ' )));
525+
526+ $ address = $ user ->addresses ->first ();
527+
528+ $ address ->setEventDispatcher ($ events = Mockery::mock ('Illuminate\Events\Dispatcher ' ));
529+ $ events ->shouldReceive ('until ' )->once ()->with ('eloquent.deleting: ' .get_class ($ address ), Mockery::mustBe ($ address ))->andReturn (false );
530+
531+ $ this ->assertEquals (0 , $ user ->addresses ()->destroy ($ address ));
532+ $ this ->assertEquals (array ('New York ' ), $ user ->addresses ->lists ('city ' ));
533+
534+ $ address ->unsetEventDispatcher ();
535+ }
536+
455537}
0 commit comments