|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Tests that embedded variables and expressions in double quoted strings are tokenized |
| 4 | + * as one double quoted string token. |
| 5 | + * |
| 6 | + * @author Juliette Reinders Folmer <[email protected]> |
| 7 | + * @copyright 2022 Squiz Pty Ltd (ABN 77 084 670 600) |
| 8 | + * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence |
| 9 | + */ |
| 10 | + |
| 11 | +namespace PHP_CodeSniffer\Tests\Core\Tokenizer; |
| 12 | + |
| 13 | +use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest; |
| 14 | + |
| 15 | +class DoubleQuotedStringTest extends AbstractMethodUnitTest |
| 16 | +{ |
| 17 | + |
| 18 | + |
| 19 | + /** |
| 20 | + * Test that double quoted strings contain the complete string. |
| 21 | + * |
| 22 | + * @param string $testMarker The comment which prefaces the target token in the test file. |
| 23 | + * @param string $expectedContent The expected content of the double quoted string. |
| 24 | + * |
| 25 | + * @dataProvider dataDoubleQuotedString |
| 26 | + * @covers PHP_CodeSniffer\Tokenizers\PHP::tokenize |
| 27 | + * |
| 28 | + * @return void |
| 29 | + */ |
| 30 | + public function testDoubleQuotedString($testMarker, $expectedContent) |
| 31 | + { |
| 32 | + $tokens = self::$phpcsFile->getTokens(); |
| 33 | + |
| 34 | + $target = $this->getTargetToken($testMarker, T_DOUBLE_QUOTED_STRING); |
| 35 | + $this->assertSame($expectedContent, $tokens[$target]['content']); |
| 36 | + |
| 37 | + }//end testDoubleQuotedString() |
| 38 | + |
| 39 | + |
| 40 | + /** |
| 41 | + * Data provider. |
| 42 | + * |
| 43 | + * @see testDoubleQuotedString() |
| 44 | + * |
| 45 | + * @return array |
| 46 | + */ |
| 47 | + public function dataDoubleQuotedString() |
| 48 | + { |
| 49 | + return [ |
| 50 | + [ |
| 51 | + 'testMarker' => '/* testSimple1 */', |
| 52 | + 'expectedContent' => '"$foo"', |
| 53 | + ], |
| 54 | + [ |
| 55 | + 'testMarker' => '/* testSimple2 */', |
| 56 | + 'expectedContent' => '"{$foo}"', |
| 57 | + ], |
| 58 | + [ |
| 59 | + 'testMarker' => '/* testSimple3 */', |
| 60 | + 'expectedContent' => '"${foo}"', |
| 61 | + ], |
| 62 | + [ |
| 63 | + 'testMarker' => '/* testDIM1 */', |
| 64 | + 'expectedContent' => '"$foo[bar]"', |
| 65 | + ], |
| 66 | + [ |
| 67 | + 'testMarker' => '/* testDIM2 */', |
| 68 | + 'expectedContent' => '"{$foo[\'bar\']}"', |
| 69 | + ], |
| 70 | + [ |
| 71 | + 'testMarker' => '/* testDIM3 */', |
| 72 | + 'expectedContent' => '"${foo[\'bar\']}"', |
| 73 | + ], |
| 74 | + [ |
| 75 | + 'testMarker' => '/* testProperty1 */', |
| 76 | + 'expectedContent' => '"$foo->bar"', |
| 77 | + ], |
| 78 | + [ |
| 79 | + 'testMarker' => '/* testProperty2 */', |
| 80 | + 'expectedContent' => '"{$foo->bar}"', |
| 81 | + ], |
| 82 | + [ |
| 83 | + 'testMarker' => '/* testMethod1 */', |
| 84 | + 'expectedContent' => '"{$foo->bar()}"', |
| 85 | + ], |
| 86 | + [ |
| 87 | + 'testMarker' => '/* testClosure1 */', |
| 88 | + 'expectedContent' => '"{$foo()}"', |
| 89 | + ], |
| 90 | + [ |
| 91 | + 'testMarker' => '/* testChain1 */', |
| 92 | + 'expectedContent' => '"{$foo[\'bar\']->baz()()}"', |
| 93 | + ], |
| 94 | + [ |
| 95 | + 'testMarker' => '/* testVariableVar1 */', |
| 96 | + 'expectedContent' => '"${$bar}"', |
| 97 | + ], |
| 98 | + [ |
| 99 | + 'testMarker' => '/* testVariableVar2 */', |
| 100 | + 'expectedContent' => '"${(foo)}"', |
| 101 | + ], |
| 102 | + [ |
| 103 | + 'testMarker' => '/* testVariableVar3 */', |
| 104 | + 'expectedContent' => '"${foo->bar}"', |
| 105 | + ], |
| 106 | + [ |
| 107 | + 'testMarker' => '/* testNested1 */', |
| 108 | + 'expectedContent' => '"${foo["${bar}"]}"', |
| 109 | + ], |
| 110 | + [ |
| 111 | + 'testMarker' => '/* testNested2 */', |
| 112 | + 'expectedContent' => '"${foo["${bar[\'baz\']}"]}"', |
| 113 | + ], |
| 114 | + [ |
| 115 | + 'testMarker' => '/* testNested3 */', |
| 116 | + 'expectedContent' => '"${foo->{$baz}}"', |
| 117 | + ], |
| 118 | + [ |
| 119 | + 'testMarker' => '/* testNested4 */', |
| 120 | + 'expectedContent' => '"${foo->{${\'a\'}}}"', |
| 121 | + ], |
| 122 | + [ |
| 123 | + 'testMarker' => '/* testNested5 */', |
| 124 | + 'expectedContent' => '"${foo->{"${\'a\'}"}}"', |
| 125 | + ], |
| 126 | + [ |
| 127 | + 'testMarker' => '/* testParseError */', |
| 128 | + 'expectedContent' => '"${foo["${bar |
| 129 | +', |
| 130 | + ], |
| 131 | + ]; |
| 132 | + |
| 133 | + }//end dataDoubleQuotedString() |
| 134 | + |
| 135 | + |
| 136 | +}//end class |
0 commit comments