123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\MessageQueue\Test\Unit\Model\Cron;
- use Magento\Framework\MessageQueue\ConnectionTypeResolver;
- use \PHPUnit_Framework_MockObject_MockObject as MockObject;
- use Magento\Framework\ShellInterface;
- use Magento\Framework\MessageQueue\Consumer\ConfigInterface as ConsumerConfigInterface;
- use Magento\Framework\MessageQueue\Consumer\Config\ConsumerConfigItemInterface;
- use Magento\Framework\App\DeploymentConfig;
- use Magento\MessageQueue\Model\Cron\ConsumersRunner;
- use Magento\MessageQueue\Model\Cron\ConsumersRunner\PidConsumerManager;
- use Symfony\Component\Process\PhpExecutableFinder;
- class ConsumersRunnerTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var PidConsumerManager|MockObject
- */
- private $pidConsumerManagerMock;
- /**
- * @var ShellInterface|MockObject
- */
- private $shellBackgroundMock;
- /**
- * @var ConsumerConfigInterface|MockObject
- */
- private $consumerConfigMock;
- /**
- * @var DeploymentConfig|MockObject
- */
- private $deploymentConfigMock;
- /**
- * @var PhpExecutableFinder|MockObject
- */
- private $phpExecutableFinderMock;
- /**
- * @var ConnectionTypeResolver
- */
- private $connectionTypeResover;
- /**
- * @var ConsumersRunner
- */
- private $consumersRunner;
- /**
- * {@inheritdoc}
- */
- protected function setUp()
- {
- require_once __DIR__ . '/../../_files/consumers_runner_functions_mocks.php';
- $this->phpExecutableFinderMock = $this->getMockBuilder(phpExecutableFinder::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->pidConsumerManagerMock = $this->getMockBuilder(PidConsumerManager::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->shellBackgroundMock = $this->getMockBuilder(ShellInterface::class)
- ->getMockForAbstractClass();
- $this->consumerConfigMock = $this->getMockBuilder(ConsumerConfigInterface::class)
- ->getMockForAbstractClass();
- $this->deploymentConfigMock = $this->getMockBuilder(DeploymentConfig::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->connectionTypeResover = $this->getMockBuilder(ConnectionTypeResolver::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->connectionTypeResover->method('getConnectionType')->willReturn('something');
- $this->consumersRunner = new ConsumersRunner(
- $this->phpExecutableFinderMock,
- $this->consumerConfigMock,
- $this->deploymentConfigMock,
- $this->shellBackgroundMock,
- $this->pidConsumerManagerMock,
- $this->connectionTypeResover
- );
- }
- public function testRunDisabled()
- {
- $this->deploymentConfigMock->expects($this->once())
- ->method('get')
- ->willReturnMap([
- ['cron_consumers_runner/cron_run', true, false],
- ['cron_consumers_runner/max_messages', 10000, 10000],
- ['cron_consumers_runner/consumers', [], []],
- ]);
- $this->consumerConfigMock->expects($this->never())
- ->method('getConsumers');
- $this->pidConsumerManagerMock->expects($this->never())
- ->method('isRun');
- $this->shellBackgroundMock->expects($this->never())
- ->method('execute');
- $this->consumersRunner->run();
- }
- /**
- * @param int $maxMessages
- * @param bool $isRun
- * @param string $php
- * @param string $command
- * @param array $arguments
- * @param array $allowedConsumers
- * @param int $shellBackgroundExpects
- * @param int $isRunExpects
- * @dataProvider runDataProvider
- */
- public function testRun(
- $maxMessages,
- $isRun,
- $php,
- $command,
- $arguments,
- array $allowedConsumers,
- $shellBackgroundExpects,
- $isRunExpects
- ) {
- $consumerName = 'consumerName';
- $pidFilePath = 'consumerName-myHostName.pid';
- $this->deploymentConfigMock->expects($this->exactly(3))
- ->method('get')
- ->willReturnMap([
- ['cron_consumers_runner/cron_run', true, true],
- ['cron_consumers_runner/max_messages', 10000, $maxMessages],
- ['cron_consumers_runner/consumers', [], $allowedConsumers],
- ]);
- /** @var ConsumerConfigInterface|MockObject $firstCunsumer */
- $consumer = $this->getMockBuilder(ConsumerConfigItemInterface::class)
- ->getMockForAbstractClass();
- $consumer->expects($this->any())
- ->method('getName')
- ->willReturn($consumerName);
- $this->phpExecutableFinderMock->expects($this->once())
- ->method('find')
- ->willReturn($php);
- $this->consumerConfigMock->expects($this->once())
- ->method('getConsumers')
- ->willReturn([$consumer]);
- $this->pidConsumerManagerMock->expects($this->exactly($isRunExpects))
- ->method('isRun')
- ->with($pidFilePath)
- ->willReturn($isRun);
- $this->shellBackgroundMock->expects($this->exactly($shellBackgroundExpects))
- ->method('execute')
- ->with($command, $arguments);
- $this->consumersRunner->run();
- }
- /**
- * @return array
- */
- public function runDataProvider()
- {
- return [
- [
- 'maxMessages' => 20000,
- 'isRun' => false,
- 'php' => '',
- 'command' => 'php '. BP . '/bin/magento queue:consumers:start %s %s %s',
- 'arguments' => ['consumerName', '--pid-file-path=consumerName-myHostName.pid', '--max-messages=20000'],
- 'allowedConsumers' => [],
- 'shellBackgroundExpects' => 1,
- 'isRunExpects' => 1,
- ],
- [
- 'maxMessages' => 10000,
- 'isRun' => false,
- 'php' => '',
- 'command' => 'php '. BP . '/bin/magento queue:consumers:start %s %s %s',
- 'arguments' => ['consumerName', '--pid-file-path=consumerName-myHostName.pid', '--max-messages=10000'],
- 'allowedConsumers' => [],
- 'shellBackgroundExpects' => 1,
- 'isRunExpects' => 1,
- ],
- [
- 'maxMessages' => 10000,
- 'isRun' => false,
- 'php' => '',
- 'command' => 'php '. BP . '/bin/magento queue:consumers:start %s %s %s',
- 'arguments' => ['consumerName', '--pid-file-path=consumerName-myHostName.pid', '--max-messages=10000'],
- 'allowedConsumers' => ['someConsumer'],
- 'shellBackgroundExpects' => 0,
- 'isRunExpects' => 0,
- ],
- [
- 'maxMessages' => 10000,
- 'isRun' => true,
- 'php' => '',
- 'command' => 'php '. BP . '/bin/magento queue:consumers:start %s %s %s',
- 'arguments' => ['consumerName', '--pid-file-path=consumerName-myHostName.pid', '--max-messages=10000'],
- 'allowedConsumers' => ['someConsumer'],
- 'shellBackgroundExpects' => 0,
- 'isRunExpects' => 0,
- ],
- [
- 'maxMessages' => 10000,
- 'isRun' => true,
- 'php' => '',
- 'command' => 'php '. BP . '/bin/magento queue:consumers:start %s %s %s',
- 'arguments' => ['consumerName', '--pid-file-path=consumerName-myHostName.pid', '--max-messages=10000'],
- 'allowedConsumers' => [],
- 'shellBackgroundExpects' => 0,
- 'isRunExpects' => 1,
- ],
- [
- 'maxMessages' => 10000,
- 'isRun' => true,
- 'php' => '',
- 'command' => 'php '. BP . '/bin/magento queue:consumers:start %s %s %s',
- 'arguments' => ['consumerName', '--pid-file-path=consumerName-myHostName.pid', '--max-messages=10000'],
- 'allowedConsumers' => ['consumerName'],
- 'shellBackgroundExpects' => 0,
- 'isRunExpects' => 1,
- ],
- [
- 'maxMessages' => 10000,
- 'isRun' => false,
- 'php' => '',
- 'command' => 'php '. BP . '/bin/magento queue:consumers:start %s %s %s',
- 'arguments' => ['consumerName', '--pid-file-path=consumerName-myHostName.pid', '--max-messages=10000'],
- 'allowedConsumers' => ['consumerName'],
- 'shellBackgroundExpects' => 1,
- 'isRunExpects' => 1,
- ],
- [
- 'maxMessages' => 0,
- 'isRun' => false,
- 'php' => '/bin/php',
- 'command' => '/bin/php '. BP . '/bin/magento queue:consumers:start %s %s',
- 'arguments' => ['consumerName', '--pid-file-path=consumerName-myHostName.pid'],
- 'allowedConsumers' => ['consumerName'],
- 'shellBackgroundExpects' => 1,
- 'isRunExpects' => 1,
- ],
- ];
- }
- }
|