Created
May 22, 2018 16:18
-
-
Save edorian/34620d24a2ddf19ff292198b09b620ff to your computer and use it in GitHub Desktop.
PHPCS integration testing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use PHPUnit\Framework\TestCase; | |
abstract class BaseTest extends TestCase { | |
public function testSniff() { | |
$sniff = substr(get_class($this), 0, -4); | |
$output = []; | |
$phpcs = __DIR__ . '/../../vendor/bin/phpcs'; | |
$standard = __DIR__ . '/../../src/YOUR_STANDARD_NAME_HERE/ruleset.xml'; | |
$sniffs = 'RgStandard.Rg.' . substr($sniff, 0, -5); | |
$testFolder = __DIR__ . '/../../test/' . $sniff . 'Test'; | |
$cmd = $phpcs . ' --standard=' . $standard . ' --sniffs=' . $sniffs . ' ' . escapeshellarg($testFolder); | |
$cmdJson = $phpcs . ' --report=json --standard=' . $standard . ' --sniffs=' . $sniffs . ' ' . escapeshellarg($testFolder); | |
exec($cmdJson, $output); | |
$fullOutput = json_decode(join("\n", $output),true); | |
$outputToCompare = []; | |
foreach ($fullOutput['files'] as $filename => $errors) { | |
$cleanedFilename = implode('/', array_slice(explode('/', $filename), -3)); | |
$outputToCompare[$cleanedFilename] = $errors; | |
} | |
$this->assertEquals($this->getExpectedOutput(), $outputToCompare, new CmdWrapper($cmd)); | |
} | |
abstract protected function getExpectedOutput(): array; | |
} | |
class CmdWrapper { | |
private $cmd; | |
public function __construct($cmd) { | |
$this->cmd = $cmd; | |
} | |
public function __toString() { | |
exec($this->cmd, $output); | |
return 'Full output was ' . "\n" . implode("\n", $output); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class ConcatenationSpacingSniffTest extends BaseTest { | |
protected function getExpectedOutput(): array { | |
return [ | |
'test/ConcatenationSpacingSniffTest/ConcatenationSpacingSniffTest.php' => [ | |
'errors' => 3, | |
'warnings' => 0, | |
'messages' => [ | |
[ | |
'message' => 'Concat operator must be surrounded by spaces', | |
'source' => 'RgStandard.Rg.ConcatenationSpacing.Missing', | |
'severity' => 5, | |
'type' => 'ERROR', | |
'line' => 3, | |
'column' => 10, | |
'fixable' => false, | |
], | |
[ | |
'message' => 'Concat operator must be surrounded by spaces', | |
'source' => 'RgStandard.Rg.ConcatenationSpacing.Missing', | |
'severity' => 5, | |
'type' => 'ERROR', | |
'line' => 4, | |
'column' => 10, | |
'fixable' => false, | |
], | |
[ | |
'message' => 'Concat operator must be surrounded by spaces', | |
'source' => 'RgStandard.Rg.ConcatenationSpacing.Missing', | |
'severity' => 5, | |
'type' => 'ERROR', | |
'line' => 5, | |
'column' => 11, | |
'fixable' => false, | |
] | |
] | |
] | |
]; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$foo = ''.''; | |
$foo = ''. ''; | |
$foo = '' .''; | |
$foo = '' . ''; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment