ProducerTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php declare(strict_types=1);
  2. namespace mikemadisonweb\rabbitmq\tests\components;
  3. use mikemadisonweb\rabbitmq\components\{
  4. Logger, Producer, Routing
  5. };
  6. use mikemadisonweb\rabbitmq\Configuration;
  7. use mikemadisonweb\rabbitmq\events\RabbitMQPublisherEvent;
  8. use mikemadisonweb\rabbitmq\exceptions\RuntimeException;
  9. use mikemadisonweb\rabbitmq\tests\TestCase;
  10. use PhpAmqpLib\Channel\AMQPChannel;
  11. use PhpAmqpLib\Connection\AMQPLazyConnection;
  12. class ProducerTest extends TestCase
  13. {
  14. /**
  15. * Test without framework
  16. */
  17. public function testPublish()
  18. {
  19. $this->loadExtension([
  20. 'components' => [
  21. 'rabbitmq' => [
  22. 'class' => Configuration::class,
  23. 'connections' => [
  24. [
  25. 'host' => 'unreal',
  26. ],
  27. ],
  28. ],
  29. ],
  30. ]);
  31. $connection = $this->getMockBuilder(AMQPLazyConnection::class)
  32. ->disableOriginalConstructor()
  33. ->setMethods(['channel'])
  34. ->getMock();
  35. $channel = $this->getMockBuilder(AMQPChannel::class)
  36. ->disableOriginalConstructor()
  37. ->getMock();
  38. $channel->expects($this->once())
  39. ->method('basic_publish');
  40. $connection
  41. ->method('channel')
  42. ->willReturn($channel);
  43. $routing = $this->createMock(Routing::class);
  44. $routing->expects($this->exactly(2))
  45. ->method('declareAll');
  46. $routing->expects($this->exactly(2))
  47. ->method('isExchangeExists')
  48. ->willReturnOnConsecutiveCalls(true, false);
  49. $logger = $this->createMock(Logger::class);
  50. $producer = new Producer($connection, $routing, $logger, true);
  51. $producer->setSafe(true);
  52. // Good attempt
  53. $producer->publish('Test message', 'exist');
  54. // Non-existing exchange
  55. $this->expectException(RuntimeException::class);
  56. $producer->publish('Test message', 'not-exist');
  57. }
  58. /**
  59. * Test events
  60. */
  61. public function testPublishEvents()
  62. {
  63. $producerName = 'test';
  64. $msg = 'Some-message';
  65. $this->loadExtension([
  66. 'components' => [
  67. 'rabbitmq' => [
  68. 'class' => Configuration::class,
  69. 'connections' => [
  70. [
  71. 'host' => 'unreal',
  72. ],
  73. ],
  74. 'producers' => [
  75. [
  76. 'name' => $producerName,
  77. ]
  78. ],
  79. 'on before_publish' => function ($event) use ($msg) {
  80. $this->assertInstanceOf(RabbitMQPublisherEvent::class, $event);
  81. $this->assertSame($msg, $event->message->getBody());
  82. },
  83. 'on after_publish' => function ($event) use ($msg) {
  84. $this->assertInstanceOf(RabbitMQPublisherEvent::class, $event);
  85. $this->assertSame($msg, $event->message->getBody());
  86. },
  87. ],
  88. ],
  89. ]);
  90. $producer = \Yii::$app->rabbitmq->getProducer($producerName);
  91. $routing = $this->createMock(Routing::class);
  92. $routing->method('declareAll');
  93. $routing->method('isExchangeExists')
  94. ->willReturn(true);
  95. $this->setInaccessibleProperty($producer, 'routing', $routing);
  96. $connection = $this->getMockBuilder(AMQPLazyConnection::class)
  97. ->disableOriginalConstructor()
  98. ->setMethods(['channel'])
  99. ->getMock();
  100. $channel = $this->getMockBuilder(AMQPChannel::class)
  101. ->disableOriginalConstructor()
  102. ->getMock();
  103. $channel->expects($this->once())
  104. ->method('basic_publish');
  105. $connection
  106. ->method('channel')
  107. ->willReturn($channel);
  108. $this->setInaccessibleProperty($producer, 'conn', $connection);
  109. $producer->publish($msg, 'exchange');
  110. }
  111. /**
  112. * Test inside framework with different message types
  113. * @dataProvider checkMsgEncoding
  114. * @param $initial
  115. * @param $encoded
  116. */
  117. public function testPublishDifferentTypes($initial, $encoded)
  118. {
  119. $producerName = 'test';
  120. $this->loadExtension([
  121. 'components' => [
  122. 'rabbitmq' => [
  123. 'class' => Configuration::class,
  124. 'connections' => [
  125. [
  126. 'host' => 'unreal',
  127. ],
  128. ],
  129. 'producers' => [
  130. [
  131. 'name' => $producerName,
  132. ]
  133. ],
  134. 'on after_publish' => function ($event) use ($encoded) {
  135. $this->assertSame($encoded, $event->message->getBody());
  136. },
  137. ],
  138. ],
  139. ]);
  140. $producer = \Yii::$app->rabbitmq->getProducer($producerName);
  141. $routing = $this->createMock(Routing::class);
  142. $routing->method('declareAll');
  143. $routing->method('isExchangeExists')
  144. ->willReturn(true);
  145. $this->setInaccessibleProperty($producer, 'routing', $routing);
  146. $connection = $this->getMockBuilder(AMQPLazyConnection::class)
  147. ->disableOriginalConstructor()
  148. ->setMethods(['channel'])
  149. ->getMock();
  150. $channel = $this->getMockBuilder(AMQPChannel::class)
  151. ->disableOriginalConstructor()
  152. ->getMock();
  153. $channel->expects($this->once())
  154. ->method('basic_publish');
  155. $connection
  156. ->method('channel')
  157. ->willReturn($channel);
  158. $this->setInaccessibleProperty($producer, 'conn', $connection);
  159. $producer->publish($initial, 'exchange');
  160. }
  161. public function checkMsgEncoding() : array
  162. {
  163. return [
  164. ['String!', 'String!'],
  165. [['array'], 'a:1:{i:0;s:5:"array";}'],
  166. [1, 'i:1;'],
  167. [null, 'N;'],
  168. [new \StdClass(), 'O:8:"stdClass":0:{}'],
  169. ];
  170. }
  171. }