public function testQueueConsumer() { $c = new AMQPPecl(AMQPCredentials::createDefault()); $channel = $c->createChannel(1); $channel->exchangeDeclare(self::EXCHANGE_NAME, AMQPExchangeConfig::create()->setType(new AMQPExchangeType(AMQPExchangeType::DIRECT))->setDurable(true)); $inQueueCount = $channel->queueDeclare(self::QUEUE_NAME, AMQPQueueConfig::create()->setDurable(true)); $this->assertSame(0, $inQueueCount); $channel->queueBind(self::QUEUE_NAME, self::EXCHANGE_NAME, self::ROUTING_KEY); //cleanup $channel->queuePurge(self::QUEUE_NAME); for ($i = 1; $i <= self::COUNT_OF_PUBLISH; $i++) { $channel->basicPublish(self::EXCHANGE_NAME, self::ROUTING_KEY, AMQPOutgoingMessage::create()->setBody("message {$i}")->setTimestamp(Timestamp::makeNow())->setAppId(__CLASS__)->setMessageId($i)->setContentEncoding('utf-8')); } $consumer = new AMQPTestCaseQueueConsumer($channel); $channel->basicConsume(self::QUEUE_NAME, false, $consumer); $i = 0; while (true) { $message = $consumer->getNextDelivery(); //send acknowledge to RabbitMQ $channel->basicAck($message->getDeliveryTag(), false); $i++; $this->assertSame($consumer->getConsumerTag(), $message->getConsumerTag()); $this->assertEquals($i, $message->getMessageId()); $this->assertEquals(__CLASS__, $message->getAppId()); $this->assertEquals("message {$i}", $message->getBody()); if ($i == self::COUNT_OF_PUBLISH) { break; } } $channel->basicCancel($consumer->getConsumerTag()); //observer logic test $this->assertSame('ABBBBBC', $consumer->getCheckString()); //drop channels and close connection $c->disconnect(); $c = new AMQPPecl(AMQPCredentials::createDefault()); $channel = $c->createChannel(1); $channel->exchangeDeclare(self::EXCHANGE_NAME, AMQPExchangeConfig::create()->setType(new AMQPExchangeType(AMQPExchangeType::DIRECT))->setDurable(true)); //queue must be empty, because we sent acknowledge $inQueueCount = $channel->queueDeclare(self::QUEUE_NAME, AMQPQueueConfig::create()->setDurable(true)); $this->assertSame(0, $inQueueCount); //cleanup $channel->queuePurge(self::QUEUE_NAME); }
/** * @param AMQPChannelInterface $channel * @param string $label * @return int */ protected function queueDeclare(AMQPChannelInterface $channel, $label) { $this->assertTrue(isset(self::$queueList[$label])); return $channel->queueDeclare(self::$queueList[$label]['name'], AMQPQueueConfig::create()->setDurable(true)->setArguments(self::$queueList[$label]['args'])); }