Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core tests: set up abstract class and remove code duplication #2295

Merged
6 changes: 6 additions & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
<file baseinstalldir="" name="AcceptTest.php" role="test" />
</dir>
</dir>
<file baseinstalldir="" name="AbstractMethodUnitTest.php" role="test" />
<file baseinstalldir="" name="AllTests.php" role="test" />
<file baseinstalldir="" name="ErrorSuppressionTest.php" role="test" />
<file baseinstalldir="" name="IsCamelCapsTest.php" role="test" />
Expand All @@ -216,6 +217,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
</dir>
<file baseinstalldir="" name="AllTests.php" role="test" />
<file baseinstalldir="" name="bootstrap.php" role="test" />
<file baseinstalldir="" name="FileList.php" role="test" />
<file baseinstalldir="" name="TestSuite.php" role="test" />
<file baseinstalldir="" name="TestSuite7.php" role="test" />
</dir>
Expand Down Expand Up @@ -1956,9 +1958,11 @@ http://pear.php.net/dtd/package-2.0.xsd">
<install as="LICENCE" name="licence.txt" />
<install as="phpunit.xml" name="phpunit.xml.dist" />
<install as="AllTests.php" name="tests/AllTests.php" />
<install as="FileList.php" name="tests/FileList.php" />
<install as="TestSuite.php" name="tests/TestSuite.php" />
<install as="TestSuite7.php" name="tests/TestSuite7.php" />
<install as="tests/bootstrap.php" name="tests/bootstrap.php" />
<install as="CodeSniffer/Core/AbstractMethodUnitTest.php" name="tests/Core/AbstractMethodUnitTest.php" />
<install as="CodeSniffer/Core/AllTests.php" name="tests/Core/AllTests.php" />
<install as="CodeSniffer/Core/IsCamelCapsTest.php" name="tests/Core/IsCamelCapsTest.php" />
<install as="CodeSniffer/Core/ErrorSuppressionTest.php" name="tests/Core/ErrorSuppressionTest.php" />
Expand Down Expand Up @@ -1992,8 +1996,10 @@ http://pear.php.net/dtd/package-2.0.xsd">
<install as="phpunit.xml" name="phpunit.xml.dist" />
<install as="tests/AllTests.php" name="tests/AllTests.php" />
<install as="tests/bootstrap.php" name="tests/bootstrap.php" />
<install as="tests/FileList.php" name="tests/FileList.php" />
<install as="tests/TestSuite.php" name="tests/TestSuite.php" />
<install as="tests/TestSuite7.php" name="tests/TestSuite7.php" />
<install as="CodeSniffer/Core/AbstractMethodUnitTest.php" name="tests/Core/AbstractMethodUnitTest.php" />
<install as="CodeSniffer/Core/AllTests.php" name="tests/Core/AllTests.php" />
<install as="CodeSniffer/Core/IsCamelCapsTest.php" name="tests/Core/IsCamelCapsTest.php" />
<install as="CodeSniffer/Core/ErrorSuppressionTest.php" name="tests/Core/ErrorSuppressionTest.php" />
Expand Down
2 changes: 2 additions & 0 deletions scripts/ValidatePEAR/ValidatePEARPackageXML.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/

use PHP_CodeSniffer\Tests\FileList;

/**
* Validate the PHP_CodeSniffer PEAR package.xml file.
*/
Expand Down
2 changes: 1 addition & 1 deletion scripts/validate-pear-package.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/

require_once __DIR__.'/ValidatePEAR/FileList.php';
require_once dirname(__DIR__).'/tests/FileList.php';
require_once __DIR__.'/ValidatePEAR/ValidatePEARPackageXML.php';

$validate = new ValidatePEARPackageXML();
Expand Down
140 changes: 140 additions & 0 deletions tests/Core/AbstractMethodUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<?php
/**
* Base class to use when testing utility methods.
*
* @author Juliette Reinders Folmer <[email protected]>
* @copyright 2018-2019 Juliette Reinders Folmer. All rights reserved.
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/

namespace PHP_CodeSniffer\Tests\Core;

use PHP_CodeSniffer\Config;
use PHP_CodeSniffer\Ruleset;
use PHP_CodeSniffer\Files\DummyFile;
use PHPUnit\Framework\TestCase;

abstract class AbstractMethodUnitTest extends TestCase
{

/**
* The file extension of the test case file (without leading dot).
*
* This allows child classes to overrule the default `inc` with, for instance,
* `js` or `css` when applicable.
*
* @var string
*/
protected static $fileExtension = 'inc';

/**
* The \PHP_CodeSniffer\Files\File object containing the parsed contents of the test case file.
*
* @var \PHP_CodeSniffer\Files\File
*/
protected static $phpcsFile;


/**
* Initialize & tokenize \PHP_CodeSniffer\Files\File with code from the test case file.
*
* The test case file for a unit test class has to be in the same directory
* directory and use the same file name as the test class, using the .inc extension.
*
* @return void
*/
public static function setUpBeforeClass()
{
$config = new Config();
$config->standards = ['PSR1'];

$ruleset = new Ruleset($config);

// Default to a file with the same name as the test class. Extension is property based.
$relativeCN = str_replace(__NAMESPACE__, '', get_called_class());
$relativePath = str_replace('\\', DIRECTORY_SEPARATOR, $relativeCN);
$pathToTestFile = realpath(__DIR__).$relativePath.'.'.static::$fileExtension;

// Make sure the file gets parsed correctly based on the file type.
$contents = 'phpcs_input_file: '.$pathToTestFile.PHP_EOL;
$contents .= file_get_contents($pathToTestFile);

self::$phpcsFile = new DummyFile($contents, $ruleset, $config);
self::$phpcsFile->process();

}//end setUpBeforeClass()


/**
* Clean up after finished test.
*
* @return void
*/
public static function tearDownAfterClass()
{
self::$phpcsFile = null;

}//end tearDownAfterClass()


/**
* Get the token pointer for a target token based on a specific comment found on the line before.
*
* Note: the test delimiter comment MUST start with "/* test" to allow this function to
* distinguish between comments used *in* a test and test delimiters.
*
* @param string $commentString The delimiter comment to look for.
* @param int|string|array $tokenType The type of token(s) to look for.
* @param string $tokenContent Optional. The token content for the target token.
*
* @return int
*/
public function getTargetToken($commentString, $tokenType, $tokenContent=null)
{
$start = (self::$phpcsFile->numTokens - 1);
$comment = self::$phpcsFile->findPrevious(
T_COMMENT,
$start,
null,
false,
$commentString
);

$tokens = self::$phpcsFile->getTokens();
$end = ($start + 1);

// Limit the token finding to between this and the next delimiter comment.
for ($i = ($comment + 1); $i < $end; $i++) {
if ($tokens[$i]['code'] !== T_COMMENT) {
continue;
}

if (stripos($tokens[$i]['content'], '/* test') === 0) {
$end = $i;
break;
}
}

$target = self::$phpcsFile->findNext(
$tokenType,
($comment + 1),
$end,
false,
$tokenContent
);

if ($target === false) {
$msg = 'Failed to find test target token for comment string: '.$commentString;
if ($tokenContent !== null) {
$msg .= ' With token content: '.$tokenContent;
}

$this->assertFalse(true, $msg);
}

return $target;

}//end getTargetToken()


}//end class
42 changes: 20 additions & 22 deletions tests/Core/AllTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,17 @@
* A test class for testing the core.
*
* @author Greg Sherwood <[email protected]>
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
* @author Juliette Reinders Folmer <[email protected]>
* @copyright 2006-2019 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/

namespace PHP_CodeSniffer\Tests\Core;

use PHP_CodeSniffer\Tests\FileList;
use PHPUnit\TextUI\TestRunner;
use PHPUnit\Framework\TestSuite;

require_once 'IsCamelCapsTest.php';
require_once 'ErrorSuppressionTest.php';
require_once 'File/FindEndOfStatementTest.php';
require_once 'File/FindExtendedClassNameTest.php';
require_once 'File/FindImplementedInterfaceNamesTest.php';
require_once 'File/GetMemberPropertiesTest.php';
require_once 'File/GetMethodParametersTest.php';
require_once 'File/GetMethodPropertiesTest.php';
require_once 'File/IsReferenceTest.php';
require_once 'Filters/Filter/AcceptTest.php';

class AllTests
{

Expand All @@ -47,16 +38,23 @@ public static function main()
public static function suite()
{
$suite = new TestSuite('PHP CodeSniffer Core');
$suite->addTestSuite('PHP_CodeSniffer\Tests\Core\IsCamelCapsTest');
$suite->addTestSuite('PHP_CodeSniffer\Tests\Core\ErrorSuppressionTest');
$suite->addTestSuite('PHP_CodeSniffer\Tests\Core\File\FindEndOfStatementTest');
$suite->addTestSuite('PHP_CodeSniffer\Tests\Core\File\FindExtendedClassNameTest');
$suite->addTestSuite('PHP_CodeSniffer\Tests\Core\File\FindImplementedInterfaceNamesTest');
$suite->addTestSuite('PHP_CodeSniffer\Tests\Core\File\GetMemberPropertiesTest');
$suite->addTestSuite('PHP_CodeSniffer\Tests\Core\File\GetMethodParametersTest');
$suite->addTestSuite('PHP_CodeSniffer\Tests\Core\File\GetMethodPropertiesTest');
$suite->addTestSuite('PHP_CodeSniffer\Tests\Core\File\IsReferenceTest');
$suite->addTestSuite('PHP_CodeSniffer\Tests\Core\Filters\Filter\AcceptTest');

$testFileIterator = new FileList(__DIR__, '', '`Test\.php$`Di');
foreach ($testFileIterator->fileIterator as $file) {
if (strpos($file, 'AbstractMethodUnitTest.php') !== false) {
continue;
}

include_once $file;

$class = str_replace(__DIR__, '', $file);
$class = str_replace('.php', '', $class);
$class = str_replace('/', '\\', $class);
$class = 'PHP_CodeSniffer\Tests\Core'.$class;

$suite->addTestSuite($class);
}

return $suite;

}//end suite()
Expand Down
Loading