-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tokenizer/PHP: add tests for consistent tokenization heredocs with in…
…terpolated strings Similar to previous PR #3604 which fixed the tokenization for complex double quoted strings with interpolated variables/expressions and added tests for it.
- Loading branch information
Showing
3 changed files
with
352 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
<?php | ||
|
||
// Test source: https://gist.github.com/iluuu1994/72e2154fc4150f2258316b0255b698f2#file-test-php | ||
|
||
/* testSimple1 */ | ||
$heredoc = <<<EOD | ||
$foo | ||
EOD; | ||
|
||
/* testSimple2 */ | ||
$heredoc = <<<"EOD" | ||
{$foo} | ||
EOD; | ||
|
||
/* testSimple3 */ | ||
$heredoc = <<<EOD | ||
${foo} | ||
EOD; | ||
|
||
/* testDIM1 */ | ||
$heredoc = <<<"EOD" | ||
$foo[bar] | ||
EOD; | ||
|
||
/* testDIM2 */ | ||
$heredoc = <<<EOD | ||
{$foo['bar']} | ||
EOD; | ||
|
||
/* testDIM3 */ | ||
$heredoc = <<<"EOD" | ||
${foo['bar']} | ||
EOD; | ||
|
||
/* testProperty1 */ | ||
$heredoc = <<<EOD | ||
$foo->bar | ||
EOD; | ||
|
||
/* testProperty2 */ | ||
$heredoc = <<<"EOD" | ||
{$foo->bar} | ||
EOD; | ||
|
||
/* testMethod1 */ | ||
$heredoc = <<<EOD | ||
{$foo->bar()} | ||
EOD; | ||
|
||
/* testClosure1 */ | ||
$heredoc = <<<"EOD" | ||
{$foo()} | ||
EOD; | ||
|
||
/* testChain1 */ | ||
$heredoc = <<<EOD | ||
{$foo['bar']->baz()()} | ||
EOD; | ||
|
||
/* testVariableVar1 */ | ||
$heredoc = <<<"EOD" | ||
${$bar} | ||
EOD; | ||
|
||
/* testVariableVar2 */ | ||
$heredoc = <<<EOD | ||
${(foo)} | ||
EOD; | ||
|
||
/* testVariableVar3 */ | ||
$heredoc = <<<"EOD" | ||
${foo->bar} | ||
EOD; | ||
|
||
/* testNested1 */ | ||
$heredoc = <<<EOD | ||
${foo["${bar}"]} | ||
EOD; | ||
|
||
/* testNested2 */ | ||
$heredoc = <<<"EOD" | ||
${foo["${bar['baz']}"]} | ||
EOD; | ||
|
||
/* testNested3 */ | ||
$heredoc = <<<EOD | ||
${foo->{$baz}} | ||
EOD; | ||
|
||
/* testNested4 */ | ||
$heredoc = <<<"EOD" | ||
${foo->{${'a'}}} | ||
EOD; | ||
|
||
/* testNested5 */ | ||
$heredoc = <<<EOD | ||
${foo->{"${'a'}"}} | ||
EOD; | ||
|
||
/* testSimple1Wrapped */ | ||
$heredoc = <<<EOD | ||
Do $foo Something | ||
EOD; | ||
|
||
/* testSimple2Wrapped */ | ||
$heredoc = <<<"EOD" | ||
Do {$foo} Something | ||
EOD; | ||
|
||
/* testSimple3Wrapped */ | ||
$heredoc = <<<EOD | ||
Do ${foo} Something | ||
EOD; | ||
|
||
/* testDIM1Wrapped */ | ||
$heredoc = <<<"EOD" | ||
Do $foo[bar] Something | ||
EOD; | ||
|
||
/* testDIM2Wrapped */ | ||
$heredoc = <<<EOD | ||
Do {$foo['bar']} Something | ||
EOD; | ||
|
||
/* testDIM3Wrapped */ | ||
$heredoc = <<<"EOD" | ||
Do ${foo['bar']} Something | ||
EOD; | ||
|
||
/* testProperty1Wrapped */ | ||
$heredoc = <<<EOD | ||
Do $foo->bar Something | ||
EOD; | ||
|
||
/* testProperty2Wrapped */ | ||
$heredoc = <<<"EOD" | ||
Do {$foo->bar} Something | ||
EOD; | ||
|
||
/* testMethod1Wrapped */ | ||
$heredoc = <<<EOD | ||
Do {$foo->bar()} Something | ||
EOD; | ||
|
||
/* testClosure1Wrapped */ | ||
$heredoc = <<<"EOD" | ||
Do {$foo()} Something | ||
EOD; | ||
|
||
/* testChain1Wrapped */ | ||
$heredoc = <<<EOD | ||
Do {$foo['bar']->baz()()} Something | ||
EOD; | ||
|
||
/* testVariableVar1Wrapped */ | ||
$heredoc = <<<"EOD" | ||
Do ${$bar} Something | ||
EOD; | ||
|
||
/* testVariableVar2Wrapped */ | ||
$heredoc = <<<EOD | ||
Do ${(foo)} Something | ||
EOD; | ||
|
||
/* testVariableVar3Wrapped */ | ||
$heredoc = <<<"EOD" | ||
Do ${foo->bar} Something | ||
EOD; | ||
|
||
/* testNested1Wrapped */ | ||
$heredoc = <<<EOD | ||
Do ${foo["${bar}"]} Something | ||
EOD; | ||
|
||
/* testNested2Wrapped */ | ||
$heredoc = <<<"EOD" | ||
Do ${foo["${bar['baz']}"]} Something | ||
EOD; | ||
|
||
/* testNested3Wrapped */ | ||
$heredoc = <<<EOD | ||
Do ${foo->{$baz}} Something | ||
EOD; | ||
|
||
/* testNested4Wrapped */ | ||
$heredoc = <<<"EOD" | ||
Do ${foo->{${'a'}}} Something | ||
EOD; | ||
|
||
/* testNested5Wrapped */ | ||
$heredoc = <<<EOD | ||
Do ${foo->{"${'a'}"}} Something | ||
EOD; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
<?php | ||
/** | ||
* Tests that embedded variables and expressions in heredoc strings are tokenized | ||
* as one heredoc string token. | ||
* | ||
* @author Juliette Reinders Folmer <[email protected]> | ||
* @copyright 2022 Squiz Pty Ltd (ABN 77 084 670 600) | ||
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence | ||
*/ | ||
|
||
namespace PHP_CodeSniffer\Tests\Core\Tokenizer; | ||
|
||
use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest; | ||
|
||
class HeredocStringTest extends AbstractMethodUnitTest | ||
{ | ||
|
||
|
||
/** | ||
* Test that heredoc strings contain the complete interpolated string. | ||
* | ||
* @param string $testMarker The comment which prefaces the target token in the test file. | ||
* @param string $expectedContent The expected content of the heredoc string. | ||
* | ||
* @dataProvider dataHeredocString | ||
* @covers PHP_CodeSniffer\Tokenizers\PHP::tokenize | ||
* | ||
* @return void | ||
*/ | ||
public function testHeredocString($testMarker, $expectedContent) | ||
{ | ||
$tokens = self::$phpcsFile->getTokens(); | ||
|
||
$target = $this->getTargetToken($testMarker, T_HEREDOC); | ||
$this->assertSame($expectedContent."\n", $tokens[$target]['content']); | ||
|
||
}//end testHeredocString() | ||
|
||
|
||
/** | ||
* Test that heredoc strings contain the complete interpolated string when combined with other texts. | ||
* | ||
* @param string $testMarker The comment which prefaces the target token in the test file. | ||
* @param string $expectedContent The expected content of the heredoc string. | ||
* | ||
* @dataProvider dataHeredocString | ||
* @covers PHP_CodeSniffer\Tokenizers\PHP::tokenize | ||
* | ||
* @return void | ||
*/ | ||
public function testHeredocStringWrapped($testMarker, $expectedContent) | ||
{ | ||
$tokens = self::$phpcsFile->getTokens(); | ||
|
||
$testMarker = substr($testMarker, 0, -3).'Wrapped */'; | ||
$target = $this->getTargetToken($testMarker, T_HEREDOC); | ||
$this->assertSame('Do '.$expectedContent." Something\n", $tokens[$target]['content']); | ||
|
||
}//end testHeredocStringWrapped() | ||
|
||
|
||
/** | ||
* Data provider. | ||
* | ||
* @see testHeredocString() | ||
* | ||
* @return array | ||
*/ | ||
public function dataHeredocString() | ||
{ | ||
return [ | ||
[ | ||
'testMarker' => '/* testSimple1 */', | ||
'expectedContent' => '$foo', | ||
], | ||
[ | ||
'testMarker' => '/* testSimple2 */', | ||
'expectedContent' => '{$foo}', | ||
], | ||
[ | ||
'testMarker' => '/* testSimple3 */', | ||
'expectedContent' => '${foo}', | ||
], | ||
[ | ||
'testMarker' => '/* testDIM1 */', | ||
'expectedContent' => '$foo[bar]', | ||
], | ||
[ | ||
'testMarker' => '/* testDIM2 */', | ||
'expectedContent' => '{$foo[\'bar\']}', | ||
], | ||
[ | ||
'testMarker' => '/* testDIM3 */', | ||
'expectedContent' => '${foo[\'bar\']}', | ||
], | ||
[ | ||
'testMarker' => '/* testProperty1 */', | ||
'expectedContent' => '$foo->bar', | ||
], | ||
[ | ||
'testMarker' => '/* testProperty2 */', | ||
'expectedContent' => '{$foo->bar}', | ||
], | ||
[ | ||
'testMarker' => '/* testMethod1 */', | ||
'expectedContent' => '{$foo->bar()}', | ||
], | ||
[ | ||
'testMarker' => '/* testClosure1 */', | ||
'expectedContent' => '{$foo()}', | ||
], | ||
[ | ||
'testMarker' => '/* testChain1 */', | ||
'expectedContent' => '{$foo[\'bar\']->baz()()}', | ||
], | ||
[ | ||
'testMarker' => '/* testVariableVar1 */', | ||
'expectedContent' => '${$bar}', | ||
], | ||
[ | ||
'testMarker' => '/* testVariableVar2 */', | ||
'expectedContent' => '${(foo)}', | ||
], | ||
[ | ||
'testMarker' => '/* testVariableVar3 */', | ||
'expectedContent' => '${foo->bar}', | ||
], | ||
[ | ||
'testMarker' => '/* testNested1 */', | ||
'expectedContent' => '${foo["${bar}"]}', | ||
], | ||
[ | ||
'testMarker' => '/* testNested2 */', | ||
'expectedContent' => '${foo["${bar[\'baz\']}"]}', | ||
], | ||
[ | ||
'testMarker' => '/* testNested3 */', | ||
'expectedContent' => '${foo->{$baz}}', | ||
], | ||
[ | ||
'testMarker' => '/* testNested4 */', | ||
'expectedContent' => '${foo->{${\'a\'}}}', | ||
], | ||
[ | ||
'testMarker' => '/* testNested5 */', | ||
'expectedContent' => '${foo->{"${\'a\'}"}}', | ||
], | ||
]; | ||
|
||
}//end dataHeredocString() | ||
|
||
|
||
}//end class |