Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ChainTranslator: Dont override the result keys, merge instead #665

Merged
merged 2 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Message/Translator/ChainTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ private function process(Translator $translator, array $messages): array
$result = [];

foreach ($messages as $message) {
$result += $translator($message);
$result = [...$result, ...$translator($message)];
}

return array_values($result);
return $result;
}
}
35 changes: 31 additions & 4 deletions tests/Unit/Message/Translator/ChainTranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,51 @@ public function testEmptyChain(): void

public function testChain(): void
{
$message = new Message(
$message1 = new Message(
new ProfileCreated(
ProfileId::fromString('1'),
Email::fromString('[email protected]'),
),
);

$message2 = new Message(
new ProfileCreated(
ProfileId::fromString('2'),
Email::fromString('[email protected]'),
),
);
$message3 = new Message(
new ProfileCreated(
ProfileId::fromString('3'),
Email::fromString('[email protected]'),
),
);

$message4 = new Message(
new ProfileCreated(
ProfileId::fromString('4'),
Email::fromString('[email protected]'),
),
);
$message5 = new Message(
new ProfileCreated(
ProfileId::fromString('5'),
Email::fromString('[email protected]'),
),
);

$child1 = $this->prophesize(Translator::class);
$child1->__invoke($message)->willReturn([$message])->shouldBeCalled();
$child1->__invoke($message1)->willReturn([$message2, $message3])->shouldBeCalled();

$child2 = $this->prophesize(Translator::class);
$child2->__invoke($message)->willReturn([$message])->shouldBeCalled();
$child2->__invoke($message2)->willReturn([$message4, $message5])->shouldBeCalled();
$child2->__invoke($message3)->willReturn([$message3])->shouldBeCalled();

$translator = new ChainTranslator([
$child1->reveal(),
$child2->reveal(),
]);

self::assertSame([$message], $translator($message));
self::assertSame([$message4, $message5, $message3], $translator($message1));
}
}
Loading