RoutingTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php declare(strict_types=1);
  2. namespace mikemadisonweb\rabbitmq\tests\components;
  3. use mikemadisonweb\rabbitmq\exceptions\RuntimeException;
  4. use mikemadisonweb\rabbitmq\Configuration;
  5. use mikemadisonweb\rabbitmq\tests\TestCase;
  6. use PhpAmqpLib\Channel\AMQPChannel;
  7. use PhpAmqpLib\Connection\AMQPLazyConnection;
  8. use PhpAmqpLib\Exception\AMQPProtocolChannelException;
  9. class RoutingTest extends TestCase
  10. {
  11. public function testRouting()
  12. {
  13. $name = 'test';
  14. $this->loadExtension([
  15. 'components' => [
  16. 'rabbitmq' => [
  17. 'class' => Configuration::class,
  18. 'connections' => [
  19. [
  20. 'name' => $name,
  21. 'url' => 'amqp://user:pass@host:5432/vhost?query',
  22. ],
  23. ],
  24. 'exchanges' => [
  25. [
  26. 'name' => $name,
  27. 'type' => 'direct'
  28. ],
  29. ],
  30. 'queues' => [
  31. [
  32. 'name' => $name,
  33. 'durable' => true,
  34. ],
  35. [
  36. 'durable' => false,
  37. ],
  38. ],
  39. 'bindings' => [
  40. [
  41. 'queue' => $name,
  42. 'exchange' => $name,
  43. 'routing_keys' => [$name],
  44. ],
  45. [
  46. 'exchange' => $name,
  47. 'to_exchange' => $name,
  48. 'routing_keys' => [$name],
  49. ],
  50. [
  51. 'queue' => $name,
  52. 'exchange' => $name,
  53. ],
  54. [
  55. 'exchange' => $name,
  56. 'to_exchange' => $name,
  57. ],
  58. [
  59. 'queue' => '',
  60. 'exchange' => $name,
  61. ],
  62. ],
  63. ],
  64. ],
  65. ]);
  66. $connection = $this->getMockBuilder(AMQPLazyConnection::class)
  67. ->disableOriginalConstructor()
  68. ->setMethods(['channel'])
  69. ->getMock();
  70. $channel = $this->getMockBuilder(AMQPChannel::class)
  71. ->disableOriginalConstructor()
  72. ->getMock();
  73. $connection->method('channel')
  74. ->willReturn($channel);
  75. $routing = \Yii::$app->rabbitmq->getRouting($connection);
  76. $this->assertTrue($routing->declareAll());
  77. $this->assertFalse($routing->declareAll());
  78. }
  79. /**
  80. * @dataProvider checkExceptions
  81. * @param $functionName
  82. */
  83. public function testRoutingExceptions($functionName)
  84. {
  85. $this->loadExtension([
  86. 'components' => [
  87. 'rabbitmq' => [
  88. 'class' => Configuration::class,
  89. 'connections' => [
  90. [
  91. 'url' => 'amqp://user:pass@host:5432/vhost?query',
  92. ],
  93. ],
  94. ],
  95. ],
  96. ]);
  97. $connection = \Yii::$app->rabbitmq->getConnection();
  98. $routing = \Yii::$app->rabbitmq->getRouting($connection);
  99. $this->expectException(RuntimeException::class);
  100. $routing->$functionName('non-existing');
  101. }
  102. /**
  103. * @return array
  104. */
  105. public function checkExceptions() : array
  106. {
  107. return [
  108. ['declareQueue'],
  109. ['declareExchange'],
  110. ['purgeQueue'],
  111. ['deleteQueue'],
  112. ['deleteExchange'],
  113. ];
  114. }
  115. public function testRoutingNonExisting()
  116. {
  117. $this->loadExtension([
  118. 'components' => [
  119. 'rabbitmq' => [
  120. 'class' => Configuration::class,
  121. 'connections' => [
  122. [
  123. 'url' => 'amqp://user:pass@host:5432/vhost?query',
  124. ],
  125. ],
  126. ],
  127. ],
  128. ]);
  129. $connection = $this->getMockBuilder(AMQPLazyConnection::class)
  130. ->disableOriginalConstructor()
  131. ->setMethods(['channel'])
  132. ->getMock();
  133. $channel = $this->getMockBuilder(AMQPChannel::class)
  134. ->disableOriginalConstructor()
  135. ->getMock();
  136. $exception = $this->createMock(AMQPProtocolChannelException::class);
  137. $channel
  138. ->expects($this->once())
  139. ->method('exchange_declare')
  140. ->willThrowException($exception);
  141. $channel
  142. ->expects($this->once())
  143. ->method('queue_declare')
  144. ->willThrowException($exception);
  145. $connection->method('channel')
  146. ->willReturn($channel);
  147. $routing = \Yii::$app->rabbitmq->getRouting($connection);
  148. $this->assertFalse($routing->isExchangeExists('non-existing'));
  149. $this->assertFalse($routing->isQueueExists('non-existing'));
  150. }
  151. public function testRoutingExisting()
  152. {
  153. $name = 'test';
  154. $this->loadExtension([
  155. 'components' => [
  156. 'rabbitmq' => [
  157. 'class' => Configuration::class,
  158. 'connections' => [
  159. [
  160. 'url' => 'amqp://user:pass@host:5432/vhost?query',
  161. ],
  162. ],
  163. 'exchanges' => [
  164. [
  165. 'name' => $name,
  166. 'type' => 'direct'
  167. ],
  168. ],
  169. 'queues' => [
  170. [
  171. 'name' => $name,
  172. 'durable' => true,
  173. ],
  174. [
  175. 'durable' => false,
  176. ],
  177. ],
  178. ],
  179. ],
  180. ]);
  181. $connection = $this->getMockBuilder(AMQPLazyConnection::class)
  182. ->disableOriginalConstructor()
  183. ->setMethods(['channel'])
  184. ->getMock();
  185. $channel = $this->getMockBuilder(AMQPChannel::class)
  186. ->disableOriginalConstructor()
  187. ->getMock();
  188. $channel
  189. ->expects($this->once())
  190. ->method('exchange_declare');
  191. $channel
  192. ->expects($this->once())
  193. ->method('queue_declare');
  194. $channel
  195. ->expects($this->once())
  196. ->method('queue_purge');
  197. $connection->method('channel')
  198. ->willReturn($channel);
  199. $routing = \Yii::$app->rabbitmq->getRouting($connection);
  200. $this->assertTrue($routing->isExchangeExists($name));
  201. $this->assertTrue($routing->isQueueExists($name));
  202. // Test purging queue
  203. $routing->purgeQueue($name);
  204. // Test deleting all schema
  205. $channel
  206. ->expects($this->exactly(2))
  207. ->method('queue_delete');
  208. $channel
  209. ->expects($this->once())
  210. ->method('exchange_delete');
  211. $routing->deleteAll();
  212. }
  213. }