diff --git a/README.md b/README.md index 60a1035..ab84d6b 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,10 @@ The message handler provider is a callable with a signature similar to the that only requires an array of key/value pairs, where _key_ is a message class and _value_ a message handler callable. + + + + ### Providing handlers The following example demonstrates how to define a message handler provider with a selection @@ -75,6 +79,10 @@ $message_handler_provider = new SimpleMessageHandlerProvider([ ]); ``` + + + + ### Providing handlers with icanboogie/service Of course, if you're using the [icanboogie/service][] package, you can use service references @@ -156,6 +164,30 @@ $provider = $container->get(AddCommandBusPass::DEFAULT_PROVIDER_SERVICE); +## Asserting messages before dispatching + +The [AssertingMessageBus][] decorator can be used to assert message before they are dispatched. One +could use the assertion to reject messages that require special permissions. + +```php + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace ICanBoogie\MessageBus; + +/** + * A MessageBus decorator that asserts messages before dispatching them. + */ +class AssertingMessageBus implements MessageBus +{ + /** + * @var MessageBus + */ + private $message_bus; + + /** + * @var callable + */ + private $assertion; + + /** + * @param MessageBus $message_bus + * @param callable $assertion A callable that should throw an exception if the message shouldn't + * be dispatched. + */ + public function __construct(MessageBus $message_bus, callable $assertion) + { + $this->message_bus = $message_bus; + $this->assertion = $assertion; + } + + /** + * @param object $message + * + * @return mixed + */ + public function dispatch($message) + { + call_user_func($this->assertion, $message); + + return $this->message_bus->dispatch($message); + } +} diff --git a/tests/AssertingMessageBusTest.php b/tests/AssertingMessageBusTest.php new file mode 100644 index 0000000..27080c0 --- /dev/null +++ b/tests/AssertingMessageBusTest.php @@ -0,0 +1,78 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace ICanBoogie\MessageBus; + +use Prophecy\Argument; + +class AssertingMessageBusTest extends \PHPUnit_Framework_TestCase +{ + use MockHelpers; + + public function test_should_not_dispatch_message() + { + $message = (object) []; + $exception = new \Exception(); + + $message_bus = $this->make_message_bus( + function ($message_bus) { + $message_bus->dispatch(Argument::any()) + ->shouldNotBeCalled(); + }, + function ($actual) use ($message, $exception) { + $this->assertSame($message, $actual); + throw $exception; + } + ); + + try + { + $message_bus->dispatch($message); + } catch (\Exception $e) { + $this->assertSame($exception, $e); + return; + } + + $this->fail("Expected exception"); + } + + public function test_should_dispatch_message() + { + $message = (object) []; + $result = uniqid(); + + $message_bus = $this->make_message_bus( + function ($message_bus) use ($message, $result) { + $message_bus->dispatch($message) + ->shouldBeCalled()->willReturn($result); + }, + function ($actual) use ($message) { + $this->assertSame($message, $actual); + } + ); + + $this->assertSame($result, $message_bus->dispatch($message)); + } + + /** + * @param callable $init_message_bus + * @param callable $assertion + * + * @return AssertingMessageBus + */ + private function make_message_bus(callable $init_message_bus, callable $assertion) + { + return new AssertingMessageBus( + $this->mock(MessageBus::class, $init_message_bus), + $assertion + ); + } +}