Skip to content
This repository was archived by the owner on Jul 19, 2024. It is now read-only.

Commit 549d47d

Browse files
fix dispatchBatch not injecting attributes + add unit test
1 parent 86a7998 commit 549d47d

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

src/EventManager.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ public function dispatch($channel, EventInterface $event)
176176
public function dispatchBatch($channel, array $events)
177177
{
178178
$messages = array_map(function (EventInterface $event) {
179+
$event = $this->prepEventForDispatch($event);
179180
return $event->toMessage();
180181
}, $events);
181182
$this->adapter->publishBatch($channel, $messages);

tests/EventManagerTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,51 @@ public function testDispatchBatch()
188188
];
189189
$manager->dispatchBatch('channel', $events);
190190
}
191+
192+
public function testDispatchBatchWithInjections()
193+
{
194+
$adapter = Mockery::mock(PubSubAdapterInterface::class);
195+
$adapter->shouldReceive('publishBatch')
196+
->withArgs([
197+
'channel',
198+
[
199+
[
200+
'event' => 'user.created',
201+
'user' => [
202+
'id' => 1234,
203+
],
204+
'service' => 'user-api',
205+
'cluster' => 'api',
206+
],
207+
[
208+
'event' => 'user.created',
209+
'user' => [
210+
'id' => 7812,
211+
],
212+
'service' => 'user-api',
213+
'cluster' => 'api',
214+
],
215+
],
216+
]);
217+
218+
$translator = Mockery::mock(MessageTranslatorInterface::class);
219+
220+
$injectors = [
221+
new GenericAttributeInjector('service', 'user-api'),
222+
function () {
223+
return [
224+
'key' => 'cluster',
225+
'value' => 'api',
226+
];
227+
},
228+
];
229+
230+
$manager = new EventManager($adapter, $translator, null, $injectors);
231+
232+
$events = [
233+
new SimpleEvent('user.created', ['user' => ['id' => 1234]]),
234+
new SimpleEvent('user.created', ['user' => ['id' => 7812]]),
235+
];
236+
$manager->dispatchBatch('channel', $events);
237+
}
191238
}

0 commit comments

Comments
 (0)