Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CopyModules.php
Go to the documentation of this file.
1 <?php
8 
13 
18 {
22  private $moduleManager;
23 
27  private $cliCommand;
28 
32  public function __construct()
33  {
34  $this->moduleManager = new TestModuleManager();
35  $this->cliCommand = new CliCommand($this->moduleManager);
36  }
37 
44  public function startTest(\PHPUnit\Framework\TestCase $test)
45  {
46  $annotations = $test->getAnnotations();
47  //This annotation can be declared only on method level
48  if (isset($annotations['method']['moduleName'])) {
49  $moduleNames = $annotations['method']['moduleName'];
50 
51  foreach ($moduleNames as $moduleName) {
52  $this->cliCommand->introduceModule($moduleName);
53  //Include module`s registration.php to load it
54  $path = MAGENTO_MODULES_PATH . explode("_", $moduleName)[1] . '/registration.php';
55  include $path;
56  }
57  }
58  }
59 
65  public function endTest(\PHPUnit\Framework\TestCase $test)
66  {
67  $annotations = $test->getAnnotations();
68  //This annotation can be declared only on method level
69  if (!empty($annotations['method']['moduleName'])) {
70  foreach ($annotations['method']['moduleName'] as $moduleName) {
71  $path = MAGENTO_MODULES_PATH .
72  //Take only module name from Magento_ModuleName
73  explode("_", $moduleName)[1];
75  $this->unsergisterModuleFromComponentRegistrar($moduleName);
76  }
77  }
78  }
79 
89  private function unsergisterModuleFromComponentRegistrar($moduleName)
90  {
91  $reflection = new \ReflectionClass(ComponentRegistrar::class);
92  $reflectionProperty = $reflection->getProperty('paths');
93  $reflectionProperty->setAccessible(true);
94  $value = $reflectionProperty->getValue();
95  unset($value[ComponentRegistrar::MODULE][$moduleName]);
96  $reflectionProperty->setValue($value);
97  }
98 }
startTest(\PHPUnit\Framework\TestCase $test)
Definition: CopyModules.php:44
$value
Definition: gender.phtml:16
endTest(\PHPUnit\Framework\TestCase $test)
Definition: CopyModules.php:65
static rmdirRecursive($dir, $recursive=true)
Definition: File.php:335