|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Tests\Happyr\MessageSerializer; |
|
6
|
|
|
|
|
7
|
|
|
use Happyr\MessageSerializer\SerializerRouter; |
|
8
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
9
|
|
|
use PHPUnit\Framework\TestCase; |
|
10
|
|
|
use Symfony\Component\Messenger\Envelope; |
|
11
|
|
|
use Symfony\Component\Messenger\Exception\MessageDecodingFailedException; |
|
12
|
|
|
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @internal |
|
16
|
|
|
*/ |
|
17
|
|
|
final class SerializerRouterTest extends TestCase |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var MockObject |
|
21
|
|
|
*/ |
|
22
|
|
|
private $happyrSerializerMock; |
|
23
|
|
|
/** |
|
24
|
|
|
* @var MockObject |
|
25
|
|
|
*/ |
|
26
|
|
|
private $symfonySerializerMock; |
|
27
|
|
|
/** |
|
28
|
|
|
* @var SerializerRouter |
|
29
|
|
|
*/ |
|
30
|
|
|
private $serializer; |
|
31
|
|
|
|
|
32
|
|
|
protected function setUp(): void |
|
33
|
|
|
{ |
|
34
|
|
|
parent::setUp(); |
|
35
|
|
|
$this->happyrSerializerMock = $this->getMockBuilder(SerializerInterface::class) |
|
36
|
|
|
->disableOriginalConstructor() |
|
37
|
|
|
->getMock(); |
|
38
|
|
|
$this->symfonySerializerMock = $this->getMockBuilder(SerializerInterface::class) |
|
39
|
|
|
->disableOriginalConstructor() |
|
40
|
|
|
->getMock(); |
|
41
|
|
|
$this->serializer = new SerializerRouter( |
|
42
|
|
|
$this->happyrSerializerMock, |
|
43
|
|
|
$this->symfonySerializerMock |
|
44
|
|
|
); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function testDecodeThrowsExceptionIfNoBody(): void |
|
48
|
|
|
{ |
|
49
|
|
|
$this->expectException(MessageDecodingFailedException::class); |
|
50
|
|
|
|
|
51
|
|
|
$this->serializer->decode([]); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function testDecodeCallsSymfonySerializerIfEnvelopeBodyNotJson(): void |
|
55
|
|
|
{ |
|
56
|
|
|
$envelope = [ |
|
57
|
|
|
'body' => serialize(new \stdClass()), |
|
58
|
|
|
]; |
|
59
|
|
|
$this->symfonySerializerMock->expects(self::once()) |
|
60
|
|
|
->method('decode') |
|
61
|
|
|
->with($envelope) |
|
62
|
|
|
->willReturn(new Envelope(new \stdClass())); |
|
63
|
|
|
$this->happyrSerializerMock->expects(self::exactly(0)) |
|
64
|
|
|
->method('decode') |
|
65
|
|
|
->willReturn(new Envelope(new \stdClass())); |
|
66
|
|
|
|
|
67
|
|
|
$this->serializer->decode($envelope); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function testDecodeCallHappyrSerializerForJsonWithHappyrSerializerStructure(): void |
|
71
|
|
|
{ |
|
72
|
|
|
$envelope = [ |
|
73
|
|
|
'body' => json_encode([ |
|
74
|
|
|
'identifier' => 'some-identifier', |
|
75
|
|
|
'version' => 1, |
|
76
|
|
|
'timestamp' => time(), |
|
77
|
|
|
'payload' => [ |
|
78
|
|
|
'message' => 'Some message', |
|
79
|
|
|
], |
|
80
|
|
|
]), |
|
81
|
|
|
]; |
|
82
|
|
|
$this->happyrSerializerMock->expects(self::once()) |
|
83
|
|
|
->method('decode') |
|
84
|
|
|
->with($envelope) |
|
85
|
|
|
->willReturn(new Envelope(new \stdClass())); |
|
86
|
|
|
$this->symfonySerializerMock->expects(self::exactly(0)) |
|
87
|
|
|
->method('decode') |
|
88
|
|
|
->willReturn(new Envelope(new \stdClass())); |
|
89
|
|
|
|
|
90
|
|
|
$this->serializer->decode($envelope); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @dataProvider getNonHappyrSerializerEncodedEnvelope |
|
95
|
|
|
*/ |
|
96
|
|
|
public function testDecodeCallsSymfonySerializerForJsonWithDifferentStructure(array $encodedEnvelope): void |
|
97
|
|
|
{ |
|
98
|
|
|
$this->symfonySerializerMock->expects(self::once()) |
|
99
|
|
|
->method('decode') |
|
100
|
|
|
->with($encodedEnvelope) |
|
101
|
|
|
->willReturn(new Envelope(new \stdClass())); |
|
102
|
|
|
$this->happyrSerializerMock->expects(self::exactly(0)) |
|
103
|
|
|
->method('decode') |
|
104
|
|
|
->willReturn(new Envelope(new \stdClass())); |
|
105
|
|
|
|
|
106
|
|
|
$this->serializer->decode($encodedEnvelope); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function getNonHappyrSerializerEncodedEnvelope(): iterable |
|
110
|
|
|
{ |
|
111
|
|
|
//missing identifier |
|
112
|
|
|
yield [[ |
|
113
|
|
|
'body' => json_encode([ |
|
114
|
|
|
'version' => 1, |
|
115
|
|
|
'timestamp' => time(), |
|
116
|
|
|
'payload' => [ |
|
117
|
|
|
'message' => 'Some message', |
|
118
|
|
|
], |
|
119
|
|
|
]), |
|
120
|
|
|
]]; |
|
121
|
|
|
//missing version |
|
122
|
|
|
yield [[ |
|
123
|
|
|
'body' => json_encode([ |
|
124
|
|
|
'identifier' => 'some-identifier', |
|
125
|
|
|
'timestamp' => time(), |
|
126
|
|
|
'payload' => [ |
|
127
|
|
|
'message' => 'Some message', |
|
128
|
|
|
], |
|
129
|
|
|
]), |
|
130
|
|
|
]]; |
|
131
|
|
|
//missing timestamp |
|
132
|
|
|
yield [[ |
|
133
|
|
|
'body' => json_encode([ |
|
134
|
|
|
'identifier' => 'some-identifier', |
|
135
|
|
|
'version' => 1, |
|
136
|
|
|
'payload' => [ |
|
137
|
|
|
'message' => 'Some message', |
|
138
|
|
|
], |
|
139
|
|
|
]), |
|
140
|
|
|
]]; |
|
141
|
|
|
//missing payload |
|
142
|
|
|
yield [[ |
|
143
|
|
|
'body' => json_encode([ |
|
144
|
|
|
'identifier' => 'some-identifier', |
|
145
|
|
|
'version' => 1, |
|
146
|
|
|
'timestamp' => time(), |
|
147
|
|
|
]), |
|
148
|
|
|
]]; |
|
149
|
|
|
// missing all |
|
150
|
|
|
yield [[ |
|
151
|
|
|
'body' => json_encode([ |
|
152
|
|
|
'some-key' => 'some-value', |
|
153
|
|
|
]), |
|
154
|
|
|
]]; |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|