Skip to content

Commit 921285e

Browse files
committed
Tests: new AbstractTokenizerTestCase class
Until now, code coverage was not being recorded for the Tokenizer tests due to the way the tests were set up. This new `AbstractTokenizerTestCase` class is intended to fix this. **Background info:** * Code run in a `setUpBeforeClass()` method does not cause code coverage to be recorded. * Code run in a `setUp()` method _does_ cause code coverage to be recorded. Now, the `AbstractMethodUnitTest` test case class was originally set up to allow for testing the utility methods in the `File` class. To do so in the most efficient way and to allow the tests to be fast, the file containing the code being used in the tests, is only tokenized once in a `setUpBeforeClass()` method. Over time, when Tokenizer tests were being introduced, the `AbstractMethodUnitTest` class also started to be used by these, which was fine as code coverage wasn't being measured or recorded anyway. However, this last part has been changed recently via PR 144, so now it is time to ensure that the Tokenizer related tests record code coverage too. This new test case class allows for that by moving the tokenization of the code used in the tests from `setUpBeforeClass()` to a `setUp()` method. As the tokenized file won't change during the test run, this is still only done once, but as that "once" is now in the `setUp()` method, it should allow for code coverage to be recorded. Note: while code coverage can now be measured and recorded for Tokenizer related tests, it will still not be very precise as the `Tokenizer\PHP` class contains "monster methods", which means that the `@covers` tags in the Tokenizer related tests cannot be set up to target only a small, specific part of that class, as targeted by the test. But that's a problem for another time. Loosely related to 146
1 parent b4dfd6a commit 921285e

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
/**
3+
* Base class to use when testing parts of the tokenizer.
4+
*
5+
* This is a near duplicate of the AbstractMethodUnitTest class, with the
6+
* difference being that it allows for recording code coverage for tokenizer tests.
7+
*
8+
* @author Juliette Reinders Folmer <[email protected]>
9+
* @copyright 2018-2019 Juliette Reinders Folmer. All rights reserved.
10+
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
11+
*/
12+
13+
namespace PHP_CodeSniffer\Tests\Core\Tokenizer;
14+
15+
use PHP_CodeSniffer\Ruleset;
16+
use PHP_CodeSniffer\Files\DummyFile;
17+
use PHP_CodeSniffer\Tests\ConfigDouble;
18+
use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest;
19+
use PHPUnit\Framework\TestCase;
20+
21+
abstract class AbstractTokenizerTestCase extends TestCase
22+
{
23+
24+
/**
25+
* The file extension of the test case file (without leading dot).
26+
*
27+
* This allows child classes to overrule the default `inc` with, for instance,
28+
* `js` or `css` when applicable.
29+
*
30+
* @var string
31+
*/
32+
protected $fileExtension = 'inc';
33+
34+
/**
35+
* The tab width setting to use when tokenizing the file.
36+
*
37+
* This allows for test case files to use a different tab width than the default.
38+
*
39+
* @var integer
40+
*/
41+
protected $tabWidth = 4;
42+
43+
/**
44+
* The \PHP_CodeSniffer\Files\File object containing the parsed contents of the test case file.
45+
*
46+
* @var \PHP_CodeSniffer\Files\File
47+
*/
48+
protected $phpcsFile;
49+
50+
51+
/**
52+
* Initialize & tokenize \PHP_CodeSniffer\Files\File with code from the test case file.
53+
*
54+
* The test case file for a unit test class has to be in the same directory
55+
* directory and use the same file name as the test class, using the .inc extension.
56+
*
57+
* @before
58+
*
59+
* @return void
60+
*/
61+
protected function initializeFile()
62+
{
63+
if (isset($this->phpcsFile) === false) {
64+
$config = new ConfigDouble();
65+
// Also set a tab-width to enable testing tab-replaced vs `orig_content`.
66+
$config->tabWidth = $this->tabWidth;
67+
68+
$ruleset = new Ruleset($config);
69+
70+
// Default to a file with the same name as the test class. Extension is property based.
71+
$relativeCN = str_replace(__NAMESPACE__, '', get_called_class());
72+
$relativePath = str_replace('\\', DIRECTORY_SEPARATOR, $relativeCN);
73+
$pathToTestFile = realpath(__DIR__).$relativePath.'.'.$this->fileExtension;
74+
75+
// Make sure the file gets parsed correctly based on the file type.
76+
$contents = 'phpcs_input_file: '.$pathToTestFile.PHP_EOL;
77+
$contents .= file_get_contents($pathToTestFile);
78+
79+
$this->phpcsFile = new DummyFile($contents, $ruleset, $config);
80+
$this->phpcsFile->process();
81+
}
82+
83+
}//end initializeFile()
84+
85+
86+
/**
87+
* Get the token pointer for a target token based on a specific comment found on the line before.
88+
*
89+
* Note: the test delimiter comment MUST start with "/* test" to allow this function to
90+
* distinguish between comments used *in* a test and test delimiters.
91+
*
92+
* @param string $commentString The delimiter comment to look for.
93+
* @param int|string|array $tokenType The type of token(s) to look for.
94+
* @param string $tokenContent Optional. The token content for the target token.
95+
*
96+
* @return int
97+
*/
98+
protected function getTargetToken($commentString, $tokenType, $tokenContent=null)
99+
{
100+
return AbstractMethodUnitTest::getTargetTokenFromFile($this->phpcsFile, $commentString, $tokenType, $tokenContent);
101+
102+
}//end getTargetToken()
103+
104+
105+
}//end class

0 commit comments

Comments
 (0)