public function setUp() { $inMemoryAdapter = new InMemoryAdapter(); $eventEmitter = new ProophActionEventEmitter(); $this->eventStore = new EventStore($inMemoryAdapter, $eventEmitter); $this->repository = new AggregateRepository($this->eventStore, AggregateType::fromAggregateRootClass('ProophTest\\EventStore\\Mock\\User'), new ConfigurableAggregateTranslator()); $this->result = []; $self = $this; $router = new CommandRouter(); $router->route(TakeSnapshot::class)->to(function (TakeSnapshot $command) use($self) { $self->result[] = ['aggregate_type' => $command->aggregateType(), 'aggregate_id' => $command->aggregateId()]; }); $commandBus = new CommandBus(); $commandBus->utilize($router); $plugin = new SnapshotPlugin($commandBus, 2); $plugin->setUp($this->eventStore); $this->eventStore->beginTransaction(); $this->eventStore->create(new Stream(new StreamName('event_stream'), new \ArrayIterator())); $this->eventStore->commit(); }
/** * @test */ public function it_publishes_take_snapshot_commands_for_all_known_aggregates() { $inMemoryAdapter = new InMemoryAdapter(); $eventEmitter = new ProophActionEventEmitter(); $eventStore = new EventStore($inMemoryAdapter, $eventEmitter); $repository = new AggregateRepository($eventStore, AggregateType::fromAggregateRootClass('ProophTest\\EventStore\\Mock\\User'), new ConfigurableAggregateTranslator()); $result = []; $router = new CommandRouter(); $router->route(TakeSnapshot::class)->to(function (TakeSnapshot $command) use(&$result) { $result[] = ['aggregate_type' => $command->aggregateType(), 'aggregate_id' => $command->aggregateId()]; }); $commandBus = new CommandBus(); $commandBus->utilize($router); $plugin = new SnapshotPlugin($commandBus, 2); $plugin->setUp($eventStore); $eventStore->beginTransaction(); $eventStore->create(new Stream(new StreamName('event_stream'), new \ArrayIterator())); $eventStore->commit(); $eventStore->beginTransaction(); $user = User::create('Alex', '*****@*****.**'); $repository->addAggregateRoot($user); $eventStore->commit(); $eventStore->beginTransaction(); $user = $repository->getAggregateRoot($user->getId()->toString()); $user->changeName('John'); $user->changeName('Jane'); $user->changeName('Jim'); $eventStore->commit(); $eventStore->beginTransaction(); $eventWithoutMetadata1 = UsernameChanged::with(['new_name' => 'John Doe'], 5); $eventWithoutMetadata2 = UsernameChanged::with(['new_name' => 'Jane Doe'], 6); $eventStore->appendTo(new StreamName('event_stream'), new \ArrayIterator([$eventWithoutMetadata1, $eventWithoutMetadata2])); $eventStore->commit(); $this->assertCount(2, $result); $this->assertArrayHasKey('aggregate_type', $result[0]); $this->assertArrayHasKey('aggregate_id', $result[0]); $this->assertArrayHasKey('aggregate_type', $result[1]); $this->assertArrayHasKey('aggregate_id', $result[1]); $this->assertEquals(User::class, $result[0]['aggregate_type']); $this->assertEquals(User::class, $result[1]['aggregate_type']); }
} /** * This method is called when message is instantiated named constructor fromArray * * @param array $payload * @return void */ protected function setPayload(array $payload) { $this->text = $payload['text']; } } } namespace { use Prooph\ServiceBus\CommandBus; use Prooph\ServiceBus\Example\Command\EchoText; use Prooph\ServiceBus\Plugin\Router\CommandRouter; $commandBus = new CommandBus(); $router = new CommandRouter(); //Register a callback as CommandHandler for the EchoText command $router->route('Prooph\\ServiceBus\\Example\\Command\\EchoText')->to(function (EchoText $aCommand) { echo $aCommand->getText(); }); //Expand command bus with the router plugin $commandBus->utilize($router); //We create a new Command $echoText = new EchoText('It works'); //... and dispatch it $commandBus->dispatch($echoText); //Output should be: It works }
/** * @test */ public function it_takes_a_routing_definition_on_instantiation() { $router = new CommandRouter(['ProophTest\\ServiceBus\\Mock\\DoSomething' => 'DoSomethingHandler']); $actionEvent = new DefaultActionEvent(MessageBus::EVENT_ROUTE, new CommandBus(), [MessageBus::EVENT_PARAM_MESSAGE_NAME => 'ProophTest\\ServiceBus\\Mock\\DoSomething']); $router->onRouteMessage($actionEvent); $this->assertEquals("DoSomethingHandler", $actionEvent->getParam(MessageBus::EVENT_PARAM_MESSAGE_HANDLER)); }