IndexerSetModeCommandTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Indexer\Test\Unit\Console\Command;
  7. use Magento\Backend\App\Area\FrontNameResolver;
  8. use Magento\Indexer\Console\Command\IndexerSetModeCommand;
  9. use Symfony\Component\Console\Tester\CommandTester;
  10. /**
  11. * Command for updating installed application after the code base has changed
  12. */
  13. class IndexerSetModeCommandTest extends AbstractIndexerCommandCommonSetup
  14. {
  15. /**
  16. * Command being tested
  17. *
  18. * @var IndexerSetModeCommand
  19. */
  20. private $command;
  21. public function testGetOptions()
  22. {
  23. $this->stateMock->expects($this->never())->method('setAreaCode')->with(FrontNameResolver::AREA_CODE);
  24. $this->command = new IndexerSetModeCommand($this->objectManagerFactory);
  25. $optionsList = $this->command->getInputList();
  26. $this->assertSame(2, sizeof($optionsList));
  27. $this->assertSame('mode', $optionsList[0]->getName());
  28. $this->assertSame('index', $optionsList[1]->getName());
  29. }
  30. /**
  31. * @expectedException InvalidArgumentException
  32. * @expectedExceptionMessage Missing argument 'mode'. Accepted values for mode are 'realtime' or 'schedule'
  33. */
  34. public function testExecuteInvalidArgument()
  35. {
  36. $this->stateMock->expects($this->never())->method('setAreaCode')->with(FrontNameResolver::AREA_CODE);
  37. $this->command = new IndexerSetModeCommand($this->objectManagerFactory);
  38. $commandTester = new CommandTester($this->command);
  39. $commandTester->execute([]);
  40. }
  41. /**
  42. * @expectedException InvalidArgumentException
  43. * @expectedExceptionMessage Accepted values for mode are 'realtime' or 'schedule'
  44. */
  45. public function testExecuteInvalidMode()
  46. {
  47. $this->stateMock->expects($this->never())->method('setAreaCode')->with(FrontNameResolver::AREA_CODE);
  48. $this->command = new IndexerSetModeCommand($this->objectManagerFactory);
  49. $commandTester = new CommandTester($this->command);
  50. $commandTester->execute(['mode' => 'wrong_mode']);
  51. }
  52. public function testExecuteAll()
  53. {
  54. $this->configureAdminArea();
  55. $indexerOne = $this->getIndexerMock(
  56. ['isScheduled', 'setScheduled'],
  57. ['indexer_id' => 'indexer_1', 'title' => 'Title_indexerOne']
  58. );
  59. $indexerOne->expects($this->exactly(2))
  60. ->method('isScheduled')
  61. ->willReturnOnConsecutiveCalls([true, false]);
  62. $indexerOne->expects($this->once())->method('setScheduled')->with(false);
  63. $this->initIndexerCollectionByItems([$indexerOne]);
  64. $this->indexerFactory->expects($this->never())->method('create');
  65. $this->command = new IndexerSetModeCommand($this->objectManagerFactory);
  66. $commandTester = new CommandTester($this->command);
  67. $commandTester->execute(['mode' => 'realtime']);
  68. $actualValue = $commandTester->getDisplay();
  69. $this->assertSame(
  70. 'Index mode for Indexer Title_indexerOne was changed from '. '\'Update by Schedule\' to \'Update on Save\''
  71. . PHP_EOL,
  72. $actualValue
  73. );
  74. }
  75. /**
  76. * @param bool $isScheduled
  77. * @param bool $previous
  78. * @param bool $current
  79. * @param string $mode
  80. * @param $expectedValue
  81. * @dataProvider executeWithIndexDataProvider
  82. */
  83. public function testExecuteWithIndex($isScheduled, $previous, $current, $mode, $expectedValue)
  84. {
  85. $this->configureAdminArea();
  86. $indexerOne = $this->getIndexerMock(
  87. ['isScheduled', 'setScheduled'],
  88. ['indexer_id' => 'id_indexerOne', 'title' => 'Title_indexerOne']
  89. );
  90. $this->initIndexerCollectionByItems([$indexerOne]);
  91. $indexerOne->expects($this->once())->method('setScheduled')->with($isScheduled);
  92. $indexerOne->expects($this->exactly(2))
  93. ->method('isScheduled')
  94. ->willReturnOnConsecutiveCalls($previous, $current);
  95. $this->command = new IndexerSetModeCommand($this->objectManagerFactory);
  96. $commandTester = new CommandTester($this->command);
  97. $commandTester->execute(['mode' => $mode, 'index' => ['id_indexerOne']]);
  98. $actualValue = $commandTester->getDisplay();
  99. $this->assertSame($expectedValue, $actualValue);
  100. }
  101. /**
  102. * @return array
  103. */
  104. public function executeWithIndexDataProvider()
  105. {
  106. return [
  107. [
  108. false,
  109. true,
  110. false,
  111. 'realtime',
  112. 'Index mode for Indexer Title_indexerOne was changed from \'Update by Schedule\' to \'Update on Save\''
  113. . PHP_EOL
  114. ],
  115. [
  116. false,
  117. false,
  118. false,
  119. 'realtime',
  120. 'Index mode for Indexer Title_indexerOne has not been changed'
  121. . PHP_EOL
  122. ],
  123. [
  124. true,
  125. true,
  126. true,
  127. 'schedule',
  128. 'Index mode for Indexer Title_indexerOne has not been changed'
  129. . PHP_EOL
  130. ],
  131. [
  132. true,
  133. false,
  134. true,
  135. 'schedule',
  136. 'Index mode for Indexer Title_indexerOne was changed from \'Update on Save\' to \'Update by Schedule\''
  137. . PHP_EOL
  138. ],
  139. ];
  140. }
  141. public function testExecuteWithLocalizedException()
  142. {
  143. $this->configureAdminArea();
  144. $indexerOne = $this->getIndexerMock(
  145. ['isScheduled', 'setScheduled'],
  146. ['indexer_id' => 'id_indexerOne']
  147. );
  148. $localizedException = new \Magento\Framework\Exception\LocalizedException(__('Some Exception Message'));
  149. $indexerOne->expects($this->once())->method('setScheduled')->will($this->throwException($localizedException));
  150. $this->initIndexerCollectionByItems([$indexerOne]);
  151. $this->command = new IndexerSetModeCommand($this->objectManagerFactory);
  152. $commandTester = new CommandTester($this->command);
  153. $commandTester->execute(['mode' => 'schedule', 'index' => ['id_indexerOne']]);
  154. $actualValue = $commandTester->getDisplay();
  155. $this->assertStringStartsWith('Some Exception Message', $actualValue);
  156. }
  157. public function testExecuteWithException()
  158. {
  159. $this->configureAdminArea();
  160. $indexerOne = $this->getIndexerMock(
  161. ['isScheduled', 'setScheduled'],
  162. ['indexer_id' => 'id_indexerOne', 'title' => 'Title_indexerOne']
  163. );
  164. $exception = new \Exception();
  165. $indexerOne->expects($this->once())->method('setScheduled')->will($this->throwException($exception));
  166. $this->initIndexerCollectionByItems([$indexerOne]);
  167. $this->command = new IndexerSetModeCommand($this->objectManagerFactory);
  168. $commandTester = new CommandTester($this->command);
  169. $commandTester->execute(['mode' => 'schedule', 'index' => ['id_indexerOne']]);
  170. $actualValue = $commandTester->getDisplay();
  171. $this->assertStringStartsWith('Title_indexerOne indexer process unknown error:', $actualValue);
  172. }
  173. }