/**
  * 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 MultipleConsumer($amqpConnection, $amqpChannel);
     $callback = function ($msg) use(&$lastQueue, $processFlag) {
         return $processFlag;
     };
     $consumer->setQueues(array('test-1' => array('callback' => $callback), 'test-2' => array('callback' => $callback)));
     // 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->processQueueMessage('test-1', $amqpMessage);
     $consumer->processQueueMessage('test-2', $amqpMessage);
 }
 /**
  * Check queues provider works well with static queues together
  *
  * @dataProvider processMessageProvider
  */
 public function testQueuesProviderAndStaticQueuesTogether($processFlag, $expectedMethod, $expectedRequeue = null)
 {
     $callback = $this->prepareCallback($processFlag);
     $this->multipleConsumer->setQueues(array('test-1' => array('callback' => $callback), 'test-2' => array('callback' => $callback)));
     $queuesProvider = $this->prepareQueuesProvider();
     $queuesProvider->expects($this->once())->method('getQueues')->will($this->returnValue(array('test-3' => array('callback' => $callback), 'test-4' => array('callback' => $callback))));
     $this->multipleConsumer->setQueuesProvider($queuesProvider);
     /**
      * We don't test consume method, which merges queues by calling $this->setupConsumer();
      * So we need to invoke it manually
      */
     $reflectionClass = new \ReflectionClass(get_class($this->multipleConsumer));
     $reflectionMethod = $reflectionClass->getMethod('mergeQueues');
     $reflectionMethod->setAccessible(true);
     $reflectionMethod->invoke($this->multipleConsumer);
     $this->prepareAMQPChannelExpectations($expectedMethod, $expectedRequeue);
     // Create a default message
     $amqpMessage = new AMQPMessage('foo body');
     $amqpMessage->delivery_info['channel'] = $this->amqpChannel;
     $amqpMessage->delivery_info['delivery_tag'] = 0;
     $this->multipleConsumer->processQueueMessage('test-1', $amqpMessage);
     $this->multipleConsumer->processQueueMessage('test-2', $amqpMessage);
     $this->multipleConsumer->processQueueMessage('test-3', $amqpMessage);
     $this->multipleConsumer->processQueueMessage('test-4', $amqpMessage);
 }
 private function loadMultipleConsumers($app)
 {
     $app['rabbit.multiple_consumer'] = $app->share(function ($app) {
         if (!isset($app['rabbit.multiple_consumers'])) {
             return;
         }
         $consumers = [];
         foreach ($app['rabbit.multiple_consumers'] as $name => $options) {
             $connection = $this->getConnection($app, $options, $app['rabbit.connections']);
             $consumer = new MultipleConsumer($connection);
             $consumer->setExchangeOptions($options['exchange_options']);
             $consumer->setQueues($options['queues']);
             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;
     });
 }