Skip to content

Tests: fix recording code coverage for Tokenizer tests #314

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 105 additions & 0 deletions tests/Core/Tokenizer/AbstractTokenizerTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php
/**
* Base class to use when testing parts of the tokenizer.
*
* This is a near duplicate of the AbstractMethodUnitTest class, with the
* difference being that it allows for recording code coverage for tokenizer tests.
*
* @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
* @copyright 2018-2019 Juliette Reinders Folmer. All rights reserved.
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/

namespace PHP_CodeSniffer\Tests\Core\Tokenizer;

use PHP_CodeSniffer\Ruleset;
use PHP_CodeSniffer\Files\DummyFile;
use PHP_CodeSniffer\Tests\ConfigDouble;
use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest;
use PHPUnit\Framework\TestCase;

abstract class AbstractTokenizerTestCase 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 $fileExtension = 'inc';

/**
* The tab width setting to use when tokenizing the file.
*
* This allows for test case files to use a different tab width than the default.
*
* @var integer
*/
protected $tabWidth = 4;

/**
* The \PHP_CodeSniffer\Files\File object containing the parsed contents of the test case file.
*
* @var \PHP_CodeSniffer\Files\File
*/
protected $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.
*
* @before
*
* @return void
*/
protected function initializeFile()
{
if (isset($this->phpcsFile) === false) {
$config = new ConfigDouble();
// Also set a tab-width to enable testing tab-replaced vs `orig_content`.
$config->tabWidth = $this->tabWidth;

$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.'.'.$this->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);

$this->phpcsFile = new DummyFile($contents, $ruleset, $config);
$this->phpcsFile->process();
}

}//end initializeFile()


/**
* Get the token pointer for a target token based on a specific comment found on the line before.
*
* Note: the test delimiter comment MUST start with "/* test" to allow this function to
* distinguish between comments used *in* a test and test delimiters.
*
* @param string $commentString The delimiter comment to look for.
* @param int|string|array $tokenType The type of token(s) to look for.
* @param string $tokenContent Optional. The token content for the target token.
*
* @return int
*/
protected function getTargetToken($commentString, $tokenType, $tokenContent=null)
{
return AbstractMethodUnitTest::getTargetTokenFromFile($this->phpcsFile, $commentString, $tokenType, $tokenContent);

}//end getTargetToken()


}//end class
10 changes: 4 additions & 6 deletions tests/Core/Tokenizer/AnonClassParenthesisOwnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@

namespace PHP_CodeSniffer\Tests\Core\Tokenizer;

use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest;

final class AnonClassParenthesisOwnerTest extends AbstractMethodUnitTest
final class AnonClassParenthesisOwnerTest extends AbstractTokenizerTestCase
{


Expand All @@ -27,7 +25,7 @@ final class AnonClassParenthesisOwnerTest extends AbstractMethodUnitTest
*/
public function testAnonClassNoParentheses($testMarker)
{
$tokens = self::$phpcsFile->getTokens();
$tokens = $this->phpcsFile->getTokens();

$anonClass = $this->getTargetToken($testMarker, T_ANON_CLASS);
$this->assertFalse(array_key_exists('parenthesis_owner', $tokens[$anonClass]));
Expand All @@ -50,7 +48,7 @@ public function testAnonClassNoParentheses($testMarker)
*/
public function testAnonClassNoParenthesesNextOpenClose($testMarker)
{
$tokens = self::$phpcsFile->getTokens();
$tokens = $this->phpcsFile->getTokens();
$function = $this->getTargetToken($testMarker, T_FUNCTION);

$opener = $this->getTargetToken($testMarker, T_OPEN_PARENTHESIS);
Expand Down Expand Up @@ -99,7 +97,7 @@ public static function dataAnonClassNoParentheses()
*/
public function testAnonClassWithParentheses($testMarker)
{
$tokens = self::$phpcsFile->getTokens();
$tokens = $this->phpcsFile->getTokens();
$anonClass = $this->getTargetToken($testMarker, T_ANON_CLASS);
$opener = $this->getTargetToken($testMarker, T_OPEN_PARENTHESIS);
$closer = $this->getTargetToken($testMarker, T_CLOSE_PARENTHESIS);
Expand Down
10 changes: 4 additions & 6 deletions tests/Core/Tokenizer/ArrayKeywordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@

namespace PHP_CodeSniffer\Tests\Core\Tokenizer;

use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest;

final class ArrayKeywordTest extends AbstractMethodUnitTest
final class ArrayKeywordTest extends AbstractTokenizerTestCase
{


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

$token = $this->getTargetToken($testMarker, [T_ARRAY, T_STRING], $testContent);
$tokenArray = $tokens[$token];
Expand Down Expand Up @@ -89,7 +87,7 @@ public static function dataArrayKeyword()
*/
public function testArrayType($testMarker, $testContent='array')
{
$tokens = self::$phpcsFile->getTokens();
$tokens = $this->phpcsFile->getTokens();

$token = $this->getTargetToken($testMarker, [T_ARRAY, T_STRING], $testContent);
$tokenArray = $tokens[$token];
Expand Down Expand Up @@ -144,7 +142,7 @@ public static function dataArrayType()
*/
public function testNotArrayKeyword($testMarker, $testContent='array')
{
$tokens = self::$phpcsFile->getTokens();
$tokens = $this->phpcsFile->getTokens();

$token = $this->getTargetToken($testMarker, [T_ARRAY, T_STRING], $testContent);
$tokenArray = $tokens[$token];
Expand Down
19 changes: 9 additions & 10 deletions tests/Core/Tokenizer/AttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@

namespace PHP_CodeSniffer\Tests\Core\Tokenizer;

use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest;

final class AttributesTest extends AbstractMethodUnitTest
final class AttributesTest extends AbstractTokenizerTestCase
{


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

$attribute = $this->getTargetToken($testMarker, T_ATTRIBUTE);
$this->assertArrayHasKey('attribute_closer', $tokens[$attribute]);
Expand Down Expand Up @@ -294,7 +292,7 @@ public static function dataAttribute()
*/
public function testTwoAttributesOnTheSameLine()
{
$tokens = self::$phpcsFile->getTokens();
$tokens = $this->phpcsFile->getTokens();

$attribute = $this->getTargetToken('/* testTwoAttributeOnTheSameLine */', T_ATTRIBUTE);
$this->assertArrayHasKey('attribute_closer', $tokens[$attribute]);
Expand All @@ -318,7 +316,7 @@ public function testTwoAttributesOnTheSameLine()
*/
public function testAttributeAndLineComment()
{
$tokens = self::$phpcsFile->getTokens();
$tokens = $this->phpcsFile->getTokens();

$attribute = $this->getTargetToken('/* testAttributeAndCommentOnTheSameLine */', T_ATTRIBUTE);
$this->assertArrayHasKey('attribute_closer', $tokens[$attribute]);
Expand Down Expand Up @@ -348,7 +346,7 @@ public function testAttributeAndLineComment()
*/
public function testAttributeOnParameters($testMarker, $position, $length, array $tokenCodes)
{
$tokens = self::$phpcsFile->getTokens();
$tokens = $this->phpcsFile->getTokens();

$function = $this->getTargetToken($testMarker, T_FUNCTION);
$attribute = ($function + $position);
Expand Down Expand Up @@ -455,7 +453,7 @@ public static function dataAttributeOnParameters()
*/
public function testAttributeContainingTextLookingLikeCloseTag($testMarker, $length, array $expectedTokensAttribute, array $expectedTokensAfter)
{
$tokens = self::$phpcsFile->getTokens();
$tokens = $this->phpcsFile->getTokens();

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

Expand Down Expand Up @@ -601,7 +599,7 @@ public static function dataAttributeOnTextLookingLikeCloseTag()
*/
public function testInvalidAttribute()
{
$tokens = self::$phpcsFile->getTokens();
$tokens = $this->phpcsFile->getTokens();

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

Expand All @@ -617,12 +615,13 @@ public function testInvalidAttribute()
* @covers PHP_CodeSniffer\Tokenizers\PHP::tokenize
* @covers PHP_CodeSniffer\Tokenizers\PHP::findCloser
* @covers PHP_CodeSniffer\Tokenizers\PHP::parsePhpAttribute
* @covers PHP_CodeSniffer\Tokenizers\PHP::createAttributesNestingMap
*
* @return void
*/
public function testNestedAttributes()
{
$tokens = self::$phpcsFile->getTokens();
$tokens = $this->phpcsFile->getTokens();
$tokenCodes = [
T_STRING,
T_NS_SEPARATOR,
Expand Down
8 changes: 3 additions & 5 deletions tests/Core/Tokenizer/BackfillEnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@

namespace PHP_CodeSniffer\Tests\Core\Tokenizer;

use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest;

final class BackfillEnumTest extends AbstractMethodUnitTest
final class BackfillEnumTest extends AbstractTokenizerTestCase
{


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

Expand Down Expand Up @@ -138,7 +136,7 @@ public static function dataEnums()
*/
public function testNotEnums($testMarker, $testContent)
{
$tokens = self::$phpcsFile->getTokens();
$tokens = $this->phpcsFile->getTokens();
$target = $this->getTargetToken($testMarker, [T_ENUM, T_STRING], $testContent);
$tokenArray = $tokens[$target];

Expand Down
6 changes: 2 additions & 4 deletions tests/Core/Tokenizer/BackfillExplicitOctalNotationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@

namespace PHP_CodeSniffer\Tests\Core\Tokenizer;

use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest;

final class BackfillExplicitOctalNotationTest extends AbstractMethodUnitTest
final class BackfillExplicitOctalNotationTest extends AbstractTokenizerTestCase
{


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

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

Expand Down
Loading