Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
IndexerSetModeCommandTest.php
Go to the documentation of this file.
1 <?php
7 
10 use Symfony\Component\Console\Tester\CommandTester;
11 
16 {
22  private $command;
23 
24  public function testGetOptions()
25  {
26  $this->stateMock->expects($this->never())->method('setAreaCode')->with(FrontNameResolver::AREA_CODE);
27  $this->command = new IndexerSetModeCommand($this->objectManagerFactory);
28  $optionsList = $this->command->getInputList();
29  $this->assertSame(2, sizeof($optionsList));
30  $this->assertSame('mode', $optionsList[0]->getName());
31  $this->assertSame('index', $optionsList[1]->getName());
32  }
33 
38  public function testExecuteInvalidArgument()
39  {
40  $this->stateMock->expects($this->never())->method('setAreaCode')->with(FrontNameResolver::AREA_CODE);
41  $this->command = new IndexerSetModeCommand($this->objectManagerFactory);
42  $commandTester = new CommandTester($this->command);
43  $commandTester->execute([]);
44  }
45 
50  public function testExecuteInvalidMode()
51  {
52  $this->stateMock->expects($this->never())->method('setAreaCode')->with(FrontNameResolver::AREA_CODE);
53  $this->command = new IndexerSetModeCommand($this->objectManagerFactory);
54  $commandTester = new CommandTester($this->command);
55  $commandTester->execute(['mode' => 'wrong_mode']);
56  }
57 
58  public function testExecuteAll()
59  {
60  $this->configureAdminArea();
61  $indexerOne = $this->getIndexerMock(
62  ['isScheduled', 'setScheduled'],
63  ['indexer_id' => 'indexer_1', 'title' => 'Title_indexerOne']
64  );
65 
66  $indexerOne->expects($this->exactly(2))
67  ->method('isScheduled')
68  ->willReturnOnConsecutiveCalls([true, false]);
69 
70  $indexerOne->expects($this->once())->method('setScheduled')->with(false);
71 
72  $this->initIndexerCollectionByItems([$indexerOne]);
73  $this->indexerFactory->expects($this->never())->method('create');
74  $this->command = new IndexerSetModeCommand($this->objectManagerFactory);
75  $commandTester = new CommandTester($this->command);
76  $commandTester->execute(['mode' => 'realtime']);
77  $actualValue = $commandTester->getDisplay();
78  $this->assertSame(
79  'Index mode for Indexer Title_indexerOne was changed from '. '\'Update by Schedule\' to \'Update on Save\''
80  . PHP_EOL,
81  $actualValue
82  );
83  }
84 
93  public function testExecuteWithIndex($isScheduled, $previous, $current, $mode, $expectedValue)
94  {
95  $this->configureAdminArea();
96  $indexerOne = $this->getIndexerMock(
97  ['isScheduled', 'setScheduled'],
98  ['indexer_id' => 'id_indexerOne', 'title' => 'Title_indexerOne']
99  );
100  $this->initIndexerCollectionByItems([$indexerOne]);
101  $indexerOne->expects($this->once())->method('setScheduled')->with($isScheduled);
102  $indexerOne->expects($this->exactly(2))
103  ->method('isScheduled')
104  ->willReturnOnConsecutiveCalls($previous, $current);
105 
106  $this->command = new IndexerSetModeCommand($this->objectManagerFactory);
107  $commandTester = new CommandTester($this->command);
108  $commandTester->execute(['mode' => $mode, 'index' => ['id_indexerOne']]);
109  $actualValue = $commandTester->getDisplay();
110  $this->assertSame($expectedValue, $actualValue);
111  }
112 
117  {
118  return [
119  [
120  false,
121  true,
122  false,
123  'realtime',
124  'Index mode for Indexer Title_indexerOne was changed from \'Update by Schedule\' to \'Update on Save\''
125  . PHP_EOL
126  ],
127  [
128  false,
129  false,
130  false,
131  'realtime',
132  'Index mode for Indexer Title_indexerOne has not been changed'
133  . PHP_EOL
134  ],
135  [
136  true,
137  true,
138  true,
139  'schedule',
140  'Index mode for Indexer Title_indexerOne has not been changed'
141  . PHP_EOL
142  ],
143  [
144  true,
145  false,
146  true,
147  'schedule',
148  'Index mode for Indexer Title_indexerOne was changed from \'Update on Save\' to \'Update by Schedule\''
149  . PHP_EOL
150  ],
151  ];
152  }
153 
155  {
156  $this->configureAdminArea();
157  $indexerOne = $this->getIndexerMock(
158  ['isScheduled', 'setScheduled'],
159  ['indexer_id' => 'id_indexerOne']
160  );
161  $localizedException = new \Magento\Framework\Exception\LocalizedException(__('Some Exception Message'));
162  $indexerOne->expects($this->once())->method('setScheduled')->will($this->throwException($localizedException));
163  $this->initIndexerCollectionByItems([$indexerOne]);
164  $this->command = new IndexerSetModeCommand($this->objectManagerFactory);
165  $commandTester = new CommandTester($this->command);
166  $commandTester->execute(['mode' => 'schedule', 'index' => ['id_indexerOne']]);
167  $actualValue = $commandTester->getDisplay();
168  $this->assertStringStartsWith('Some Exception Message', $actualValue);
169  }
170 
171  public function testExecuteWithException()
172  {
173  $this->configureAdminArea();
174  $indexerOne = $this->getIndexerMock(
175  ['isScheduled', 'setScheduled'],
176  ['indexer_id' => 'id_indexerOne', 'title' => 'Title_indexerOne']
177  );
178  $exception = new \Exception();
179  $indexerOne->expects($this->once())->method('setScheduled')->will($this->throwException($exception));
180  $this->initIndexerCollectionByItems([$indexerOne]);
181  $this->command = new IndexerSetModeCommand($this->objectManagerFactory);
182  $commandTester = new CommandTester($this->command);
183  $commandTester->execute(['mode' => 'schedule', 'index' => ['id_indexerOne']]);
184  $actualValue = $commandTester->getDisplay();
185  $this->assertStringStartsWith('Title_indexerOne indexer process unknown error:', $actualValue);
186  }
187 }
testExecuteWithIndex($isScheduled, $previous, $current, $mode, $expectedValue)
__()
Definition: __.php:13
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
Definition: grid.phtml:15