Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Zalas\Infrastructure\Scraper;
- use OldSound\RabbitMqBundle\RabbitMq\Consumer;
- use OldSound\RabbitMqBundle\RabbitMq\ConsumerInterface;
- use OldSound\RabbitMqBundle\RabbitMq\ProducerInterface;
- use PhpAmqpLib\Connection\AbstractConnection;
- use PhpAmqpLib\Connection\AMQPLazyConnection;
- use PhpAmqpLib\Exception\AMQPTimeoutException;
- use PhpAmqpLib\Message\AMQPMessage;
- use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
- /**
- * @group integration
- * @group rabbitmq
- */
- class QueueScraperTest extends KernelTestCase
- {
- /**
- * @var ProducerInterface
- */
- private $queueProducer;
- /**
- * @var AMQPMessage
- */
- private $consumedMessage;
- protected function setUp()
- {
- self::bootKernel();
- $this->queueProducer = $this->getProducer();
- }
- public function testItPushesScrapeRequestToTheQueue()
- {
- $this->requiresRabbitMQ();
- $uuid = new Uuid('1f8192fe-1349-49c7-a04d-240455af1063');
- $scraper = new QueueScraper($uuid);
- $scraper->scrape($uuid);
- $this->consumeMessage();
- $this->assertInstanceOf(AMQPMessage::class, $this->consumedMessage);
- $this->assertSame((string) $uuid, $this->consumedMessage->getBody());
- }
- private function consumeMessage()
- {
- try {
- $this->getConsumer()->consume(1);
- } catch (AMQPTimeoutException $e) {
- $this->fail('Failed to consume a message: ' . $e->getMessage());
- }
- }
- public function handleMessage(AMQPMessage $message)
- {
- $this->consumedMessage = $message;
- }
- /**
- * @return ProducerInterface
- */
- private function getProducer()
- {
- return self::$kernel->getContainer()->get('old_sound_rabbit_mq.scrape_product_producer');
- }
- /**
- * @return ConsumerInterface
- */
- private function getConsumer()
- {
- $consumer = new Consumer($this->getConnection());
- $consumer->setExchangeOptions(['name' => 'scrape-product', 'type' => 'direct']);
- $consumer->setQueueOptions(['name' => 'scrape-product']);
- $consumer->setCallback([$this, 'handleMessage']);
- $consumer->setIdleTimeout(2);
- return $consumer;
- }
- /**
- * @return AbstractConnection
- */
- private function getConnection()
- {
- return self::$kernel->getContainer()->get('old_sound_rabbit_mq.connection.checkout');
- }
- private function requiresRabbitMQ()
- {
- $connection = $this->getConnection();
- if ($connection instanceof AMQPLazyConnection) {
- try {
- $connection->reconnect();
- } catch (\ErrorException $e) {
- }
- }
- if (!$connection->isConnected()) {
- $this->markTestSkipped('RabbitMQ is not available.');
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement