1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Dsantang\DomainEventsDoctrine\Tests\Unit\Outbox; |
6
|
|
|
|
7
|
|
|
use Dsantang\DomainEventsDoctrine\Outbox\MapBased; |
8
|
|
|
use Dsantang\DomainEventsDoctrine\Tests\OutboxSubClass; |
9
|
|
|
use Dsantang\DomainEventsDoctrine\Tests\RandomDomainEvent; |
10
|
|
|
use Dsantang\DomainEventsDoctrine\Tests\Unit\Outbox\Stub\Converters\FirstOutboxConverter; |
11
|
|
|
use Dsantang\DomainEventsDoctrine\Tests\Unit\Outbox\Stub\Converters\SecondOutboxConverter; |
12
|
|
|
use Dsantang\DomainEventsDoctrine\Tests\Unit\Outbox\Stub\Converters\ThirdOutboxConverter; |
13
|
|
|
use Dsantang\DomainEventsDoctrine\Tests\Unit\Outbox\Stub\DomainEvents\FirstDomainEvent; |
14
|
|
|
use Dsantang\DomainEventsDoctrine\Tests\Unit\Outbox\Stub\DomainEvents\SecondDomainEvent; |
15
|
|
|
use Dsantang\DomainEventsDoctrine\Tests\Unit\Outbox\Stub\DomainEvents\ThirdDomainEvent; |
16
|
|
|
use InvalidArgumentException; |
17
|
|
|
use PHPUnit\Framework\TestCase; |
18
|
|
|
use stdClass; |
19
|
|
|
use function array_pop; |
20
|
|
|
|
21
|
|
|
final class MapBasedTest extends TestCase |
22
|
|
|
{ |
23
|
|
|
/** @var converter[] */ |
24
|
|
|
private $conversionMap; |
25
|
|
|
|
26
|
|
|
/** @var MapBased */ |
27
|
|
|
private $mapBasedEventsHandler; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @before |
31
|
|
|
*/ |
32
|
|
|
public function setUpDependencies() : void |
33
|
|
|
{ |
34
|
|
|
$this->conversionMap = |
35
|
|
|
[ |
|
|
|
|
36
|
|
|
'Dsantang\DomainEventsDoctrine\Tests\Unit\Outbox\Stub\DomainEvents\FirstDomainEvent' => |
37
|
|
|
new FirstOutboxConverter(), |
38
|
|
|
|
39
|
|
|
'Dsantang\DomainEventsDoctrine\Tests\Unit\Outbox\Stub\DomainEvents\SecondDomainEvent' => |
40
|
|
|
new SecondOutboxConverter(), |
41
|
|
|
|
42
|
|
|
'Dsantang\DomainEventsDoctrine\Tests\Unit\Outbox\Stub\DomainEvents\ThirdDomainEvent' => |
43
|
|
|
new ThirdOutboxConverter(), |
44
|
|
|
]; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function testDealWithInvalidKeyConversionMap() : void |
48
|
|
|
{ |
49
|
|
|
$this->expectException(InvalidArgumentException::class); |
50
|
|
|
|
51
|
|
|
$this->conversionMap = [ |
|
|
|
|
52
|
|
|
'Dsantang\DomainEventsDoctrine\Tests\Unit\Outbox\Stub\DomainEvents\NonExistentDomainEvent' => |
53
|
|
|
new FirstOutboxConverter() |
54
|
|
|
]; |
55
|
|
|
|
56
|
|
|
$this->mapBasedEventsHandler = new MapBased(new OutboxSubClass(), $this->conversionMap); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testDealWithInvalidValueConversionMap() : void |
60
|
|
|
{ |
61
|
|
|
$this->expectException(InvalidArgumentException::class); |
62
|
|
|
|
63
|
|
|
$this->conversionMap = [ |
|
|
|
|
64
|
|
|
'Dsantang\DomainEventsDoctrine\Tests\Unit\Outbox\Stub\DomainEvents\ThirdDomainEvent' => |
65
|
|
|
new stdClass() |
66
|
|
|
]; |
67
|
|
|
|
68
|
|
|
$this->mapBasedEventsHandler = new MapBased(new OutboxSubClass(), $this->conversionMap); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testConvert() : void |
72
|
|
|
{ |
73
|
|
|
$this->mapBasedEventsHandler = new MapBased(new OutboxSubClass(), $this->conversionMap); |
74
|
|
|
|
75
|
|
|
$domainEvents = [new ThirdDomainEvent(), new FirstDomainEvent(), new SecondDomainEvent()]; |
76
|
|
|
|
77
|
|
|
$returnedOutboxEvents = $this->mapBasedEventsHandler->convert(...$domainEvents); |
78
|
|
|
|
79
|
|
|
$expectedOutboxEvents = [ |
80
|
|
|
(new ThirdOutboxConverter())->convert(new ThirdDomainEvent()), |
81
|
|
|
(new FirstOutboxConverter())->convert(new FirstDomainEvent()), |
82
|
|
|
(new SecondOutboxConverter())->convert(new SecondDomainEvent()), |
83
|
|
|
]; |
84
|
|
|
|
85
|
|
|
self::assertEquals($expectedOutboxEvents, $returnedOutboxEvents); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function testConvertWithANonOutboxEntryDomainEvent() : void |
89
|
|
|
{ |
90
|
|
|
array_pop($this->conversionMap); |
91
|
|
|
$this->mapBasedEventsHandler = new MapBased(new OutboxSubClass(), $this->conversionMap); |
92
|
|
|
|
93
|
|
|
$domainEvents = [new FirstDomainEvent(), new SecondDomainEvent(), new ThirdDomainEvent()]; |
94
|
|
|
|
95
|
|
|
$returnedOutboxEvents = $this->mapBasedEventsHandler->convert(...$domainEvents); |
96
|
|
|
|
97
|
|
|
$expectedOutboxEvents = [ |
98
|
|
|
(new FirstOutboxConverter())->convert(new FirstDomainEvent()), |
99
|
|
|
(new SecondOutboxConverter())->convert(new SecondDomainEvent()), |
100
|
|
|
]; |
101
|
|
|
|
102
|
|
|
self::assertEquals($expectedOutboxEvents, $returnedOutboxEvents); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function testConvertANonOutboxRelatedDomainEvent() : void |
106
|
|
|
{ |
107
|
|
|
$this->mapBasedEventsHandler = new MapBased(new OutboxSubClass(), $this->conversionMap); |
108
|
|
|
|
109
|
|
|
$domainEvents = [new RandomDomainEvent(), new SecondDomainEvent()]; |
110
|
|
|
|
111
|
|
|
$returnedOutboxEvents = $this->mapBasedEventsHandler->convert(...$domainEvents); |
112
|
|
|
|
113
|
|
|
$expectedOutboxEvents = [ |
114
|
|
|
(new SecondOutboxConverter())->convert(new SecondDomainEvent()) |
115
|
|
|
]; |
116
|
|
|
|
117
|
|
|
self::assertEquals($expectedOutboxEvents, $returnedOutboxEvents); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
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..