|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Dsantang\DomainEventsDoctrine\Tests\Integration; |
|
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\OrderedEventRegistry; |
|
14
|
|
|
use Dsantang\DomainEventsDoctrine\Dispatcher\SimpleEventsDispatcher; |
|
15
|
|
|
use Dsantang\DomainEventsDoctrine\Dispatcher\SymfonyEvent; |
|
16
|
|
|
use Dsantang\DomainEventsDoctrine\EventsRecorder\OrderedDoctrineEventsRecorder; |
|
17
|
|
|
use Dsantang\DomainEventsDoctrine\EventsTracker; |
|
18
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
19
|
|
|
use PHPUnit\Framework\TestCase; |
|
20
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
21
|
|
|
|
|
22
|
|
|
final class OrderedEventsIntegrationTest extends TestCase |
|
23
|
|
|
{ |
|
24
|
|
|
private EventsTracker $tracker; |
|
25
|
|
|
|
|
26
|
|
|
private OrderedDoctrineEventsRecorder $recorder; |
|
27
|
|
|
|
|
28
|
|
|
private EventDispatcherInterface|MockObject $eventDispatcher; |
|
29
|
|
|
|
|
30
|
|
|
private SimpleEventsDispatcher $doctrineDispatcher; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @before |
|
34
|
|
|
*/ |
|
35
|
|
|
public function setUpDependencies(): void |
|
36
|
|
|
{ |
|
37
|
|
|
$this->tracker = new EventsTracker(); |
|
38
|
|
|
$this->recorder = new OrderedDoctrineEventsRecorder($this->tracker); |
|
39
|
|
|
$this->eventDispatcher = $this->createMock(EventDispatcherInterface::class); |
|
40
|
|
|
$this->doctrineDispatcher = new SimpleEventsDispatcher( |
|
41
|
|
|
$this->tracker, |
|
42
|
|
|
$this->eventDispatcher |
|
43
|
|
|
); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @test |
|
48
|
|
|
*/ |
|
49
|
|
|
public function orderInWhichTheEventsHaveBeenRaisedIsPreserved(): void |
|
50
|
|
|
{ |
|
51
|
|
|
$event1 = new class implements DomainEvent { |
|
52
|
|
|
public function getName(): string |
|
53
|
|
|
{ |
|
54
|
|
|
return 'first'; |
|
55
|
|
|
} |
|
56
|
|
|
}; |
|
57
|
|
|
$event2 = new class implements DomainEvent { |
|
58
|
|
|
public function getName(): string |
|
59
|
|
|
{ |
|
60
|
|
|
return 'second'; |
|
61
|
|
|
} |
|
62
|
|
|
}; |
|
63
|
|
|
$event3 = new class implements DomainEvent { |
|
64
|
|
|
public function getName(): string |
|
65
|
|
|
{ |
|
66
|
|
|
return 'third'; |
|
67
|
|
|
} |
|
68
|
|
|
}; |
|
69
|
|
|
|
|
70
|
|
|
$entity1 = new class implements EventAware{ |
|
71
|
|
|
use OrderedEventRegistry; |
|
72
|
|
|
|
|
73
|
|
|
public function trigger(DomainEvent $event): void |
|
74
|
|
|
{ |
|
75
|
|
|
$this->triggeredA($event); |
|
76
|
|
|
} |
|
77
|
|
|
}; |
|
78
|
|
|
|
|
79
|
|
|
$entity2 = new class implements EventAware { |
|
80
|
|
|
use OrderedEventRegistry; |
|
81
|
|
|
|
|
82
|
|
|
public function trigger(DomainEvent $event): void |
|
83
|
|
|
{ |
|
84
|
|
|
$this->triggeredA($event); |
|
85
|
|
|
} |
|
86
|
|
|
}; |
|
87
|
|
|
|
|
88
|
|
|
$entity1->trigger($event1); |
|
89
|
|
|
$entity2->trigger($event2); |
|
90
|
|
|
$entity1->trigger($event3); |
|
91
|
|
|
|
|
92
|
|
|
$uow = $this->createMock(UnitOfWork::class); |
|
93
|
|
|
|
|
94
|
|
|
$uow->expects(self::once()) |
|
95
|
|
|
->method('getScheduledEntityInsertions') |
|
96
|
|
|
->willReturn([$entity1]); |
|
97
|
|
|
|
|
98
|
|
|
$uow->expects(self::once()) |
|
99
|
|
|
->method('getScheduledEntityUpdates') |
|
100
|
|
|
->willReturn([$entity2]); |
|
101
|
|
|
|
|
102
|
|
|
$uow->expects(self::once()) |
|
103
|
|
|
->method('getScheduledEntityDeletions') |
|
104
|
|
|
->willReturn([]); |
|
105
|
|
|
|
|
106
|
|
|
$em = $this->createMock(EntityManagerInterface::class); |
|
107
|
|
|
|
|
108
|
|
|
$em->expects(self::once())->method('getUnitOfWork')->willReturn($uow); |
|
109
|
|
|
|
|
110
|
|
|
$eventArgs = $this->createMock(OnFlushEventArgs::class); |
|
111
|
|
|
$eventArgs->expects(self::once())->method('getEntityManager')->willReturn($em); |
|
112
|
|
|
|
|
113
|
|
|
$this->eventDispatcher->/** @scrutinizer ignore-call */expects(self::exactly(3)) |
|
114
|
|
|
->method('dispatch') |
|
115
|
|
|
->withConsecutive( |
|
116
|
|
|
[new SymfonyEvent($event1), 'first'], |
|
117
|
|
|
[new SymfonyEvent($event2), 'second'], |
|
118
|
|
|
[new SymfonyEvent($event3), 'third'] |
|
119
|
|
|
); |
|
120
|
|
|
|
|
121
|
|
|
self::assertEquals(3, Counter::getNext()); |
|
122
|
|
|
|
|
123
|
|
|
$this->recorder->onFlush($eventArgs); |
|
124
|
|
|
|
|
125
|
|
|
self::assertEquals(0, Counter::getNext()); |
|
126
|
|
|
|
|
127
|
|
|
$this->doctrineDispatcher->postFlush(); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|