@@ -20,7 +20,15 @@ public function testEmbedsManySave()
20
20
$ user = User::create (['name ' => 'John Doe ' ]);
21
21
$ address = new Address (['city ' => 'London ' ]);
22
22
23
+ $ address ->setEventDispatcher ($ events = Mockery::mock ('Illuminate\Events\Dispatcher ' ));
24
+ $ events ->shouldReceive ('fire ' )->with ('eloquent.retrieved: ' . get_class ($ address ), anything ());
25
+ $ events ->shouldReceive ('until ' )->once ()->with ('eloquent.saving: ' . get_class ($ address ), $ address )->andReturn (true );
26
+ $ events ->shouldReceive ('until ' )->once ()->with ('eloquent.creating: ' . get_class ($ address ), $ address )->andReturn (true );
27
+ $ events ->shouldReceive ('fire ' )->once ()->with ('eloquent.created: ' . get_class ($ address ), $ address );
28
+ $ events ->shouldReceive ('fire ' )->once ()->with ('eloquent.saved: ' . get_class ($ address ), $ address );
29
+
23
30
$ address = $ user ->addresses ()->save ($ address );
31
+ $ address ->unsetEventDispatcher ();
24
32
25
33
$ this ->assertNotNull ($ user ->addresses );
26
34
$ this ->assertInstanceOf ('Illuminate\Database\Eloquent\Collection ' , $ user ->addresses );
@@ -38,8 +46,16 @@ public function testEmbedsManySave()
38
46
$ user = User::find ($ user ->_id );
39
47
$ this ->assertEquals (['London ' , 'Paris ' ], $ user ->addresses ->pluck ('city ' )->all ());
40
48
49
+ $ address ->setEventDispatcher ($ events = Mockery::mock ('Illuminate\Events\Dispatcher ' ));
50
+ $ events ->shouldReceive ('fire ' )->with ('eloquent.retrieved: ' . get_class ($ address ), anything ());
51
+ $ events ->shouldReceive ('until ' )->once ()->with ('eloquent.saving: ' . get_class ($ address ), $ address )->andReturn (true );
52
+ $ events ->shouldReceive ('until ' )->once ()->with ('eloquent.updating: ' . get_class ($ address ), $ address )->andReturn (true );
53
+ $ events ->shouldReceive ('fire ' )->once ()->with ('eloquent.updated: ' . get_class ($ address ), $ address );
54
+ $ events ->shouldReceive ('fire ' )->once ()->with ('eloquent.saved: ' . get_class ($ address ), $ address );
55
+
41
56
$ address ->city = 'New York ' ;
42
57
$ user ->addresses ()->save ($ address );
58
+ $ address ->unsetEventDispatcher ();
43
59
44
60
$ this ->assertEquals (2 , count ($ user ->addresses ));
45
61
$ this ->assertEquals (2 , count ($ user ->addresses ()->get ()));
@@ -196,9 +212,16 @@ public function testEmbedsManyDestroy()
196
212
197
213
$ address = $ user ->addresses ->first ();
198
214
215
+ $ address ->setEventDispatcher ($ events = Mockery::mock ('Illuminate\Events\Dispatcher ' ));
216
+ $ events ->shouldReceive ('fire ' )->with ('eloquent.retrieved: ' . get_class ($ address ), anything ());
217
+ $ events ->shouldReceive ('until ' )->once ()->with ('eloquent.deleting: ' . get_class ($ address ), Mockery::type ('Address ' ))->andReturn (true );
218
+ $ events ->shouldReceive ('fire ' )->once ()->with ('eloquent.deleted: ' . get_class ($ address ), Mockery::type ('Address ' ));
219
+
199
220
$ user ->addresses ()->destroy ($ address ->_id );
200
221
$ this ->assertEquals (['Bristol ' , 'Bruxelles ' ], $ user ->addresses ->pluck ('city ' )->all ());
201
222
223
+ $ address ->unsetEventDispatcher ();
224
+
202
225
$ address = $ user ->addresses ->first ();
203
226
$ user ->addresses ()->destroy ($ address );
204
227
$ this ->assertEquals (['Bruxelles ' ], $ user ->addresses ->pluck ('city ' )->all ());
@@ -228,11 +251,18 @@ public function testEmbedsManyDelete()
228
251
229
252
$ address = $ user ->addresses ->first ();
230
253
254
+ $ address ->setEventDispatcher ($ events = Mockery::mock ('Illuminate\Events\Dispatcher ' ));
255
+ $ events ->shouldReceive ('fire ' )->with ('eloquent.retrieved: ' . get_class ($ address ), anything ());
256
+ $ events ->shouldReceive ('until ' )->once ()->with ('eloquent.deleting: ' . get_class ($ address ), Mockery::type ('Address ' ))->andReturn (true );
257
+ $ events ->shouldReceive ('fire ' )->once ()->with ('eloquent.deleted: ' . get_class ($ address ), Mockery::type ('Address ' ));
258
+
231
259
$ address ->delete ();
232
260
233
261
$ this ->assertEquals (2 , $ user ->addresses ()->count ());
234
262
$ this ->assertEquals (2 , $ user ->addresses ->count ());
235
263
264
+ $ address ->unsetEventDispatcher ();
265
+
236
266
$ address = $ user ->addresses ->first ();
237
267
$ address ->delete ();
238
268
@@ -270,11 +300,13 @@ public function testEmbedsManyCreatingEventReturnsFalse()
270
300
$ user = User::create (['name ' => 'John Doe ' ]);
271
301
$ address = new Address (['city ' => 'London ' ]);
272
302
273
- $ address ::creating (function () {
274
- return false ;
275
- });
303
+ $ address ->setEventDispatcher ($ events = Mockery::mock ('Illuminate\Events\Dispatcher ' ));
304
+ $ events ->shouldReceive ('fire ' )->with ('eloquent.retrieved: ' . get_class ($ address ), anything ());
305
+ $ events ->shouldReceive ('until ' )->once ()->with ('eloquent.saving: ' . get_class ($ address ), $ address )->andReturn (true );
306
+ $ events ->shouldReceive ('until ' )->once ()->with ('eloquent.creating: ' . get_class ($ address ), $ address )->andReturn (false );
276
307
277
308
$ this ->assertFalse ($ user ->addresses ()->save ($ address ));
309
+ $ address ->unsetEventDispatcher ();
278
310
}
279
311
280
312
public function testEmbedsManySavingEventReturnsFalse ()
@@ -283,11 +315,12 @@ public function testEmbedsManySavingEventReturnsFalse()
283
315
$ address = new Address (['city ' => 'Paris ' ]);
284
316
$ address ->exists = true ;
285
317
286
- $ address:: saving ( function () {
287
- return false ;
288
- } );
318
+ $ address-> setEventDispatcher ( $ events = Mockery:: mock ( ' Illuminate\Events\Dispatcher ' ));
319
+ $ events -> shouldReceive ( ' fire ' )-> with ( ' eloquent.retrieved: ' . get_class ( $ address ), anything ()) ;
320
+ $ events -> shouldReceive ( ' until ' )-> once ()-> with ( ' eloquent.saving: ' . get_class ( $ address ), $ address )-> andReturn ( false );
289
321
290
322
$ this ->assertFalse ($ user ->addresses ()->save ($ address ));
323
+ $ address ->unsetEventDispatcher ();
291
324
}
292
325
293
326
public function testEmbedsManyUpdatingEventReturnsFalse ()
@@ -296,13 +329,15 @@ public function testEmbedsManyUpdatingEventReturnsFalse()
296
329
$ address = new Address (['city ' => 'New York ' ]);
297
330
$ user ->addresses ()->save ($ address );
298
331
299
- $ address ::updating (function () {
300
- return false ;
301
- });
332
+ $ address ->setEventDispatcher ($ events = Mockery::mock ('Illuminate\Events\Dispatcher ' ));
333
+ $ events ->shouldReceive ('fire ' )->with ('eloquent.retrieved: ' . get_class ($ address ), anything ());
334
+ $ events ->shouldReceive ('until ' )->once ()->with ('eloquent.saving: ' . get_class ($ address ), $ address )->andReturn (true );
335
+ $ events ->shouldReceive ('until ' )->once ()->with ('eloquent.updating: ' . get_class ($ address ), $ address )->andReturn (false );
302
336
303
337
$ address ->city = 'Warsaw ' ;
304
338
305
339
$ this ->assertFalse ($ user ->addresses ()->save ($ address ));
340
+ $ address ->unsetEventDispatcher ();
306
341
}
307
342
308
343
public function testEmbedsManyDeletingEventReturnsFalse ()
@@ -312,12 +347,14 @@ public function testEmbedsManyDeletingEventReturnsFalse()
312
347
313
348
$ address = $ user ->addresses ->first ();
314
349
315
- $ address:: deleting ( function () {
316
- return false ;
317
- } );
350
+ $ address-> setEventDispatcher ( $ events = Mockery:: mock ( ' Illuminate\Events\Dispatcher ' ));
351
+ $ events -> shouldReceive ( ' fire ' )-> with ( ' eloquent.retrieved: ' . get_class ( $ address ), anything ()) ;
352
+ $ events -> shouldReceive ( ' until ' )-> once ()-> with ( ' eloquent.deleting: ' . get_class ( $ address ), Mockery:: mustBe ( $ address ))-> andReturn ( false );
318
353
319
354
$ this ->assertEquals (0 , $ user ->addresses ()->destroy ($ address ));
320
355
$ this ->assertEquals (['New York ' ], $ user ->addresses ->pluck ('city ' )->all ());
356
+
357
+ $ address ->unsetEventDispatcher ();
321
358
}
322
359
323
360
public function testEmbedsManyFindOrContains ()
@@ -414,7 +451,15 @@ public function testEmbedsOne()
414
451
$ user = User::create (['name ' => 'John Doe ' ]);
415
452
$ father = new User (['name ' => 'Mark Doe ' ]);
416
453
454
+ $ father ->setEventDispatcher ($ events = Mockery::mock ('Illuminate\Events\Dispatcher ' ));
455
+ $ events ->shouldReceive ('fire ' )->with ('eloquent.retrieved: ' . get_class ($ father ), anything ());
456
+ $ events ->shouldReceive ('until ' )->once ()->with ('eloquent.saving: ' . get_class ($ father ), $ father )->andReturn (true );
457
+ $ events ->shouldReceive ('until ' )->once ()->with ('eloquent.creating: ' . get_class ($ father ), $ father )->andReturn (true );
458
+ $ events ->shouldReceive ('fire ' )->once ()->with ('eloquent.created: ' . get_class ($ father ), $ father );
459
+ $ events ->shouldReceive ('fire ' )->once ()->with ('eloquent.saved: ' . get_class ($ father ), $ father );
460
+
417
461
$ father = $ user ->father ()->save ($ father );
462
+ $ father ->unsetEventDispatcher ();
418
463
419
464
$ this ->assertNotNull ($ user ->father );
420
465
$ this ->assertEquals ('Mark Doe ' , $ user ->father ->name );
@@ -426,15 +471,31 @@ public function testEmbedsOne()
426
471
$ raw = $ father ->getAttributes ();
427
472
$ this ->assertInstanceOf ('MongoDB\BSON\ObjectID ' , $ raw ['_id ' ]);
428
473
474
+ $ father ->setEventDispatcher ($ events = Mockery::mock ('Illuminate\Events\Dispatcher ' ));
475
+ $ events ->shouldReceive ('fire ' )->with ('eloquent.retrieved: ' . get_class ($ father ), anything ());
476
+ $ events ->shouldReceive ('until ' )->once ()->with ('eloquent.saving: ' . get_class ($ father ), $ father )->andReturn (true );
477
+ $ events ->shouldReceive ('until ' )->once ()->with ('eloquent.updating: ' . get_class ($ father ), $ father )->andReturn (true );
478
+ $ events ->shouldReceive ('fire ' )->once ()->with ('eloquent.updated: ' . get_class ($ father ), $ father );
479
+ $ events ->shouldReceive ('fire ' )->once ()->with ('eloquent.saved: ' . get_class ($ father ), $ father );
480
+
429
481
$ father ->name = 'Tom Doe ' ;
430
482
$ user ->father ()->save ($ father );
483
+ $ father ->unsetEventDispatcher ();
431
484
432
485
$ this ->assertNotNull ($ user ->father );
433
486
$ this ->assertEquals ('Tom Doe ' , $ user ->father ->name );
434
487
435
488
$ father = new User (['name ' => 'Jim Doe ' ]);
436
489
490
+ $ father ->setEventDispatcher ($ events = Mockery::mock ('Illuminate\Events\Dispatcher ' ));
491
+ $ events ->shouldReceive ('fire ' )->with ('eloquent.retrieved: ' . get_class ($ father ), anything ());
492
+ $ events ->shouldReceive ('until ' )->once ()->with ('eloquent.saving: ' . get_class ($ father ), $ father )->andReturn (true );
493
+ $ events ->shouldReceive ('until ' )->once ()->with ('eloquent.creating: ' . get_class ($ father ), $ father )->andReturn (true );
494
+ $ events ->shouldReceive ('fire ' )->once ()->with ('eloquent.created: ' . get_class ($ father ), $ father );
495
+ $ events ->shouldReceive ('fire ' )->once ()->with ('eloquent.saved: ' . get_class ($ father ), $ father );
496
+
437
497
$ father = $ user ->father ()->save ($ father );
498
+ $ father ->unsetEventDispatcher ();
438
499
439
500
$ this ->assertNotNull ($ user ->father );
440
501
$ this ->assertEquals ('Jim Doe ' , $ user ->father ->name );
@@ -445,7 +506,12 @@ public function testEmbedsOneAssociate()
445
506
$ user = User::create (['name ' => 'John Doe ' ]);
446
507
$ father = new User (['name ' => 'Mark Doe ' ]);
447
508
509
+ $ father ->setEventDispatcher ($ events = Mockery::mock ('Illuminate\Events\Dispatcher ' ));
510
+ $ events ->shouldReceive ('fire ' )->with ('eloquent.retrieved: ' . get_class ($ father ), anything ());
511
+ $ events ->shouldReceive ('until ' )->times (0 )->with ('eloquent.saving: ' . get_class ($ father ), $ father );
512
+
448
513
$ father = $ user ->father ()->associate ($ father );
514
+ $ father ->unsetEventDispatcher ();
449
515
450
516
$ this ->assertNotNull ($ user ->father );
451
517
$ this ->assertEquals ('Mark Doe ' , $ user ->father ->name );
0 commit comments