1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
<?php
/**
* Tests the tokenization of heredoc/nowdoc opener tokens.
*
* @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
* @copyright 2024 PHPCSStandards and contributors
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/
namespace PHP_CodeSniffer\Tests\Core\Tokenizers\Tokenizer;
use PHP_CodeSniffer\Tests\Core\Tokenizers\AbstractTokenizerTestCase;
/**
* Heredoc/nowdoc opener token test.
*
* @covers PHP_CodeSniffer\Tokenizers\Tokenizer::createPositionMap
*/
final class CreatePositionMapHeredocNowdocOpenerTest extends AbstractTokenizerTestCase
{
/**
* Verify that spaces/tabs in a heredoc/nowdoc opener token get the tab replacement treatment.
*
* @param string $testMarker The comment prefacing the target token.
* @param array<string, int|string|null> $expected Expectations for the token array.
*
* @dataProvider dataHeredocNowdocOpenerTabReplacement
*
* @return void
*/
public function testHeredocNowdocOpenerTabReplacement($testMarker, $expected)
{
$tokens = $this->phpcsFile->getTokens();
$opener = $this->getTargetToken($testMarker, [T_START_HEREDOC, T_START_NOWDOC]);
foreach ($expected as $key => $value) {
if ($key === 'orig_content' && $value === null) {
$this->assertArrayNotHasKey($key, $tokens[$opener], "Unexpected 'orig_content' key found in the token array.");
continue;
}
$this->assertArrayHasKey($key, $tokens[$opener], "Key $key not found in the token array.");
$this->assertSame($value, $tokens[$opener][$key], "Value for key $key does not match expectation.");
}
}//end testHeredocNowdocOpenerTabReplacement()
/**
* Data provider.
*
* @see testHeredocNowdocOpenerTabReplacement()
*
* @return array<string, array<string, string|array<string, int|string|null>>>
*/
public static function dataHeredocNowdocOpenerTabReplacement()
{
return [
'Heredoc opener without space' => [
'testMarker' => '/* testHeredocOpenerNoSpace */',
'expected' => [
'length' => 6,
'content' => '<<<EOD
',
'orig_content' => null,
],
],
'Nowdoc opener without space' => [
'testMarker' => '/* testNowdocOpenerNoSpace */',
'expected' => [
'length' => 8,
'content' => "<<<'EOD'
",
'orig_content' => null,
],
],
'Heredoc opener with space(s)' => [
'testMarker' => '/* testHeredocOpenerHasSpace */',
'expected' => [
'length' => 7,
'content' => '<<< END
',
'orig_content' => null,
],
],
'Nowdoc opener with space(s)' => [
'testMarker' => '/* testNowdocOpenerHasSpace */',
'expected' => [
'length' => 21,
'content' => "<<< 'END'
",
'orig_content' => null,
],
],
'Heredoc opener with tab(s)' => [
'testMarker' => '/* testHeredocOpenerHasTab */',
'expected' => [
'length' => 18,
'content' => '<<< "END"
',
'orig_content' => '<<< "END"
',
],
],
'Nowdoc opener with tab(s)' => [
'testMarker' => '/* testNowdocOpenerHasTab */',
'expected' => [
'length' => 11,
'content' => "<<< 'END'
",
'orig_content' => "<<< 'END'
",
],
],
];
}//end dataHeredocNowdocOpenerTabReplacement()
}//end class
|