Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Zalas\Infrastructure\Scraper;
  4.  
  5. use OldSound\RabbitMqBundle\RabbitMq\Consumer;
  6. use OldSound\RabbitMqBundle\RabbitMq\ConsumerInterface;
  7. use OldSound\RabbitMqBundle\RabbitMq\ProducerInterface;
  8. use PhpAmqpLib\Connection\AbstractConnection;
  9. use PhpAmqpLib\Connection\AMQPLazyConnection;
  10. use PhpAmqpLib\Exception\AMQPTimeoutException;
  11. use PhpAmqpLib\Message\AMQPMessage;
  12. use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
  13.  
  14. /**
  15. * @group integration
  16. * @group rabbitmq
  17. */
  18. class QueueScraperTest extends KernelTestCase
  19. {
  20. /**
  21. * @var ProducerInterface
  22. */
  23. private $queueProducer;
  24.  
  25. /**
  26. * @var AMQPMessage
  27. */
  28. private $consumedMessage;
  29.  
  30. protected function setUp()
  31. {
  32. self::bootKernel();
  33.  
  34. $this->queueProducer = $this->getProducer();
  35. }
  36.  
  37. public function testItPushesScrapeRequestToTheQueue()
  38. {
  39. $this->requiresRabbitMQ();
  40.  
  41. $uuid = new Uuid('1f8192fe-1349-49c7-a04d-240455af1063');
  42. $scraper = new QueueScraper($uuid);
  43. $scraper->scrape($uuid);
  44.  
  45. $this->consumeMessage();
  46.  
  47. $this->assertInstanceOf(AMQPMessage::class, $this->consumedMessage);
  48. $this->assertSame((string) $uuid, $this->consumedMessage->getBody());
  49. }
  50.  
  51. private function consumeMessage()
  52. {
  53. try {
  54. $this->getConsumer()->consume(1);
  55. } catch (AMQPTimeoutException $e) {
  56. $this->fail('Failed to consume a message: ' . $e->getMessage());
  57. }
  58. }
  59.  
  60. public function handleMessage(AMQPMessage $message)
  61. {
  62. $this->consumedMessage = $message;
  63. }
  64.  
  65. /**
  66. * @return ProducerInterface
  67. */
  68. private function getProducer()
  69. {
  70. return self::$kernel->getContainer()->get('old_sound_rabbit_mq.scrape_product_producer');
  71. }
  72.  
  73. /**
  74. * @return ConsumerInterface
  75. */
  76. private function getConsumer()
  77. {
  78. $consumer = new Consumer($this->getConnection());
  79.  
  80. $consumer->setExchangeOptions(['name' => 'scrape-product', 'type' => 'direct']);
  81. $consumer->setQueueOptions(['name' => 'scrape-product']);
  82. $consumer->setCallback([$this, 'handleMessage']);
  83. $consumer->setIdleTimeout(2);
  84.  
  85. return $consumer;
  86. }
  87.  
  88. /**
  89. * @return AbstractConnection
  90. */
  91. private function getConnection()
  92. {
  93. return self::$kernel->getContainer()->get('old_sound_rabbit_mq.connection.checkout');
  94. }
  95.  
  96. private function requiresRabbitMQ()
  97. {
  98. $connection = $this->getConnection();
  99.  
  100. if ($connection instanceof AMQPLazyConnection) {
  101. try {
  102. $connection->reconnect();
  103. } catch (\ErrorException $e) {
  104. }
  105. }
  106.  
  107. if (!$connection->isConnected()) {
  108. $this->markTestSkipped('RabbitMQ is not available.');
  109. }
  110. }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement