|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Test\Legacy\App\Action; |
| 9 | + |
| 10 | +use Exception; |
| 11 | +use Magento\Framework\App\Action\AbstractAction; |
| 12 | +use Magento\Framework\App\ActionInterface; |
| 13 | +use Magento\Framework\App\Utility\Files; |
| 14 | +use Magento\Framework\Exception\LocalizedException; |
| 15 | +use Magento\TestFramework\Utility\ClassNameExtractor; |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | +use ReflectionClass; |
| 18 | + |
| 19 | +/** |
| 20 | + * Test newly created controllers must do not extend AbstractAction. |
| 21 | + */ |
| 22 | +class AbstractActionTest extends TestCase |
| 23 | +{ |
| 24 | + /** |
| 25 | + * @var ClassNameExtractor |
| 26 | + */ |
| 27 | + private $classNameExtractor; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var Files |
| 31 | + */ |
| 32 | + private $fileUtilities; |
| 33 | + |
| 34 | + /** |
| 35 | + * @throws LocalizedException |
| 36 | + */ |
| 37 | + protected function setUp(): void |
| 38 | + { |
| 39 | + $this->classNameExtractor = new ClassNameExtractor(); |
| 40 | + $this->fileUtilities = Files::init(); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Test new |
| 45 | + * |
| 46 | + */ |
| 47 | + public function testNewControllersDoNotExtendAbstractAction(): void |
| 48 | + { |
| 49 | + $files = $this->getTestFiles(); |
| 50 | + $found = []; |
| 51 | + |
| 52 | + foreach ($files as $file) { |
| 53 | + $class = $this->classNameExtractor->getNameWithNamespace(file_get_contents($file[0])); |
| 54 | + |
| 55 | + if ($class) { |
| 56 | + try { |
| 57 | + $classReflection = new ReflectionClass($class); |
| 58 | + if ($classReflection->isSubclassOf(AbstractAction::class)) { |
| 59 | + $found[] = $class; |
| 60 | + } |
| 61 | + } catch (Exception $exception) { |
| 62 | + $this->addWarning('Skipped due to exception: ' . $class); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + $this->assertEmpty( |
| 68 | + $found, |
| 69 | + "The following new controller(s) extend " . AbstractAction::class . "\r\n" |
| 70 | + . "All new controller classes must implement " . ActionInterface::class . " instead.\r\n" |
| 71 | + . print_r($found, true) |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Provide files for test. |
| 77 | + * |
| 78 | + * @return array |
| 79 | + */ |
| 80 | + private function getTestFiles(): array |
| 81 | + { |
| 82 | + $phpFiles = self::getAddedFilesList(self::getChangedFilesBaseDir()); |
| 83 | + |
| 84 | + $phpFiles = Files::composeDataSets($phpFiles); |
| 85 | + $fileTypes = Files::INCLUDE_APP_CODE | Files::INCLUDE_LIBS | Files::AS_DATA_SET; |
| 86 | + return array_intersect_key($phpFiles, $this->fileUtilities->getPhpFiles($fileTypes)); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Provide list of new files. |
| 91 | + * |
| 92 | + * @param $changedFilesBaseDir |
| 93 | + * |
| 94 | + * @return string[] |
| 95 | + */ |
| 96 | + private static function getAddedFilesList($changedFilesBaseDir) |
| 97 | + { |
| 98 | + return self::getFilesFromListFile( |
| 99 | + $changedFilesBaseDir, |
| 100 | + 'changed_files*.added.*', |
| 101 | + function () { |
| 102 | + // if no list files, probably, this is the dev environment |
| 103 | + // phpcs:ignore Generic.PHP.NoSilencedErrors,Magento2.Security.InsecureFunction |
| 104 | + @exec('git diff --cached --name-only --diff-filter=A', $addedFiles); |
| 105 | + return $addedFiles; |
| 106 | + } |
| 107 | + ); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Read files from generated lists. |
| 112 | + * |
| 113 | + * @param string $listsBaseDir |
| 114 | + * @param string $listFilePattern |
| 115 | + * @param callable $noListCallback |
| 116 | + * @return string[] |
| 117 | + */ |
| 118 | + private static function getFilesFromListFile( |
| 119 | + string $listsBaseDir, |
| 120 | + string $listFilePattern, |
| 121 | + callable $noListCallback |
| 122 | + ): array { |
| 123 | + $filesDefinedInList = []; |
| 124 | + |
| 125 | + $listFiles = glob($listsBaseDir . '/_files/' . $listFilePattern); |
| 126 | + if (!empty($listFiles)) { |
| 127 | + foreach ($listFiles as $listFile) { |
| 128 | + // phpcs:ignore Magento2.Performance.ForeachArrayMerge.ForeachArrayMerge |
| 129 | + $filesDefinedInList = array_merge( |
| 130 | + $filesDefinedInList, |
| 131 | + file($listFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) |
| 132 | + ); |
| 133 | + } |
| 134 | + } else { |
| 135 | + $filesDefinedInList = call_user_func($noListCallback); |
| 136 | + } |
| 137 | + |
| 138 | + array_walk( |
| 139 | + $filesDefinedInList, |
| 140 | + function (&$file) { |
| 141 | + $file = BP . '/' . $file; |
| 142 | + } |
| 143 | + ); |
| 144 | + |
| 145 | + $filesDefinedInList = array_values(array_unique($filesDefinedInList)); |
| 146 | + |
| 147 | + return $filesDefinedInList; |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Returns base directory for generated lists. |
| 152 | + * |
| 153 | + * @return string |
| 154 | + */ |
| 155 | + private static function getChangedFilesBaseDir(): string |
| 156 | + { |
| 157 | + return BP . DIRECTORY_SEPARATOR . 'dev' . DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR . 'static' . |
| 158 | + DIRECTORY_SEPARATOR . 'testsuite' . DIRECTORY_SEPARATOR . 'Magento' . DIRECTORY_SEPARATOR . 'Test'; |
| 159 | + } |
| 160 | +} |
0 commit comments