From 0703167315ad54572679aa73121599067bcefcc8 Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Thu, 6 Apr 2017 15:36:52 +0200 Subject: [PATCH] FEATURE: Allow to overwrite cli arguments for test * Enable overwriting using Arguments.php. * Add multiple sub tests for AjaxRegistrationSniff. Relates: #46 --- .../AjaxRegistrationSniff/Arguments.php | 29 ++++ .../ext_localconf/Expected.json | 33 +++++ .../ext_localconf/ext_localconf.php | 23 ++++ .../ext_tables/Expected.json | 33 +++++ .../ext_tables/ext_tables.php | 23 ++++ tests/SniffsTest.php | 125 ++++++++++++++---- 6 files changed, 237 insertions(+), 29 deletions(-) create mode 100644 tests/Fixtures/Standards/Typo3Update/Sniffs/Deprecated/AjaxRegistrationSniff/Arguments.php create mode 100644 tests/Fixtures/Standards/Typo3Update/Sniffs/Deprecated/AjaxRegistrationSniff/ext_localconf/Expected.json create mode 100644 tests/Fixtures/Standards/Typo3Update/Sniffs/Deprecated/AjaxRegistrationSniff/ext_localconf/ext_localconf.php create mode 100644 tests/Fixtures/Standards/Typo3Update/Sniffs/Deprecated/AjaxRegistrationSniff/ext_tables/Expected.json create mode 100644 tests/Fixtures/Standards/Typo3Update/Sniffs/Deprecated/AjaxRegistrationSniff/ext_tables/ext_tables.php diff --git a/tests/Fixtures/Standards/Typo3Update/Sniffs/Deprecated/AjaxRegistrationSniff/Arguments.php b/tests/Fixtures/Standards/Typo3Update/Sniffs/Deprecated/AjaxRegistrationSniff/Arguments.php new file mode 100644 index 0000000..2fa88ab --- /dev/null +++ b/tests/Fixtures/Standards/Typo3Update/Sniffs/Deprecated/AjaxRegistrationSniff/Arguments.php @@ -0,0 +1,29 @@ + + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +return [ + 'ext_tables' => [ + 'inputFileName' => 'ext_tables.php', + ], + 'ext_localconf' => [ + 'inputFileName' => 'ext_localconf.php', + ], +]; diff --git a/tests/Fixtures/Standards/Typo3Update/Sniffs/Deprecated/AjaxRegistrationSniff/ext_localconf/Expected.json b/tests/Fixtures/Standards/Typo3Update/Sniffs/Deprecated/AjaxRegistrationSniff/ext_localconf/Expected.json new file mode 100644 index 0000000..5fe396f --- /dev/null +++ b/tests/Fixtures/Standards/Typo3Update/Sniffs/Deprecated/AjaxRegistrationSniff/ext_localconf/Expected.json @@ -0,0 +1,33 @@ +{ + "files": { + "ext_localconf.php": { + "errors": 0, + "messages": [ + { + "column": 54, + "fixable": false, + "line": 22, + "message": "Defining AJAX using $GLOBALS['TYPO3_CONF_VARS']['BE']['AJAX'][$ajaxID] is no longer supported with a single String like 'something'. Since TYPO3 7.6, use PSR-7-based Routing for Backend AJAX Requests. See: https://docs.typo3.org/typo3cms/extensions/core/Changelog/7.6/Feature-69916-PSR-7-basedRoutingForBackendAJAXRequests.html", + "severity": 5, + "source": "Typo3Update.Deprecated.AjaxRegistration", + "type": "WARNING" + }, + { + "column": 54, + "fixable": false, + "line": 23, + "message": "Defining AJAX using $GLOBALS['TYPO3_CONF_VARS']['BE']['AJAX'][$ajaxID] is no longer supported with a single String like \"something\". Since TYPO3 7.6, use PSR-7-based Routing for Backend AJAX Requests. See: https://docs.typo3.org/typo3cms/extensions/core/Changelog/7.6/Feature-69916-PSR-7-basedRoutingForBackendAJAXRequests.html", + "severity": 5, + "source": "Typo3Update.Deprecated.AjaxRegistration", + "type": "WARNING" + } + ], + "warnings": 2 + } + }, + "totals": { + "errors": 0, + "fixable": 0, + "warnings": 2 + } +} diff --git a/tests/Fixtures/Standards/Typo3Update/Sniffs/Deprecated/AjaxRegistrationSniff/ext_localconf/ext_localconf.php b/tests/Fixtures/Standards/Typo3Update/Sniffs/Deprecated/AjaxRegistrationSniff/ext_localconf/ext_localconf.php new file mode 100644 index 0000000..3e6570a --- /dev/null +++ b/tests/Fixtures/Standards/Typo3Update/Sniffs/Deprecated/AjaxRegistrationSniff/ext_localconf/ext_localconf.php @@ -0,0 +1,23 @@ + + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +$GLOBALS['TYPO3_CONF_VARS']['BE']['AJAX'][$ajaxID] = 'something'; +$GLOBALS["TYPO3_CONF_VARS"]["BE"]["AJAX"][$ajaxID] = "something"; diff --git a/tests/Fixtures/Standards/Typo3Update/Sniffs/Deprecated/AjaxRegistrationSniff/ext_tables/Expected.json b/tests/Fixtures/Standards/Typo3Update/Sniffs/Deprecated/AjaxRegistrationSniff/ext_tables/Expected.json new file mode 100644 index 0000000..2f4fd52 --- /dev/null +++ b/tests/Fixtures/Standards/Typo3Update/Sniffs/Deprecated/AjaxRegistrationSniff/ext_tables/Expected.json @@ -0,0 +1,33 @@ +{ + "files": { + "ext_tables.php": { + "errors": 0, + "messages": [ + { + "column": 54, + "fixable": false, + "line": 22, + "message": "Defining AJAX using $GLOBALS['TYPO3_CONF_VARS']['BE']['AJAX'][$ajaxID] is no longer supported with a single String like 'something'. Since TYPO3 7.6, use PSR-7-based Routing for Backend AJAX Requests. See: https://docs.typo3.org/typo3cms/extensions/core/Changelog/7.6/Feature-69916-PSR-7-basedRoutingForBackendAJAXRequests.html", + "severity": 5, + "source": "Typo3Update.Deprecated.AjaxRegistration", + "type": "WARNING" + }, + { + "column": 54, + "fixable": false, + "line": 23, + "message": "Defining AJAX using $GLOBALS['TYPO3_CONF_VARS']['BE']['AJAX'][$ajaxID] is no longer supported with a single String like \"something\". Since TYPO3 7.6, use PSR-7-based Routing for Backend AJAX Requests. See: https://docs.typo3.org/typo3cms/extensions/core/Changelog/7.6/Feature-69916-PSR-7-basedRoutingForBackendAJAXRequests.html", + "severity": 5, + "source": "Typo3Update.Deprecated.AjaxRegistration", + "type": "WARNING" + } + ], + "warnings": 2 + } + }, + "totals": { + "errors": 0, + "fixable": 0, + "warnings": 2 + } +} diff --git a/tests/Fixtures/Standards/Typo3Update/Sniffs/Deprecated/AjaxRegistrationSniff/ext_tables/ext_tables.php b/tests/Fixtures/Standards/Typo3Update/Sniffs/Deprecated/AjaxRegistrationSniff/ext_tables/ext_tables.php new file mode 100644 index 0000000..3e6570a --- /dev/null +++ b/tests/Fixtures/Standards/Typo3Update/Sniffs/Deprecated/AjaxRegistrationSniff/ext_tables/ext_tables.php @@ -0,0 +1,23 @@ + + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +$GLOBALS['TYPO3_CONF_VARS']['BE']['AJAX'][$ajaxID] = 'something'; +$GLOBALS["TYPO3_CONF_VARS"]["BE"]["AJAX"][$ajaxID] = "something"; diff --git a/tests/SniffsTest.php b/tests/SniffsTest.php index 7136d0d..4a1e4f7 100644 --- a/tests/SniffsTest.php +++ b/tests/SniffsTest.php @@ -51,35 +51,76 @@ class SniffsTest extends TestCase ); foreach ($finder->directories()->name('*Sniff') as $folder) { + if (is_file($this->getArgumentsFile($folder))) { + $arguments = require $this->getArgumentsFile($folder); + $this->executeSniffSubfolders($folder, $arguments); + continue; + } + $this->executeSniff($folder); } } + /** + * Execute sniff using subfolders. + * + * @param SplFileInfo $folder + * @param array $arguments + * @return void + */ + protected function executeSniffSubfolders(SplFileInfo $folder, array $arguments = []) + { + $finder = new Finder(); + $finder->in($folder->getRealPath()); + + foreach ($arguments as $subFolder => $values) { + $folderName = $folder->getRealPath() . DIRECTORY_SEPARATOR . $subFolder; + $this->executeSniff(new SplFileInfo($folderName, $folderName, $folderName), $values); + } + } + /** * Execute phpunit assertion for sniff based on $folder. * * @param SplFileInfo $folder + * @param array $arguments * @return void */ - protected function executeSniff(SplFileInfo $folder) + protected function executeSniff(SplFileInfo $folder, array $arguments = []) { + $internalArguments = array_merge([ + 'runtime-set' => 'mappingFile ' + . __DIR__ . DIRECTORY_SEPARATOR + . 'Fixtures' . DIRECTORY_SEPARATOR + . 'LegacyClassnames.php', + 'report' => 'json', + 'sniffs' => $this->getSniffByFolder($folder), + 'inputFile' => $folder->getRealPath() . DIRECTORY_SEPARATOR . 'InputFileForIssues.php', + ], $arguments); + + if (isset($internalArguments['inputFileName'])) { + $internalArguments['inputFile'] = $folder->getRealPath() . DIRECTORY_SEPARATOR . $internalArguments['inputFileName']; + unset($internalArguments['inputFileName']); + } + $this->assertEquals( $this->getExpectedJsonOutput($folder), - $this->getOutput($folder, 'json')['output'], + $this->getOutput($folder, $internalArguments)['output'], 'Checking Sniff "' . $this->getSniffByFolder($folder) . '"' . ' did not produce expected output for input file ' - . $this->getInputFile($folder) - . ' called: ' . $this->getPhpcsCall($folder, 'json') + . $internalArguments['inputFile'] + . ' called: ' . $this->getPhpcsCall($folder, $internalArguments) ); try { + $internalArguments['report'] = 'diff'; $this->assertEquals( $this->getExpectedDiffOutput($folder), - $this->getOutput($folder, 'diff')['output'], + $this->getOutput($folder, $internalArguments)['output'], 'Fixing Sniff "' . $this->getSniffByFolder($folder) . '"' . ' did not produce expected diff for input file ' - . $this->getInputFile($folder) - . ' called: ' . $this->getPhpcsCall($folder, 'diff') + . $internalArguments['inputFile'] + . ' called: ' . $this->getPhpcsCall($folder, $internalArguments) ); } catch (FileNotFoundException $e) { // Ok, ignore, we don't have an diff. @@ -126,61 +167,83 @@ class SniffsTest extends TestCase */ protected function getSniffByFolder(SplFileInfo $folder) { - $folderParts = explode(DIRECTORY_SEPARATOR, $folder->getPath()); + $folderParts = array_filter(explode(DIRECTORY_SEPARATOR, $folder->getPathName())); + $sniffNamePosition; - return array_slice($folderParts, -3)[0] - . '.' . array_slice($folderParts, -1)[0] - . '.' . substr($folder->getFilename(), 0, -5); + foreach ($folderParts as $index => $folderPart) { + if (strpos($folderPart, 'Sniff', 1) !== false) { + $sniffNamePosition = $index; + break; + } + } + + if ($sniffNamePosition === null) { + throw new \Exception('Could not detect sniff name by folder: ' . var_export($folder, true), 1491485369); + } + + return $folderParts[$sniffNamePosition - 3] + . '.' . $folderParts[$sniffNamePosition - 1] + . '.' . substr($folderParts[$sniffNamePosition], 0, -5); } /** - * Returns file to use as input for phpcs. + * Get absolute file path to file containing further arguments. * * @param SplFileInfo $folder * @return string */ - protected function getInputFile(SplFileInfo $folder) + protected function getArgumentsFile(SplFileInfo $folder) { - return $folder->getRealPath() . DIRECTORY_SEPARATOR . 'InputFileForIssues.php'; + return $folder->getRealPath() . DIRECTORY_SEPARATOR . 'Arguments.php'; } /** * Build cli call for phpcs. * * @param SplFileInfo $folder - * @param string $report Defined the report format to use for output. + * @param array $arguments * @return string */ - protected function getPhpcsCall(SplFileInfo $folder, $report) + protected function getPhpcsCall(SplFileInfo $folder, array $arguments) { $bin = './vendor/bin/phpcs'; - $arguments = '--sniffs=' . $this->getSniffByFolder($folder) - . ' --report=' . $report - . ' --runtime-set mappingFile ' - . __DIR__ . DIRECTORY_SEPARATOR . 'Fixtures' . DIRECTORY_SEPARATOR . 'LegacyClassnames.php' - . ' ' - . $this->getInputFile($folder) - ; + $preparedArguments = []; - return $bin . ' ' . $arguments; + foreach ($arguments as $argumentName => $argumentValue) { + if ($argumentName === 'inputFile') { + continue; + } + + $prefix = "--$argumentName="; + if (in_array($argumentName, ['runtime-set'])) { + $prefix = "--$argumentName "; + } + + $preparedArguments[] = "$prefix$argumentValue"; + } + + return $bin + . ' ' . implode(' ', $preparedArguments) + . ' ' . $arguments['inputFile'] + ; } /** * 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. + * @param array $arguments * @return array */ - protected function getOutput(SplFileInfo $folder, $report) + protected function getOutput(SplFileInfo $folder, array $arguments) { $output = ''; $returnValue; - exec($this->getPhpcsCall($folder, $report), $output, $returnValue); + exec($this->getPhpcsCall($folder, $arguments), $output, $returnValue); - if ($report === 'json') { + if ($arguments['report'] === 'json') { $output = $this->prepareJsonOutput($output); - } if ($report === 'diff') { + } if ($arguments['report'] === 'diff') { $output = $this->prepareDiffOutput($output); } @@ -200,6 +263,10 @@ class SniffsTest extends TestCase { $preparedOutput = json_decode($output[0], true); + if ($preparedOutput === null) { + throw new \Exception('Output for phpcs was not valid json: ' . var_export($output, true), 1491485173); + } + foreach (array_keys($preparedOutput['files']) as $fileName) { $newKey = basename($fileName); $preparedOutput['files'][$newKey] = $preparedOutput['files'][$fileName];