Skip to content

Commit

Permalink
New Tests\Core\AbstractMethodUnitTest class
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jrfnl committed Aug 27, 2019
1 parent 988c945 commit a9d2351
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
3 changes: 3 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 Down Expand Up @@ -1959,6 +1960,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
<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 @@ -1994,6 +1996,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
<install as="tests/bootstrap.php" name="tests/bootstrap.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
80 changes: 80 additions & 0 deletions tests/Core/AbstractMethodUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php
/**
* Base class to use when testing utility methods.
*
* @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
* @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()


}//end class

0 comments on commit a9d2351

Please sign in to comment.