LiveCodeTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Migration\Test\Php;
  7. use Magento\Framework\App\Utility;
  8. use Magento\TestFramework\CodingStandard\Tool\CodeMessDetector;
  9. use Magento\TestFramework\CodingStandard\Tool\CodeSniffer;
  10. use Magento\TestFramework\CodingStandard\Tool\CodeSniffer\Wrapper;
  11. use PHPMD\TextUI\Command;
  12. use PHPUnit_Framework_TestCase;
  13. use Magento\Framework\App\Utility\Files;
  14. /**
  15. * Set of tests for static code analysis, e.g. code style, code complexity, copy paste detecting, etc.
  16. */
  17. class LiveCodeTest extends \PHPUnit\Framework\TestCase
  18. {
  19. /**
  20. * @var string
  21. */
  22. protected static $reportDir = '';
  23. /**
  24. * @var string
  25. */
  26. protected static $pathToSource = '';
  27. /**
  28. * @var string
  29. */
  30. protected static $magentoDir = '';
  31. /**
  32. * @var array
  33. */
  34. protected static $whiteList = [];
  35. /**
  36. * @var array
  37. */
  38. protected static $blackList = [];
  39. /**
  40. * Setup basics for all tests
  41. *
  42. * @return void
  43. */
  44. public static function setUpBeforeClass()
  45. {
  46. self::$pathToSource = Utility\Files::init()->getPathToSource();
  47. self::$reportDir = self::$pathToSource . '/vendor/magento/data-migration-tool/tests/static/report';
  48. self::$magentoDir = require __DIR__ . '/../../../../../etc/magento_path.php';
  49. if (!is_dir(self::$reportDir)) {
  50. mkdir(self::$reportDir, 0770);
  51. }
  52. self::setupFileLists();
  53. }
  54. /**
  55. * Helper method to setup the black and white lists
  56. *
  57. * @param string $type
  58. * @return void
  59. */
  60. public static function setupFileLists($type = '')
  61. {
  62. if ($type != '' && !preg_match('/\/$/', $type)) {
  63. $type = $type . '/';
  64. }
  65. self::$whiteList = Files::init()->readLists(__DIR__ . '/_files/' . $type . 'whitelist/*.txt');
  66. self::$blackList = Files::init()->readLists(__DIR__ . '/_files/' . $type . 'blacklist/*.txt');
  67. }
  68. /**
  69. * Run the PSR2 code sniffs on the code
  70. *
  71. * @TODO: combine with testCodeStyle
  72. * @return void
  73. */
  74. public function testCodeStylePsr2()
  75. {
  76. $reportFile = self::$reportDir . '/phpcs_psr2_report.xml';
  77. $wrapper = new Wrapper();
  78. $codeSniffer = new CodeSniffer('PSR2', $reportFile, $wrapper);
  79. if (!$codeSniffer->canRun()) {
  80. $this->markTestSkipped('PHP Code Sniffer is not installed.');
  81. }
  82. if (version_compare($wrapper->version(), '1.4.7') === -1) {
  83. $this->markTestSkipped('PHP Code Sniffer Build Too Old.');
  84. }
  85. self::setupFileLists('phpcs');
  86. $result = $codeSniffer->run(self::$whiteList, self::$blackList, ['php']);
  87. $this->assertFileExists(
  88. $reportFile,
  89. 'Expected ' . $reportFile . ' to be created by phpcs run with PSR2 standard'
  90. );
  91. $this->assertEquals(
  92. 0,
  93. $result,
  94. "PHP Code Sniffer has found {$result} error(s): See detailed report in {$reportFile}"
  95. );
  96. }
  97. /**
  98. * Run the magento specific coding standards on the code
  99. *
  100. * @return void
  101. */
  102. public function testCodeStyle()
  103. {
  104. $reportFile = self::$reportDir . '/phpcs_report.xml';
  105. $wrapper = new Wrapper();
  106. $codeSniffer = new CodeSniffer(realpath(__DIR__ . '/_files/phpcs'), $reportFile, $wrapper);
  107. if (!$codeSniffer->canRun()) {
  108. $this->markTestSkipped('PHP Code Sniffer is not installed.');
  109. }
  110. self::setupFileLists();
  111. $result = $codeSniffer->run(self::$whiteList, self::$blackList, ['php', 'phtml']);
  112. $this->assertEquals(
  113. 0,
  114. $result,
  115. "PHP Code Sniffer has found {$result} error(s): See detailed report in {$reportFile}"
  116. );
  117. }
  118. /**
  119. * Run the annotations sniffs on the code
  120. *
  121. * @return void
  122. * @todo Combine with normal code style at some point.
  123. */
  124. public function testAnnotationStandard()
  125. {
  126. $reportFile = self::$reportDir . '/phpcs_annotations_report.xml';
  127. $wrapper = new Wrapper();
  128. $codeSniffer = new CodeSniffer(
  129. realpath(self::$magentoDir . '/dev/tests/static/framework/Magento/ruleset.xml'),
  130. $reportFile,
  131. $wrapper
  132. );
  133. if (!$codeSniffer->canRun()) {
  134. $this->markTestSkipped('PHP Code Sniffer is not installed.');
  135. }
  136. self::setupFileLists('annotation');
  137. $severity = 0; // Change to 5 to see the warnings
  138. $this->assertEquals(
  139. 0,
  140. $result = $codeSniffer->run(self::$whiteList, self::$blackList, ['php'], $severity),
  141. "PHP Code Sniffer has found {$result} error(s): See detailed report in {$reportFile}"
  142. );
  143. }
  144. /**
  145. * Run mess detector on code
  146. *
  147. * @return void
  148. */
  149. public function testCodeMess()
  150. {
  151. $reportFile = self::$reportDir . '/phpmd_report.xml';
  152. $codeMessDetector = new CodeMessDetector(realpath(__DIR__ . '/_files/phpmd/ruleset.xml'), $reportFile);
  153. if (!$codeMessDetector->canRun()) {
  154. $this->markTestSkipped('PHP Mess Detector is not available.');
  155. }
  156. self::setupFileLists('phpmd');
  157. $this->assertEquals(
  158. Command::EXIT_SUCCESS,
  159. $codeMessDetector->run(self::$whiteList),
  160. "PHP Code Mess has found error(s): See detailed report in {$reportFile}"
  161. );
  162. // delete empty reports
  163. if (file_exists($reportFile)) {
  164. unlink($reportFile);
  165. }
  166. }
  167. }