RabbitMQControllerTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php declare(strict_types=1);
  2. namespace mikemadisonweb\rabbitmq\tests\controllers;
  3. use mikemadisonweb\rabbitmq\components\Consumer;
  4. use mikemadisonweb\rabbitmq\components\Producer;
  5. use mikemadisonweb\rabbitmq\components\Routing;
  6. use mikemadisonweb\rabbitmq\Configuration;
  7. use mikemadisonweb\rabbitmq\controllers\RabbitMQController;
  8. use mikemadisonweb\rabbitmq\tests\TestCase;
  9. use yii\base\InvalidConfigException;
  10. use yii\console\Controller;
  11. class RabbitMQControllerTest extends TestCase
  12. {
  13. protected $controller;
  14. public function setUp(): void
  15. {
  16. $this->loadExtension(
  17. [
  18. 'components' => [
  19. 'rabbitmq' => [
  20. 'class' => Configuration::class,
  21. 'connections' => [
  22. [
  23. 'host' => 'unreal',
  24. ],
  25. ],
  26. ],
  27. ],
  28. ]
  29. );
  30. $this->controller = $this->getMockBuilder(RabbitMQController::class)
  31. ->setConstructorArgs([Configuration::EXTENSION_CONTROLLER_ALIAS, \Yii::$app])
  32. ->setMethods(['stderr', 'stdout'])
  33. ->getMock();
  34. }
  35. public function testConsumeAction()
  36. {
  37. // Check console flags existence
  38. $this->assertTrue(isset($this->controller->optionAliases()['l']));
  39. $this->assertTrue(isset($this->controller->optionAliases()['m']));
  40. $this->assertSame('messagesLimit', $this->controller->optionAliases()['m']);
  41. $this->assertSame('memoryLimit', $this->controller->optionAliases()['l']);
  42. $this->assertTrue(in_array('messagesLimit', $this->controller->options('consume'), true));
  43. $this->assertTrue(in_array('memoryLimit', $this->controller->options('consume'), true));
  44. // Invalid consumer name
  45. $this->expectException(InvalidConfigException::class);
  46. $response = $this->controller->runAction('consume', ['unknown']);
  47. $this->assertSame(Controller::EXIT_CODE_ERROR, $response);
  48. // Valid consumer name
  49. $name = 'valid';
  50. $consumer = $this->getMockBuilder(Consumer::class)
  51. ->disableOriginalConstructor()
  52. ->setMethods(['getConsumerTag', 'consume'])
  53. ->getMock();
  54. \Yii::$container->set(sprintf(Configuration::CONSUMER_SERVICE_NAME, $name), $consumer);
  55. $this->controller->debug = 'false';
  56. $this->controller->memoryLimit = '1024';
  57. $response = $this->controller->runAction('consume', [$name]);
  58. $this->assertSame(Controller::EXIT_CODE_NORMAL, $response);
  59. }
  60. public function testPublishAction()
  61. {
  62. // Invalid producer name
  63. $this->expectException(InvalidConfigException::class);
  64. $response = $this->controller->runAction('publish', ['unknown', 'unknown']);
  65. $this->assertSame(Controller::EXIT_CODE_ERROR, $response);
  66. // No data
  67. $name = 'valid';
  68. $producer = $this->getMockBuilder(Producer::class)
  69. ->disableOriginalConstructor()
  70. ->setMethods(['publish'])
  71. ->getMock();
  72. \Yii::$container->set(sprintf(Configuration::PRODUCER_SERVICE_NAME, $name), $producer);
  73. $response = $this->controller->runAction('publish', [$name, 'does not matter']);
  74. $this->assertSame(Controller::EXIT_CODE_ERROR, $response);
  75. }
  76. public function testDeclareAllAction()
  77. {
  78. $routing = $this->createMock(Routing::class);
  79. $routing->expects($this->exactly(2))
  80. ->method('declareAll')
  81. ->willReturnOnConsecutiveCalls(false, true);
  82. \Yii::$container->setSingleton(Configuration::ROUTING_SERVICE_NAME, $routing);
  83. $response = $this->controller->runAction('declare-all');
  84. $this->assertSame(Controller::EXIT_CODE_ERROR, $response);
  85. $response = $this->controller->runAction('declare-all');
  86. $this->assertSame(Controller::EXIT_CODE_NORMAL, $response);
  87. }
  88. public function testDeclareQueueAction()
  89. {
  90. $routing = $this->createMock(Routing::class);
  91. $routing->expects($this->exactly(2))
  92. ->method('isQueueExists')
  93. ->willReturnOnConsecutiveCalls(true, false);
  94. $routing->expects($this->once())
  95. ->method('declareQueue');
  96. \Yii::$container->setSingleton(Configuration::ROUTING_SERVICE_NAME, $routing);
  97. $response = $this->controller->runAction('declare-queue', ['queue-name']);
  98. $this->assertSame(Controller::EXIT_CODE_ERROR, $response);
  99. $response = $this->controller->runAction('declare-queue', ['queue-name']);
  100. $this->assertSame(Controller::EXIT_CODE_NORMAL, $response);
  101. }
  102. public function testDeclareExchangeAction()
  103. {
  104. $routing = $this->createMock(Routing::class);
  105. $routing->expects($this->exactly(2))
  106. ->method('isExchangeExists')
  107. ->willReturnOnConsecutiveCalls(true, false);
  108. $routing->expects($this->once())
  109. ->method('declareExchange');
  110. \Yii::$container->setSingleton(Configuration::ROUTING_SERVICE_NAME, $routing);
  111. $response = $this->controller->runAction('declare-exchange', ['exchange-name']);
  112. $this->assertSame(Controller::EXIT_CODE_ERROR, $response);
  113. $response = $this->controller->runAction('declare-exchange', ['exchange-name']);
  114. $this->assertSame(Controller::EXIT_CODE_NORMAL, $response);
  115. }
  116. public function testDeleteAllAction()
  117. {
  118. $this->controller->interactive = false;
  119. $routing = $this->createMock(Routing::class);
  120. $routing->expects($this->once())
  121. ->method('deleteAll')
  122. ->willReturnOnConsecutiveCalls(false, true);
  123. \Yii::$container->setSingleton(Configuration::ROUTING_SERVICE_NAME, $routing);
  124. $response = $this->controller->runAction('delete-all');
  125. $this->assertSame(Controller::EXIT_CODE_NORMAL, $response);
  126. }
  127. public function testDeleteQueueAction()
  128. {
  129. $this->controller->interactive = false;
  130. $routing = $this->createMock(Routing::class);
  131. $routing->expects($this->once())
  132. ->method('deleteQueue');
  133. \Yii::$container->setSingleton(Configuration::ROUTING_SERVICE_NAME, $routing);
  134. $response = $this->controller->runAction('delete-queue', ['queue-name']);
  135. $this->assertSame(Controller::EXIT_CODE_NORMAL, $response);
  136. }
  137. public function testDeleteExchangeAction()
  138. {
  139. $this->controller->interactive = false;
  140. $routing = $this->createMock(Routing::class);
  141. $routing->expects($this->once())
  142. ->method('deleteExchange');
  143. \Yii::$container->setSingleton(Configuration::ROUTING_SERVICE_NAME, $routing);
  144. $response = $this->controller->runAction('delete-exchange', ['exchange-name']);
  145. $this->assertSame(Controller::EXIT_CODE_NORMAL, $response);
  146. }
  147. public function testPurgeQueueAction()
  148. {
  149. $this->controller->interactive = false;
  150. $routing = $this->createMock(Routing::class);
  151. $routing->expects($this->once())
  152. ->method('purgeQueue');
  153. \Yii::$container->setSingleton(Configuration::ROUTING_SERVICE_NAME, $routing);
  154. $response = $this->controller->runAction('purge-queue', ['queue-name']);
  155. $this->assertSame(Controller::EXIT_CODE_NORMAL, $response);
  156. }
  157. }