CopyModules.php 3.04 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Magento\TestFramework\Annotation;

use Magento\Framework\Component\ComponentRegistrar;
use Magento\Framework\Filesystem\Io\File;
use Magento\TestFramework\Deploy\CliCommand;
use Magento\TestFramework\Deploy\TestModuleManager;

/**
 * Handler for applying reinstallMagento annotation.
 */
class CopyModules
{
    /**
     * @var TestModuleManager
     */
    private $moduleManager;

    /**
     * @var CliCommand
     */
    private $cliCommand;

    /**
     * CopyModules constructor.
     */
    public function __construct()
    {
        $this->moduleManager = new TestModuleManager();
        $this->cliCommand = new CliCommand($this->moduleManager);
    }

    /**
     * Handler for 'startTest' event.
     *
     * @param  \PHPUnit\Framework\TestCase $test
     * @throws \Magento\Framework\Exception\LocalizedException
     */
    public function startTest(\PHPUnit\Framework\TestCase $test)
    {
        $annotations = $test->getAnnotations();
        //This annotation can be declared only on method level
        if (isset($annotations['method']['moduleName'])) {
            $moduleNames = $annotations['method']['moduleName'];

            foreach ($moduleNames as $moduleName) {
                $this->cliCommand->introduceModule($moduleName);
                //Include module`s registration.php to load it
                $path = MAGENTO_MODULES_PATH . explode("_", $moduleName)[1] . '/registration.php';
                include $path;
            }
        }
    }

    /**
     * Handler for 'startTest' event
     *
     * @param \PHPUnit\Framework\TestCase $test
     */
    public function endTest(\PHPUnit\Framework\TestCase $test)
    {
        $annotations = $test->getAnnotations();
        //This annotation can be declared only on method level
        if (!empty($annotations['method']['moduleName'])) {
            foreach ($annotations['method']['moduleName'] as $moduleName) {
                $path = MAGENTO_MODULES_PATH .
                    //Take only module name from Magento_ModuleName
                    explode("_", $moduleName)[1];
                File::rmdirRecursive($path);
                $this->unsergisterModuleFromComponentRegistrar($moduleName);
            }
        }
    }

    /**
     * Unregister module from component registrar.
     * The component registrar uses static private variable and does not provide unregister method,
     * however unregister is required to remove registered modules after they are deleted from app/code.
     *
     * @param $moduleName
     *
     * @return void
     */
    private function unsergisterModuleFromComponentRegistrar($moduleName)
    {
        $reflection = new \ReflectionClass(ComponentRegistrar::class);
        $reflectionProperty = $reflection->getProperty('paths');
        $reflectionProperty->setAccessible(true);
        $value = $reflectionProperty->getValue();
        unset($value[ComponentRegistrar::MODULE][$moduleName]);
        $reflectionProperty->setValue($value);
    }
}