public function testConsuming() { $message = new Message('{"a": 10, "b": 15}', ['headers' => ['to' => 'phpunit']]); $queue = $this->channel->queue()->define(QueueInterface::FLAG_AUTO_DELETE); $this->channel->publish($message, '', $queue); $this->handler->expects($this->once())->method('handle')->willReturnCallback(function (Delivery $message) { $message->cancel(); }); $this->consumer->consume($queue); }
<?php require_once __DIR__ . '/../vendor/autoload.php'; use ServiceBus\Broadcaster\NullBroadcaster; use ServiceBus\Discovery\NullDiscovery; use ServiceBus\Identity; use ServiceBus\Service\Handling\CallableHandler; use ServiceBus\Service\Middleware\Delegate\Router\SimpleRouter; use ServiceBus\Service\Middleware\RoutingMiddleware; use ServiceBus\ServiceBus; use ServiceBus\Protocol\AMQP; $env = function ($key, $default = null) { return isset($_SERVER[$key]) ? $_SERVER[$key] : $default; }; // Driver only matter for sending requests. This example shows how to handle requests, // so discovery and broadcaster are not important. $bus = new ServiceBus(new NullDiscovery(), new NullBroadcaster()); // Define a service and few methods $service = $bus->define(new Identity('calc'))->apply(new RoutingMiddleware(new SimpleRouter(['add' => function ($a, $b) { return $a + $b; }, 'sub' => function ($a, $b) { return $a - $b; }, 'div' => function ($a, $b) { return $a / $b; }]))); $connection = \ButterAMQP\ConnectionBuilder::make()->create($env('AMQP_URL', 'amqp://*****:*****@localhost/%2f')); // Create topology $topology = (new AMQP\Topology($connection))->addExchange(new AMQP\Topology\Exchange('calc', 'topic'))->addQueue(new AMQP\Topology\Queue('calc', 0, [new AMQP\Topology\Binding('calc', 'add'), new AMQP\Topology\Binding('calc', 'sub'), new AMQP\Topology\Binding('calc', 'div')])); // Create consumer $consumer = new AMQP\Consumer($connection, new AMQP\Handler($service)); $consumer->consume($topology->queue('calc'));