Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ModuleEnableDisableCommandTest.php
Go to the documentation of this file.
1 <?php
7 
10 use Symfony\Component\Console\Tester\CommandTester;
11 
15 class ModuleEnableDisableCommandTest extends \PHPUnit\Framework\TestCase
16 {
20  private $objectManagerProviderMock;
21 
25  private $statusMock;
26 
30  private $cacheMock;
31 
35  private $cleanupFilesMock;
36 
40  private $fullModuleListMock;
41 
45  private $deploymentConfigMock;
46 
50  private $generatedFiles;
51 
52  protected function setUp()
53  {
54  $this->objectManagerProviderMock = $this->createMock(\Magento\Setup\Model\ObjectManagerProvider::class);
55  $objectManager = $this->getMockForAbstractClass(\Magento\Framework\ObjectManagerInterface::class);
56  $this->objectManagerProviderMock->expects($this->any())
57  ->method('get')
58  ->will($this->returnValue($objectManager));
59  $this->statusMock = $this->createMock(\Magento\Framework\Module\Status::class);
60  $this->cacheMock = $this->createMock(\Magento\Framework\App\Cache::class);
61  $this->cleanupFilesMock = $this->createMock(\Magento\Framework\App\State\CleanupFiles::class);
62  $this->fullModuleListMock = $this->createMock(\Magento\Framework\Module\FullModuleList::class);
63  $this->deploymentConfigMock = $this->createMock(\Magento\Framework\App\DeploymentConfig::class);
64  $this->generatedFiles = $this->createMock(\Magento\Framework\Code\GeneratedFiles::class);
65  $objectManager->expects($this->any())
66  ->method('get')
67  ->will($this->returnValueMap([
68  [\Magento\Framework\Module\Status::class, $this->statusMock],
69  [\Magento\Framework\App\Cache::class, $this->cacheMock],
70  [\Magento\Framework\App\State\CleanupFiles::class, $this->cleanupFilesMock],
71  [\Magento\Framework\Module\FullModuleList::class, $this->fullModuleListMock],
72  ]));
73  }
74 
82  public function testExecute($isEnable, $clearStaticContent, $expectedMessage)
83  {
84  $this->statusMock->expects($this->once())
85  ->method('getModulesToChange')
86  ->with($isEnable, ['Magento_Module1', 'Magento_Module2'])
87  ->will($this->returnValue(['Magento_Module1']));
88  $this->statusMock->expects($this->any())
89  ->method('checkConstraints')
90  ->will($this->returnValue([]));
91  $this->statusMock->expects($this->once())
92  ->method('setIsEnabled')
93  ->with($isEnable, ['Magento_Module1']);
94  $this->cacheMock->expects($this->once())
95  ->method('clean');
96  $this->cleanupFilesMock->expects($this->once())
97  ->method('clearCodeGeneratedClasses');
98  $this->cleanupFilesMock->expects($clearStaticContent ? $this->once() : $this->never())
99  ->method('clearMaterializedViewFiles');
100  $commandTester = $this->getCommandTester($isEnable);
101  $input = ['module' => ['Magento_Module1', 'Magento_Module2']];
102  if ($clearStaticContent) {
103  $input['--clear-static-content'] = true;
104  }
105  $commandTester->execute($input);
106  $display = $commandTester->getDisplay();
107  $this->assertStringMatchesFormat($expectedMessage, $display);
108  }
109 
113  public function executeDataProvider()
114  {
115  return [
116  'enable, do not clear static content' => [
117  true,
118  false,
119  '%amodules have been enabled%aMagento_Module1%a'
120  . "Info: Some modules might require static view files to be cleared. To do this, run "
121  . "'module:enable' with the --clear-static-content%a"
122  ],
123  'disable, do not clear static content' => [
124  false,
125  false,
126  '%amodules have been disabled%aMagento_Module1%a'
127  . "Info: Some modules might require static view files to be cleared. To do this, run "
128  . "'module:disable' with the --clear-static-content%a"
129  ],
130  'enable, clear static content' => [
131  true,
132  true,
133  '%amodules have been enabled%aMagento_Module1%aGenerated static view files cleared%a'
134  ],
135  'disable, clear static content' => [
136  false,
137  true,
138  '%amodules have been disabled%aMagento_Module1%aGenerated static view files cleared%a'
139  ]
140  ];
141  }
142 
144  {
145  $this->statusMock->expects($this->once())
146  ->method('getModulesToChange')
147  ->with(true, ['invalid'])
148  ->willThrowException(new \LogicException('Unknown module(s): invalid'));
149  $commandTester = $this->getCommandTester(true);
150  $input = ['module' => ['invalid']];
151  $commandTester->execute($input);
152  $this->assertEquals('Unknown module(s): invalid' . PHP_EOL, $commandTester->getDisplay());
153  }
154 
156  {
157  $this->statusMock->expects($this->once())
158  ->method('getModulesToChange')
159  ->with(false, ['invalid'])
160  ->willThrowException(new \LogicException('Unknown module(s): invalid'));
161  $commandTester = $this->getCommandTester(false);
162  $input = ['module' => ['invalid']];
163  $commandTester->execute($input);
164  $this->assertEquals('Unknown module(s): invalid' . PHP_EOL, $commandTester->getDisplay());
165  }
166 
173  public function testExecuteAll($isEnable, $expectedMessage)
174  {
175  $setupUpgradeMessage = 'To make sure that the enabled modules are properly registered, run \'setup:upgrade\'.';
176  $this->fullModuleListMock->expects($this->once())
177  ->method('getNames')
178  ->will($this->returnValue(['Magento_Module1', 'Magento_Module2']));
179  $this->statusMock->expects($this->once())
180  ->method('getModulesToChange')
181  ->with($isEnable, ['Magento_Module1', 'Magento_Module2'])
182  ->will($this->returnValue(['Magento_Module1']));
183  $this->statusMock->expects($this->any())
184  ->method('checkConstraints')
185  ->will($this->returnValue([]));
186  $this->statusMock->expects($this->once())
187  ->method('setIsEnabled')
188  ->with($isEnable, ['Magento_Module1']);
189  if ($isEnable) {
190  $this->deploymentConfigMock->expects($this->once())
191  ->method('isAvailable')
192  ->willReturn(['Magento_Module1']);
193  } else {
194  $this->deploymentConfigMock->expects($this->never())
195  ->method('isAvailable');
196  }
197  $commandTester = $this->getCommandTester($isEnable);
198  $input = ['--all' => true];
199  $commandTester->execute($input);
200  $output = $commandTester->getDisplay();
201  $this->assertStringMatchesFormat($expectedMessage, $output);
202  if ($isEnable) {
203  $this->assertContains($setupUpgradeMessage, $output);
204  } else {
205  $this->assertNotContains($setupUpgradeMessage, $output);
206  }
207  }
208 
212  public function executeAllDataProvider()
213  {
214  return [
215  'enable' => [true, '%amodules have been enabled%aMagento_Module1%a'],
216  'disable' => [false, '%amodules have been disabled%aMagento_Module1%a'],
217  ];
218  }
219 
225  public function testExecuteWithConstraints($isEnable)
226  {
227  $this->statusMock->expects($this->once())
228  ->method('getModulesToChange')
229  ->with($isEnable, ['Magento_Module1', 'Magento_Module2'])
230  ->will($this->returnValue(['Magento_Module1']));
231  $this->statusMock->expects($this->any())
232  ->method('checkConstraints')
233  ->will($this->returnValue(['constraint1', 'constraint2']));
234  $this->statusMock->expects($this->never())
235  ->method('setIsEnabled');
236  $commandTester = $this->getCommandTester($isEnable);
237  $commandTester->execute(['module' => ['Magento_Module1', 'Magento_Module2']]);
238  $this->assertStringMatchesFormat(
239  'Unable to change status of modules%aconstraint1%aconstraint2%a',
240  $commandTester->getDisplay()
241  );
242  }
243 
248  {
249  return [
250  'enable' => [true],
251  'disable' => [false],
252  ];
253  }
254 
261  public function testExecuteForce($isEnable, $expectedMessage)
262  {
263  $this->statusMock->expects($this->once())
264  ->method('getModulesToChange')
265  ->with($isEnable, ['Magento_Module1', 'Magento_Module2'])
266  ->will($this->returnValue(['Magento_Module1']));
267  $this->statusMock->expects($this->never())
268  ->method('checkConstraints');
269  $this->statusMock->expects($this->once())
270  ->method('setIsEnabled')
271  ->with($isEnable, ['Magento_Module1']);
272  $commandTester = $this->getCommandTester($isEnable);
273  $commandTester->execute(['module' => ['Magento_Module1', 'Magento_Module2'], '--force' => true]);
274  $this->assertStringMatchesFormat(
275  $expectedMessage . '%amodules might not function properly%a',
276  $commandTester->getDisplay()
277  );
278  }
279 
284  {
285  return [
286  'enable' => [true, '%amodules have been enabled%aMagento_Module1%a'],
287  'disable' => [false, '%amodules have been disabled%aMagento_Module1%a'],
288  ];
289  }
290 
296  public function testExecuteNoChanges($isEnable)
297  {
298  $this->statusMock->expects($this->once())
299  ->method('getModulesToChange')
300  ->with($isEnable, ['Magento_Module1', 'Magento_Module2'])
301  ->will($this->returnValue([]));
302  $this->statusMock->expects($this->never())
303  ->method('setIsEnabled');
304  $commandTester = $this->getCommandTester($isEnable);
305  $commandTester->execute(['module' => ['Magento_Module1', 'Magento_Module2']]);
306  $this->assertStringMatchesFormat(
307  'No modules were changed%a',
308  $commandTester->getDisplay()
309  );
310  }
311 
316  private function getCommandTester($isEnable)
317  {
318  $class = $isEnable ? ModuleEnableCommand::class : ModuleDisableCommand::class;
319  $command = new $class($this->objectManagerProviderMock);
320  $deploymentConfigProperty = new \ReflectionProperty($class, 'deploymentConfig');
321  $deploymentConfigProperty->setAccessible(true);
322  $deploymentConfigProperty->setValue($command, $this->deploymentConfigMock);
323  $deploymentConfigProperty = new \ReflectionProperty($class, 'generatedFiles');
324  $deploymentConfigProperty->setAccessible(true);
325  $deploymentConfigProperty->setValue($command, $this->generatedFiles);
326  return new CommandTester($command);
327  }
328 }
$objectManager
Definition: bootstrap.php:17
$_option $_optionId $class
Definition: date.phtml:13