|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Tests the tokenization of goto declarations and statements. |
| 4 | + * |
| 5 | + * @author Juliette Reinders Folmer <[email protected]> |
| 6 | + * @copyright 2020 Squiz Pty Ltd (ABN 77 084 670 600) |
| 7 | + * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence |
| 8 | + */ |
| 9 | + |
| 10 | +namespace PHP_CodeSniffer\Tests\Core\Tokenizer; |
| 11 | + |
| 12 | +use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest; |
| 13 | + |
| 14 | +class GotoLabelTest extends AbstractMethodUnitTest |
| 15 | +{ |
| 16 | + |
| 17 | + |
| 18 | + /** |
| 19 | + * Verify that the label in a goto statement is tokenized as T_STRING. |
| 20 | + * |
| 21 | + * @param string $testMarker The comment prefacing the target token. |
| 22 | + * @param string $testContent The token content to expect. |
| 23 | + * |
| 24 | + * @dataProvider dataGotoStatement |
| 25 | + * @covers PHP_CodeSniffer\Tokenizers\PHP::tokenize |
| 26 | + * |
| 27 | + * @return void |
| 28 | + */ |
| 29 | + public function testGotoStatement($testMarker, $testContent) |
| 30 | + { |
| 31 | + $tokens = self::$phpcsFile->getTokens(); |
| 32 | + |
| 33 | + $label = $this->getTargetToken($testMarker, T_STRING); |
| 34 | + |
| 35 | + $this->assertInternalType('int', $label); |
| 36 | + $this->assertSame($testContent, $tokens[$label]['content']); |
| 37 | + |
| 38 | + }//end testGotoStatement() |
| 39 | + |
| 40 | + |
| 41 | + /** |
| 42 | + * Data provider. |
| 43 | + * |
| 44 | + * @see testGotoStatement() |
| 45 | + * |
| 46 | + * @return array |
| 47 | + */ |
| 48 | + public function dataGotoStatement() |
| 49 | + { |
| 50 | + return [ |
| 51 | + [ |
| 52 | + '/* testGotoStatement */', |
| 53 | + 'marker', |
| 54 | + ], |
| 55 | + [ |
| 56 | + '/* testGotoStatementInLoop */', |
| 57 | + 'end', |
| 58 | + ], |
| 59 | + ]; |
| 60 | + |
| 61 | + }//end dataGotoStatement() |
| 62 | + |
| 63 | + |
| 64 | + /** |
| 65 | + * Verify that the label in a goto declaration is tokenized as T_GOTO_LABEL. |
| 66 | + * |
| 67 | + * @param string $testMarker The comment prefacing the target token. |
| 68 | + * @param string $testContent The token content to expect. |
| 69 | + * |
| 70 | + * @dataProvider dataGotoDeclaration |
| 71 | + * @covers PHP_CodeSniffer\Tokenizers\PHP::tokenize |
| 72 | + * |
| 73 | + * @return void |
| 74 | + */ |
| 75 | + public function testGotoDeclaration($testMarker, $testContent) |
| 76 | + { |
| 77 | + $tokens = self::$phpcsFile->getTokens(); |
| 78 | + |
| 79 | + $label = $this->getTargetToken($testMarker, T_GOTO_LABEL); |
| 80 | + |
| 81 | + $this->assertInternalType('int', $label); |
| 82 | + $this->assertSame($testContent, $tokens[$label]['content']); |
| 83 | + |
| 84 | + }//end testGotoDeclaration() |
| 85 | + |
| 86 | + |
| 87 | + /** |
| 88 | + * Data provider. |
| 89 | + * |
| 90 | + * @see testGotoDeclaration() |
| 91 | + * |
| 92 | + * @return array |
| 93 | + */ |
| 94 | + public function dataGotoDeclaration() |
| 95 | + { |
| 96 | + return [ |
| 97 | + [ |
| 98 | + '/* testGotoDeclaration */', |
| 99 | + 'marker:', |
| 100 | + ], |
| 101 | + [ |
| 102 | + '/* testGotoDeclarationOutsideLoop */', |
| 103 | + 'end:', |
| 104 | + ], |
| 105 | + ]; |
| 106 | + |
| 107 | + }//end dataGotoDeclaration() |
| 108 | + |
| 109 | + |
| 110 | + /** |
| 111 | + * Verify that the constant used in a switch - case statement is not confused with a goto label. |
| 112 | + * |
| 113 | + * @param string $testMarker The comment prefacing the target token. |
| 114 | + * @param string $testContent The token content to expect. |
| 115 | + * |
| 116 | + * @dataProvider dataNotAGotoDeclaration |
| 117 | + * @covers PHP_CodeSniffer\Tokenizers\PHP::tokenize |
| 118 | + * |
| 119 | + * @return void |
| 120 | + */ |
| 121 | + public function testNotAGotoDeclaration($testMarker, $testContent) |
| 122 | + { |
| 123 | + $tokens = self::$phpcsFile->getTokens(); |
| 124 | + $target = $this->getTargetToken($testMarker, [T_GOTO_LABEL, T_STRING], $testContent); |
| 125 | + |
| 126 | + $this->assertSame(T_STRING, $tokens[$target]['code']); |
| 127 | + $this->assertSame('T_STRING', $tokens[$target]['type']); |
| 128 | + |
| 129 | + }//end testNotAGotoDeclaration() |
| 130 | + |
| 131 | + |
| 132 | + /** |
| 133 | + * Data provider. |
| 134 | + * |
| 135 | + * @see testNotAGotoDeclaration() |
| 136 | + * |
| 137 | + * @return array |
| 138 | + */ |
| 139 | + public function dataNotAGotoDeclaration() |
| 140 | + { |
| 141 | + return [ |
| 142 | + [ |
| 143 | + '/* testNotGotoDeclarationGlobalConstant */', |
| 144 | + 'CONSTANT', |
| 145 | + ], |
| 146 | + [ |
| 147 | + '/* testNotGotoDeclarationNamespacedConstant */', |
| 148 | + 'CONSTANT', |
| 149 | + ], |
| 150 | + [ |
| 151 | + '/* testNotGotoDeclarationClassConstant */', |
| 152 | + 'CONSTANT', |
| 153 | + ], |
| 154 | + [ |
| 155 | + '/* testNotGotoDeclarationClassProperty */', |
| 156 | + 'property', |
| 157 | + ], |
| 158 | + [ |
| 159 | + '/* testNotGotoDeclarationGlobalConstantInTernary */', |
| 160 | + 'CONST_A', |
| 161 | + ], |
| 162 | + [ |
| 163 | + '/* testNotGotoDeclarationGlobalConstantInTernary */', |
| 164 | + 'CONST_B', |
| 165 | + ], |
| 166 | + ]; |
| 167 | + |
| 168 | + }//end dataNotAGotoDeclaration() |
| 169 | + |
| 170 | + |
| 171 | +}//end class |
0 commit comments