Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
StartConsumerCommandTest.php
Go to the documentation of this file.
1 <?php
8 
12 
16 class StartConsumerCommandTest extends \PHPUnit\Framework\TestCase
17 {
21  private $consumerFactory;
22 
26  private $appState;
27 
31  private $objectManager;
32 
36  private $writeFactoryMock;
37 
41  private $pidConsumerManagerMock;
42 
46  private $command;
47 
51  protected function setUp()
52  {
53  $this->pidConsumerManagerMock = $this->getMockBuilder(PidConsumerManager::class)
54  ->disableOriginalConstructor()
55  ->getMock();
56  $this->consumerFactory = $this->getMockBuilder(\Magento\Framework\MessageQueue\ConsumerFactory::class)
57  ->disableOriginalConstructor()->getMock();
58  $this->appState = $this->getMockBuilder(\Magento\Framework\App\State::class)
59  ->disableOriginalConstructor()->getMock();
60  $this->writeFactoryMock = $this->getMockBuilder(WriteFactory::class)
61  ->disableOriginalConstructor()
62  ->getMock();
63 
64  $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
65  $this->command = $this->objectManager->getObject(
66  \Magento\MessageQueue\Console\StartConsumerCommand::class,
67  [
68  'consumerFactory' => $this->consumerFactory,
69  'appState' => $this->appState,
70  'writeFactory' => $this->writeFactoryMock,
71  'pidConsumerManager' => $this->pidConsumerManagerMock,
72  ]
73  );
74  parent::setUp();
75  }
76 
89  public function testExecute(
90  $pidFilePath,
91  $savePidExpects,
92  $isRunExpects,
93  $isRun,
94  $runProcessExpects,
95  $expectedReturn
96  ) {
97  $areaCode = 'area_code';
98  $numberOfMessages = 10;
99  $batchSize = null;
100  $consumerName = 'consumer_name';
101  $input = $this->getMockBuilder(\Symfony\Component\Console\Input\InputInterface::class)
102  ->disableOriginalConstructor()->getMock();
103  $output = $this->getMockBuilder(\Symfony\Component\Console\Output\OutputInterface::class)
104  ->disableOriginalConstructor()->getMock();
105  $input->expects($this->once())->method('getArgument')
106  ->with(\Magento\MessageQueue\Console\StartConsumerCommand::ARGUMENT_CONSUMER)
107  ->willReturn($consumerName);
108  $input->expects($this->exactly(4))->method('getOption')
109  ->withConsecutive(
111  [\Magento\MessageQueue\Console\StartConsumerCommand::OPTION_BATCH_SIZE],
112  [\Magento\MessageQueue\Console\StartConsumerCommand::OPTION_AREACODE],
113  [\Magento\MessageQueue\Console\StartConsumerCommand::PID_FILE_PATH]
114  )->willReturnOnConsecutiveCalls(
115  $numberOfMessages,
116  $batchSize,
117  $areaCode,
118  $pidFilePath
119  );
120  $this->appState->expects($this->exactly($runProcessExpects))->method('setAreaCode')->with($areaCode);
121  $consumer = $this->getMockBuilder(\Magento\Framework\MessageQueue\ConsumerInterface::class)
122  ->disableOriginalConstructor()->getMock();
123  $this->consumerFactory->expects($this->exactly($runProcessExpects))
124  ->method('get')->with($consumerName, $batchSize)->willReturn($consumer);
125  $consumer->expects($this->exactly($runProcessExpects))->method('process')->with($numberOfMessages);
126 
127  $this->pidConsumerManagerMock->expects($this->exactly($isRunExpects))
128  ->method('isRun')
129  ->with($pidFilePath)
130  ->willReturn($isRun);
131 
132  $this->pidConsumerManagerMock->expects($this->exactly($savePidExpects))
133  ->method('savePid')
134  ->with($pidFilePath);
135 
136  $this->assertEquals(
137  $expectedReturn,
138  $this->command->run($input, $output)
139  );
140  }
141 
145  public function executeDataProvider()
146  {
147  return [
148  [
149  'pidFilePath' => null,
150  'savePidExpects' => 0,
151  'isRunExpects' => 0,
152  'isRun' => false,
153  'runProcessExpects' => 1,
155  ],
156  [
157  'pidFilePath' => '/var/consumer.pid',
158  'savePidExpects' => 1,
159  'isRunExpects' => 1,
160  'isRun' => false,
161  'runProcessExpects' => 1,
163  ],
164  [
165  'pidFilePath' => '/var/consumer.pid',
166  'savePidExpects' => 0,
167  'isRunExpects' => 1,
168  'isRun' => true,
169  'runProcessExpects' => 0,
171  ],
172  ];
173  }
174 
180  public function testConfigure()
181  {
182  $this->assertEquals(StartConsumerCommand::COMMAND_QUEUE_CONSUMERS_START, $this->command->getName());
183  $this->assertEquals('Start MessageQueue consumer', $this->command->getDescription());
185  $this->command->getDefinition()->getArgument(StartConsumerCommand::ARGUMENT_CONSUMER);
186  $this->command->getDefinition()->getOption(StartConsumerCommand::OPTION_NUMBER_OF_MESSAGES);
187  $this->command->getDefinition()->getOption(StartConsumerCommand::OPTION_AREACODE);
188  $this->command->getDefinition()->getOption(StartConsumerCommand::PID_FILE_PATH);
189  $this->assertContains('To start consumer which will process', $this->command->getHelp());
190  }
191 }
testExecute( $pidFilePath, $savePidExpects, $isRunExpects, $isRun, $runProcessExpects, $expectedReturn)