1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Dsantang\DomainEventsDoctrine\Tests\Unit\Outbox; |
6
|
|
|
|
7
|
|
|
use Doctrine\Common\EventArgs; |
8
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
9
|
|
|
use Doctrine\ORM\Event\OnFlushEventArgs; |
10
|
|
|
use Doctrine\ORM\UnitOfWork; |
11
|
|
|
use Dsantang\DomainEvents\Counter; |
12
|
|
|
use Dsantang\DomainEvents\DomainEvent; |
13
|
|
|
use Dsantang\DomainEvents\EventAware; |
14
|
|
|
use Dsantang\DomainEvents\Registry\OrderedEventRegistry; |
15
|
|
|
use Dsantang\DomainEventsDoctrine\Tests\OutboxSubClass; |
16
|
|
|
use Dsantang\DomainEventsDoctrine\Tests\Unit\Outbox\Stub\Converters\FirstOutboxConverter; |
17
|
|
|
use Dsantang\DomainEventsDoctrine\Tests\Unit\Outbox\Stub\Converters\SecondOutboxConverter; |
18
|
|
|
use Dsantang\DomainEventsDoctrine\Tests\Unit\Outbox\Stub\Converters\ThirdOutboxConverter; |
19
|
|
|
use Dsantang\DomainEventsDoctrine\Tests\Unit\Outbox\Stub\DomainEvents\FirstDomainEvent; |
20
|
|
|
use Dsantang\DomainEventsDoctrine\Tests\Unit\Outbox\Stub\DomainEvents\SecondDomainEvent; |
21
|
|
|
use Dsantang\DomainEventsDoctrine\Tests\Unit\Outbox\Stub\DomainEvents\ThirdDomainEvent; |
22
|
|
|
use Dsantang\DomainEventsDoctrine\Tests\Unit\Outbox\Stub\OutboxEntries\FirstOutboxEntry; |
23
|
|
|
use Dsantang\DomainEventsDoctrine\Tests\Unit\Outbox\Stub\OutboxEntries\SecondOutboxEntry; |
24
|
|
|
use Dsantang\DomainEventsDoctrine\Tests\Unit\Outbox\Stub\OutboxEntries\ThirdOutboxEntry; |
25
|
|
|
use Dsantang\DomainEventsDoctrine\Tests\Unit\Outbox\Stub\StubMapBased; |
26
|
|
|
use PHPUnit\Framework\TestCase; |
27
|
|
|
use function array_pop; |
28
|
|
|
|
29
|
|
|
final class MapBasedTest extends TestCase |
30
|
|
|
{ |
31
|
|
|
/** @var converter[] */ |
32
|
|
|
private $conversionMap; |
33
|
|
|
|
34
|
|
|
/** @var StubMapBased */ |
35
|
|
|
private $mapBasedEventsHandler; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @before |
39
|
|
|
*/ |
40
|
|
|
public function setUpDependencies() : void |
41
|
|
|
{ |
42
|
|
|
$this->conversionMap = |
43
|
|
|
[ |
|
|
|
|
44
|
|
|
'Dsantang\DomainEventsDoctrine\Tests\Unit\Outbox\Stub\DomainEvents\FirstDomainEvent' => |
45
|
|
|
new FirstOutboxConverter(), |
46
|
|
|
|
47
|
|
|
'Dsantang\DomainEventsDoctrine\Tests\Unit\Outbox\Stub\DomainEvents\SecondDomainEvent' => |
48
|
|
|
new SecondOutboxConverter(), |
49
|
|
|
|
50
|
|
|
'Dsantang\DomainEventsDoctrine\Tests\Unit\Outbox\Stub\DomainEvents\ThirdDomainEvent' => |
51
|
|
|
new ThirdOutboxConverter(), |
52
|
|
|
]; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testGetDomainsEvents() : void |
56
|
|
|
{ |
57
|
|
|
Counter::reset(); |
58
|
|
|
|
59
|
|
|
$eventArgs = $this->getEventArgs(); |
60
|
|
|
|
61
|
|
|
self::assertEquals(3, Counter::getNext()); |
62
|
|
|
|
63
|
|
|
$this->mapBasedEventsHandler = new StubMapBased(new OutboxSubClass(), $this->conversionMap); |
64
|
|
|
|
65
|
|
|
$this->mapBasedEventsHandler->getDomainsEvents($eventArgs); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function testConvert() : void |
69
|
|
|
{ |
70
|
|
|
$this->mapBasedEventsHandler = new StubMapBased(new OutboxSubClass(), $this->conversionMap); |
71
|
|
|
|
72
|
|
|
$domainEvents = [new ThirdDomainEvent(), new FirstDomainEvent(), new SecondDomainEvent()]; |
73
|
|
|
|
74
|
|
|
$returnedOutboxEvents = $this->mapBasedEventsHandler->convert(...$domainEvents); |
75
|
|
|
|
76
|
|
|
$expectedOutboxEvents = [ |
77
|
|
|
(new ThirdOutboxConverter())->convert(new ThirdDomainEvent()), |
78
|
|
|
(new FirstOutboxConverter())->convert(new FirstDomainEvent()), |
79
|
|
|
(new SecondOutboxConverter())->convert(new SecondDomainEvent()), |
80
|
|
|
]; |
81
|
|
|
|
82
|
|
|
self::assertEquals($expectedOutboxEvents, $returnedOutboxEvents); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function testConvertWithANonOutboxEntryDomainEvent() : void |
86
|
|
|
{ |
87
|
|
|
array_pop($this->conversionMap); |
88
|
|
|
$this->mapBasedEventsHandler = new StubMapBased(new OutboxSubClass(), $this->conversionMap); |
89
|
|
|
|
90
|
|
|
$domainEvents = [new FirstDomainEvent(), new SecondDomainEvent(), new ThirdDomainEvent()]; |
91
|
|
|
|
92
|
|
|
$returnedOutboxEvents = $this->mapBasedEventsHandler->convert(...$domainEvents); |
93
|
|
|
|
94
|
|
|
$expectedOutboxEvents = [ |
95
|
|
|
(new FirstOutboxConverter())->convert(new FirstDomainEvent()), |
96
|
|
|
(new SecondOutboxConverter())->convert(new SecondDomainEvent()), |
97
|
|
|
]; |
98
|
|
|
|
99
|
|
|
self::assertEquals($expectedOutboxEvents, $returnedOutboxEvents); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function testPersist() : void |
103
|
|
|
{ |
104
|
|
|
$eventArgs = $this->getEventArgs(); |
105
|
|
|
$outboxSubClass = new OutboxSubClass(); |
106
|
|
|
|
107
|
|
|
$this->mapBasedEventsHandler = new StubMapBased($outboxSubClass, $this->conversionMap); |
108
|
|
|
$this->mapBasedEventsHandler->getDomainsEvents($eventArgs); |
109
|
|
|
|
110
|
|
|
$outboxEvents = [ |
111
|
|
|
new ThirdOutboxEntry(new ThirdDomainEvent()), |
112
|
|
|
new FirstOutboxEntry(new FirstDomainEvent()), |
113
|
|
|
new SecondOutboxEntry(new SecondDomainEvent()), |
114
|
|
|
]; |
115
|
|
|
|
116
|
|
|
$eventArgs->getEntityManager()->expects(self::exactly(3)) |
|
|
|
|
117
|
|
|
->method('persist') |
118
|
|
|
->withConsecutive( |
119
|
|
|
[$outboxSubClass->withOutboxEntry($outboxEvents[0])], |
120
|
|
|
[$outboxSubClass->withOutboxEntry($outboxEvents[1])], |
121
|
|
|
[$outboxSubClass->withOutboxEntry($outboxEvents[2])] |
122
|
|
|
); |
123
|
|
|
|
124
|
|
|
$this->mapBasedEventsHandler->persist( |
125
|
|
|
$eventArgs->getEntityManager(), |
126
|
|
|
...$outboxEvents |
127
|
|
|
); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
private function getEventArgs() : EventArgs |
131
|
|
|
{ |
132
|
|
|
$entity1 = $this->getEntityClass(); |
133
|
|
|
$entity2 = $this->getEntityClass(); |
134
|
|
|
$entity3 = $this->getEntityClass(); |
135
|
|
|
|
136
|
|
|
$domainEvent1 = new FirstDomainEvent(); |
137
|
|
|
$domainEvent2 = new SecondDomainEvent(); |
138
|
|
|
$domainEvent3 = new ThirdDomainEvent(); |
139
|
|
|
|
140
|
|
|
$entity1->trigger($domainEvent1); |
141
|
|
|
$entity2->trigger($domainEvent2); |
142
|
|
|
$entity3->trigger($domainEvent3); |
143
|
|
|
|
144
|
|
|
$unitOfWork = $this->createMock(UnitOfWork::class); |
145
|
|
|
$unitOfWork->expects(self::once())->method('getScheduledEntityInsertions')->willReturn([$entity1]); |
146
|
|
|
$unitOfWork->expects(self::once())->method('getScheduledEntityUpdates')->willReturn([]); |
147
|
|
|
$unitOfWork->expects(self::once())->method('getScheduledEntityDeletions')->willReturn([$entity2, $entity3]); |
148
|
|
|
|
149
|
|
|
$entityManager = $this->createMock(EntityManagerInterface::class); |
150
|
|
|
$entityManager->expects(self::any())->method('getUnitOfWork')->willReturn($unitOfWork); |
151
|
|
|
|
152
|
|
|
$eventArgs = $this->createMock(OnFlushEventArgs::class); |
153
|
|
|
$eventArgs->expects(self::any())->method('getEntityManager')->willReturn($entityManager); |
154
|
|
|
|
155
|
|
|
return $eventArgs; |
|
|
|
|
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
private function getEntityClass() : EventAware |
159
|
|
|
{ |
160
|
|
|
return new class() implements EventAware { |
161
|
|
|
use OrderedEventRegistry; |
162
|
|
|
|
163
|
|
|
public function trigger(DomainEvent $event) : void |
164
|
|
|
{ |
165
|
|
|
$this->triggeredA($event); |
166
|
|
|
} |
167
|
|
|
}; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..