Created
February 21, 2016 13:38
-
-
Save Axxon/979272ae462e21e6f3f7 to your computer and use it in GitHub Desktop.
A asynchronous command & event bus with bernard
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
imports: | |
- { resource: config_dev.yml } | |
framework: | |
test: ~ | |
session: | |
storage_id: session.storage.mock_file | |
profiler: | |
collect: false | |
web_profiler: | |
toolbar: false | |
intercept_redirects: false | |
jms_serializer: | |
metadata: | |
cache: file | |
debug: "%kernel.debug%" | |
file_cache: | |
dir: "%kernel.cache_dir%/serializer" | |
bernard: | |
driver: predis | |
options: | |
phpredis_service: snc_redis.bernard | |
simple_bus_bernard_bundle_bridge: | |
commands: ~ | |
logger: monolog.logger | |
snc_redis: | |
clients: | |
default: | |
type: predis | |
alias: bernard | |
dsn: redis://localhost | |
sub: | |
type: predis | |
alias: sub | |
dsn: redis://localhost | |
services: | |
test_service_handler: | |
class: AppBundle\Tests\TestHandler | |
tags: | |
- {'name': 'asynchronous_command_handler', 'handles': 'AppBundle\Tests\TestCommand'} | |
arguments: | |
- @simple_bus.event_bus.public_event_recorder | |
test_event_create_subscriber: | |
class: AppBundle\Tests\TestEventCreateSubscriber | |
tags: | |
- { name: event_subscriber, subscribes_to: AppBundle\Tests\TestEventCreate } | |
messaging.queue_bus_with_bernard: | |
class: SimpleBus\Message\Bus\Middleware\MessageBusSupportingMiddleware | |
arguments: | |
- | |
- @simple_bus.command_bus.finishes_command_before_handling_next_middleware | |
- @simple_bus.event_bus.handles_recorded_mesages_middleware | |
- @simple_bus.asynchronous.command_bus.publishes_unhandled_commands_middleware | |
- @simple_bus.command_bus.delegates_to_message_handler_middleware |
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
<?php | |
namespace AppBundle\Tests; | |
use SimpleBus\Message\Bus\MessageBus; | |
use SimpleBus\Message\Recorder\PublicMessageRecorder; | |
use Symfony\Bundle\FrameworkBundle\Console\Application; | |
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | |
use Symfony\Component\Console\Input\ArrayInput; | |
use Symfony\Component\Console\Output\ConsoleOutput; | |
use Symfony\Component\DependencyInjection\Container; | |
use JMS\Serializer\Annotation\Type; | |
class TestCase1Test extends KernelTestCase | |
{ | |
/** | |
* @var Container | |
*/ | |
protected static $container; | |
/** | |
* @var MessageBus | |
*/ | |
protected $customBus; | |
public static function setUpBeforeClass() { | |
$kernel = static::createKernel(); | |
$kernel->boot(); | |
self::$kernel = $kernel; | |
self::$container = $kernel->getContainer(); | |
} | |
protected function setUp() | |
{ | |
parent::setUp(); | |
$this->customBus = self::$container->get('messaging.queue_bus_with_bernard'); | |
} | |
private function consumeMessagesFromQueue($queueName) | |
{ | |
$application = new Application(self::$kernel); | |
$application->setAutoExit(false); | |
$input = new ArrayInput([ | |
'command' => "bernard:consume", | |
'queue' => $queueName, | |
'--max-messages' => '1' | |
]); | |
$output = new ConsoleOutput(); | |
$application->run($input, $output); | |
} | |
public function test_case_1() | |
{ | |
$testClass = new TestClass($this->customBus); | |
$testClass->execute(); | |
$this->consumeMessagesFromQueue('asynchronous_commands'); | |
} | |
} | |
class TestClass | |
{ | |
/** | |
* @var MessageBus | |
*/ | |
protected $simpleBus; | |
public function __construct(MessageBus $simpleBus) | |
{ | |
$this->simpleBus = $simpleBus; | |
} | |
public function execute() | |
{ | |
$c = new TestCommand(); | |
$c->setVar("I'm a asynchronous"); | |
$this->simpleBus->handle($c); | |
} | |
} | |
class TestHandler | |
{ | |
/** | |
* @var PublicMessageRecorder | |
*/ | |
private $eventRecorder; | |
/** | |
* TestHandler constructor. | |
* @param PublicMessageRecorder $eventRecorder | |
*/ | |
public function __construct(PublicMessageRecorder $eventRecorder) | |
{ | |
$this->eventRecorder = $eventRecorder; | |
} | |
public function handle(TestCommand $command) | |
{ | |
echo $command->var; | |
$this->eventRecorder->record(new TestEventCreate()); | |
} | |
} | |
class TestEventCreate | |
{ | |
private $m = ' event !'; | |
public function getM() | |
{ | |
return $this->m; | |
} | |
} | |
class TestCommand | |
{ | |
/** | |
* @Type("string") | |
* @var string | |
*/ | |
public $var; | |
public function setVar($var) | |
{ | |
$this->var = $var; | |
} | |
} | |
class TestEventCreateSubscriber | |
{ | |
public function notify($event) | |
{ | |
echo $event->getM(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So it will echo on terminal "I'm a asynchronous event !". (Edit: @todo for 21/05:2016 = "I'm a asynchronous command" instead.)