1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Dsantang\DomainEventsDoctrine\Tests\Unit\EventsRecorder; |
6
|
|
|
|
7
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
8
|
|
|
use Doctrine\ORM\Event\OnFlushEventArgs; |
9
|
|
|
use Doctrine\ORM\UnitOfWork; |
10
|
|
|
use Dsantang\DomainEvents\Counter; |
11
|
|
|
use Dsantang\DomainEvents\DomainEvent; |
12
|
|
|
use Dsantang\DomainEvents\EventAware; |
13
|
|
|
use Dsantang\DomainEvents\Registry\EventsRegistry; |
14
|
|
|
use Dsantang\DomainEventsDoctrine\Aggregator; |
15
|
|
|
use Dsantang\DomainEventsDoctrine\EventsRecorder\OrderedDoctrineEventsRecorder; |
16
|
|
|
use Dsantang\DomainEventsDoctrine\Tests\RandomDomainEvent; |
17
|
|
|
use PHPUnit\Framework\TestCase; |
18
|
|
|
|
19
|
|
|
final class OrderedDoctrineEventsRecorderTest extends TestCase |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @param DomainEvent[] $domainEvents |
23
|
|
|
* |
24
|
|
|
* @dataProvider provideEventAwareChangedEntities |
25
|
|
|
* |
26
|
|
|
* @test |
27
|
|
|
*/ |
28
|
|
|
public function onFlushWillAggregateAllTheEventAwareEntities(object $eventAwareEntity, array $domainEvents): void |
29
|
|
|
{ |
30
|
|
|
$unitOfWork = $this->createMock(UnitOfWork::class); |
31
|
|
|
|
32
|
|
|
$unitOfWork->expects(self::once()) |
33
|
|
|
->method('getScheduledEntityDeletions') |
34
|
|
|
->willReturn([]); |
35
|
|
|
|
36
|
|
|
$unitOfWork->expects(self::once()) |
37
|
|
|
->method('getScheduledEntityInsertions') |
38
|
|
|
->willReturn([$eventAwareEntity]); |
39
|
|
|
|
40
|
|
|
$unitOfWork->expects(self::once()) |
41
|
|
|
->method('getScheduledEntityUpdates') |
42
|
|
|
->willReturn([$eventAwareEntity]); |
43
|
|
|
|
44
|
|
|
$entityManager = $this->createMock(EntityManagerInterface::class); |
45
|
|
|
$entityManager->expects(self::once()) |
46
|
|
|
->method('getUnitOfWork') |
47
|
|
|
->willReturn($unitOfWork); |
48
|
|
|
|
49
|
|
|
$aggregator = $this->createMock(Aggregator::class); |
50
|
|
|
|
51
|
|
|
$aggregator->expects(self::once()) |
52
|
|
|
->method('aggregate') |
53
|
|
|
->with(...$domainEvents); |
54
|
|
|
|
55
|
|
|
$eventRecorder = new OrderedDoctrineEventsRecorder($aggregator); |
56
|
|
|
|
57
|
|
|
$eventArgs = $this->createMock(OnFlushEventArgs::class); |
58
|
|
|
$eventArgs->expects(self::once()) |
59
|
|
|
->method('getEntityManager') |
60
|
|
|
->willReturn($entityManager); |
61
|
|
|
|
62
|
|
|
Counter::getNext(); |
63
|
|
|
|
64
|
|
|
$eventRecorder->onFlush($eventArgs); |
65
|
|
|
|
66
|
|
|
self::assertEquals(0, Counter::getNext()); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return mixed[] array |
71
|
|
|
*/ |
72
|
|
|
public function provideEventAwareChangedEntities(): array |
73
|
|
|
{ |
74
|
|
|
$awareEntity = new class (new RandomDomainEvent()) implements EventAware { |
75
|
|
|
use EventsRegistry; |
76
|
|
|
|
77
|
|
|
public function __construct(DomainEvent $domainEvent) |
78
|
|
|
{ |
79
|
|
|
$this->recordedEvents = [1 => $domainEvent, 0 => $domainEvent]; |
80
|
|
|
} |
81
|
|
|
}; |
82
|
|
|
|
83
|
|
|
$object = new class (){ |
84
|
|
|
}; |
85
|
|
|
|
86
|
|
|
return [ |
87
|
|
|
'with no changed entities' => [$object, []], |
88
|
|
|
'with a changed entity' => [$awareEntity, [new RandomDomainEvent(), new RandomDomainEvent()]], |
89
|
|
|
]; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|