PageRenderTime 38ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/dev/tests/static/testsuite/Magento/Test/Legacy/RestrictedCodeTest.php

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