8
8
use Behat \Gherkin \Node \PyStringNode ;
9
9
use Behat \Hook \BeforeScenario ;
10
10
use Exception ;
11
- use JsonException ;
12
11
use Symfony \Component \DependencyInjection \ContainerInterface ;
13
12
use Symfony \Component \Messenger \Transport \InMemory \InMemoryTransport ;
13
+ use Symfony \Component \Messenger \Transport \Sync \SyncTransport ;
14
14
use Symfony \Component \Serializer \Normalizer \NormalizerInterface ;
15
15
16
16
class MessengerContext implements Context
@@ -62,8 +62,8 @@ public function transportShouldContainMessageWithJson(string $transportName, PyS
62
62
throw new Exception (
63
63
sprintf (
64
64
'The transport doesn \'t contain message with such JSON, actual messages: %s ' ,
65
- $ this ->getPrettyJson ($ actualMessageList )
66
- )
65
+ $ this ->getPrettyJson ($ actualMessageList ),
66
+ ),
67
67
);
68
68
}
69
69
@@ -73,7 +73,7 @@ public function transportShouldContainMessageWithJson(string $transportName, PyS
73
73
public function transportShouldContainMessageWithJsonAndVariableFields (
74
74
string $ transportName ,
75
75
string $ variableFields ,
76
- PyStringNode $ expectedMessage
76
+ PyStringNode $ expectedMessage,
77
77
): void {
78
78
$ variableFields = $ variableFields ? array_map ('trim ' , explode (', ' , $ variableFields )) : [];
79
79
$ expectedMessage = $ this ->decodeExpectedJson ($ expectedMessage );
@@ -92,8 +92,8 @@ public function transportShouldContainMessageWithJsonAndVariableFields(
92
92
throw new Exception (
93
93
sprintf (
94
94
'The transport doesn \'t contain message with such JSON, actual messages: %s ' ,
95
- $ this ->getPrettyJson ($ actualMessageList )
96
- )
95
+ $ this ->getPrettyJson ($ actualMessageList ),
96
+ ),
97
97
);
98
98
}
99
99
@@ -114,8 +114,8 @@ public function allTransportMessagesShouldBeJson(string $transportName, PyString
114
114
throw new Exception (
115
115
sprintf (
116
116
'The expected transport messages doesn \'t match actual: %s ' ,
117
- $ this ->getPrettyJson ($ actualMessageList )
118
- )
117
+ $ this ->getPrettyJson ($ actualMessageList ),
118
+ ),
119
119
);
120
120
}
121
121
}
@@ -126,7 +126,7 @@ public function allTransportMessagesShouldBeJson(string $transportName, PyString
126
126
public function allTransportMessagesShouldBeJsonWithVariableFields (
127
127
string $ transportName ,
128
128
string $ variableFields ,
129
- PyStringNode $ expectedMessageList
129
+ PyStringNode $ expectedMessageList,
130
130
): void {
131
131
$ variableFields = $ variableFields ? array_map ('trim ' , explode (', ' , $ variableFields )) : [];
132
132
$ expectedMessageList = $ this ->decodeExpectedJson ($ expectedMessageList );
@@ -137,12 +137,12 @@ public function allTransportMessagesShouldBeJsonWithVariableFields(
137
137
$ actualMessageList [] = $ this ->convertToArray ($ envelope ->getMessage ());
138
138
}
139
139
140
- if (!$ this ->isMessagesAreSimilar ($ expectedMessageList , $ actualMessageList , $ variableFields )) {
140
+ if (!$ this ->isMessagesAreSimilar ($ expectedMessageList , $ actualMessageList , $ variableFields, true )) {
141
141
throw new Exception (
142
142
sprintf (
143
143
'The expected transport messages doesn \'t match actual: %s ' ,
144
- $ this ->getPrettyJson ($ actualMessageList )
145
- )
144
+ $ this ->getPrettyJson ($ actualMessageList ),
145
+ ),
146
146
);
147
147
}
148
148
}
@@ -160,14 +160,15 @@ public function thereIsCountMessagesInTransport(int $expectedMessageCount, strin
160
160
sprintf (
161
161
'In transport exist actual count: %s, but expected count: %s ' ,
162
162
$ actualMessageCount ,
163
- $ expectedMessageCount
164
- )
163
+ $ expectedMessageCount,
164
+ ),
165
165
);
166
166
}
167
167
}
168
168
169
169
/**
170
170
* @param array<mixed> $message
171
+ *
171
172
* @return string|bool
172
173
*/
173
174
private function getPrettyJson (array $ message )
@@ -177,28 +178,25 @@ private function getPrettyJson(array $message)
177
178
178
179
/**
179
180
* @param mixed $object
181
+ *
180
182
* @return array<mixed>
181
183
*/
182
184
private function convertToArray ($ object ): array
183
185
{
184
- return (array ) $ this ->normalizer ->normalize ($ object );
186
+ return (array )$ this ->normalizer ->normalize ($ object );
185
187
}
186
188
187
189
/**
188
190
* @return array<mixed>
189
191
*/
190
192
private function decodeExpectedJson (PyStringNode $ expectedJson ): array
191
193
{
192
- try {
193
- return json_decode (
194
- trim ($ expectedJson ->getRaw ()),
195
- true ,
196
- 512 ,
197
- JSON_THROW_ON_ERROR
198
- );
199
- } catch (JsonException $ e ) {
200
- dd (trim ($ expectedJson ->getRaw ()), $ expectedJson ->getRaw (), $ e );
201
- }
194
+ return json_decode (
195
+ trim ($ expectedJson ->getRaw ()),
196
+ true ,
197
+ 512 ,
198
+ JSON_THROW_ON_ERROR ,
199
+ );
202
200
}
203
201
204
202
private function getMessengerTransportByName (string $ transportName ): InMemoryTransport
@@ -217,22 +215,33 @@ private function getMessengerTransportByName(string $transportName): InMemoryTra
217
215
}
218
216
219
217
throw new Exception (
220
- 'In memory transport ' . $ fullName . ' not found '
218
+ 'In memory transport ' . $ fullName . ' not found ' ,
221
219
);
222
220
}
223
221
224
222
/**
225
- * @param array $actual<mixed>
226
- * @param array $expected<mixed>
223
+ * @param array $actual <mixed>
224
+ * @param array $expected <mixed>
227
225
* @param string[]|null $requiredFields
228
226
*
229
227
* @return bool
230
228
*/
231
229
private function isMessagesAreSimilar (
232
- array $ actual ,
233
230
array $ expected ,
231
+ array $ actual ,
234
232
?array $ requiredFields = null ,
233
+ bool $ multipleActual = false ,
235
234
): bool {
235
+ if ($ multipleActual ) {
236
+ foreach ($ actual as $ nextActualItem ) {
237
+ if (!$ this ->isMessagesAreSimilar ($ expected , $ nextActualItem , $ requiredFields )) {
238
+ return false ;
239
+ }
240
+ }
241
+
242
+ return true ;
243
+ }
244
+
236
245
$ requiredFields = $ requiredFields ?? array_keys ($ expected );
237
246
238
247
foreach ($ requiredFields as $ requiredField ) {
@@ -244,10 +253,10 @@ private function isMessagesAreSimilar(
244
253
return false ;
245
254
}
246
255
247
- if (is_string ($ expected [$ requiredField ]) && str_starts_with (' ~ ' , $ expected [$ requiredField ])) {
256
+ if (is_string ($ expected [$ requiredField ]) && str_starts_with ($ expected [$ requiredField ], ' ~ ' )) {
248
257
$ pregMatchValue = preg_match (
249
258
sprintf ('|%s| ' , substr ($ expected [$ requiredField ], 1 )),
250
- sprintf ('%s ' , $ actual [$ requiredField ])
259
+ sprintf ('%s ' , $ actual [$ requiredField ]),
251
260
);
252
261
253
262
return !($ pregMatchValue === 0 || $ pregMatchValue === false );
0 commit comments