public function when($command, array $metaData = array()) { try { $this->finalizeConfiguration(); $resultValidator = new ResultValidatorImpl($this->storedEvents, $this->publishedEvents); $this->commandBus->setHandlerInterceptors(array(new AggregateRegisteringInterceptor($this->workingAggregate))); $this->commandBus->dispatch(GenericCommandMessage::asCommandMessage($command)->andMetaData($metaData), $resultValidator); $this->detectIllegalStateChanges(); $resultValidator->assertValidRecording(); return $resultValidator; } finally { //FixtureResourceParameterResolverFactory.clear(); } }
public function testDispatchCommand_ImplicitUnitOfWorkIsRolledBackOnException() { $command = new TestCommand("Say hi!"); $test = $this; $this->handlerRegistry->subscribe(TestCommand::class, new CallbackCommandHandler(function (CommandMessageInterface $commandMessage, UnitOfWorkInterface $unitOfWork) use($test) { $test->assertTrue(CurrentUnitOfWork::isStarted()); $test->assertNotNull(CurrentUnitOfWork::get()); throw new \RuntimeException("exception"); })); $callback = new ClosureCommandCallback(function ($result) use($command) { $this->fail("Did not expect exception"); }, function ($exception) { $this->assertEquals(\RuntimeException::class, get_class($exception)); }); $this->commandBus->dispatch(GenericCommandMessage::asCommandMessage($command), $callback); $this->assertFalse(CurrentUnitOfWork::isStarted()); }
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())); }
/** * @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())); }
break; } } } class UserEventListener implements EventListenerInterface { public function handle(EventMessageInterface $event) { print_r($event->getPayload()); } } // set up logging $logger = new Logger('governor'); $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);
} class ChangeUserEmailCommand extends AbstractUserCommand { } class UserEventListener implements EventListenerInterface { public function handle(EventMessageInterface $event) { print_r($event->getPayload()); } } // set up logging $logger = new Logger('governor'); $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 AnnotatedAggregateCommandHandler::subscribe(User::class, $repository, $commandBus); //6. create and register the eventlistener