/**
  * @covers \PhpCqrs\Command\Handler\Exception\NoHandleMethodFound::forMessage
  */
 public function testCanConstructFromCommand()
 {
     $message = GenericCommandMessage::forPayload(new RegisterBook('1234', 'Title!'));
     $exception = NoHandleMethodFound::forMessage($message);
     $this->assertInstanceOf(NoHandleMethodFound::class, $exception);
     $this->assertSame('No handle method for command RegisterBook.', $exception->getMessage());
 }
 /**
  * @return void
  */
 protected function process()
 {
     $stream = $this->events();
     if ($stream->contains('CarRented', 'FlightBooked', 'HotelReserved') && !$stream->contains('OrderReceived')) {
         $this->commands[] = GenericCommandMessage::forPayload(new ConfirmOrder((string) $this->id()));
     }
 }
 /**
  * @covers \PhpCqrs\Command\Dispatcher\MiddlewareCommandDispatcher::__construct
  * @covers \PhpCqrs\Command\Dispatcher\MiddlewareCommandDispatcher::dispatch
  */
 public function testCanDispatchCommand()
 {
     $chain = new ProcessableMiddlewareChain();
     $message = GenericCommandMessage::forPayload(new RegisterBook('1234', 'Title!'));
     $dispatcher = new MiddlewareCommandDispatcher($chain);
     $dispatcher->dispatch($message);
     $this->assertSame('RegisterBook', $chain->processed());
 }
 /**
  * @covers \PhpCqrs\Command\Dispatcher\LocatorCommandDispatcher::__construct
  * @covers \PhpCqrs\Command\Dispatcher\LocatorCommandDispatcher::dispatch
  */
 public function testCanDispatchCommand()
 {
     $locator = new InMemoryLocator();
     $handler = new PublishBookCommandHandler();
     $locator->register($handler, 'PublishBook');
     $dispatcher = new LocatorCommandDispatcher($locator);
     $payload = new PublishBook('1234');
     $message = GenericCommandMessage::forPayload($payload);
     $dispatcher->dispatch($message);
     $this->assertSame($payload, $handler->handled());
 }
 /**
  * @covers \PhpCqrs\Command\Dispatcher\Middleware\InMemoryMiddlewareChain::proceed
  * @covers \PhpCqrs\Command\Dispatcher\Middleware\InMemoryMiddlewareChain::next
  * @covers \PhpCqrs\Command\Dispatcher\Middleware\InMemoryMiddlewareChain::end
  */
 public function testCanProceedMultipleCommand()
 {
     $middleware = new CounterMiddleware();
     $chain = new InMemoryMiddlewareChain();
     $chain->register($middleware);
     $message = GenericCommandMessage::forPayload(new PublishBook('1234'));
     $chain->proceed($message);
     $chain->proceed($message);
     $chain->proceed($message);
     $this->assertCount(3, $middleware);
 }
 /**
  * @covers \PhpCqrs\Command\Dispatcher\Middleware\CommandHandler\CommandHandlerMiddleware::__construct
  * @covers \PhpCqrs\Command\Dispatcher\Middleware\CommandHandler\CommandHandlerMiddleware::handle
  */
 public function testCanHandleCommand()
 {
     $payload = new PublishBook('1234');
     $handler = new PublishBookCommandHandler();
     $message = GenericCommandMessage::forPayload($payload);
     $locator = new InMemoryLocator();
     $locator->register($handler, 'PublishBook');
     $middleware = new CommandHandlerMiddleware($locator);
     $chain = new InMemoryMiddlewareChain();
     $this->assertSame($message, $middleware->handle($message, $chain));
     $this->assertSame($payload, $handler->handled());
 }
 /**
  * @covers \PhpCqrs\Command\Handler\Locator\InMemoryLocator::locate
  * @expectedException \PhpCqrs\Command\Handler\Locator\Exception\NoCommandHandlerFound
  */
 public function testCanNotLocateCommandHandler()
 {
     $message = GenericCommandMessage::forPayload(new PublishBook('1234'));
     $locator = new InMemoryLocator();
     $locator->locate($message);
 }
 /**
  * @covers \PhpCqrs\Command\Handler\CallableCommandHandler::handle
  * @covers \PhpCqrs\Command\Handler\CallableCommandHandler::method
  * @expectedException \PhpCqrs\Command\Handler\Exception\NoHandleMethodFound
  */
 public function testCanNotHandleMethod()
 {
     $handler = new RegisterBookCommandHandler();
     $message = GenericCommandMessage::forPayload(new PublishBook('1234'));
     $handler->handle($message);
 }
 /**
  * @covers \PhpCqrs\Command\Message\GenericCommandMessage::forPayloadAndMetaData
  */
 public function testCanConstructForPayloadAndMetaData()
 {
     $message = GenericCommandMessage::forPayloadAndMetaData(new RegisterBook('1234', 'Title!'), new MetaData());
     $this->assertInstanceOf(GenericCommandMessage::class, $message);
 }