public function testAndMetaData()
 {
     $payload = new \stdClass();
     $metaDataMap = array("key" => "value");
     $metaData = new MetaData($metaDataMap);
     $message = new GenericCommandMessage($payload, $metaData);
     $message1 = $message->andMetaData();
     $message2 = $message->andMetaData(array("key" => "otherValue"));
     $this->assertEquals(1, count($message1->getMetaData()));
     $this->assertEquals("value", $message1->getMetaData()->get("key"));
     $this->assertEquals(1, count($message2->getMetaData()));
     $this->assertEquals("otherValue", $message2->getMetaData()->get("key"));
 }
 public function testSubscribe()
 {
     $commandHandler = $this->getMock(CommandHandlerInterface::class);
     $this->subject->subscribe(get_class(new TestCommand('hi')), $commandHandler);
     $handler = $this->subject->findCommandHandlerFor(GenericCommandMessage::asCommandMessage(new TestCommand('hi')));
     $this->assertInstanceOf(CommandHandlerInterface::class, $handler);
 }
 public function testResolveTarget_WithAnnotatedFields()
 {
     $aggregateIdentifier = Uuid::uuid1();
     $version = 1;
     $actual = $this->testSubject->resolveTarget(GenericCommandMessage::asCommandMessage(new FieldAnnotatedCommand($aggregateIdentifier, $version)));
     $this->assertEquals($aggregateIdentifier, $actual->getIdentifier());
     $this->assertEquals($version, $actual->getVersion());
 }
Пример #4
0
 public function testFromAndToBytes()
 {
     $serializer = new JMSSerializer();
     $commandMessage = GenericCommandMessage::asCommandMessage(new ResultPayload("string", 10, 5.1));
     $dispatchMessage1 = new DispatchMessage($commandMessage, $serializer, false);
     $bytes = $dispatchMessage1->toBytes();
     $dispatchMessage2 = DispatchMessage::fromBytes($serializer, $bytes);
     $this->assertEquals($dispatchMessage1->getCommandIdentifier(), $dispatchMessage2->getCommandIdentifier());
     $this->assertEquals($dispatchMessage1->isExpectReply(), $dispatchMessage2->isExpectReply());
     $this->assertEquals($dispatchMessage1->getCommandMessage(), $dispatchMessage2->getCommandMessage());
     $this->assertFalse($dispatchMessage1->isExpectReply());
     $this->assertFalse($dispatchMessage2->isExpectReply());
 }
 public function handle(CommandMessageInterface $commandMessage, UnitOfWorkInterface $unitOfWork, InterceptorChainInterface $interceptorChain)
 {
     return $interceptorChain->proceed(GenericCommandMessage::asCommandMessage(new Payload("testing")));
 }
 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()));
 }
 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()));
 }
 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();
     }
 }
Пример #10
0
 private function listOfOneCommandMessage($msg)
 {
     return array(GenericCommandMessage::asCommandMessage($msg));
 }
 /**
  * @param $command
  * @return GenericCommandMessage
  */
 protected function createCommandMessage($command)
 {
     $message = GenericCommandMessage::asCommandMessage($command);
     return $message->withMetaData(CorrelationDataHolder::getCorrelationData())->andMetaData($message->getMetaData()->all());
 }