Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ThemeUninstallCommandTest.php
Go to the documentation of this file.
1 <?php
8 
14 use Symfony\Component\Console\Tester\CommandTester;
16 
20 class ThemeUninstallCommandTest extends \PHPUnit\Framework\TestCase
21 {
25  private $maintenanceMode;
26 
30  private $dependencyChecker;
31 
35  private $collection;
36 
40  private $cache;
41 
45  private $cleanupFiles;
46 
50  private $command;
51 
55  private $backupRollbackFactory;
56 
62  private $themeValidator;
63 
67  private $themeUninstaller;
68 
72  private $themeDependencyChecker;
73 
77  private $themePackageInfo;
78 
82  private $tester;
83 
84  protected function setUp()
85  {
86  $this->maintenanceMode = $this->createMock(\Magento\Framework\App\MaintenanceMode::class);
87  $composerInformation = $this->createMock(\Magento\Framework\Composer\ComposerInformation::class);
88  $composerInformation->expects($this->any())
89  ->method('getRootRequiredPackages')
90  ->willReturn(['magento/theme-a', 'magento/theme-b', 'magento/theme-c']);
91  $this->dependencyChecker = $this->createMock(\Magento\Framework\Composer\DependencyChecker::class);
92  $this->collection = $this->createMock(\Magento\Theme\Model\Theme\Data\Collection::class);
93  $this->cache = $this->createMock(\Magento\Framework\App\Cache::class);
94  $this->cleanupFiles = $this->createMock(\Magento\Framework\App\State\CleanupFiles::class);
95  $this->backupRollbackFactory = $this->createMock(\Magento\Framework\Setup\BackupRollbackFactory::class);
96  $this->themeValidator = $this->createMock(\Magento\Theme\Model\ThemeValidator::class);
97  $this->themeUninstaller = $this->createMock(\Magento\Theme\Model\Theme\ThemeUninstaller::class);
98  $this->themeDependencyChecker = $this->createMock(\Magento\Theme\Model\Theme\ThemeDependencyChecker::class);
99  $this->themePackageInfo = $this->createMock(\Magento\Theme\Model\Theme\ThemePackageInfo::class);
100  $this->command = new ThemeUninstallCommand(
101  $this->cache,
102  $this->cleanupFiles,
103  $composerInformation,
104  $this->maintenanceMode,
105  $this->dependencyChecker,
106  $this->collection,
107  $this->backupRollbackFactory,
108  $this->themeValidator,
109  $this->themePackageInfo,
110  $this->themeUninstaller,
111  $this->themeDependencyChecker,
112  new MaintenanceModeEnabler($this->maintenanceMode)
113  );
114  $this->tester = new CommandTester($this->command);
115  }
116 
118  {
119  $this->themePackageInfo->expects($this->at(0))->method('getPackageName')->willReturn('dummy');
120  $this->themePackageInfo->expects($this->at(1))->method('getPackageName')->willReturn('magento/theme-a');
121  $this->collection->expects($this->any())
122  ->method('getThemeByFullPath')
123  ->willReturn(
124  $this->getMockForAbstractClass(
125  \Magento\Framework\View\Design\ThemeInterface::class,
126  [],
127  '',
128  false
129  )
130  );
131  $this->collection->expects($this->any())->method('hasTheme')->willReturn(true);
132  $this->tester->execute(['theme' => ['area/vendor/test1', 'area/vendor/test2']]);
133  $this->assertContains(
134  'test1 is not an installed Composer package',
135  $this->tester->getDisplay()
136  );
137  $this->assertNotContains(
138  'test2 is not an installed Composer package',
139  $this->tester->getDisplay()
140  );
141  }
142 
144  {
145  $this->themePackageInfo->expects($this->exactly(2))->method('getPackageName')->willReturn('');
146  $this->collection->expects($this->any())
147  ->method('getThemeByFullPath')
148  ->willReturn(
149  $this->getMockForAbstractClass(
150  \Magento\Framework\View\Design\ThemeInterface::class,
151  [],
152  '',
153  false
154  )
155  );
156  $this->collection->expects($this->any())->method('hasTheme')->willReturn(false);
157  $this->tester->execute(['theme' => ['area/vendor/test1', 'area/vendor/test2']]);
158  $this->assertContains(
159  'Unknown theme(s): area/vendor/test1, area/vendor/test2' . PHP_EOL,
160  $this->tester->getDisplay()
161  );
162  }
163 
165  {
166  $this->themePackageInfo->expects($this->exactly(4))
167  ->method('getPackageName')
168  ->will($this->returnValueMap([
169  ['area/vendor/test1', 'dummy1'],
170  ['area/vendor/test2', 'magento/theme-b'],
171  ['area/vendor/test3', ''],
172  ['area/vendor/test4', 'dummy2'],
173  ]));
174  $this->collection->expects($this->any())
175  ->method('getThemeByFullPath')
176  ->willReturn(
177  $this->getMockForAbstractClass(
178  \Magento\Framework\View\Design\ThemeInterface::class,
179  [],
180  '',
181  false
182  )
183  );
184  $this->collection->expects($this->at(1))->method('hasTheme')->willReturn(true);
185  $this->collection->expects($this->at(3))->method('hasTheme')->willReturn(true);
186  $this->collection->expects($this->at(5))->method('hasTheme')->willReturn(false);
187  $this->collection->expects($this->at(7))->method('hasTheme')->willReturn(true);
188  $this->tester->execute([
189  'theme' => [
190  'area/vendor/test1',
191  'area/vendor/test2',
192  'area/vendor/test3',
193  'area/vendor/test4',
194  ],
195  ]);
196  $this->assertContains(
197  'area/vendor/test1, area/vendor/test4 are not installed Composer packages',
198  $this->tester->getDisplay()
199  );
200  $this->assertNotContains(
201  'area/vendor/test2 is not an installed Composer package',
202  $this->tester->getDisplay()
203  );
204  $this->assertContains(
205  'Unknown theme(s): area/vendor/test3' . PHP_EOL,
206  $this->tester->getDisplay()
207  );
208  }
209 
210  public function setUpPassValidation()
211  {
212  $this->themePackageInfo->expects($this->any())->method('getPackageName')->willReturn('magento/theme-a');
213  $this->collection->expects($this->any())
214  ->method('getThemeByFullPath')
215  ->willReturn(
216  $this->getMockForAbstractClass(
217  \Magento\Framework\View\Design\ThemeInterface::class,
218  [],
219  '',
220  false
221  )
222  );
223  $this->themeDependencyChecker->expects($this->any())->method('checkChildTheme')->willReturn([]);
224  $this->collection->expects($this->any())->method('hasTheme')->willReturn(true);
225  }
226 
227  public function setupPassChildThemeCheck()
228  {
229  $theme = $this->createMock(\Magento\Theme\Model\Theme::class);
230  $theme->expects($this->any())->method('hasChildThemes')->willReturn(false);
231  $this->collection->expects($this->any())->method('getIterator')->willReturn(new \ArrayIterator([]));
232  }
233 
234  public function setupPassThemeInUseCheck()
235  {
236  $this->themeValidator->expects($this->once())->method('validateIsThemeInUse')->willReturn([]);
237  }
238 
239  public function setupPassDependencyCheck()
240  {
241  $this->dependencyChecker->expects($this->once())->method('checkDependencies')->willReturn([]);
242  }
243 
245  {
246  $this->setUpPassValidation();
247  $this->setupPassChildThemeCheck();
248  $this->setupPassDependencyCheck();
249  $this->themeValidator
250  ->expects($this->once())
251  ->method('validateIsThemeInUse')
252  ->willReturn(['frontend/Magento/a is in use in default config']);
253  $this->tester->execute(['theme' => ['frontend/Magento/a']]);
254  $this->assertEquals(
255  'Unable to uninstall. Please resolve the following issues:' . PHP_EOL
256  . 'frontend/Magento/a is in use in default config' . PHP_EOL,
257  $this->tester->getDisplay()
258  );
259  }
260 
262  {
263  $this->setUpPassValidation();
264  $this->setupPassThemeInUseCheck();
265  $this->setupPassChildThemeCheck();
266  $this->dependencyChecker->expects($this->once())
267  ->method('checkDependencies')
268  ->willReturn(['magento/theme-a' => ['magento/theme-b', 'magento/theme-c']]);
269  $this->tester->execute(['theme' => ['frontend/Magento/a']]);
270  $this->assertContains(
271  'Unable to uninstall. Please resolve the following issues:' . PHP_EOL .
272  'frontend/Magento/a has the following dependent package(s):'
273  . PHP_EOL . "\tmagento/theme-b" . PHP_EOL . "\tmagento/theme-c",
274  $this->tester->getDisplay()
275  );
276  }
277 
278  public function setUpExecute()
279  {
280  $this->setUpPassValidation();
281  $this->setupPassThemeInUseCheck();
282  $this->setupPassChildThemeCheck();
283  $this->setupPassDependencyCheck();
284  $this->cache->expects($this->once())->method('clean');
285 
286  $this->themeUninstaller->expects($this->once())
287  ->method('uninstallRegistry')
288  ->with($this->isInstanceOf(\Symfony\Component\Console\Output\OutputInterface::class), $this->anything());
289  $this->themeUninstaller->expects($this->once())
290  ->method('uninstallCode')
291  ->with($this->isInstanceOf(\Symfony\Component\Console\Output\OutputInterface::class), $this->anything());
292  }
293 
294  public function testExecuteWithBackupCode()
295  {
296  $this->setUpExecute();
297  $backupRollback = $this->createMock(\Magento\Framework\Setup\BackupRollback::class);
298  $this->backupRollbackFactory->expects($this->once())
299  ->method('create')
300  ->willReturn($backupRollback);
301  $this->tester->execute(['theme' => ['area/vendor/test'], '--backup-code' => true]);
302  $this->tester->getDisplay();
303  }
304 
305  public function testExecute()
306  {
307  $this->setUpExecute();
308  $this->cleanupFiles->expects($this->never())->method('clearMaterializedViewFiles');
309  $this->tester->execute(['theme' => ['area/vendor/test']]);
310  $this->assertContains('Enabling maintenance mode', $this->tester->getDisplay());
311  $this->assertContains('Disabling maintenance mode', $this->tester->getDisplay());
312  $this->assertContains('Alert: Generated static view files were not cleared.', $this->tester->getDisplay());
313  $this->assertNotContains('Generated static view files cleared successfully', $this->tester->getDisplay());
314  }
315 
316  public function testExecuteCleanStaticFiles()
317  {
318  $this->setUpExecute();
319  $this->cleanupFiles->expects($this->once())->method('clearMaterializedViewFiles');
320  $this->tester->execute(['theme' => ['area/vendor/test'], '-c' => true]);
321  $this->assertContains('Enabling maintenance mode', $this->tester->getDisplay());
322  $this->assertContains('Disabling maintenance mode', $this->tester->getDisplay());
323  $this->assertNotContains('Alert: Generated static view files were not cleared.', $this->tester->getDisplay());
324  $this->assertContains('Generated static view files cleared successfully', $this->tester->getDisplay());
325  }
326 
331  public function testExecuteWrongThemeFormat($themePath)
332  {
333  $this->tester->execute(['theme' => [$themePath]]);
334  $this->assertContains(
335  'Theme path should be specified as full path which is area/vendor/name.',
336  $this->tester->getDisplay()
337  );
338  }
339 
343  public function dataProviderThemeFormat()
344  {
345  return [
346  ['test1'],
347  ['/test1'],
348  ['test1/'],
349  ['/test1/'],
350  ['vendor/test1'],
351  ['/vendor/test1'],
352  ['vendor/test1/'],
353  ['/vendor/test1/'],
354  ['area/vendor/test1/'],
355  ['/area/vendor/test1'],
356  ];
357  }
358 }
$theme