From 7c35998c626e20e1d893584ec6b7a6696398010e Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Thu, 6 Apr 2017 10:55:17 +0200 Subject: [PATCH] FEATURE: Also enable testing phpcbf * Add diff to test actual fixes. Relates: #46 --- .../DocCommentSniff/Expected.diff | 14 +++ ...ctedOutputForIssues.json => Expected.json} | 0 tests/SniffsTest.php | 88 +++++++++++++------ 3 files changed, 77 insertions(+), 25 deletions(-) create mode 100644 tests/Fixtures/Standards/Typo3Update/Sniffs/LegacyClassnames/DocCommentSniff/Expected.diff rename tests/Fixtures/Standards/Typo3Update/Sniffs/LegacyClassnames/DocCommentSniff/{ExpectedOutputForIssues.json => Expected.json} (100%) diff --git a/tests/Fixtures/Standards/Typo3Update/Sniffs/LegacyClassnames/DocCommentSniff/Expected.diff b/tests/Fixtures/Standards/Typo3Update/Sniffs/LegacyClassnames/DocCommentSniff/Expected.diff new file mode 100644 index 0000000..aa7add2 --- /dev/null +++ b/tests/Fixtures/Standards/Typo3Update/Sniffs/LegacyClassnames/DocCommentSniff/Expected.diff @@ -0,0 +1,14 @@ +--- tests/Fixtures/Standards/Typo3Update/Sniffs/LegacyClassnames/DocCommentSniff/InputFileForIssues.php ++++ PHP_CodeSniffer +@@ -22,9 +22,9 @@ + class InputFileForIssues + { + /** +- * @param t3lib_div ++ * @param \TYPO3\CMS\Core\Utility\GeneralUtility + * +- * @return Tx_Extbase_Configuration_Configurationmanager ++ * @return \TYPO3\CMS\Extbase\Configuration\ConfigurationManager + */ + public function doSomething($something) + { diff --git a/tests/Fixtures/Standards/Typo3Update/Sniffs/LegacyClassnames/DocCommentSniff/ExpectedOutputForIssues.json b/tests/Fixtures/Standards/Typo3Update/Sniffs/LegacyClassnames/DocCommentSniff/Expected.json similarity index 100% rename from tests/Fixtures/Standards/Typo3Update/Sniffs/LegacyClassnames/DocCommentSniff/ExpectedOutputForIssues.json rename to tests/Fixtures/Standards/Typo3Update/Sniffs/LegacyClassnames/DocCommentSniff/Expected.json diff --git a/tests/SniffsTest.php b/tests/SniffsTest.php index 49ebd4a..7995d35 100644 --- a/tests/SniffsTest.php +++ b/tests/SniffsTest.php @@ -64,12 +64,45 @@ class SniffsTest extends TestCase protected function executeSniff(SplFileInfo $folder) { $this->assertEquals( - $this->getExpectedOutput($folder), - $this->getOutput($folder)['output'], + $this->getExpectedJsonOutput($folder), + $this->getOutput($folder, 'json')['output'], 'Sniff ' . $this->getSniffByFolder($folder) . ' did not produce expected output for input file ' - . $this->getExpectedOutputFile($folder) + . $this->getInputFile($folder) ); + + $this->assertEquals( + $this->getExpectedDiffOutput($folder), + $this->getOutput($folder, 'diff')['output'], + 'Sniff ' . $this->getSniffByFolder($folder) + . ' did not produce expected diff for input file ' + . $this->getInputFile($folder) + ); + } + + /** + * Get expected json output for comparison. + * + * @param SplFileInfo $folder + * @return array + */ + protected function getExpectedJsonOutput(SplFileInfo $folder) + { + return json_decode( + file_get_contents($folder->getRealPath() . DIRECTORY_SEPARATOR . 'Expected.json'), + true + ); + } + + /** + * Returns absolute file path to diff file containing expected output. + * + * @param SplFileInfo $folder + * @return string + */ + protected function getExpectedDiffOutput(SplFileInfo $folder) + { + return file_get_contents($folder->getRealPath() . DIRECTORY_SEPARATOR . 'Expected.diff'); } /** @@ -81,57 +114,51 @@ class SniffsTest extends TestCase protected function getSniffByFolder(SplFileInfo $folder) { $folderParts = explode(DIRECTORY_SEPARATOR, $folder->getPath()); + return array_slice($folderParts, -3)[0] . '.' . array_slice($folderParts, -1)[0] . '.' . substr($folder->getFilename(), 0, -5); } /** - * Returns absolute file path to json file containing expected output. + * Returns file to use as input for phpcs. * * @param SplFileInfo $folder * @return string */ - protected function getExpectedOutputFile(SplFileInfo $folder) + protected function getInputFile(SplFileInfo $folder) { - return $folder->getRealPath() . DIRECTORY_SEPARATOR . 'ExpectedOutputForIssues.json'; - } - - /** - * Get expected output for comparison. - * - * @param SplFileInfo $folder - * @return array - */ - protected function getExpectedOutput(SplFileInfo $folder) - { - return json_decode( - file_get_contents($this->getExpectedOutputFile($folder)), - true - ); + return $folder->getRealPath() . DIRECTORY_SEPARATOR . 'InputFileForIssues.php'; } /** * Executes phpcs for sniff based on $folder and returns the generated output. * * @param SplFileInfo $folder + * @param string $report Defined the report format to use for output. * @return array */ - protected function getOutput(SplFileInfo $folder) + protected function getOutput(SplFileInfo $folder, $report) { $bin = './vendor/bin/phpcs'; $arguments = '--sniffs=' . $this->getSniffByFolder($folder) - . ' --report=json' + . ' --report=' . $report . ' ' - . $folder->getRealPath() . DIRECTORY_SEPARATOR . 'InputFileForIssues.php' + . $this->getInputFile($folder) ; $output = ''; $returnValue; exec($bin . ' ' . $arguments, $output, $returnValue); + if ($report === 'json') { + $output = $this->prepareJsonOutput($output); + } if ($report === 'diff') { + $output = $this->prepareDiffOutput($output); + } + return [ - 'output' => $this->prepareOutput($output), + 'output' => $output, 'returnValue' => $returnValue, ]; } @@ -142,7 +169,7 @@ class SniffsTest extends TestCase * @param array $output * @return array */ - protected function prepareOutput(array $output) + protected function prepareJsonOutput(array $output) { $preparedOutput = json_decode($output[0], true); @@ -154,4 +181,15 @@ class SniffsTest extends TestCase return $preparedOutput; } + + /** + * Prepare phpcs output for comparison. + * + * @param array $output + * @return string + */ + protected function prepareDiffOutput(array $output) + { + return implode("\n", $output); + } }