CopyModules.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\TestFramework\Annotation;
  7. use Magento\Framework\Component\ComponentRegistrar;
  8. use Magento\Framework\Filesystem\Io\File;
  9. use Magento\TestFramework\Deploy\CliCommand;
  10. use Magento\TestFramework\Deploy\TestModuleManager;
  11. /**
  12. * Handler for applying reinstallMagento annotation.
  13. */
  14. class CopyModules
  15. {
  16. /**
  17. * @var TestModuleManager
  18. */
  19. private $moduleManager;
  20. /**
  21. * @var CliCommand
  22. */
  23. private $cliCommand;
  24. /**
  25. * CopyModules constructor.
  26. */
  27. public function __construct()
  28. {
  29. $this->moduleManager = new TestModuleManager();
  30. $this->cliCommand = new CliCommand($this->moduleManager);
  31. }
  32. /**
  33. * Handler for 'startTest' event.
  34. *
  35. * @param \PHPUnit\Framework\TestCase $test
  36. * @throws \Magento\Framework\Exception\LocalizedException
  37. */
  38. public function startTest(\PHPUnit\Framework\TestCase $test)
  39. {
  40. $annotations = $test->getAnnotations();
  41. //This annotation can be declared only on method level
  42. if (isset($annotations['method']['moduleName'])) {
  43. $moduleNames = $annotations['method']['moduleName'];
  44. foreach ($moduleNames as $moduleName) {
  45. $this->cliCommand->introduceModule($moduleName);
  46. //Include module`s registration.php to load it
  47. $path = MAGENTO_MODULES_PATH . explode("_", $moduleName)[1] . '/registration.php';
  48. include $path;
  49. }
  50. }
  51. }
  52. /**
  53. * Handler for 'startTest' event
  54. *
  55. * @param \PHPUnit\Framework\TestCase $test
  56. */
  57. public function endTest(\PHPUnit\Framework\TestCase $test)
  58. {
  59. $annotations = $test->getAnnotations();
  60. //This annotation can be declared only on method level
  61. if (!empty($annotations['method']['moduleName'])) {
  62. foreach ($annotations['method']['moduleName'] as $moduleName) {
  63. $path = MAGENTO_MODULES_PATH .
  64. //Take only module name from Magento_ModuleName
  65. explode("_", $moduleName)[1];
  66. File::rmdirRecursive($path);
  67. $this->unsergisterModuleFromComponentRegistrar($moduleName);
  68. }
  69. }
  70. }
  71. /**
  72. * Unregister module from component registrar.
  73. * The component registrar uses static private variable and does not provide unregister method,
  74. * however unregister is required to remove registered modules after they are deleted from app/code.
  75. *
  76. * @param $moduleName
  77. *
  78. * @return void
  79. */
  80. private function unsergisterModuleFromComponentRegistrar($moduleName)
  81. {
  82. $reflection = new \ReflectionClass(ComponentRegistrar::class);
  83. $reflectionProperty = $reflection->getProperty('paths');
  84. $reflectionProperty->setAccessible(true);
  85. $value = $reflectionProperty->getValue();
  86. unset($value[ComponentRegistrar::MODULE][$moduleName]);
  87. $reflectionProperty->setValue($value);
  88. }
  89. }