-
Notifications
You must be signed in to change notification settings - Fork 42
/
AggregateRepositoryTest.php
711 lines (557 loc) · 19.5 KB
/
AggregateRepositoryTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
<?php
/**
* This file is part of prooph/event-sourcing.
* (c) 2014-2021 Alexander Miertsch <kontakt@codeliner.ws>
* (c) 2015-2021 Sascha-Oliver Prolic <saschaprolic@googlemail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace ProophTest\EventSourcing\Aggregate;
use ArrayIterator;
use DateTimeImmutable;
use DateTimeZone;
use EmptyIterator;
use Prooph\Common\Event\ActionEvent;
use Prooph\EventSourcing\Aggregate\AggregateRepository;
use Prooph\EventSourcing\Aggregate\AggregateType;
use Prooph\EventSourcing\Aggregate\Exception\AggregateTypeException;
use Prooph\EventSourcing\EventStoreIntegration\AggregateTranslator;
use Prooph\EventStore\ActionEventEmitterEventStore;
use Prooph\EventStore\EventStore;
use Prooph\EventStore\Stream;
use Prooph\EventStore\StreamName;
use Prooph\SnapshotStore\InMemorySnapshotStore;
use Prooph\SnapshotStore\Snapshot;
use Prooph\SnapshotStore\SnapshotStore;
use ProophTest\EventSourcing\Mock\User;
use ProophTest\EventSourcing\Mock\UserCreated;
use ProophTest\EventSourcing\Mock\UsernameChanged;
use ProophTest\EventStore\ActionEventEmitterEventStoreTestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use ReflectionClass;
class AggregateRepositoryTest extends ActionEventEmitterEventStoreTestCase
{
use ProphecyTrait;
/**
* @var AggregateRepository
*/
private $repository;
/**
* @var SnapshotStore
*/
private $snapshotStore;
protected function setUp(): void
{
parent::setUp();
$this->repository = new AggregateRepository(
$this->eventStore,
AggregateType::fromAggregateRootClass(User::class),
new AggregateTranslator()
);
$this->eventStore->create(new Stream(new StreamName('event_stream'), new ArrayIterator()));
}
/**
* @test
*/
public function it_adds_a_new_aggregate(): void
{
$user = User::nameNew('John Doe');
$this->repository->saveAggregateRoot($user);
$fetchedUser = $this->repository->getAggregateRoot(
$user->id()
);
$this->assertInstanceOf(User::class, $fetchedUser);
$this->assertNotSame($user, $fetchedUser);
$this->assertEquals('John Doe', $fetchedUser->name());
}
/**
* @test
*/
public function it_ignores_identity_map_if_disabled(): void
{
$this->repository = new AggregateRepository(
$this->eventStore,
AggregateType::fromAggregateRootClass(User::class),
new AggregateTranslator(),
null,
null,
false,
true
);
$user = User::nameNew('John Doe');
$this->repository->saveAggregateRoot($user);
$fetchedUser = $this->repository->getAggregateRoot(
$user->id()
);
$fetchedUser2 = $this->repository->getAggregateRoot(
$user->id()
);
$this->assertNotSame($fetchedUser, $fetchedUser2);
}
/**
* @test
*/
public function it_removes_aggregate_from_identity_map_when_save_is_called(): void
{
$user = User::nameNew('John Doe');
$this->repository->saveAggregateRoot($user);
$fetchedUser = $this->repository->getAggregateRoot(
$user->id()
);
$this->assertNotSame($user, $fetchedUser);
$fetchedUser2 = $this->repository->getAggregateRoot(
$user->id()
);
$this->assertSame($fetchedUser, $fetchedUser2);
$fetchedUser->changeName('Max Mustermann');
$this->repository->saveAggregateRoot($fetchedUser);
$fetchedUser2 = $this->repository->getAggregateRoot(
$user->id()
);
$this->assertNotSame($fetchedUser, $fetchedUser2);
$this->assertEquals('Max Mustermann', $fetchedUser2->name());
}
/**
* @test
* Test for https://github.com/prooph/event-store/issues/99
*/
public function it_does_not_interfere_with_other_aggregate_roots_in_pending_events_index(): void
{
$user = User::nameNew('John Doe');
$this->repository->saveAggregateRoot($user);
$user2 = User::nameNew('Max Mustermann');
$this->repository->saveAggregateRoot($user2);
//Fetch users from repository to simulate a normal program flow
$user = $this->repository->getAggregateRoot($user->id());
$user2 = $this->repository->getAggregateRoot($user2->id());
$user->changeName('Daniel Doe');
$user2->changeName('Jens Mustermann');
$fetchedUser1 = $this->repository->getAggregateRoot(
$user->id()
);
$fetchedUser2 = $this->repository->getAggregateRoot(
$user2->id()
);
$this->assertEquals('Daniel Doe', $fetchedUser1->name());
$this->assertEquals('Jens Mustermann', $fetchedUser2->name());
}
/**
* @test
*/
public function it_asserts_correct_aggregate_type(): void
{
$this->expectException(AggregateTypeException::class);
$this->expectExceptionMessage('Aggregate root must be an object but type of string given');
$this->repository->saveAggregateRoot('invalid');
}
/**
* @test
*/
public function it_returns_early_on_get_aggregate_root_when_there_are_no_stream_events(): void
{
$this->assertNull($this->repository->getAggregateRoot('something'));
}
/**
* @test
*/
public function it_loads_the_entire_stream_if_one_stream_per_aggregate_is_enabled(): void
{
$eventStore = $this->prophesize(ActionEventEmitterEventStore::class);
$eventStore
->load(
Argument::that(function (StreamName $streamName) {
return $streamName->toString() === User::class . '-123';
}),
1,
null,
null
)->willReturn(new EmptyIterator());
$repository = new AggregateRepository(
$eventStore->reveal(),
AggregateType::fromAggregateRootClass(User::class),
new AggregateTranslator(),
null,
null,
true
);
$this->assertNull($repository->getAggregateRoot('123'));
}
/**
* @test
*/
public function it_uses_snapshot_store(): void
{
$this->prepareSnapshotStoreAggregateRepository();
$user = User::nameNew('John Doe');
$this->repository->saveAggregateRoot($user);
$now = new DateTimeImmutable('now', new DateTimeZone('UTC'));
$snapshot = new Snapshot(
User::class,
$user->id(),
$user,
1,
$now
);
// short getter assertion
$this->assertSame($now, $snapshot->createdAt());
$this->snapshotStore->save($snapshot);
$loadedEvents = [];
$this->eventStore->attach(
'load',
function (ActionEvent $event) use (&$loadedEvents) {
foreach ($event->getParam('streamEvents', []) as $streamEvent) {
$loadedEvents[] = $streamEvent;
}
},
-1000
);
$this->repository->getAggregateRoot(
$user->id()
);
$this->assertEmpty($loadedEvents);
}
/**
* @test
*/
public function it_uses_snapshot_store_with_one_stream_per_aggregate(): void
{
$this->snapshotStore = new InMemorySnapshotStore();
$this->repository = new AggregateRepository(
$this->eventStore,
AggregateType::fromAggregateRootClass(User::class),
new AggregateTranslator(),
$this->snapshotStore,
null,
true
);
$user = User::nameNew('John Doe');
$this->repository->saveAggregateRoot($user);
$now = new DateTimeImmutable('now', new DateTimeZone('UTC'));
$snapshot = new Snapshot(
User::class,
$user->id(),
$user,
1,
$now
);
// short getter assertion
$this->assertSame($now, $snapshot->createdAt());
$this->snapshotStore->save($snapshot);
$loadedEvents = [];
$this->eventStore->attach(
'load',
function (ActionEvent $event) use (&$loadedEvents) {
foreach ($event->getParam('streamEvents', []) as $streamEvent) {
$loadedEvents[] = $streamEvent;
}
},
-1000
);
$this->repository->getAggregateRoot(
$user->id()
);
$this->assertEmpty($loadedEvents);
}
/**
* @test
*/
public function it_uses_snapshot_store_while_snapshot_store_is_empty(): void
{
$this->prepareSnapshotStoreAggregateRepository();
$user = User::nameNew('John Doe');
$this->repository->saveAggregateRoot($user);
$loadedEvents = [];
$this->eventStore->attach(
'load',
function (ActionEvent $event) use (&$loadedEvents) {
$streamEvents = $event->getParam('streamEvents');
foreach ($streamEvents as $streamEvent) {
$loadedEvents[] = $streamEvent;
}
},
-1000
);
$this->repository->getAggregateRoot(
$user->id()
);
$this->assertCount(1, $loadedEvents);
$this->assertInstanceOf(UserCreated::class, $loadedEvents[0]);
}
/**
* @test
*/
public function it_uses_snapshot_store_and_applies_pending_events(): void
{
$this->prepareSnapshotStoreAggregateRepository();
$user = User::nameNew('John Doe');
$this->repository->saveAggregateRoot($user);
$snapshot = new Snapshot(
User::class,
$user->id(),
$user,
1,
new DateTimeImmutable('now', new DateTimeZone('UTC'))
);
$this->snapshotStore->save($snapshot);
$fetchedUser = $this->repository->getAggregateRoot($user->id());
$fetchedUser->changeName('Max Mustermann');
$this->repository->saveAggregateRoot($fetchedUser);
$loadedEvents = [];
$this->eventStore->attach(
'load',
function (ActionEvent $event) use (&$loadedEvents) {
$streamEvents = $event->getParam('streamEvents');
foreach ($streamEvents as $streamEvent) {
$loadedEvents[] = $streamEvent;
}
$event->getParam('streamEvents')->rewind();
},
-1000
);
$this->repository->getAggregateRoot($user->id());
$this->assertCount(1, $loadedEvents);
$this->assertInstanceOf(UsernameChanged::class, $loadedEvents[0]);
$this->assertEquals(2, $this->repository->extractAggregateVersion($fetchedUser));
}
/**
* @test
*/
public function it_uses_snapshot_store_and_applies_pending_events_when_snapshot_store_found_nothing(): void
{
$this->prepareSnapshotStoreAggregateRepository();
$user = User::nameNew('John Doe');
$this->repository->saveAggregateRoot($user);
$fetchedUser = $this->repository->getAggregateRoot($user->id());
$fetchedUser->changeName('Max Mustermann');
$this->repository->saveAggregateRoot($fetchedUser);
$loadedEvents = [];
$this->eventStore->attach(
'load',
function (ActionEvent $event) use (&$loadedEvents) {
$streamEvents = $event->getParam('streamEvents');
foreach ($streamEvents as $streamEvent) {
$loadedEvents[] = $streamEvent;
}
$event->getParam('streamEvents')->rewind();
},
-1000
);
$this->repository->getAggregateRoot($user->id());
$this->assertCount(2, $loadedEvents);
$this->assertInstanceOf(UserCreated::class, $loadedEvents[0]);
$this->assertInstanceOf(UsernameChanged::class, $loadedEvents[1]);
$this->assertEquals(2, $this->repository->extractAggregateVersion($fetchedUser));
}
protected function prepareSnapshotStoreAggregateRepository(): void
{
parent::setUp();
$this->snapshotStore = new InMemorySnapshotStore();
$this->repository = new AggregateRepository(
$this->eventStore,
AggregateType::fromAggregateRootClass(User::class),
new AggregateTranslator(),
$this->snapshotStore
);
$this->eventStore->create(new Stream(new StreamName('event_stream'), new ArrayIterator()));
}
/**
* @test
* Test for https://github.com/prooph/event-store/issues/179
*/
public function it_tracks_changes_of_aggregate_but_returns_a_same_instance_within_transaction(): void
{
$user = User::nameNew('John Doe');
$this->repository->saveAggregateRoot($user);
$fetchedUser1 = $this->repository->getAggregateRoot(
$user->id()
);
$fetchedUser2 = $this->repository->getAggregateRoot(
$user->id()
);
$this->assertSame($fetchedUser1, $fetchedUser2);
$fetchedUser1->changeName('Max Mustermann');
$this->assertSame($fetchedUser1, $fetchedUser2);
}
/**
* @test
*/
public function it_clears_identity_map_manually(): void
{
$user = User::nameNew('John Doe');
$this->repository->saveAggregateRoot($user);
// fill identity map
$this->repository->getAggregateRoot(
$user->id()
);
$reflectionClass = new ReflectionClass($this->repository);
$reflectionProperty = $reflectionClass->getProperty('identityMap');
$reflectionProperty->setAccessible(true);
self::assertCount(1, $reflectionProperty->getValue($this->repository));
$this->repository->clearIdentityMap();
self::assertCount(0, $reflectionProperty->getValue($this->repository));
}
/**
* @test
*/
public function it_uses_provided_stream_name(): void
{
$streamName = $this->prophesize(StreamName::class);
$streamName->toString()->willReturn('foo')->shouldBeCalled();
$streamName = $streamName->reveal();
$this->repository = new AggregateRepository(
$this->eventStore,
AggregateType::fromAggregateRootClass(User::class),
new AggregateTranslator(),
null,
$streamName
);
$this->eventStore->create(new Stream($streamName, new ArrayIterator()));
$user = User::nameNew('John Doe');
$this->repository->saveAggregateRoot($user);
}
/**
* @test
* Test for https://github.com/prooph/event-sourcing/issues/42
*/
public function it_does_not_throw_an_exception_if_no_pending_event_is_present(): void
{
$user = User::nameNew('John Doe');
$this->assertNull($this->repository->saveAggregateRoot($user));
$this->assertNull($this->repository->saveAggregateRoot($user));
}
/**
* @test
*/
public function it_uses_custom_aggregate_type_names(): void
{
$this->repository = new AggregateRepository(
$this->eventStore,
AggregateType::fromMapping(['user' => User::class]),
new AggregateTranslator(),
null,
null,
false
);
$user = User::nameNew('John Doe');
$this->repository->saveAggregateRoot($user);
$events = $this->eventStore->load(new StreamName('event_stream'));
$this->assertTrue($events->valid());
$event = $events->current();
$this->assertSame('user', $event->metadata()['_aggregate_type']);
}
/**
* @test
*/
public function it_returns_nothing_when_no_stream_found(): void
{
$this->repository = new AggregateRepository(
$this->eventStore,
AggregateType::fromMapping(['user' => User::class]),
new AggregateTranslator(),
null,
new StreamName('unknown'),
false
);
$this->assertNull($this->repository->getAggregateRoot('some_id'));
}
/**
* @test
*/
public function it_returns_nothing_when_no_stream_found_using_one_stream_per_aggregate(): void
{
$this->repository = new AggregateRepository(
$this->eventStore,
AggregateType::fromMapping(['user' => User::class]),
new AggregateTranslator(),
null,
new StreamName('unknown'),
true
);
$this->assertNull($this->repository->getAggregateRoot('some_id'));
}
/**
* @test
*/
public function it_returns_nothing_when_no_stream_found_using_snapshot_store(): void
{
$this->repository = new AggregateRepository(
$this->eventStore,
AggregateType::fromMapping(['user' => User::class]),
new AggregateTranslator(),
new InMemorySnapshotStore(),
new StreamName('unknown'),
false
);
$this->assertNull($this->repository->getAggregateRoot('some_id'));
}
/**
* @test
*/
public function it_returns_nothing_when_no_stream_found_using_one_stream_per_aggregate_and_snapshot_store(): void
{
$this->repository = new AggregateRepository(
$this->eventStore,
AggregateType::fromMapping(['user' => User::class]),
new AggregateTranslator(),
new InMemorySnapshotStore(),
new StreamName('unknown'),
true
);
$this->assertNull($this->repository->getAggregateRoot('some_id'));
}
/**
* @test
*/
public function it_adds_a_new_aggregate_with_metadata_stream(): void
{
$this->repository = new AggregateRepository(
$this->eventStore,
AggregateType::fromAggregateRootClass(User::class),
new AggregateTranslator(),
null,
null,
false,
true,
['_aggregate_type' => User::class]
);
$user = User::nameNew('John Doe');
$this->repository->saveAggregateRoot($user);
$fetchedUser = $this->repository->getAggregateRoot(
$user->id()
);
$this->assertInstanceOf(User::class, $fetchedUser);
$this->assertNotSame($user, $fetchedUser);
$this->assertEquals('John Doe', $fetchedUser->name());
}
/**
* @test
*/
public function it_uses_provided_metadata(): void
{
$streamName = 'foo';
$user = User::nameNew('John Doe');
$eventstore = $this->prophesize(EventStore::class);
$callback = function (Stream $stream) use ($user, $streamName) {
$this->assertEquals(new StreamName($streamName . '-' . $user->id()), (string) $stream->streamName());
$this->assertSame(['_aggregate_type' => User::class], $stream->metadata());
return true;
};
$eventstore->create(
Argument::that($callback)
)->shouldBeCalled();
$this->repository = new AggregateRepository(
$eventstore->reveal(),
AggregateType::fromAggregateRootClass(User::class),
new AggregateTranslator(),
null,
new StreamName($streamName),
true,
false,
['_aggregate_type' => User::class]
);
$this->repository->saveAggregateRoot($user);
}
}