WIP|TASK: Migrate to unit testing

* Use a single test as example to migrate.
This commit is contained in:
Daniel Siepmann 2017-06-01 11:26:52 +02:00
parent 0427911c4d
commit 8b90cf3019
Signed by: Daniel Siepmann
GPG key ID: 33D6629915560EF4
7 changed files with 119 additions and 5 deletions

View file

@ -12,7 +12,8 @@
"autoload-dev": { "autoload-dev": {
"psr-4": { "psr-4": {
"Typo3Update\\Tests\\": "tests/", "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": { "autoload": {

View file

@ -0,0 +1,113 @@
<?php
namespace Typo3Update\Tests;
/*
* Copyright (C) 2017 Daniel Siepmann <coding@daniel-siepmann.de>
*
* 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<int, int>
*/
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;
}
}

View file

@ -54,7 +54,7 @@ class LegacyClassnameMappingTest extends TestCase
$this->fileSystem = vfsStream::setup('root', null, [ $this->fileSystem = vfsStream::setup('root', null, [
'LegacyClassnames.php' => file_get_contents($this->getFixturePath('MappingContent.php')), '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(); $this->subject = LegacyClassnameMapping::getInstance();
} }

View file

@ -1,6 +1,6 @@
<?php <?php
namespace Typo3Update\Tests\Sniffs\Classname; namespace Typo3Update\Tests\Classname;
/* /*
* Copyright (C) 2017 Daniel Siepmann <coding@daniel-siepmann.de> * Copyright (C) 2017 Daniel Siepmann <coding@daniel-siepmann.de>
@ -21,8 +21,8 @@ namespace Typo3Update\Tests\Sniffs\Classname;
* 02110-1301, USA. * 02110-1301, USA.
*/ */
use Typo3Update\Tests\SniffsTest; use Typo3Update\Tests\AbstractSniffUnitTest;
class InheritanceSniffTest extends SniffsTest class InheritanceSniff extends AbstractSniffUnitTest
{ {
} }