Skip to content

Commit dabd744

Browse files
authored
Merge pull request #314 from PHPCSStandards/feature/tests-record-code-coverage-tokenizer
Tests: fix recording code coverage for Tokenizer tests
2 parents b4dfd6a + 1f0a0ad commit dabd744

28 files changed

+228
-168
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 <phpcs_nospam@adviesenzo.nl>
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

tests/Core/Tokenizer/AnonClassParenthesisOwnerTest.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99

1010
namespace PHP_CodeSniffer\Tests\Core\Tokenizer;
1111

12-
use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest;
13-
14-
final class AnonClassParenthesisOwnerTest extends AbstractMethodUnitTest
12+
final class AnonClassParenthesisOwnerTest extends AbstractTokenizerTestCase
1513
{
1614

1715

@@ -27,7 +25,7 @@ final class AnonClassParenthesisOwnerTest extends AbstractMethodUnitTest
2725
*/
2826
public function testAnonClassNoParentheses($testMarker)
2927
{
30-
$tokens = self::$phpcsFile->getTokens();
28+
$tokens = $this->phpcsFile->getTokens();
3129

3230
$anonClass = $this->getTargetToken($testMarker, T_ANON_CLASS);
3331
$this->assertFalse(array_key_exists('parenthesis_owner', $tokens[$anonClass]));
@@ -50,7 +48,7 @@ public function testAnonClassNoParentheses($testMarker)
5048
*/
5149
public function testAnonClassNoParenthesesNextOpenClose($testMarker)
5250
{
53-
$tokens = self::$phpcsFile->getTokens();
51+
$tokens = $this->phpcsFile->getTokens();
5452
$function = $this->getTargetToken($testMarker, T_FUNCTION);
5553

5654
$opener = $this->getTargetToken($testMarker, T_OPEN_PARENTHESIS);
@@ -99,7 +97,7 @@ public static function dataAnonClassNoParentheses()
9997
*/
10098
public function testAnonClassWithParentheses($testMarker)
10199
{
102-
$tokens = self::$phpcsFile->getTokens();
100+
$tokens = $this->phpcsFile->getTokens();
103101
$anonClass = $this->getTargetToken($testMarker, T_ANON_CLASS);
104102
$opener = $this->getTargetToken($testMarker, T_OPEN_PARENTHESIS);
105103
$closer = $this->getTargetToken($testMarker, T_CLOSE_PARENTHESIS);

tests/Core/Tokenizer/ArrayKeywordTest.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99

1010
namespace PHP_CodeSniffer\Tests\Core\Tokenizer;
1111

12-
use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest;
13-
14-
final class ArrayKeywordTest extends AbstractMethodUnitTest
12+
final class ArrayKeywordTest extends AbstractTokenizerTestCase
1513
{
1614

1715

@@ -29,7 +27,7 @@ final class ArrayKeywordTest extends AbstractMethodUnitTest
2927
*/
3028
public function testArrayKeyword($testMarker, $testContent='array')
3129
{
32-
$tokens = self::$phpcsFile->getTokens();
30+
$tokens = $this->phpcsFile->getTokens();
3331

3432
$token = $this->getTargetToken($testMarker, [T_ARRAY, T_STRING], $testContent);
3533
$tokenArray = $tokens[$token];
@@ -89,7 +87,7 @@ public static function dataArrayKeyword()
8987
*/
9088
public function testArrayType($testMarker, $testContent='array')
9189
{
92-
$tokens = self::$phpcsFile->getTokens();
90+
$tokens = $this->phpcsFile->getTokens();
9391

9492
$token = $this->getTargetToken($testMarker, [T_ARRAY, T_STRING], $testContent);
9593
$tokenArray = $tokens[$token];
@@ -144,7 +142,7 @@ public static function dataArrayType()
144142
*/
145143
public function testNotArrayKeyword($testMarker, $testContent='array')
146144
{
147-
$tokens = self::$phpcsFile->getTokens();
145+
$tokens = $this->phpcsFile->getTokens();
148146

149147
$token = $this->getTargetToken($testMarker, [T_ARRAY, T_STRING], $testContent);
150148
$tokenArray = $tokens[$token];

tests/Core/Tokenizer/AttributesTest.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99

1010
namespace PHP_CodeSniffer\Tests\Core\Tokenizer;
1111

12-
use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest;
13-
14-
final class AttributesTest extends AbstractMethodUnitTest
12+
final class AttributesTest extends AbstractTokenizerTestCase
1513
{
1614

1715

@@ -31,7 +29,7 @@ final class AttributesTest extends AbstractMethodUnitTest
3129
*/
3230
public function testAttribute($testMarker, $length, $tokenCodes)
3331
{
34-
$tokens = self::$phpcsFile->getTokens();
32+
$tokens = $this->phpcsFile->getTokens();
3533

3634
$attribute = $this->getTargetToken($testMarker, T_ATTRIBUTE);
3735
$this->assertArrayHasKey('attribute_closer', $tokens[$attribute]);
@@ -294,7 +292,7 @@ public static function dataAttribute()
294292
*/
295293
public function testTwoAttributesOnTheSameLine()
296294
{
297-
$tokens = self::$phpcsFile->getTokens();
295+
$tokens = $this->phpcsFile->getTokens();
298296

299297
$attribute = $this->getTargetToken('/* testTwoAttributeOnTheSameLine */', T_ATTRIBUTE);
300298
$this->assertArrayHasKey('attribute_closer', $tokens[$attribute]);
@@ -318,7 +316,7 @@ public function testTwoAttributesOnTheSameLine()
318316
*/
319317
public function testAttributeAndLineComment()
320318
{
321-
$tokens = self::$phpcsFile->getTokens();
319+
$tokens = $this->phpcsFile->getTokens();
322320

323321
$attribute = $this->getTargetToken('/* testAttributeAndCommentOnTheSameLine */', T_ATTRIBUTE);
324322
$this->assertArrayHasKey('attribute_closer', $tokens[$attribute]);
@@ -348,7 +346,7 @@ public function testAttributeAndLineComment()
348346
*/
349347
public function testAttributeOnParameters($testMarker, $position, $length, array $tokenCodes)
350348
{
351-
$tokens = self::$phpcsFile->getTokens();
349+
$tokens = $this->phpcsFile->getTokens();
352350

353351
$function = $this->getTargetToken($testMarker, T_FUNCTION);
354352
$attribute = ($function + $position);
@@ -455,7 +453,7 @@ public static function dataAttributeOnParameters()
455453
*/
456454
public function testAttributeContainingTextLookingLikeCloseTag($testMarker, $length, array $expectedTokensAttribute, array $expectedTokensAfter)
457455
{
458-
$tokens = self::$phpcsFile->getTokens();
456+
$tokens = $this->phpcsFile->getTokens();
459457

460458
$attribute = $this->getTargetToken($testMarker, T_ATTRIBUTE);
461459

@@ -601,7 +599,7 @@ public static function dataAttributeOnTextLookingLikeCloseTag()
601599
*/
602600
public function testInvalidAttribute()
603601
{
604-
$tokens = self::$phpcsFile->getTokens();
602+
$tokens = $this->phpcsFile->getTokens();
605603

606604
$attribute = $this->getTargetToken('/* testInvalidAttribute */', T_ATTRIBUTE);
607605

@@ -617,12 +615,13 @@ public function testInvalidAttribute()
617615
* @covers PHP_CodeSniffer\Tokenizers\PHP::tokenize
618616
* @covers PHP_CodeSniffer\Tokenizers\PHP::findCloser
619617
* @covers PHP_CodeSniffer\Tokenizers\PHP::parsePhpAttribute
618+
* @covers PHP_CodeSniffer\Tokenizers\PHP::createAttributesNestingMap
620619
*
621620
* @return void
622621
*/
623622
public function testNestedAttributes()
624623
{
625-
$tokens = self::$phpcsFile->getTokens();
624+
$tokens = $this->phpcsFile->getTokens();
626625
$tokenCodes = [
627626
T_STRING,
628627
T_NS_SEPARATOR,

tests/Core/Tokenizer/BackfillEnumTest.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99

1010
namespace PHP_CodeSniffer\Tests\Core\Tokenizer;
1111

12-
use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest;
13-
14-
final class BackfillEnumTest extends AbstractMethodUnitTest
12+
final class BackfillEnumTest extends AbstractTokenizerTestCase
1513
{
1614

1715

@@ -30,7 +28,7 @@ final class BackfillEnumTest extends AbstractMethodUnitTest
3028
*/
3129
public function testEnums($testMarker, $testContent, $openerOffset, $closerOffset)
3230
{
33-
$tokens = self::$phpcsFile->getTokens();
31+
$tokens = $this->phpcsFile->getTokens();
3432
$enum = $this->getTargetToken($testMarker, [T_ENUM, T_STRING], $testContent);
3533
$tokenArray = $tokens[$enum];
3634

@@ -138,7 +136,7 @@ public static function dataEnums()
138136
*/
139137
public function testNotEnums($testMarker, $testContent)
140138
{
141-
$tokens = self::$phpcsFile->getTokens();
139+
$tokens = $this->phpcsFile->getTokens();
142140
$target = $this->getTargetToken($testMarker, [T_ENUM, T_STRING], $testContent);
143141
$tokenArray = $tokens[$target];
144142

tests/Core/Tokenizer/BackfillExplicitOctalNotationTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99

1010
namespace PHP_CodeSniffer\Tests\Core\Tokenizer;
1111

12-
use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest;
13-
14-
final class BackfillExplicitOctalNotationTest extends AbstractMethodUnitTest
12+
final class BackfillExplicitOctalNotationTest extends AbstractTokenizerTestCase
1513
{
1614

1715

@@ -30,7 +28,7 @@ final class BackfillExplicitOctalNotationTest extends AbstractMethodUnitTest
3028
*/
3129
public function testExplicitOctalNotation($marker, $value, $nextToken, $nextContent)
3230
{
33-
$tokens = self::$phpcsFile->getTokens();
31+
$tokens = $this->phpcsFile->getTokens();
3432

3533
$number = $this->getTargetToken($marker, [T_LNUMBER]);
3634

0 commit comments

Comments
 (0)