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

https://gitlab.com/axeltizon/magento-demopoweraccess · PHP · 128 lines · 94 code · 9 blank · 25 comment · 5 complexity · 80ac5a853b29f604ede50bcbbccfd5b3 MD5 · raw file

  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Scans source code for references to classes and see if they indeed exist
  8. */
  9. namespace Magento\Test\Legacy;
  10. use Magento\Framework\App\Utility\Files;
  11. class ClassesTest extends \PHPUnit_Framework_TestCase
  12. {
  13. public function testPhpCode()
  14. {
  15. $invoker = new \Magento\Framework\App\Utility\AggregateInvoker($this);
  16. $invoker(
  17. /**
  18. * @param string $file
  19. */
  20. function ($file) {
  21. $classes = \Magento\Framework\App\Utility\Classes::collectPhpCodeClasses(file_get_contents($file));
  22. $this->_assertNonFactoryName($classes, $file);
  23. },
  24. Files::init()->getPhpFiles(
  25. Files::INCLUDE_APP_CODE
  26. | Files::INCLUDE_PUB_CODE
  27. | Files::INCLUDE_LIBS
  28. | Files::INCLUDE_TEMPLATES
  29. | Files::AS_DATA_SET
  30. | Files::INCLUDE_NON_CLASSES
  31. )
  32. );
  33. }
  34. public function testConfiguration()
  35. {
  36. $invoker = new \Magento\Framework\App\Utility\AggregateInvoker($this);
  37. $invoker(
  38. /**
  39. * @param string $path
  40. */
  41. function ($path) {
  42. $xml = simplexml_load_file($path);
  43. $classes = \Magento\Framework\App\Utility\Classes::collectClassesInConfig($xml);
  44. $this->_assertNonFactoryName($classes, $path);
  45. $modules = \Magento\Framework\App\Utility\Classes::getXmlAttributeValues($xml, '//@module', 'module');
  46. $this->_assertNonFactoryName(array_unique($modules), $path, false, true);
  47. },
  48. Files::init()->getConfigFiles()
  49. );
  50. }
  51. public function testLayouts()
  52. {
  53. $invoker = new \Magento\Framework\App\Utility\AggregateInvoker($this);
  54. $invoker(
  55. /**
  56. * @param string $path
  57. */
  58. function ($path) {
  59. $xml = simplexml_load_file($path);
  60. $classes = \Magento\Framework\App\Utility\Classes::collectLayoutClasses($xml);
  61. foreach (\Magento\Framework\App\Utility\Classes::getXmlAttributeValues(
  62. $xml,
  63. '/layout//@helper',
  64. 'helper'
  65. ) as $class) {
  66. $classes[] = \Magento\Framework\App\Utility\Classes::getCallbackClass($class);
  67. }
  68. $classes = array_merge(
  69. $classes,
  70. \Magento\Framework\App\Utility\Classes::getXmlAttributeValues($xml, '/layout//@module', 'module')
  71. );
  72. $this->_assertNonFactoryName(array_unique($classes), $path);
  73. $tabs = \Magento\Framework\App\Utility\Classes::getXmlNodeValues(
  74. $xml,
  75. '/layout//action[@method="addTab"]/block'
  76. );
  77. $this->_assertNonFactoryName(array_unique($tabs), $path, true);
  78. },
  79. Files::init()->getLayoutFiles()
  80. );
  81. }
  82. /**
  83. * Check whether specified classes or module names correspond to a file according PSR-1 Standard.
  84. *
  85. * Suppressing "unused variable" because of the "catch" block
  86. *
  87. * @param array $names
  88. * @param bool $softComparison
  89. * @SuppressWarnings(PHPMD.UnusedLocalVariable)
  90. */
  91. protected function _assertNonFactoryName($names, $file, $softComparison = false, $moduleBlock = false)
  92. {
  93. if (!$names) {
  94. return;
  95. }
  96. $factoryNames = [];
  97. foreach ($names as $name) {
  98. try {
  99. if ($softComparison) {
  100. $this->assertNotRegExp('/\//', $name);
  101. } elseif ($moduleBlock) {
  102. $this->assertFalse(false === strpos($name, '_'));
  103. $this->assertRegExp('/^([A-Z][A-Za-z\d_]+)+$/', $name);
  104. } else {
  105. if (strpos($name, 'Magento') === false) {
  106. continue;
  107. }
  108. $this->assertFalse(false === strpos($name, '\\'));
  109. $this->assertRegExp('/^([A-Z\\\\][A-Za-z\d\\\\]+)+$/', $name);
  110. }
  111. } catch (\PHPUnit_Framework_AssertionFailedError $e) {
  112. $factoryNames[] = $name;
  113. }
  114. }
  115. if ($factoryNames) {
  116. $this->fail("Obsolete factory name(s) detected in {$file}:" . "\n" . implode("\n", $factoryNames));
  117. }
  118. }
  119. }