AMQPTest.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. class AMQPTest extends \PHPUnit\Framework\TestCase
  3. {
  4. protected $config = array(
  5. 'host' => 'localhost',
  6. 'username' => 'guest',
  7. 'password' => 'guest',
  8. 'port' => '5672',
  9. 'vhost' => '/',
  10. 'cleanup' => false,
  11. 'queues' => array('queue1')
  12. );
  13. /**
  14. * @var \Codeception\Module\AMQP
  15. */
  16. protected $module = null;
  17. public function setUp()
  18. {
  19. $this->module = new \Codeception\Module\AMQP(make_container());
  20. $this->module->_setConfig($this->config);
  21. $res = @stream_socket_client('tcp://localhost:5672');
  22. if ($res === false) {
  23. $this->markTestSkipped('AMQP is not running');
  24. }
  25. $this->module->_initialize();
  26. $connection = $this->module->connection;
  27. $connection->channel()->queue_declare('queue1');
  28. }
  29. public function testPushToQueue()
  30. {
  31. $this->module->pushToQueue('queue1', 'hello');
  32. $this->module->seeMessageInQueueContainsText('queue1', 'hello');
  33. }
  34. public function testCountQueue()
  35. {
  36. $this->module->pushToQueue('queue1', 'hello');
  37. $this->module->pushToQueue('queue1', 'world');
  38. $this->module->dontSeeQueueIsEmpty('queue1');
  39. $this->module->seeNumberOfMessagesInQueue('queue1', 2);
  40. $this->module->purgeAllQueues();
  41. $this->module->seeQueueIsEmpty('queue1');
  42. }
  43. public function testPushToExchange()
  44. {
  45. $queue = 'test-queue';
  46. $exchange = 'test-exchange';
  47. $topic = 'test.3';
  48. $message = 'test-message';
  49. $this->module->declareExchange($exchange, 'topic', false, true, false);
  50. $this->module->declareQueue($queue, false, true, false, false);
  51. $this->module->bindQueueToExchange($queue, $exchange, 'test.#');
  52. $this->module->pushToExchange($exchange, $message, $topic);
  53. $this->module->seeMessageInQueueContainsText($queue, $message);
  54. }
  55. }