Skip to content

Commit a411bfa

Browse files
committed
Return event dispatcher to tests and add 'retrieved' handling
1 parent 64a2766 commit a411bfa

File tree

1 file changed

+78
-12
lines changed

1 file changed

+78
-12
lines changed

tests/EmbeddedRelationsTest.php

+78-12
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,15 @@ public function testEmbedsManySave()
2020
$user = User::create(['name' => 'John Doe']);
2121
$address = new Address(['city' => 'London']);
2222

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+
2330
$address = $user->addresses()->save($address);
31+
$address->unsetEventDispatcher();
2432

2533
$this->assertNotNull($user->addresses);
2634
$this->assertInstanceOf('Illuminate\Database\Eloquent\Collection', $user->addresses);
@@ -38,8 +46,16 @@ public function testEmbedsManySave()
3846
$user = User::find($user->_id);
3947
$this->assertEquals(['London', 'Paris'], $user->addresses->pluck('city')->all());
4048

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+
4156
$address->city = 'New York';
4257
$user->addresses()->save($address);
58+
$address->unsetEventDispatcher();
4359

4460
$this->assertEquals(2, count($user->addresses));
4561
$this->assertEquals(2, count($user->addresses()->get()));
@@ -196,9 +212,16 @@ public function testEmbedsManyDestroy()
196212

197213
$address = $user->addresses->first();
198214

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+
199220
$user->addresses()->destroy($address->_id);
200221
$this->assertEquals(['Bristol', 'Bruxelles'], $user->addresses->pluck('city')->all());
201222

223+
$address->unsetEventDispatcher();
224+
202225
$address = $user->addresses->first();
203226
$user->addresses()->destroy($address);
204227
$this->assertEquals(['Bruxelles'], $user->addresses->pluck('city')->all());
@@ -228,11 +251,18 @@ public function testEmbedsManyDelete()
228251

229252
$address = $user->addresses->first();
230253

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+
231259
$address->delete();
232260

233261
$this->assertEquals(2, $user->addresses()->count());
234262
$this->assertEquals(2, $user->addresses->count());
235263

264+
$address->unsetEventDispatcher();
265+
236266
$address = $user->addresses->first();
237267
$address->delete();
238268

@@ -270,11 +300,13 @@ public function testEmbedsManyCreatingEventReturnsFalse()
270300
$user = User::create(['name' => 'John Doe']);
271301
$address = new Address(['city' => 'London']);
272302

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);
276307

277308
$this->assertFalse($user->addresses()->save($address));
309+
$address->unsetEventDispatcher();
278310
}
279311

280312
public function testEmbedsManySavingEventReturnsFalse()
@@ -283,11 +315,12 @@ public function testEmbedsManySavingEventReturnsFalse()
283315
$address = new Address(['city' => 'Paris']);
284316
$address->exists = true;
285317

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);
289321

290322
$this->assertFalse($user->addresses()->save($address));
323+
$address->unsetEventDispatcher();
291324
}
292325

293326
public function testEmbedsManyUpdatingEventReturnsFalse()
@@ -296,13 +329,15 @@ public function testEmbedsManyUpdatingEventReturnsFalse()
296329
$address = new Address(['city' => 'New York']);
297330
$user->addresses()->save($address);
298331

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);
302336

303337
$address->city = 'Warsaw';
304338

305339
$this->assertFalse($user->addresses()->save($address));
340+
$address->unsetEventDispatcher();
306341
}
307342

308343
public function testEmbedsManyDeletingEventReturnsFalse()
@@ -312,12 +347,14 @@ public function testEmbedsManyDeletingEventReturnsFalse()
312347

313348
$address = $user->addresses->first();
314349

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);
318353

319354
$this->assertEquals(0, $user->addresses()->destroy($address));
320355
$this->assertEquals(['New York'], $user->addresses->pluck('city')->all());
356+
357+
$address->unsetEventDispatcher();
321358
}
322359

323360
public function testEmbedsManyFindOrContains()
@@ -414,7 +451,15 @@ public function testEmbedsOne()
414451
$user = User::create(['name' => 'John Doe']);
415452
$father = new User(['name' => 'Mark Doe']);
416453

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+
417461
$father = $user->father()->save($father);
462+
$father->unsetEventDispatcher();
418463

419464
$this->assertNotNull($user->father);
420465
$this->assertEquals('Mark Doe', $user->father->name);
@@ -426,15 +471,31 @@ public function testEmbedsOne()
426471
$raw = $father->getAttributes();
427472
$this->assertInstanceOf('MongoDB\BSON\ObjectID', $raw['_id']);
428473

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+
429481
$father->name = 'Tom Doe';
430482
$user->father()->save($father);
483+
$father->unsetEventDispatcher();
431484

432485
$this->assertNotNull($user->father);
433486
$this->assertEquals('Tom Doe', $user->father->name);
434487

435488
$father = new User(['name' => 'Jim Doe']);
436489

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+
437497
$father = $user->father()->save($father);
498+
$father->unsetEventDispatcher();
438499

439500
$this->assertNotNull($user->father);
440501
$this->assertEquals('Jim Doe', $user->father->name);
@@ -445,7 +506,12 @@ public function testEmbedsOneAssociate()
445506
$user = User::create(['name' => 'John Doe']);
446507
$father = new User(['name' => 'Mark Doe']);
447508

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+
448513
$father = $user->father()->associate($father);
514+
$father->unsetEventDispatcher();
449515

450516
$this->assertNotNull($user->father);
451517
$this->assertEquals('Mark Doe', $user->father->name);

0 commit comments

Comments
 (0)