public function testProcessMessage() { $amqpChannel = $this->getMockBuilder('AMQPChannel')->disableOriginalConstructor()->getMock(); $amqpChannel->expects($this->once())->method('getPrefetchCount')->willReturn(3); $message = $this->getMockBuilder('AMQPEnvelope')->disableOriginalConstructor()->getMock(); $amqpQueue = $this->getMockBuilder('AMQPQueue')->disableOriginalConstructor()->getMock(); $amqpExchange = $this->getMockBuilder('AMQPExchange')->disableOriginalConstructor()->getMock(); $eventResponse = $this->getMock('Zend\\EventManager\\ResponseCollection'); $eventManager = m::mock('Zend\\EventManager\\EventManager'); $eventManager->shouldReceive('setIdentifiers'); $message->expects($this->once())->method('getDeliveryTag')->willReturn('delivery-tag'); $message->expects($this->once())->method('getCorrelationId')->willReturn('correlation-id'); $message->expects($this->once())->method('getReplyTo')->willReturn('reply-to'); $amqpQueue->expects($this->once())->method('getChannel')->willReturn($amqpChannel); $amqpQueue->expects($this->any())->method('get')->willReturn($message); $reponse = json_encode(array('success' => true, 'result' => 'response-result')); $amqpExchange->expects($this->once())->method('publish')->with($this->equalTo($reponse), $this->equalTo('reply-to'), $this->equalTo(AMQP_NOPARAM), $this->callback(function ($param) { return is_array($param); })); $rpcServer = new RpcServer($amqpQueue, 1, 1 * 1000 * 500); $rpcServer->setEventManager($eventManager); $rpcServer->setExchange($amqpExchange); $eventResponse->expects($this->atLeast(1))->method('last')->willReturn('response-result'); $eventManager->shouldReceive('trigger')->once()->with('delivery', $rpcServer, ['message' => $message, 'queue' => $amqpQueue])->andReturn($eventResponse); $eventManager->shouldReceive('trigger')->once()->with('ack', $rpcServer, m::any()); $amqpQueue->expects($this->once())->method('ack'); $rpcServer->consume(1); }
/** * Create service with name * * @param ServiceLocatorInterface $serviceLocator * @param string $name * @param string $requestedName * @return RpcServer * @throws Exception\InvalidArgumentException */ public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) { // get global service locator, if we are in a plugin manager if ($serviceLocator instanceof AbstractPluginManager) { $serviceLocator = $serviceLocator->getServiceLocator(); } $spec = $this->getSpec($serviceLocator, $name, $requestedName); $this->validateSpec($serviceLocator, $spec, $requestedName); $connection = $this->getConnection($serviceLocator, $spec); $channel = $this->createChannel($connection, $spec); $queueSpec = $this->getQueueSpec($serviceLocator, $spec['queue']); $queue = $this->getQueue($queueSpec, $channel, $this->useAutoSetupFabric($spec)); $idleTimeout = isset($spec['idle_timeout']) ? $spec['idle_timeout'] : 5.0; $waitTimeout = isset($spec['wait_timeout']) ? $spec['wait_timeout'] : 100000; $rpcServer = new RpcServer($queue, $idleTimeout, $waitTimeout); if (isset($spec['logger'])) { if (!$serviceLocator->has($spec['logger'])) { throw new Exception\InvalidArgumentException('The logger ' . $spec['logger'] . ' is not configured'); } /** @var \Zend\Log\LoggerInterface $logger */ $logger = $serviceLocator->get($spec['logger']); $loggerListener = new LoggerListener($logger); $rpcServer->getEventManager()->attachAggregate($loggerListener); } $callbackManager = $this->getCallbackManager($serviceLocator); if (isset($spec['callback'])) { if (!$callbackManager->has($spec['callback'])) { throw new Exception\InvalidArgumentException('The required callback ' . $spec['callback'] . ' can not be found'); } /** @var callable $callback */ $callback = $callbackManager->get($spec['callback']); if ($callback) { $rpcServer->getEventManager()->attach('delivery', $callback); } } if (isset($spec['listeners']) and is_array($spec['listeners'])) { foreach ($spec['listeners'] as $listener) { if (is_string($listener)) { /** @var \Zend\EventManager\ListenerAggregateInterface $listener */ $listener = $serviceLocator->get($listener); } $rpcServer->getEventManager()->attachAggregate($listener); } } return $rpcServer; }
/** * @return void */ public function stopRpcServer() { $this->rpcServer->handleShutdownSignal(); }