/** * Check if the message is requeued or not correctly. * * @dataProvider processMessageProvider */ public function testProcessMessage($processFlag, $expectedMethod, $expectedRequeue = null) { $amqpConnection = $this->getMockBuilder('\\PhpAmqpLib\\Connection\\AMQPConnection')->disableOriginalConstructor()->getMock(); $amqpChannel = $this->getMockBuilder('\\PhpAmqpLib\\Channel\\AMQPChannel')->disableOriginalConstructor()->getMock(); $consumer = new Consumer($amqpConnection, $amqpChannel); $callbackFunction = function () use($processFlag) { return $processFlag; }; // Create a callback function with a return value set by the data provider. $consumer->setCallback($callbackFunction); // Create a default message $amqpMessage = new AMQPMessage('foo body'); $amqpMessage->delivery_info['channel'] = $amqpChannel; $amqpMessage->delivery_info['delivery_tag'] = 0; $amqpChannel->expects($this->any())->method('basic_reject')->will($this->returnCallback(function ($delivery_tag, $requeue) use($expectedMethod, $expectedRequeue) { \PHPUnit_Framework_Assert::assertSame($expectedMethod, 'basic_reject'); // Check if this function should be called. \PHPUnit_Framework_Assert::assertSame($requeue, $expectedRequeue); // Check if the message should be requeued. })); $amqpChannel->expects($this->any())->method('basic_ack')->will($this->returnCallback(function ($delivery_tag) use($expectedMethod) { \PHPUnit_Framework_Assert::assertSame($expectedMethod, 'basic_ack'); // Check if this function should be called. })); $consumer->processMessage($amqpMessage); }
private function loadConsumers($app) { $app['rabbit.consumer'] = $app->share(function ($app) { if (!isset($app['rabbit.consumers'])) { return; } $consumers = []; foreach ($app['rabbit.consumers'] as $name => $options) { $connection = $this->getConnection($app, $options, $app['rabbit.connections']); $consumer = new Consumer($connection); $consumer->setExchangeOptions($options['exchange_options']); $consumer->setQueueOptions($options['queue_options']); $consumer->setCallback(array($app[$options['callback']], 'execute')); if (array_key_exists('qos_options', $options)) { $consumer->setQosOptions($options['qos_options']['prefetch_size'], $options['qos_options']['prefetch_count'], $options['qos_options']['global']); } if (array_key_exists('qos_options', $options)) { $consumer->setIdleTimeout($options['idle_timeout']); } if (array_key_exists('auto_setup_fabric', $options) && !$options['auto_setup_fabric']) { $consumer->disableAutoSetupFabric(); } $consumers[$name] = $consumer; } return $consumers; }); }