Skip to content

Commit

Permalink
Fix subsequent messages raised within upcasting chain (#381)
Browse files Browse the repository at this point in the history
* Add a failing test to demonstrate the fact that subsequent messages raised by an upcaster are removed by the upcaster chain when calling further upcasters

* Add a fix for the issue of subsequent messages raised by an upcaster are being removed by the upcaster chain when calling further upcasters

* Fix code style issues

* Fix unused import
  • Loading branch information
chrisharrison authored and prolic committed Oct 24, 2019
1 parent 720bc85 commit 654e023
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Upcasting/UpcasterChain.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function upcast(Message $message): array
$result = [];

foreach ($messages as $message) {
$result += $upcaster->upcast($message);
$result = \array_merge($result, $upcaster->upcast($message));
}

$messages = $result;
Expand Down
55 changes: 55 additions & 0 deletions tests/Upcasting/UpcasterChainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Prooph\Common\Messaging\Message;
use Prooph\EventStore\Upcasting\NoOpEventUpcaster;
use Prooph\EventStore\Upcasting\SingleEventUpcaster;
use Prooph\EventStore\Upcasting\Upcaster;
use Prooph\EventStore\Upcasting\UpcasterChain;

class UpcasterChainTest extends TestCase
Expand Down Expand Up @@ -55,6 +56,60 @@ public function it_chains_upcasts(): void
$this->assertSame($upcastedMessage3, $messages[1]);
}

/**
* @test
*/
public function it_doesnt_remove_messages_when_a_subsequent_upcaster_returns_fewer_messages()
{
$initialMessage = $this->prophesize(Message::class);
$initialMessage->payload()->willReturn(['name' => 'initialMessage']);
$initialMessage = $initialMessage->reveal();

$subsequentMessage = $this->prophesize(Message::class);
$subsequentMessage->payload()->willReturn(['name' => 'subsequentMessage']);
$subsequentMessage = $subsequentMessage->reveal();

$modifiedSubsequentMessage = $this->prophesize(Message::class);
$modifiedSubsequentMessage->payload()->willReturn(['name' => 'subsequentMessage-modified']);
$modifiedSubsequentMessage = $modifiedSubsequentMessage->reveal();

$furtherSubsequentMessage = $this->prophesize(Message::class);
$furtherSubsequentMessage->payload()->willReturn(['name' => 'furtherSubsequentMessage']);
$furtherSubsequentMessage = $furtherSubsequentMessage->reveal();

$upcasterOne = $this->prophesize(Upcaster::class);
$upcasterOne->upcast($initialMessage)->willReturn([$initialMessage]);

$upcasterTwo = $this->prophesize(Upcaster::class);
$upcasterTwo->upcast($initialMessage)->willReturn([$initialMessage, $subsequentMessage]);

$upcasterThree = $this->prophesize(Upcaster::class);
$upcasterThree->upcast($initialMessage)->willReturn([$initialMessage]);
$upcasterThree->upcast($subsequentMessage)->willReturn([$modifiedSubsequentMessage, $furtherSubsequentMessage]);

$upcasterFour = new NoOpEventUpcaster();

$upcasterChain = new UpcasterChain(
$upcasterOne->reveal(),
$upcasterTwo->reveal(),
$upcasterThree->reveal(),
$upcasterFour
);

$messages = $upcasterChain->upcast($initialMessage);

$expectedMessagePayloads = [
['name' => 'initialMessage'],
['name' => 'subsequentMessage-modified'],
['name' => 'furtherSubsequentMessage'],
];
$messagePayloads = \array_map(function (Message $message) {
return $message->payload();
}, $messages);

$this->assertEquals($expectedMessagePayloads, $messagePayloads);
}

protected function createUpcasterWhoCanUpcast(): SingleEventUpcaster
{
return new class() extends SingleEventUpcaster {
Expand Down

0 comments on commit 654e023

Please sign in to comment.