-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from Nyholm/predefined
Added a middleware to only publish predefined messages
- Loading branch information
Showing
3 changed files
with
182 additions
and
3 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,52 @@ | ||
<?php | ||
|
||
namespace SimpleBus\Asynchronous\MessageBus; | ||
|
||
use SimpleBus\Asynchronous\Publisher\Publisher; | ||
use SimpleBus\Message\Bus\Middleware\MessageBusMiddleware; | ||
use SimpleBus\Message\Name\MessageNameResolver; | ||
|
||
class PublishesPredefinedMessages implements MessageBusMiddleware | ||
{ | ||
/** | ||
* @var \SimpleBus\Asynchronous\Publisher\Publisher | ||
*/ | ||
private $publisher; | ||
|
||
/** | ||
* @var MessageNameResolver | ||
*/ | ||
private $messageNameResolver; | ||
|
||
/** | ||
* @var array names | ||
*/ | ||
private $names; | ||
|
||
/** | ||
* @param Publisher $publisher | ||
* @param MessageNameResolver $messageNameResolver | ||
* @param array $names an array with names on messages to be published. | ||
*/ | ||
public function __construct(Publisher $publisher, MessageNameResolver $messageNameResolver, array $names) | ||
{ | ||
$this->publisher = $publisher; | ||
$this->messageNameResolver = $messageNameResolver; | ||
$this->names = $names; | ||
} | ||
|
||
/** | ||
* Handle a message by publishing it to a queue (always), then calling the next middleware | ||
* | ||
* {@inheritdoc} | ||
*/ | ||
public function handle($message, callable $next) | ||
{ | ||
$name = $this->messageNameResolver->resolve($message); | ||
if (in_array($name, $this->names)) { | ||
$this->publisher->publish($message); | ||
} | ||
|
||
$next($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,93 @@ | ||
<?php | ||
|
||
namespace SimpleBus\Asynchronous\Tests\MessageBus; | ||
|
||
use SimpleBus\Asynchronous\MessageBus\PublishesPredefinedMessages; | ||
use SimpleBus\Asynchronous\Publisher\Publisher; | ||
use SimpleBus\Message\CallableResolver\Exception\UndefinedCallable; | ||
use SimpleBus\Message\Name\MessageNameResolver; | ||
|
||
class PublishesPredefinedMessagesTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @test | ||
*/ | ||
public function it_calls_the_next_middleware_and_when_the_message_is_handled_it_does_not_publish_it() | ||
{ | ||
$message = $this->dummyMessage(); | ||
|
||
$nextCallableCalled = false; | ||
$alwaysSucceedingNextCallable = function ($actualMessage) use ($message, &$nextCallableCalled) { | ||
$nextCallableCalled = true; | ||
$this->assertSame($message, $actualMessage); | ||
}; | ||
|
||
$publisher = $this->mockPublisher(); | ||
$publisher | ||
->expects($this->never()) | ||
->method('publish'); | ||
|
||
$nameResolver = $this->mockNameResolver(); | ||
$nameResolver->method('resolve') | ||
->willReturn('foo'); | ||
|
||
$middleware = new PublishesPredefinedMessages($publisher, $nameResolver, ['baz', 'bar']); | ||
|
||
$middleware->handle($message, $alwaysSucceedingNextCallable); | ||
|
||
$this->assertTrue($nextCallableCalled); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_calls_the_next_middleware_and_when_the_message_has_no_handler_it_publishes_it() | ||
{ | ||
$message = $this->dummyMessage(); | ||
|
||
$nextCallableCalled = false; | ||
$alwaysSucceedingNextCallable = function ($actualMessage) use ($message, &$nextCallableCalled) { | ||
$nextCallableCalled = true; | ||
$this->assertSame($message, $actualMessage); | ||
}; | ||
|
||
$publisher = $this->mockPublisher(); | ||
$publisher | ||
->expects($this->once()) | ||
->method('publish') | ||
->with($this->identicalTo($message)); | ||
|
||
$nameResolver = $this->mockNameResolver(); | ||
$nameResolver->method('resolve') | ||
->willReturn('foo'); | ||
|
||
$middleware = new PublishesPredefinedMessages($publisher, $nameResolver, ['baz', 'foo', 'bar']); | ||
|
||
$middleware->handle($message, $alwaysSucceedingNextCallable); | ||
|
||
$this->assertTrue($nextCallableCalled); | ||
} | ||
|
||
/** | ||
* @return \PHPUnit_Framework_MockObject_MockObject|Publisher | ||
*/ | ||
private function mockPublisher() | ||
{ | ||
return $this->getMock('SimpleBus\Asynchronous\Publisher\Publisher'); | ||
} | ||
/** | ||
* @return \PHPUnit_Framework_MockObject_MockObject|MessageNameResolver | ||
*/ | ||
private function mockNameResolver() | ||
{ | ||
return $this->getMock('SimpleBus\Message\Name\MessageNameResolver'); | ||
} | ||
|
||
/** | ||
* @return \PHPUnit_Framework_MockObject_MockObject|object | ||
*/ | ||
private function dummyMessage() | ||
{ | ||
return new \stdClass(); | ||
} | ||
} |