Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
); | ||
} | ||
} |