ConsumersRunnerTest.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\MessageQueue\Test\Unit\Model\Cron;
  7. use Magento\Framework\MessageQueue\ConnectionTypeResolver;
  8. use \PHPUnit_Framework_MockObject_MockObject as MockObject;
  9. use Magento\Framework\ShellInterface;
  10. use Magento\Framework\MessageQueue\Consumer\ConfigInterface as ConsumerConfigInterface;
  11. use Magento\Framework\MessageQueue\Consumer\Config\ConsumerConfigItemInterface;
  12. use Magento\Framework\App\DeploymentConfig;
  13. use Magento\MessageQueue\Model\Cron\ConsumersRunner;
  14. use Magento\MessageQueue\Model\Cron\ConsumersRunner\PidConsumerManager;
  15. use Symfony\Component\Process\PhpExecutableFinder;
  16. class ConsumersRunnerTest extends \PHPUnit\Framework\TestCase
  17. {
  18. /**
  19. * @var PidConsumerManager|MockObject
  20. */
  21. private $pidConsumerManagerMock;
  22. /**
  23. * @var ShellInterface|MockObject
  24. */
  25. private $shellBackgroundMock;
  26. /**
  27. * @var ConsumerConfigInterface|MockObject
  28. */
  29. private $consumerConfigMock;
  30. /**
  31. * @var DeploymentConfig|MockObject
  32. */
  33. private $deploymentConfigMock;
  34. /**
  35. * @var PhpExecutableFinder|MockObject
  36. */
  37. private $phpExecutableFinderMock;
  38. /**
  39. * @var ConnectionTypeResolver
  40. */
  41. private $connectionTypeResover;
  42. /**
  43. * @var ConsumersRunner
  44. */
  45. private $consumersRunner;
  46. /**
  47. * {@inheritdoc}
  48. */
  49. protected function setUp()
  50. {
  51. require_once __DIR__ . '/../../_files/consumers_runner_functions_mocks.php';
  52. $this->phpExecutableFinderMock = $this->getMockBuilder(phpExecutableFinder::class)
  53. ->disableOriginalConstructor()
  54. ->getMock();
  55. $this->pidConsumerManagerMock = $this->getMockBuilder(PidConsumerManager::class)
  56. ->disableOriginalConstructor()
  57. ->getMock();
  58. $this->shellBackgroundMock = $this->getMockBuilder(ShellInterface::class)
  59. ->getMockForAbstractClass();
  60. $this->consumerConfigMock = $this->getMockBuilder(ConsumerConfigInterface::class)
  61. ->getMockForAbstractClass();
  62. $this->deploymentConfigMock = $this->getMockBuilder(DeploymentConfig::class)
  63. ->disableOriginalConstructor()
  64. ->getMock();
  65. $this->connectionTypeResover = $this->getMockBuilder(ConnectionTypeResolver::class)
  66. ->disableOriginalConstructor()
  67. ->getMock();
  68. $this->connectionTypeResover->method('getConnectionType')->willReturn('something');
  69. $this->consumersRunner = new ConsumersRunner(
  70. $this->phpExecutableFinderMock,
  71. $this->consumerConfigMock,
  72. $this->deploymentConfigMock,
  73. $this->shellBackgroundMock,
  74. $this->pidConsumerManagerMock,
  75. $this->connectionTypeResover
  76. );
  77. }
  78. public function testRunDisabled()
  79. {
  80. $this->deploymentConfigMock->expects($this->once())
  81. ->method('get')
  82. ->willReturnMap([
  83. ['cron_consumers_runner/cron_run', true, false],
  84. ['cron_consumers_runner/max_messages', 10000, 10000],
  85. ['cron_consumers_runner/consumers', [], []],
  86. ]);
  87. $this->consumerConfigMock->expects($this->never())
  88. ->method('getConsumers');
  89. $this->pidConsumerManagerMock->expects($this->never())
  90. ->method('isRun');
  91. $this->shellBackgroundMock->expects($this->never())
  92. ->method('execute');
  93. $this->consumersRunner->run();
  94. }
  95. /**
  96. * @param int $maxMessages
  97. * @param bool $isRun
  98. * @param string $php
  99. * @param string $command
  100. * @param array $arguments
  101. * @param array $allowedConsumers
  102. * @param int $shellBackgroundExpects
  103. * @param int $isRunExpects
  104. * @dataProvider runDataProvider
  105. */
  106. public function testRun(
  107. $maxMessages,
  108. $isRun,
  109. $php,
  110. $command,
  111. $arguments,
  112. array $allowedConsumers,
  113. $shellBackgroundExpects,
  114. $isRunExpects
  115. ) {
  116. $consumerName = 'consumerName';
  117. $pidFilePath = 'consumerName-myHostName.pid';
  118. $this->deploymentConfigMock->expects($this->exactly(3))
  119. ->method('get')
  120. ->willReturnMap([
  121. ['cron_consumers_runner/cron_run', true, true],
  122. ['cron_consumers_runner/max_messages', 10000, $maxMessages],
  123. ['cron_consumers_runner/consumers', [], $allowedConsumers],
  124. ]);
  125. /** @var ConsumerConfigInterface|MockObject $firstCunsumer */
  126. $consumer = $this->getMockBuilder(ConsumerConfigItemInterface::class)
  127. ->getMockForAbstractClass();
  128. $consumer->expects($this->any())
  129. ->method('getName')
  130. ->willReturn($consumerName);
  131. $this->phpExecutableFinderMock->expects($this->once())
  132. ->method('find')
  133. ->willReturn($php);
  134. $this->consumerConfigMock->expects($this->once())
  135. ->method('getConsumers')
  136. ->willReturn([$consumer]);
  137. $this->pidConsumerManagerMock->expects($this->exactly($isRunExpects))
  138. ->method('isRun')
  139. ->with($pidFilePath)
  140. ->willReturn($isRun);
  141. $this->shellBackgroundMock->expects($this->exactly($shellBackgroundExpects))
  142. ->method('execute')
  143. ->with($command, $arguments);
  144. $this->consumersRunner->run();
  145. }
  146. /**
  147. * @return array
  148. */
  149. public function runDataProvider()
  150. {
  151. return [
  152. [
  153. 'maxMessages' => 20000,
  154. 'isRun' => false,
  155. 'php' => '',
  156. 'command' => 'php '. BP . '/bin/magento queue:consumers:start %s %s %s',
  157. 'arguments' => ['consumerName', '--pid-file-path=consumerName-myHostName.pid', '--max-messages=20000'],
  158. 'allowedConsumers' => [],
  159. 'shellBackgroundExpects' => 1,
  160. 'isRunExpects' => 1,
  161. ],
  162. [
  163. 'maxMessages' => 10000,
  164. 'isRun' => false,
  165. 'php' => '',
  166. 'command' => 'php '. BP . '/bin/magento queue:consumers:start %s %s %s',
  167. 'arguments' => ['consumerName', '--pid-file-path=consumerName-myHostName.pid', '--max-messages=10000'],
  168. 'allowedConsumers' => [],
  169. 'shellBackgroundExpects' => 1,
  170. 'isRunExpects' => 1,
  171. ],
  172. [
  173. 'maxMessages' => 10000,
  174. 'isRun' => false,
  175. 'php' => '',
  176. 'command' => 'php '. BP . '/bin/magento queue:consumers:start %s %s %s',
  177. 'arguments' => ['consumerName', '--pid-file-path=consumerName-myHostName.pid', '--max-messages=10000'],
  178. 'allowedConsumers' => ['someConsumer'],
  179. 'shellBackgroundExpects' => 0,
  180. 'isRunExpects' => 0,
  181. ],
  182. [
  183. 'maxMessages' => 10000,
  184. 'isRun' => true,
  185. 'php' => '',
  186. 'command' => 'php '. BP . '/bin/magento queue:consumers:start %s %s %s',
  187. 'arguments' => ['consumerName', '--pid-file-path=consumerName-myHostName.pid', '--max-messages=10000'],
  188. 'allowedConsumers' => ['someConsumer'],
  189. 'shellBackgroundExpects' => 0,
  190. 'isRunExpects' => 0,
  191. ],
  192. [
  193. 'maxMessages' => 10000,
  194. 'isRun' => true,
  195. 'php' => '',
  196. 'command' => 'php '. BP . '/bin/magento queue:consumers:start %s %s %s',
  197. 'arguments' => ['consumerName', '--pid-file-path=consumerName-myHostName.pid', '--max-messages=10000'],
  198. 'allowedConsumers' => [],
  199. 'shellBackgroundExpects' => 0,
  200. 'isRunExpects' => 1,
  201. ],
  202. [
  203. 'maxMessages' => 10000,
  204. 'isRun' => true,
  205. 'php' => '',
  206. 'command' => 'php '. BP . '/bin/magento queue:consumers:start %s %s %s',
  207. 'arguments' => ['consumerName', '--pid-file-path=consumerName-myHostName.pid', '--max-messages=10000'],
  208. 'allowedConsumers' => ['consumerName'],
  209. 'shellBackgroundExpects' => 0,
  210. 'isRunExpects' => 1,
  211. ],
  212. [
  213. 'maxMessages' => 10000,
  214. 'isRun' => false,
  215. 'php' => '',
  216. 'command' => 'php '. BP . '/bin/magento queue:consumers:start %s %s %s',
  217. 'arguments' => ['consumerName', '--pid-file-path=consumerName-myHostName.pid', '--max-messages=10000'],
  218. 'allowedConsumers' => ['consumerName'],
  219. 'shellBackgroundExpects' => 1,
  220. 'isRunExpects' => 1,
  221. ],
  222. [
  223. 'maxMessages' => 0,
  224. 'isRun' => false,
  225. 'php' => '/bin/php',
  226. 'command' => '/bin/php '. BP . '/bin/magento queue:consumers:start %s %s',
  227. 'arguments' => ['consumerName', '--pid-file-path=consumerName-myHostName.pid'],
  228. 'allowedConsumers' => ['consumerName'],
  229. 'shellBackgroundExpects' => 1,
  230. 'isRunExpects' => 1,
  231. ],
  232. ];
  233. }
  234. }