From 8b90cf3019d7d553ac6309186afbbbc7e3751e2f Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Thu, 1 Jun 2017 11:26:52 +0200 Subject: [PATCH] WIP|TASK: Migrate to unit testing * Use a single test as example to migrate. --- composer.json | 3 +- tests/AbstractSniffUnitTest.php | 113 ++++++++++++++++++ tests/Feature/LegacyClassnameMappingTest.php | 2 +- ...Expected.diff => InheritanceExpected.diff} | 0 ...Expected.json => InheritanceExpected.json} | 0 ...eForIssues.php => InheritanceUnitTest.inc} | 0 .../Sniffs/Classname/InheritanceSniffTest.php | 6 +- 7 files changed, 119 insertions(+), 5 deletions(-) create mode 100644 tests/AbstractSniffUnitTest.php rename tests/Fixtures/Standards/Typo3Update/Sniffs/Classname/{InheritanceSniff/Expected.diff => InheritanceExpected.diff} (100%) rename tests/Fixtures/Standards/Typo3Update/Sniffs/Classname/{InheritanceSniff/Expected.json => InheritanceExpected.json} (100%) rename tests/Fixtures/Standards/Typo3Update/Sniffs/Classname/{InheritanceSniff/InputFileForIssues.php => InheritanceUnitTest.inc} (100%) diff --git a/composer.json b/composer.json index f708bf3..f6424ba 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,8 @@ "autoload-dev": { "psr-4": { "Typo3Update\\Tests\\": "tests/", - "PHP_CodeSniffer\\": "vendor/squizlabs/php_codesniffer/src/" + "PHP_CodeSniffer\\": "vendor/squizlabs/php_codesniffer/src/", + "PHP_CodeSniffer\\Tests\\": "vendor/squizlabs/php_codesniffer/tests/" } }, "autoload": { diff --git a/tests/AbstractSniffUnitTest.php b/tests/AbstractSniffUnitTest.php new file mode 100644 index 0000000..ac6b21d --- /dev/null +++ b/tests/AbstractSniffUnitTest.php @@ -0,0 +1,113 @@ + + * + * 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. + */ + +use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest as PhpCsAbstractSniffUnitTest; +use Symfony\Component\Finder\Finder; + +use PHP_CodeSniffer\Tests\Standards\AllSniffs; + +abstract class AbstractSniffUnitTest extends PhpCsAbstractSniffUnitTest +{ + protected function setUp() + { + // Trigger include for bootstrapping. + // TODO: Move to bootstrap file. + class_exists(AllSniffs::class); + $GLOBALS['PHP_CODESNIFFER_SNIFF_CODES'] = []; + $GLOBALS['PHP_CODESNIFFER_FIXABLE_CODES'] = []; + + $class = get_class($this); + $GLOBALS['PHP_CODESNIFFER_STANDARD_DIRS'][$class] = implode( + DIRECTORY_SEPARATOR, + [__DIR__, '..', 'src', 'Standards', 'Typo3Update', 'Sniffs', ''] + ); + $GLOBALS['PHP_CODESNIFFER_TEST_DIRS'][$class] = $this->getFixturePath(); + + parent::setUp(); + } + + /** + * Returns the lines where errors should occur. + * + * The key of the array should represent the line number and the value + * should represent the number of errors that should occur on that line. + * + * @return array + */ + protected function getErrorList() + { + return $this->getExpectdOutput()['ERROR']; + } + + /** + * Returns the lines where warnings should occur. + * + * The key of the array should represent the line number and the value + * should represent the number of warnings that should occur on that line. + * + * @return array(int => int) + */ + protected function getWarningList() + { + return $this->getExpectdOutput()['WARNING']; + } + + protected function getFixturePath() + { + return implode( + DIRECTORY_SEPARATOR, + [__DIR__, 'Fixtures', 'Standards', 'Typo3Update', 'Sniffs', ''] + ); + } + + protected function getExpectdOutput() + { + $list = [ + 'WARNING' => [], + 'ERROR' => [], + ]; + + $file = $this->getFixturePath() . implode( + DIRECTORY_SEPARATOR, + array_slice(explode('\\', str_replace('Sniff', '', get_class($this))), 2) + ) . 'Expected.json'; + + if (!is_file($file)) { + throw new \Exception('Could not load file: ' . $file, 1491486050); + } + + $content = json_decode(file_get_contents($file), true); + + foreach ($content['files']['InputFileForIssues.php']['messages'] as $message) { + $type = $message['type']; + + if (!isset($list[$type][$message['line']])) { + $list[$type][$message['line']] = 1; + continue; + } + + ++$list[$type][$message['line']]; + } + + return $list; + } +} diff --git a/tests/Feature/LegacyClassnameMappingTest.php b/tests/Feature/LegacyClassnameMappingTest.php index 7a169d9..acda663 100644 --- a/tests/Feature/LegacyClassnameMappingTest.php +++ b/tests/Feature/LegacyClassnameMappingTest.php @@ -54,7 +54,7 @@ class LegacyClassnameMappingTest extends TestCase $this->fileSystem = vfsStream::setup('root', null, [ 'LegacyClassnames.php' => file_get_contents($this->getFixturePath('MappingContent.php')), ]); - Config::setConfigData('mappingFile', vfsStream::url('root/LegacyClassnames.php')); + // Config::setConfigData('mappingFile', vfsStream::url('root/LegacyClassnames.php')); $this->subject = LegacyClassnameMapping::getInstance(); } diff --git a/tests/Fixtures/Standards/Typo3Update/Sniffs/Classname/InheritanceSniff/Expected.diff b/tests/Fixtures/Standards/Typo3Update/Sniffs/Classname/InheritanceExpected.diff similarity index 100% rename from tests/Fixtures/Standards/Typo3Update/Sniffs/Classname/InheritanceSniff/Expected.diff rename to tests/Fixtures/Standards/Typo3Update/Sniffs/Classname/InheritanceExpected.diff diff --git a/tests/Fixtures/Standards/Typo3Update/Sniffs/Classname/InheritanceSniff/Expected.json b/tests/Fixtures/Standards/Typo3Update/Sniffs/Classname/InheritanceExpected.json similarity index 100% rename from tests/Fixtures/Standards/Typo3Update/Sniffs/Classname/InheritanceSniff/Expected.json rename to tests/Fixtures/Standards/Typo3Update/Sniffs/Classname/InheritanceExpected.json diff --git a/tests/Fixtures/Standards/Typo3Update/Sniffs/Classname/InheritanceSniff/InputFileForIssues.php b/tests/Fixtures/Standards/Typo3Update/Sniffs/Classname/InheritanceUnitTest.inc similarity index 100% rename from tests/Fixtures/Standards/Typo3Update/Sniffs/Classname/InheritanceSniff/InputFileForIssues.php rename to tests/Fixtures/Standards/Typo3Update/Sniffs/Classname/InheritanceUnitTest.inc diff --git a/tests/Sniffs/Classname/InheritanceSniffTest.php b/tests/Sniffs/Classname/InheritanceSniffTest.php index 56c8bcb..dc4d941 100644 --- a/tests/Sniffs/Classname/InheritanceSniffTest.php +++ b/tests/Sniffs/Classname/InheritanceSniffTest.php @@ -1,6 +1,6 @@ @@ -21,8 +21,8 @@ namespace Typo3Update\Tests\Sniffs\Classname; * 02110-1301, USA. */ -use Typo3Update\Tests\SniffsTest; +use Typo3Update\Tests\AbstractSniffUnitTest; -class InheritanceSniffTest extends SniffsTest +class InheritanceSniff extends AbstractSniffUnitTest { }