|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace HelloFresh\Tests\Engine\CommandBus; |
| 4 | + |
| 5 | +use HelloFresh\Engine\CommandBus\TacticianCommandBus; |
| 6 | +use HelloFresh\Tests\Engine\Mock\InvalidHandler; |
| 7 | +use HelloFresh\Tests\Engine\Mock\TestCommand; |
| 8 | +use HelloFresh\Tests\Engine\Mock\TestHandler; |
| 9 | +use League\Tactician\CommandBus; |
| 10 | +use League\Tactician\Handler\CommandHandlerMiddleware; |
| 11 | +use League\Tactician\Handler\CommandNameExtractor\ClassNameExtractor; |
| 12 | +use League\Tactician\Handler\Locator\HandlerLocator; |
| 13 | +use League\Tactician\Handler\Locator\InMemoryLocator; |
| 14 | +use League\Tactician\Handler\MethodNameInflector\HandleInflector; |
| 15 | +use League\Tactician\Plugins\LockingMiddleware; |
| 16 | + |
| 17 | +class TacticianCommandBusTest extends \PHPUnit_Framework_TestCase |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @var InMemoryLocator |
| 21 | + */ |
| 22 | + private $locator; |
| 23 | + /** |
| 24 | + * @var CommandBus |
| 25 | + */ |
| 26 | + private $internalCommandBus; |
| 27 | + /** |
| 28 | + * @var TacticianCommandBus |
| 29 | + */ |
| 30 | + private $commandBus; |
| 31 | + |
| 32 | + protected function setUp() |
| 33 | + { |
| 34 | + $this->locator = new InMemoryLocator(); |
| 35 | + $this->internalCommandBus = self::createASimpleBus($this->locator); |
| 36 | + |
| 37 | + $this->commandBus = new TacticianCommandBus($this->internalCommandBus); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * @test |
| 42 | + */ |
| 43 | + public function itExecutesAMessage() |
| 44 | + { |
| 45 | + $handler = new TestHandler(); |
| 46 | + $this->locator->addHandler($handler, TestCommand::class); |
| 47 | + |
| 48 | + $command = new TestCommand("hey"); |
| 49 | + $this->commandBus->execute($command); |
| 50 | + $this->commandBus->execute($command); |
| 51 | + $this->commandBus->execute($command); |
| 52 | + |
| 53 | + $this->assertSame(3, $handler->getCounter()); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * @test |
| 58 | + * @expectedException \HelloFresh\Engine\CommandBus\Exception\MissingHandlerException |
| 59 | + */ |
| 60 | + public function itFailsWhenThereIsNoHandlers() |
| 61 | + { |
| 62 | + $command = new TestCommand("hey"); |
| 63 | + $this->commandBus->execute($command); |
| 64 | + |
| 65 | + $handler = new TestHandler(); |
| 66 | + $this->assertSame(0, $handler->getCounter()); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @test |
| 71 | + * @expectedException \HelloFresh\Engine\CommandBus\Exception\CanNotInvokeHandlerException |
| 72 | + */ |
| 73 | + public function itFailsWhenHandlerHasAnInvalidHandleMethod() |
| 74 | + { |
| 75 | + $handler = new InvalidHandler(); |
| 76 | + $this->locator->addHandler($handler, TestCommand::class); |
| 77 | + |
| 78 | + $command = new TestCommand("hey"); |
| 79 | + $this->commandBus->execute($command); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Create a tactician command bus that uses the same convention as SimpleCommandBus. |
| 84 | + * |
| 85 | + * @param HandlerLocator $commandLocator |
| 86 | + * @return CommandBus |
| 87 | + */ |
| 88 | + public static function createASimpleBus(HandlerLocator $commandLocator) |
| 89 | + { |
| 90 | + return new CommandBus([ |
| 91 | + new LockingMiddleware(), |
| 92 | + new CommandHandlerMiddleware( |
| 93 | + new ClassNameExtractor(), |
| 94 | + $commandLocator, |
| 95 | + new HandleInflector() |
| 96 | + ) |
| 97 | + ]); |
| 98 | + } |
| 99 | +} |
0 commit comments