ExceptionHierarchyTest.php 4.05 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
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Test\Integrity;

use Magento\Framework\App\Utility\Files as UtilityFiles;

/**
 * Checks that all Exceptions inherit LocalizedException
 */
class ExceptionHierarchyTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @param \ReflectionClass $reflectionException
     * @dataProvider isInheritedLocalizedExceptionDataProvider
     */
    public function testIsInheritedLocalizedException(\ReflectionClass $reflectionException)
    {
        $this->assertTrue(
            $reflectionException->isSubclassOf('Magento\Framework\Exception\LocalizedException'),
            "{$reflectionException->name} is not inherited LocalizedException"
        );
    }

    /**
     * @return array
     */
    public function isInheritedLocalizedExceptionDataProvider()
    {
        $files = UtilityFiles::init()->getPhpFiles(UtilityFiles::INCLUDE_APP_CODE | UtilityFiles::INCLUDE_LIBS);
        $blacklistExceptions = $this->getBlacklistExceptions();

        $data = [];
        foreach ($files as $file) {
            $className = $this->convertPathToClassName($file);
            try {
                $reflectionClass = new \ReflectionClass($className);
                if ($reflectionClass->isSubclassOf('Exception') && !in_array($className, $blacklistExceptions)) {
                    $data[$className] = [$reflectionClass];
                }
            } catch (\Exception $e) {
                $this->fail("File name and class name '{$className}' is not appropriate");
            }
        }
        return $data;
    }

    /**
     * @param string $filePath
     * @return string
     */
    protected function convertPathToClassName($filePath)
    {
        $componentRegistrar = new \Magento\Framework\Component\ComponentRegistrar();
        $foundItems = null;
        $moduleNamespace = null;
        $foundItems = array_filter(
            $componentRegistrar->getPaths(\Magento\Framework\Component\ComponentRegistrar::MODULE),
            function ($item) use ($filePath) {
                if (strpos($filePath, $item . '/') !== false) {
                    return true;
                } else {
                    return false;
                }
            }
        );
        if ($foundItems) {
            $moduleNamespace = str_replace('_', '\\', array_keys($foundItems)[0]);
            $classPath = str_replace('/', '\\', str_replace(array_shift($foundItems), '', $filePath));
        } else {
            $foundItems = array_filter(
                $componentRegistrar->getPaths(\Magento\Framework\Component\ComponentRegistrar::LIBRARY),
                function ($item) use ($filePath) {
                    if (strpos($filePath, $item . '/') !== false) {
                        return true;
                    } else {
                        return false;
                    }
                }
            );
            $libName = array_keys($foundItems)[0];
            $libName = str_replace('framework-', 'framework/', $libName);
            $namespaceParts = explode('/', $libName);
            $namespaceParts = array_map(
                function ($item) {
                    return str_replace(' ', '', ucwords(str_replace('-', ' ', $item)));
                },
                $namespaceParts
            );
            $moduleNamespace = implode('\\', $namespaceParts);
            $classPath = str_replace('/', '\\', str_replace(array_shift($foundItems), '', $filePath));
        }

        $className = '\\' . $moduleNamespace . $classPath;
        $className = str_replace('.php', '', $className);
        return $className;
    }

    /**
     * @return array
     */
    protected function getBlacklistExceptions()
    {
        $blacklistFiles = str_replace('\\', '/', realpath(__DIR__)) . '/_files/blacklist/exception_hierarchy*.txt';
        $exceptions = [];
        foreach (glob($blacklistFiles) as $fileName) {
            $exceptions = array_merge($exceptions, file($fileName, FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES));
        }
        return $exceptions;
    }
}