RestrictedCodeTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Test\Legacy;
  7. use Magento\Framework\Component\ComponentRegistrar;
  8. /**
  9. * Tests to find usage of restricted code
  10. */
  11. class RestrictedCodeTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**@#+
  14. * Lists of restricted entities from fixtures
  15. *
  16. * @var array
  17. */
  18. private static $_classes = [];
  19. /**#@-*/
  20. /**
  21. * List of fixtures that contain restricted classes and should not be tested
  22. *
  23. * @var array
  24. */
  25. private static $_fixtureFiles = [];
  26. /**
  27. * @var ComponentRegistrar
  28. */
  29. private $componentRegistrar;
  30. protected function setUp()
  31. {
  32. $this->componentRegistrar = new ComponentRegistrar();
  33. }
  34. /**
  35. * Read fixtures into memory as arrays
  36. *
  37. * @return void
  38. */
  39. public static function setUpBeforeClass()
  40. {
  41. self::_loadData(self::$_classes, 'restricted_classes*.php');
  42. }
  43. /**
  44. * Loads and merges data from fixtures
  45. *
  46. * @param array $data
  47. * @param string $filePattern
  48. * @return void
  49. */
  50. protected static function _loadData(array &$data, $filePattern)
  51. {
  52. foreach (glob(__DIR__ . '/_files/' . $filePattern) as $file) {
  53. $relativePath = str_replace(
  54. '\\',
  55. '/',
  56. str_replace(BP . DIRECTORY_SEPARATOR, '', $file)
  57. );
  58. array_push(self::$_fixtureFiles, $relativePath);
  59. $data = array_merge_recursive($data, self::_readList($file));
  60. }
  61. }
  62. /**
  63. * Isolate including a file into a method to reduce scope
  64. *
  65. * @param string $file
  66. * @return array
  67. */
  68. protected static function _readList($file)
  69. {
  70. return include $file;
  71. }
  72. /**
  73. * Test that restricted entities are not used in PHP files
  74. *
  75. * @return void
  76. */
  77. public function testPhpFiles()
  78. {
  79. $invoker = new \Magento\Framework\App\Utility\AggregateInvoker($this);
  80. $testFiles = \Magento\TestFramework\Utility\ChangedFiles::getPhpFiles(__DIR__ . '/../_files/changed_files*');
  81. foreach (self::$_fixtureFiles as $fixtureFile) {
  82. if (array_key_exists($fixtureFile, $testFiles)) {
  83. unset($testFiles[$fixtureFile]);
  84. }
  85. }
  86. $invoker(
  87. function ($file) {
  88. $this->_testRestrictedClasses($file);
  89. },
  90. $testFiles
  91. );
  92. }
  93. /**
  94. * Assert that restricted classes are not used in the file
  95. *
  96. * @param string $file
  97. * @return void
  98. */
  99. protected function _testRestrictedClasses($file)
  100. {
  101. $content = file_get_contents($file);
  102. foreach (self::$_classes as $restrictedClass => $classRules) {
  103. foreach ($classRules['exclude'] as $skippedPathInfo) {
  104. if (strpos($file, $this->getExcludedFilePath($skippedPathInfo)) === 0) {
  105. continue 2;
  106. }
  107. }
  108. $this->assertFalse(
  109. \Magento\TestFramework\Utility\CodeCheck::isClassUsed($restrictedClass, $content),
  110. sprintf(
  111. "Class '%s' is restricted in %s. Suggested replacement: %s",
  112. $restrictedClass,
  113. $file,
  114. $classRules['replacement']
  115. )
  116. );
  117. }
  118. }
  119. /**
  120. * Get full path for excluded file
  121. *
  122. * @param array $pathInfo
  123. * @return string
  124. */
  125. private function getExcludedFilePath($pathInfo)
  126. {
  127. if ($pathInfo['type'] != 'setup') {
  128. return $this->componentRegistrar->getPath($pathInfo['type'], $pathInfo['name']) . '/' . $pathInfo['path'];
  129. }
  130. return BP . '/setup/' . $pathInfo['path'];
  131. }
  132. }