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()); }
/** * @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())); }