Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
RestrictedCodeTest.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Test\Legacy;
7 
9 
13 class RestrictedCodeTest extends \PHPUnit\Framework\TestCase
14 {
20  private static $_classes = [];
28  private static $_fixtureFiles = [];
29 
33  private $componentRegistrar;
34 
35  protected function setUp()
36  {
37  $this->componentRegistrar = new ComponentRegistrar();
38  }
39 
45  public static function setUpBeforeClass()
46  {
47  self::_loadData(self::$_classes, 'restricted_classes*.php');
48  }
49 
57  protected static function _loadData(array &$data, $filePattern)
58  {
59  foreach (glob(__DIR__ . '/_files/' . $filePattern) as $file) {
60  $relativePath = str_replace(
61  '\\',
62  '/',
63  str_replace(BP . DIRECTORY_SEPARATOR, '', $file)
64  );
65  array_push(self::$_fixtureFiles, $relativePath);
66  $data = array_merge_recursive($data, self::_readList($file));
67  }
68  }
69 
76  protected static function _readList($file)
77  {
78  return include $file;
79  }
80 
86  public function testPhpFiles()
87  {
88  $invoker = new \Magento\Framework\App\Utility\AggregateInvoker($this);
89  $testFiles = \Magento\TestFramework\Utility\ChangedFiles::getPhpFiles(__DIR__ . '/../_files/changed_files*');
90  foreach (self::$_fixtureFiles as $fixtureFile) {
91  if (array_key_exists($fixtureFile, $testFiles)) {
92  unset($testFiles[$fixtureFile]);
93  }
94  }
95  $invoker(
96  function ($file) {
97  $this->_testRestrictedClasses($file);
98  },
99  $testFiles
100  );
101  }
102 
109  protected function _testRestrictedClasses($file)
110  {
111  $content = file_get_contents($file);
112  foreach (self::$_classes as $restrictedClass => $classRules) {
113  foreach ($classRules['exclude'] as $skippedPathInfo) {
114  if (strpos($file, $this->getExcludedFilePath($skippedPathInfo)) === 0) {
115  continue 2;
116  }
117  }
118 
119  $this->assertFalse(
120  \Magento\TestFramework\Utility\CodeCheck::isClassUsed($restrictedClass, $content),
121  sprintf(
122  "Class '%s' is restricted in %s. Suggested replacement: %s",
123  $restrictedClass,
124  $file,
125  $classRules['replacement']
126  )
127  );
128  }
129  }
130 
137  private function getExcludedFilePath($pathInfo)
138  {
139  if ($pathInfo['type'] != 'setup') {
140  return $this->componentRegistrar->getPath($pathInfo['type'], $pathInfo['name']) . '/' . $pathInfo['path'];
141  }
142  return BP . '/setup/' . $pathInfo['path'];
143  }
144 }
static getPhpFiles($changedFilesList, $fileTypes=0)
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60
static _loadData(array &$data, $filePattern)
const BP
Definition: autoload.php:14
$relativePath
Definition: get.php:35