DependencyTest.php 4.98 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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Test\Integrity\Library;

use Magento\Framework\App\Utility\Files;
use Magento\Framework\App\Utility\AggregateInvoker;
use Magento\Framework\Component\ComponentRegistrar;
use Magento\TestFramework\Integrity\Library\Injectable;
use Magento\TestFramework\Integrity\Library\PhpParser\ParserFactory;
use Magento\TestFramework\Integrity\Library\PhpParser\Tokens;
use Zend\Code\Reflection\FileReflection;

/**
 * Test check if Magento library components contain incorrect dependencies to application layer
 *
 */
class DependencyTest extends \PHPUnit\Framework\TestCase
{
    /**
     * Collect errors
     *
     * @var array
     */
    protected $errors = [];

    /**
     * Allowed sub namespaces
     *
     * @return array
     */
    protected function getAllowedNamespaces()
    {
        return [
            'Framework',
            'SomeModule',
            'ModuleName',
            'Setup\Console\CommandList',
            'Setup\Console\CompilerPreparation',
            'Setup\Model\ObjectManagerProvider',
            'Setup\Mvc\Bootstrap\InitParamListener',
            'Store\Model\ScopeInterface',
            'Store\Model\StoreManagerInterface',
            'Directory\Model\CurrencyFactory',
            'PageCache\Model\Cache\Type',
            'Backup\Model\ResourceModel\Db',
            'Backend\Block\Widget\Button',
            'Ui\Component\Container',
            'SalesRule\Model\Rule',
            'SalesRule\Api\Data\RuleInterface',
            'SalesRule\Model\Rule\Interceptor',
            'SalesRule\Model\Rule\Proxy',
            'Theme\Model\View\Design'
        ];
    }

    public function testCheckDependencies()
    {
        $invoker = new \Magento\Framework\App\Utility\AggregateInvoker($this);
        $invoker(
            /**
             * @param string $file
             */
            function ($file) {
                $componentRegistrar = new ComponentRegistrar();
                $fileReflection = new FileReflection($file);
                $tokens = new Tokens($fileReflection->getContents(), new ParserFactory());
                $tokens->parseContent();

                $dependencies = array_merge(
                    (new Injectable())->getDependencies($fileReflection),
                    $tokens->getDependencies()
                );
                $allowedNamespaces = str_replace('\\', '\\\\', implode('|', $this->getAllowedNamespaces()));
                $pattern = '#Magento\\\\(?!' . $allowedNamespaces . ').*#';
                foreach ($dependencies as $dependency) {
                    $dependencyPaths = explode('\\', $dependency);
                    $dependencyPaths = array_slice($dependencyPaths, 2);
                    $dependencyPath = implode('\\', $dependencyPaths);
                    $libraryPaths = $componentRegistrar->getPaths(ComponentRegistrar::LIBRARY);
                    foreach ($libraryPaths as $libraryPath) {
                        $filePath = str_replace('\\', '/', $libraryPath .  '/' . $dependencyPath . '.php');
                        if (preg_match($pattern, $dependency) && !file_exists($filePath)) {
                            $this->errors[$fileReflection->getFileName()][] = $dependency;
                        }
                    }
                }

                if (!empty($this->errors)) {
                    $this->fail($this->getFailMessage());
                }
            },
            $this->libraryDataProvider()
        );
    }

    /**
     * @inheritdoc
     */
    public function tearDown()
    {
        $this->errors = [];
    }

    /**
     * Prepare failed message
     *
     * @return string
     */
    protected function getFailMessage()
    {
        $failMessage = '';
        foreach ($this->errors as $class => $dependencies) {
            $failMessage .= $class . ' depends for non-library ' . (count($dependencies) > 1 ? 'classes ' : 'class ');
            foreach ($dependencies as $dependency) {
                $failMessage .= $dependency . ' ';
            }
            $failMessage = trim($failMessage) . PHP_EOL;
        }
        return $failMessage;
    }

    /**
     * Contains all library files
     *
     * @return array
     */
    public function libraryDataProvider()
    {
        // @TODO: remove this code when class Magento\Framework\Data\Collection will fixed
        $componentRegistrar = new ComponentRegistrar();
        include_once $componentRegistrar->getPath(ComponentRegistrar::LIBRARY, 'magento/framework')
            . '/Option/ArrayInterface.php';
        $blackList = Files::init()->readLists(__DIR__ . '/_files/blacklist*.txt');
        $dataProvider = Files::init()->getPhpFiles(Files::INCLUDE_LIBS | Files::AS_DATA_SET);

        foreach ($dataProvider as $key => $data) {
            if (in_array($data[0], $blackList)) {
                unset($dataProvider[$key]);
            } else {
                include_once $data[0];
            }
        }
        return $dataProvider;
    }
}