Skip to content

Commit a9d2351

Browse files
committed
New Tests\Core\AbstractMethodUnitTest class
Refactor to remove duplicate code. This new class abstracts out the `setUp()` and `tearDown()` methods which were included in nearly all the utility method test classes. The methods have been changed to static `setUpBeforeClass()` and `tearDownAfterClass()` with a static property as the initial processed `File` doesn't change between tests, so there is no need to tokenize and process the unit test case file for each test individually. This makes the utility method unit tests significantly faster as well as lower the memory usage. Additionally, the _standard_ against which the files are processed has been changed from `Generic` (75 sniffs) to `PSR1` (7 sniffs), which again makes the unit test suite for the Core utility functions significantly faster. The current implementation allows for only one test case file per utility method test file, but as the utility method tests are not sniff specific, adding additional tests files for a method if needed, should not be a problem. By default test case files are expected to be PHP `inc` files. Child classes can overload the static `$fileExtension` property when the test case file is a JS or CSS file.
1 parent 988c945 commit a9d2351

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

package.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
206206
<file baseinstalldir="" name="AcceptTest.php" role="test" />
207207
</dir>
208208
</dir>
209+
<file baseinstalldir="" name="AbstractMethodUnitTest.php" role="test" />
209210
<file baseinstalldir="" name="AllTests.php" role="test" />
210211
<file baseinstalldir="" name="ErrorSuppressionTest.php" role="test" />
211212
<file baseinstalldir="" name="IsCamelCapsTest.php" role="test" />
@@ -1959,6 +1960,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
19591960
<install as="TestSuite.php" name="tests/TestSuite.php" />
19601961
<install as="TestSuite7.php" name="tests/TestSuite7.php" />
19611962
<install as="tests/bootstrap.php" name="tests/bootstrap.php" />
1963+
<install as="CodeSniffer/Core/AbstractMethodUnitTest.php" name="tests/Core/AbstractMethodUnitTest.php" />
19621964
<install as="CodeSniffer/Core/AllTests.php" name="tests/Core/AllTests.php" />
19631965
<install as="CodeSniffer/Core/IsCamelCapsTest.php" name="tests/Core/IsCamelCapsTest.php" />
19641966
<install as="CodeSniffer/Core/ErrorSuppressionTest.php" name="tests/Core/ErrorSuppressionTest.php" />
@@ -1994,6 +1996,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
19941996
<install as="tests/bootstrap.php" name="tests/bootstrap.php" />
19951997
<install as="tests/TestSuite.php" name="tests/TestSuite.php" />
19961998
<install as="tests/TestSuite7.php" name="tests/TestSuite7.php" />
1999+
<install as="CodeSniffer/Core/AbstractMethodUnitTest.php" name="tests/Core/AbstractMethodUnitTest.php" />
19972000
<install as="CodeSniffer/Core/AllTests.php" name="tests/Core/AllTests.php" />
19982001
<install as="CodeSniffer/Core/IsCamelCapsTest.php" name="tests/Core/IsCamelCapsTest.php" />
19992002
<install as="CodeSniffer/Core/ErrorSuppressionTest.php" name="tests/Core/ErrorSuppressionTest.php" />

tests/Core/AbstractMethodUnitTest.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
}//end class

0 commit comments

Comments
 (0)