Skip to content

Commit aafc304

Browse files
committed
Merge branch 'feature/core-tests-remove-code-duplication' of https://github.com/jrfnl/PHP_CodeSniffer
2 parents 14fadc2 + 3bb2ffa commit aafc304

16 files changed

+384
-903
lines changed

package.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
209209
<file baseinstalldir="" name="AcceptTest.php" role="test" />
210210
</dir>
211211
</dir>
212+
<file baseinstalldir="" name="AbstractMethodUnitTest.php" role="test" />
212213
<file baseinstalldir="" name="AllTests.php" role="test" />
213214
<file baseinstalldir="" name="ErrorSuppressionTest.php" role="test" />
214215
<file baseinstalldir="" name="IsCamelCapsTest.php" role="test" />
@@ -219,6 +220,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
219220
</dir>
220221
<file baseinstalldir="" name="AllTests.php" role="test" />
221222
<file baseinstalldir="" name="bootstrap.php" role="test" />
223+
<file baseinstalldir="" name="FileList.php" role="test" />
222224
<file baseinstalldir="" name="TestSuite.php" role="test" />
223225
<file baseinstalldir="" name="TestSuite7.php" role="test" />
224226
</dir>
@@ -1962,9 +1964,11 @@ http://pear.php.net/dtd/package-2.0.xsd">
19621964
<install as="LICENCE" name="licence.txt" />
19631965
<install as="phpunit.xml" name="phpunit.xml.dist" />
19641966
<install as="AllTests.php" name="tests/AllTests.php" />
1967+
<install as="FileList.php" name="tests/FileList.php" />
19651968
<install as="TestSuite.php" name="tests/TestSuite.php" />
19661969
<install as="TestSuite7.php" name="tests/TestSuite7.php" />
19671970
<install as="tests/bootstrap.php" name="tests/bootstrap.php" />
1971+
<install as="CodeSniffer/Core/AbstractMethodUnitTest.php" name="tests/Core/AbstractMethodUnitTest.php" />
19681972
<install as="CodeSniffer/Core/AllTests.php" name="tests/Core/AllTests.php" />
19691973
<install as="CodeSniffer/Core/IsCamelCapsTest.php" name="tests/Core/IsCamelCapsTest.php" />
19701974
<install as="CodeSniffer/Core/ErrorSuppressionTest.php" name="tests/Core/ErrorSuppressionTest.php" />
@@ -1998,8 +2002,10 @@ http://pear.php.net/dtd/package-2.0.xsd">
19982002
<install as="phpunit.xml" name="phpunit.xml.dist" />
19992003
<install as="tests/AllTests.php" name="tests/AllTests.php" />
20002004
<install as="tests/bootstrap.php" name="tests/bootstrap.php" />
2005+
<install as="tests/FileList.php" name="tests/FileList.php" />
20012006
<install as="tests/TestSuite.php" name="tests/TestSuite.php" />
20022007
<install as="tests/TestSuite7.php" name="tests/TestSuite7.php" />
2008+
<install as="CodeSniffer/Core/AbstractMethodUnitTest.php" name="tests/Core/AbstractMethodUnitTest.php" />
20032009
<install as="CodeSniffer/Core/AllTests.php" name="tests/Core/AllTests.php" />
20042010
<install as="CodeSniffer/Core/IsCamelCapsTest.php" name="tests/Core/IsCamelCapsTest.php" />
20052011
<install as="CodeSniffer/Core/ErrorSuppressionTest.php" name="tests/Core/ErrorSuppressionTest.php" />

scripts/ValidatePEAR/ValidatePEARPackageXML.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
1212
*/
1313

14+
use PHP_CodeSniffer\Tests\FileList;
15+
1416
/**
1517
* Validate the PHP_CodeSniffer PEAR package.xml file.
1618
*/

scripts/validate-pear-package.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
1313
*/
1414

15-
require_once __DIR__.'/ValidatePEAR/FileList.php';
15+
require_once dirname(__DIR__).'/tests/FileList.php';
1616
require_once __DIR__.'/ValidatePEAR/ValidatePEARPackageXML.php';
1717

1818
$validate = new ValidatePEARPackageXML();

tests/Core/AbstractMethodUnitTest.php

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
<?php
2+
/**
3+
* Base class to use when testing utility methods.
4+
*
5+
* @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
6+
* @copyright 2018-2019 Juliette Reinders Folmer. All rights reserved.
7+
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8+
*/
9+
10+
namespace PHP_CodeSniffer\Tests\Core;
11+
12+
use PHP_CodeSniffer\Config;
13+
use PHP_CodeSniffer\Ruleset;
14+
use PHP_CodeSniffer\Files\DummyFile;
15+
use PHPUnit\Framework\TestCase;
16+
17+
abstract class AbstractMethodUnitTest extends TestCase
18+
{
19+
20+
/**
21+
* The file extension of the test case file (without leading dot).
22+
*
23+
* This allows child classes to overrule the default `inc` with, for instance,
24+
* `js` or `css` when applicable.
25+
*
26+
* @var string
27+
*/
28+
protected static $fileExtension = 'inc';
29+
30+
/**
31+
* The \PHP_CodeSniffer\Files\File object containing the parsed contents of the test case file.
32+
*
33+
* @var \PHP_CodeSniffer\Files\File
34+
*/
35+
protected static $phpcsFile;
36+
37+
38+
/**
39+
* Initialize & tokenize \PHP_CodeSniffer\Files\File with code from the test case file.
40+
*
41+
* The test case file for a unit test class has to be in the same directory
42+
* directory and use the same file name as the test class, using the .inc extension.
43+
*
44+
* @return void
45+
*/
46+
public static function setUpBeforeClass()
47+
{
48+
$config = new Config();
49+
$config->standards = ['PSR1'];
50+
51+
$ruleset = new Ruleset($config);
52+
53+
// Default to a file with the same name as the test class. Extension is property based.
54+
$relativeCN = str_replace(__NAMESPACE__, '', get_called_class());
55+
$relativePath = str_replace('\\', DIRECTORY_SEPARATOR, $relativeCN);
56+
$pathToTestFile = realpath(__DIR__).$relativePath.'.'.static::$fileExtension;
57+
58+
// Make sure the file gets parsed correctly based on the file type.
59+
$contents = 'phpcs_input_file: '.$pathToTestFile.PHP_EOL;
60+
$contents .= file_get_contents($pathToTestFile);
61+
62+
self::$phpcsFile = new DummyFile($contents, $ruleset, $config);
63+
self::$phpcsFile->process();
64+
65+
}//end setUpBeforeClass()
66+
67+
68+
/**
69+
* Clean up after finished test.
70+
*
71+
* @return void
72+
*/
73+
public static function tearDownAfterClass()
74+
{
75+
self::$phpcsFile = null;
76+
77+
}//end tearDownAfterClass()
78+
79+
80+
/**
81+
* Get the token pointer for a target token based on a specific comment found on the line before.
82+
*
83+
* Note: the test delimiter comment MUST start with "/* test" to allow this function to
84+
* distinguish between comments used *in* a test and test delimiters.
85+
*
86+
* @param string $commentString The delimiter comment to look for.
87+
* @param int|string|array $tokenType The type of token(s) to look for.
88+
* @param string $tokenContent Optional. The token content for the target token.
89+
*
90+
* @return int
91+
*/
92+
public function getTargetToken($commentString, $tokenType, $tokenContent=null)
93+
{
94+
$start = (self::$phpcsFile->numTokens - 1);
95+
$comment = self::$phpcsFile->findPrevious(
96+
T_COMMENT,
97+
$start,
98+
null,
99+
false,
100+
$commentString
101+
);
102+
103+
$tokens = self::$phpcsFile->getTokens();
104+
$end = ($start + 1);
105+
106+
// Limit the token finding to between this and the next delimiter comment.
107+
for ($i = ($comment + 1); $i < $end; $i++) {
108+
if ($tokens[$i]['code'] !== T_COMMENT) {
109+
continue;
110+
}
111+
112+
if (stripos($tokens[$i]['content'], '/* test') === 0) {
113+
$end = $i;
114+
break;
115+
}
116+
}
117+
118+
$target = self::$phpcsFile->findNext(
119+
$tokenType,
120+
($comment + 1),
121+
$end,
122+
false,
123+
$tokenContent
124+
);
125+
126+
if ($target === false) {
127+
$msg = 'Failed to find test target token for comment string: '.$commentString;
128+
if ($tokenContent !== null) {
129+
$msg .= ' With token content: '.$tokenContent;
130+
}
131+
132+
$this->assertFalse(true, $msg);
133+
}
134+
135+
return $target;
136+
137+
}//end getTargetToken()
138+
139+
140+
}//end class

tests/Core/AllTests.php

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,17 @@
33
* A test class for testing the core.
44
*
55
* @author Greg Sherwood <gsherwood@squiz.net>
6-
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
6+
* @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
7+
* @copyright 2006-2019 Squiz Pty Ltd (ABN 77 084 670 600)
78
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
89
*/
910

1011
namespace PHP_CodeSniffer\Tests\Core;
1112

13+
use PHP_CodeSniffer\Tests\FileList;
1214
use PHPUnit\TextUI\TestRunner;
1315
use PHPUnit\Framework\TestSuite;
1416

15-
require_once 'IsCamelCapsTest.php';
16-
require_once 'ErrorSuppressionTest.php';
17-
require_once 'File/FindEndOfStatementTest.php';
18-
require_once 'File/FindExtendedClassNameTest.php';
19-
require_once 'File/FindImplementedInterfaceNamesTest.php';
20-
require_once 'File/GetMemberPropertiesTest.php';
21-
require_once 'File/GetMethodParametersTest.php';
22-
require_once 'File/GetMethodPropertiesTest.php';
23-
require_once 'File/IsReferenceTest.php';
24-
require_once 'Filters/Filter/AcceptTest.php';
25-
2617
class AllTests
2718
{
2819

@@ -47,16 +38,23 @@ public static function main()
4738
public static function suite()
4839
{
4940
$suite = new TestSuite('PHP CodeSniffer Core');
50-
$suite->addTestSuite('PHP_CodeSniffer\Tests\Core\IsCamelCapsTest');
51-
$suite->addTestSuite('PHP_CodeSniffer\Tests\Core\ErrorSuppressionTest');
52-
$suite->addTestSuite('PHP_CodeSniffer\Tests\Core\File\FindEndOfStatementTest');
53-
$suite->addTestSuite('PHP_CodeSniffer\Tests\Core\File\FindExtendedClassNameTest');
54-
$suite->addTestSuite('PHP_CodeSniffer\Tests\Core\File\FindImplementedInterfaceNamesTest');
55-
$suite->addTestSuite('PHP_CodeSniffer\Tests\Core\File\GetMemberPropertiesTest');
56-
$suite->addTestSuite('PHP_CodeSniffer\Tests\Core\File\GetMethodParametersTest');
57-
$suite->addTestSuite('PHP_CodeSniffer\Tests\Core\File\GetMethodPropertiesTest');
58-
$suite->addTestSuite('PHP_CodeSniffer\Tests\Core\File\IsReferenceTest');
59-
$suite->addTestSuite('PHP_CodeSniffer\Tests\Core\Filters\Filter\AcceptTest');
41+
42+
$testFileIterator = new FileList(__DIR__, '', '`Test\.php$`Di');
43+
foreach ($testFileIterator->fileIterator as $file) {
44+
if (strpos($file, 'AbstractMethodUnitTest.php') !== false) {
45+
continue;
46+
}
47+
48+
include_once $file;
49+
50+
$class = str_replace(__DIR__, '', $file);
51+
$class = str_replace('.php', '', $class);
52+
$class = str_replace('/', '\\', $class);
53+
$class = 'PHP_CodeSniffer\Tests\Core'.$class;
54+
55+
$suite->addTestSuite($class);
56+
}
57+
6058
return $suite;
6159

6260
}//end suite()

0 commit comments

Comments
 (0)