From 033863653b01430f8a635c61029c624b2d613d9d Mon Sep 17 00:00:00 2001 From: Vlad Veselov Date: Sun, 26 Feb 2017 22:41:31 +0200 Subject: [PATCH] [PSR-2 Compliance] Fix #8612: Hundreds of PHPCS-based static tests violations in mainline - enable PHPCS-based tests for the whole codebase - combine three PHPCS-based tests into one - polish up Magento Coding Standard into one ruleset.xml: no new rules added, only those rules removed which are already contained in PSR2 - run static tests under PHP 7 which is ~3 times faster than PHP 5.6 --- .travis.yml | 5 +- .../CodingStandard/Tool/CodeSniffer.php | 7 +- .../static/framework/Magento/ruleset.xml | 10 ++ .../Magento/Test/Php/LiveCodeTest.php | 99 ++----------------- .../Magento/Test/Php/_files/phpcs/ruleset.xml | 34 ------- .../Test/Php/_files/whitelist/common.txt | 15 ++- 6 files changed, 34 insertions(+), 136 deletions(-) delete mode 100644 dev/tests/static/testsuite/Magento/Test/Php/_files/phpcs/ruleset.xml diff --git a/.travis.yml b/.travis.yml index 74e374412fff4..6c147707b3342 100644 --- a/.travis.yml +++ b/.travis.yml @@ -26,12 +26,11 @@ cache: directories: $HOME/.composer/cache matrix: exclude: - - php: 7.0 + - php: 5.6.29 env: TEST_SUITE=static before_install: ./dev/travis/before_install.sh install: composer install --no-interaction --prefer-dist before_script: ./dev/travis/before_script.sh script: - - cd dev/tests/$TEST_SUITE - test $TEST_SUITE = "static" && TEST_FILTER='--filter "Magento\\Test\\Php\\LiveCodeTest"' || true - - phpunit $TEST_FILTER + - phpunit -c dev/tests/$TEST_SUITE $TEST_FILTER diff --git a/dev/tests/static/framework/Magento/TestFramework/CodingStandard/Tool/CodeSniffer.php b/dev/tests/static/framework/Magento/TestFramework/CodingStandard/Tool/CodeSniffer.php index eba745bfe2da9..d0b9868796be8 100644 --- a/dev/tests/static/framework/Magento/TestFramework/CodingStandard/Tool/CodeSniffer.php +++ b/dev/tests/static/framework/Magento/TestFramework/CodingStandard/Tool/CodeSniffer.php @@ -40,7 +40,7 @@ class CodeSniffer implements ToolInterface, ExtensionInterface * * @var array */ - private $extensions = ['php']; + private $extensions = ['php', 'phtml']; /** * Constructor @@ -51,8 +51,11 @@ class CodeSniffer implements ToolInterface, ExtensionInterface */ public function __construct($rulesetDir, $reportFile, Wrapper $wrapper) { - $this->reportFile = $reportFile; $this->rulesetDir = $rulesetDir; + if (!file_exists($rulesetDir) && file_exists($fullPath = realpath(__DIR__ . '/../../../../' . $rulesetDir))) { + $this->rulesetDir = $fullPath; + } + $this->reportFile = $reportFile; $this->wrapper = $wrapper; } diff --git a/dev/tests/static/framework/Magento/ruleset.xml b/dev/tests/static/framework/Magento/ruleset.xml index a636b13c7ff99..d877eb17104d4 100644 --- a/dev/tests/static/framework/Magento/ruleset.xml +++ b/dev/tests/static/framework/Magento/ruleset.xml @@ -7,6 +7,9 @@ --> Custom Magento coding standard. + + + @@ -16,4 +19,11 @@ */_files/* + + + + + + + diff --git a/dev/tests/static/testsuite/Magento/Test/Php/LiveCodeTest.php b/dev/tests/static/testsuite/Magento/Test/Php/LiveCodeTest.php index 036805558830f..2dcf36a4087ee 100644 --- a/dev/tests/static/testsuite/Magento/Test/Php/LiveCodeTest.php +++ b/dev/tests/static/testsuite/Magento/Test/Php/LiveCodeTest.php @@ -189,101 +189,27 @@ function ($file) use ($fileHasAllowedType, $fileIsInAllowedDirectory) { } /** - * Run the PSR2 code sniffs on the code + * Retrieves full list of codebase paths without any files/folders filtered out * - * @TODO: combine with testCodeStyle - * @return void + * @return array */ - public function testCodeStylePsr2() + private function getFullWhitelist() { - $reportFile = self::$reportDir . '/phpcs_psr2_report.txt'; - $wrapper = new Wrapper(); - $codeSniffer = new CodeSniffer('PSR2', $reportFile, $wrapper); - if (!$codeSniffer->canRun()) { - $this->markTestSkipped('PHP Code Sniffer is not installed.'); - } - if (version_compare($wrapper->version(), '1.4.7') === -1) { - $this->markTestSkipped('PHP Code Sniffer Build Too Old.'); - } - - $result = $codeSniffer->run(self::getWhitelist()); - - $output = ""; - if (file_exists($reportFile)) { - $output = file_get_contents($reportFile); - } - $this->assertEquals( - 0, - $result, - "PHP Code Sniffer has found {$result} error(s): " . PHP_EOL . $output - ); + return Files::init()->readLists(__DIR__ . '/_files/whitelist/common.txt'); } - /** - * Run the magento specific coding standards on the code - * - * @return void - */ - public function testCodeStyle() + public function testNoViolationsDetectedByPhpCodeSniffer() { $reportFile = self::$reportDir . '/phpcs_report.txt'; - $wrapper = new Wrapper(); - $codeSniffer = new CodeSniffer(realpath(__DIR__ . '/_files/phpcs'), $reportFile, $wrapper); - if (!$codeSniffer->canRun()) { - $this->markTestSkipped('PHP Code Sniffer is not installed.'); - } - $codeSniffer->setExtensions(['php', 'phtml']); - $result = $codeSniffer->run(self::getWhitelist(['php', 'phtml'])); - - $output = ""; - if (file_exists($reportFile)) { - $output = file_get_contents($reportFile); - } - - $this->assertEquals( - 0, - $result, - "PHP Code Sniffer has found {$result} error(s): " . PHP_EOL . $output - ); - } - - /** - * Run the annotations sniffs on the code - * - * @return void - * @todo Combine with normal code style at some point. - */ - public function testAnnotationStandard() - { - $reportFile = self::$reportDir . '/phpcs_annotations_report.txt'; - $wrapper = new Wrapper(); - $codeSniffer = new CodeSniffer( - realpath(__DIR__ . '/../../../../framework/Magento/ruleset.xml'), - $reportFile, - $wrapper - ); - if (!$codeSniffer->canRun()) { - $this->markTestSkipped('PHP Code Sniffer is not installed.'); - } - - $result = $codeSniffer->run(self::getWhitelist(['php'])); - $output = ""; - if (file_exists($reportFile)) { - $output = file_get_contents($reportFile); - } + $codeSniffer = new CodeSniffer('Magento', $reportFile, new Wrapper()); $this->assertEquals( 0, - $result, - "PHP Code Sniffer has found {$result} error(s): " . PHP_EOL . $output + $result = $codeSniffer->run($this->getFullWhitelist()), + "PHP Code Sniffer detected {$result} violation(s): " . PHP_EOL . file_get_contents($reportFile) ); } - /** - * Run mess detector on code - * - * @return void - */ - public function testCodeMess() + public function testNoViolationsDetectedByPhpMessDetector() { $reportFile = self::$reportDir . '/phpmd_report.txt'; $codeMessDetector = new CodeMessDetector(realpath(__DIR__ . '/_files/phpmd/ruleset.xml'), $reportFile); @@ -312,12 +238,7 @@ public function testCodeMess() } } - /** - * Run copy paste detector on code - * - * @return void - */ - public function testCopyPaste() + public function testNoViolationsDetectedByPhpCopyPasteDetector() { $reportFile = self::$reportDir . '/phpcpd_report.xml'; $copyPasteDetector = new CopyPasteDetector($reportFile); diff --git a/dev/tests/static/testsuite/Magento/Test/Php/_files/phpcs/ruleset.xml b/dev/tests/static/testsuite/Magento/Test/Php/_files/phpcs/ruleset.xml deleted file mode 100644 index 8a3a701c15feb..0000000000000 --- a/dev/tests/static/testsuite/Magento/Test/Php/_files/phpcs/ruleset.xml +++ /dev/null @@ -1,34 +0,0 @@ - - - - Magento coding standard. - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/dev/tests/static/testsuite/Magento/Test/Php/_files/whitelist/common.txt b/dev/tests/static/testsuite/Magento/Test/Php/_files/whitelist/common.txt index e5c31e5afcb0c..8e1e6f16bc351 100644 --- a/dev/tests/static/testsuite/Magento/Test/Php/_files/whitelist/common.txt +++ b/dev/tests/static/testsuite/Magento/Test/Php/_files/whitelist/common.txt @@ -1,8 +1,7 @@ -module * / -library * / -dev/tools/Magento -dev/tests/api-functional -dev/tests/functional -dev/tests/integration -dev/tests/static -setup \ No newline at end of file +# Format: or simply +* * / +dev +phpserver +pub +setup +*.php