Skip to content

Commit

Permalink
[PSR-2 Compliance] Fix #8612: Hundreds of PHPCS-based static tests vi…
Browse files Browse the repository at this point in the history
…olations 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
  • Loading branch information
orlangur committed Mar 11, 2017
1 parent 65d8a67 commit 0338636
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 136 deletions.
5 changes: 2 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class CodeSniffer implements ToolInterface, ExtensionInterface
*
* @var array
*/
private $extensions = ['php'];
private $extensions = ['php', 'phtml'];

/**
* Constructor
Expand All @@ -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;
}

Expand Down
10 changes: 10 additions & 0 deletions dev/tests/static/framework/Magento/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
-->
<ruleset name="Magento">
<description>Custom Magento coding standard.</description>

<rule ref="PSR2"/>

<rule ref="Magento.Files.LineLength">
<properties>
<property name="lineLimit" value="120"/>
Expand All @@ -16,4 +19,11 @@
<rule ref="Magento.LiteralNamespaces.LiteralNamespaces">
<exclude-pattern>*/_files/*</exclude-pattern>
</rule>

<rule ref="Generic.Functions.CallTimePassByReference"/>
<rule ref="Generic.PHP.DeprecatedFunctions"/>

<rule ref="Squiz.Commenting.DocCommentAlignment"/>
<rule ref="Squiz.Functions.GlobalFunction"/>
<rule ref="Squiz.WhiteSpace.LogicalOperatorSpacing"/>
</ruleset>
99 changes: 10 additions & 89 deletions dev/tests/static/testsuite/Magento/Test/Php/LiveCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
module * /
library * /
dev/tools/Magento
dev/tests/api-functional
dev/tests/functional
dev/tests/integration
dev/tests/static
setup
# Format: <componentType=module|library|theme|language|*> <componentName> <globPattern> or simply <globPattern>
* * /
dev
phpserver
pub
setup
*.php

0 comments on commit 0338636

Please sign in to comment.