public function registerCommandHandler($commandName, CommandHandlerInterface $commandHandler)
 {
     $this->registerAggregateCommandHandlers();
     $this->explicitCommandHandlersSet = true;
     $this->commandBus->subscribe($commandName, $commandHandler);
     return $this;
 }
 public function testRemoteDispatchReplyTimeout()
 {
     $this->localSegment->subscribe(TestCommand::class, new TestCommandHandler());
     $remoteTemplate = new RedisTemplate('tcp://127.0.0.1:6379?read_write_timeout=-1', 'test-node2', []);
     $remoteTemplate->subscribe(TestCommand::class);
     $this->template->setTimeout(1);
     $interceptor = new DispatchInterceptor();
     $this->localSegment->setDispatchInterceptors([$interceptor]);
     $callback = new ClosureCommandCallback(function ($result) {
         $this->fail('Exception expected');
     }, function ($error) {
         $this->assertInstanceOf(CommandTimeoutException::class, $error);
     });
     $this->testSubject->send('key', GenericCommandMessage::asCommandMessage(new TestCommand('key')), $callback);
     $this->assertEmpty($interceptor->commands);
     $this->assertEmpty($this->template->getClient()->keys('governor:response:*'));
     $this->assertCount(1, $this->template->getPendingCommands($remoteTemplate->getNodeName()));
 }
Ejemplo n.º 3
0
 /**
  * @Test
  * public void testDispatchCommand_UnitOfWorkIsCommittedOnCheckedException() {
  * UnitOfWorkFactory mockUnitOfWorkFactory = mock(DefaultUnitOfWorkFactory.class);
  * UnitOfWork mockUnitOfWork = mock(UnitOfWork.class);
  * when(mockUnitOfWorkFactory.createUnitOfWork()).thenReturn(mockUnitOfWork);
  *
  * testSubject.setUnitOfWorkFactory(mockUnitOfWorkFactory);
  * testSubject.subscribe(String.class.getName(), new CommandHandler<String>() {
  * @Override
  * public Object handle(CommandMessage<String> command, UnitOfWork unitOfWork) throws Throwable {
  * throw new Exception();
  * }
  * });
  * testSubject.setRollbackConfiguration(new RollbackOnUncheckedExceptionConfiguration());
  *
  * testSubject.dispatch(GenericCommandMessage.asCommandMessage("Say hi!"), new CommandCallback<Object>() {
  * @Override
  * public void onSuccess(Object result) {
  * fail("Expected exception");
  * }
  *
  * @Override
  * public void onFailure(Throwable cause) {
  * assertThat(cause, is(Exception.class));
  * }
  * });
  *
  * verify(mockUnitOfWork).commit();
  * }*/
 public function testInterceptorChain_CommandHandledSuccessfully()
 {
     $mockInterceptor1 = \Phake::mock(CommandHandlerInterceptorInterface::class);
     $mockInterceptor2 = \Phake::mock(CommandHandlerInterceptorInterface::class);
     $commandHandler = \Phake::mock(CommandHandlerInterface::class);
     \Phake::when($mockInterceptor1)->handle(\Phake::anyParameters())->thenGetReturnByLambda(function (CommandMessageInterface $commandMessage, UnitOfWorkInterface $unitOfWork, InterceptorChainInterface $interceptorChain) use($mockInterceptor2) {
         return $mockInterceptor2->handle($commandMessage, $unitOfWork, $interceptorChain);
     });
     \Phake::when($mockInterceptor2)->handle(\Phake::anyParameters())->thenGetReturnByLambda(function (CommandMessageInterface $commandMessage, UnitOfWorkInterface $unitOfWork, InterceptorChainInterface $interceptorChain) use($commandHandler) {
         return $commandHandler->handle($commandMessage, $unitOfWork);
     });
     \Phake::when($commandHandler)->handle(\Phake::anyParameters())->thenReturn(new TestCommand("Hi there!"));
     $subject = new SimpleCommandBus(new DefaultUnitOfWorkFactory());
     $subject->setHandlerInterceptors([$mockInterceptor1, $mockInterceptor2]);
     $subject->subscribe(TestCommand::class, $commandHandler);
     $command = GenericCommandMessage::asCommandMessage(new TestCommand("Hi there!"));
     $callback = new ClosureCommandCallback(function ($result) {
         $this->assertEquals("Hi there!", $result->getText());
     }, function ($exception) {
         $this->fail("Did not expect exception");
     });
     $subject->dispatch($command, $callback);
     \Phake::inOrder(\Phake::verify($mockInterceptor1)->handle(\Phake::anyParameters()), \Phake::verify($mockInterceptor2)->handle(\Phake::anyParameters()), \Phake::verify($commandHandler)->handle(\Phake::anyParameters()));
 }
$logger->pushHandler(new StreamHandler('php://stdout', Logger::DEBUG));
// 1. create a command bus and command gateway
$commandBus = new SimpleCommandBus();
$commandBus->setLogger($logger);
$commandGateway = new DefaultCommandGateway($commandBus);
$rootDirectory = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'CommandHandlerExample';
@mkdir($rootDirectory);
echo sprintf("Initializing FileSystemEventStore in %s\n", $rootDirectory);
// 2. initialize the event store
$eventStore = new FilesystemEventStore(new SimpleEventFileResolver($rootDirectory), new JMSSerializer());
// 3. create the event bus
$eventBus = new SimpleEventBus();
$eventBus->setLogger($logger);
// 4. create an event sourcing repository
$repository = new EventSourcingRepository(User::class, $eventBus, new NullLockManager(), $eventStore, new GenericAggregateFactory(User::class));
//5. create and register our commands
$commandHandler = new UserCommandHandler($repository);
$commandBus->subscribe('CommandHandlerExample\\CreateUserCommand', $commandHandler);
$commandBus->subscribe('CommandHandlerExample\\ChangeUserEmailCommand', $commandHandler);
//6. create and register the eventlistener
$eventListener = new UserEventListener();
$eventBus->subscribe($eventListener);
//7. send commands
$aggregateIdentifier = Uuid::uuid1()->toString();
$commandGateway->send(new CreateUserCommand($aggregateIdentifier, '*****@*****.**'));
$commandGateway->send(new ChangeUserEmailCommand($aggregateIdentifier, '*****@*****.**'));
//8. read back aggregate from store
$uow = DefaultUnitOfWork::startAndGet($logger);
$aggregate = $repository->load($aggregateIdentifier);
echo sprintf("\n\nUser identifier:%s, email:%s, version:%s\n\n", $aggregate->getIdentifier(), $aggregate->getEmail(), $aggregate->getVersion());
$uow->rollback();