Skip to content

Commit

Permalink
Add AssertingMessageBus
Browse files Browse the repository at this point in the history
  • Loading branch information
olvlvl committed Sep 23, 2017
1 parent 2c07b3b commit cd0fc36
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 0 deletions.
33 changes: 33 additions & 0 deletions README.md
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
<?php

use ICanBoogie\MessageBus\AssertingMessageBus;

/* @var ICanBoogie\MessageBus\MessageBus $message_bus */

$asserting_message_bus = new AssertingMessageBus($message_bus, function ($message) {

if (/* some condition */)
throw new \LogicException("The message should not be dispatched.");

});
```





----------


Expand Down Expand Up @@ -228,6 +260,7 @@ The package is continuously tested by [Travis CI](http://about.travis-ci.org/).


[documentation]: https://icanboogie.org/api/message-bus/master/
[AssertingMessageBus]: https://icanboogie.org/api/message-bus/master/class-ICanBoogie.MessageBus.AssertingMessageBus.html
[MessageHandlerProvider]: https://icanboogie.org/api/message-bus/master/class-ICanBoogie.MessageBus.MessageHandlerProvider.html
[ShouldBePushed]: https://icanboogie.org/api/message-bus/master/class-ICanBoogie.MessageBus.ShouldBePushed.html
[AddCommandBusPass]: https://icanboogie.org/api/message-bus/master/class-ICanBoogie.MessageBus.Symfony.AddCommandBusPass.html
Expand Down
51 changes: 51 additions & 0 deletions lib/AssertingMessageBus.php
@@ -0,0 +1,51 @@
<?php

/*
* This file is part of the ICanBoogie package.
*
* (c) Olivier Laviale <olivier.laviale@gmail.com>
*
* 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);
}
}
78 changes: 78 additions & 0 deletions tests/AssertingMessageBusTest.php
@@ -0,0 +1,78 @@
<?php

/*
* This file is part of the ICanBoogie package.
*
* (c) Olivier Laviale <olivier.laviale@gmail.com>
*
* 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
);
}
}

0 comments on commit cd0fc36

Please sign in to comment.