Skip to content

Commit

Permalink
Merge pull request #665 from patchlevel/fix-chain-translator
Browse files Browse the repository at this point in the history
Fix ChainTranslator: Dont override the result keys, merge instead
  • Loading branch information
DavidBadura authored Dec 30, 2024
2 parents d9a2790 + ff5470a commit 7724096
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
6 changes: 2 additions & 4 deletions src/Message/Translator/ChainTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

use Patchlevel\EventSourcing\Message\Message;

use function array_values;

final class ChainTranslator implements Translator
{
/** @param iterable<Translator> $translators */
Expand Down Expand Up @@ -38,9 +36,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));
}
}

0 comments on commit 7724096

Please sign in to comment.