|
11 | 11 |
|
12 | 12 | namespace Prooph\EventMachineTest; |
13 | 13 |
|
| 14 | +use Prooph\Common\Messaging\Message as ProophMessage; |
| 15 | +use Prooph\EventMachine\Commanding\CommandPreProcessor; |
| 16 | +use Prooph\EventMachine\Container\EventMachineContainer; |
14 | 17 | use Prooph\EventMachine\EventMachine; |
| 18 | +use Prooph\EventMachine\Messaging\Message; |
15 | 19 | use Prooph\EventMachine\Messaging\MessageDispatcher; |
16 | 20 | use Prooph\EventMachine\Persistence\DocumentStore; |
17 | 21 | use Prooph\EventMachine\Runtime\Flavour; |
18 | 22 | use Prooph\EventMachine\Runtime\PrototypingFlavour; |
19 | 23 | use ProophExample\PrototypingFlavour\Aggregate\UserDescription; |
20 | 24 | use ProophExample\PrototypingFlavour\Aggregate\UserState; |
| 25 | +use ProophExample\PrototypingFlavour\Messaging\CommandWithCustomHandler; |
21 | 26 | use ProophExample\PrototypingFlavour\Messaging\MessageDescription; |
22 | 27 | use ProophExample\PrototypingFlavour\ProcessManager\SendWelcomeEmail; |
23 | 28 | use ProophExample\PrototypingFlavour\Projector\RegisteredUsersProjector; |
@@ -83,4 +88,47 @@ public function it_throws_exception_if_config_should_be_cached_but_contains_clos |
83 | 88 |
|
84 | 89 | $eventMachine->compileCacheableConfig(); |
85 | 90 | } |
| 91 | + |
| 92 | + /** |
| 93 | + * @test |
| 94 | + */ |
| 95 | + public function it_stops_dispatch_if_preprocessor_sets_metadata_flag() |
| 96 | + { |
| 97 | + $eventMachine = new EventMachine(); |
| 98 | + |
| 99 | + $eventMachine->load(CommandWithCustomHandler::class); |
| 100 | + |
| 101 | + $noOpHandler = new class() implements CommandPreProcessor { |
| 102 | + private $msg; |
| 103 | + |
| 104 | + /** |
| 105 | + * {@inheritdoc} |
| 106 | + */ |
| 107 | + public function preProcess(ProophMessage $message): ProophMessage |
| 108 | + { |
| 109 | + if ($message instanceof Message) { |
| 110 | + $this->msg = $message->get('msg'); |
| 111 | + } |
| 112 | + |
| 113 | + return $message->withAddedMetadata(EventMachine::CMD_METADATA_STOP_DISPATCH, true); |
| 114 | + } |
| 115 | + |
| 116 | + public function msg(): ?string |
| 117 | + { |
| 118 | + return $this->msg; |
| 119 | + } |
| 120 | + }; |
| 121 | + |
| 122 | + $eventMachine->initialize(new EventMachineContainer($eventMachine)); |
| 123 | + |
| 124 | + $eventMachine->bootstrapInTestMode([], [ |
| 125 | + CommandWithCustomHandler::NO_OP_HANDLER => $noOpHandler, |
| 126 | + ]); |
| 127 | + |
| 128 | + $eventMachine->dispatch(CommandWithCustomHandler::CMD_DO_NOTHING, [ |
| 129 | + 'msg' => 'test', |
| 130 | + ]); |
| 131 | + |
| 132 | + $this->assertEquals('test', $noOpHandler->msg()); |
| 133 | + } |
86 | 134 | } |
0 commit comments