|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Tests setting the scope for T_SWITCH token (normal and alternative syntax). |
| 4 | + * |
| 5 | + * @author Rodrigo Primo <[email protected]> |
| 6 | + * @copyright 2024 PHPCSStandards and contributors |
| 7 | + * @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence |
| 8 | + */ |
| 9 | + |
| 10 | +namespace PHP_CodeSniffer\Tests\Core\Tokenizers\Tokenizer; |
| 11 | + |
| 12 | +use PHP_CodeSniffer\Tests\Core\Tokenizers\AbstractTokenizerTestCase; |
| 13 | + |
| 14 | +final class RecurseScopeMapSwitchTokenScopeTest extends AbstractTokenizerTestCase |
| 15 | +{ |
| 16 | + |
| 17 | + |
| 18 | + /** |
| 19 | + * Tests setting the scope for T_SWITCH token (normal and alternative syntax). |
| 20 | + * |
| 21 | + * @param string $testMarker The comment which prefaces the target token in the test file. |
| 22 | + * @param array<string, int|string> $expectedTokens The expected token codes for the scope opener/closer. |
| 23 | + * @param string|null $testCloserMarker Optional. The comment which prefaces the scope closer if different |
| 24 | + * from the test marker. |
| 25 | + * |
| 26 | + * @link https://github.com/squizlabs/PHP_CodeSniffer/issues/497#ref-commit-b24b96b |
| 27 | + * |
| 28 | + * @dataProvider dataSwitchScope |
| 29 | + * @covers \PHP_CodeSniffer\Tokenizers\Tokenizer::recurseScopeMap |
| 30 | + * |
| 31 | + * @return void |
| 32 | + */ |
| 33 | + public function testSwitchScope($testMarker, $expectedTokens, $testCloserMarker=null) |
| 34 | + { |
| 35 | + $tokens = $this->phpcsFile->getTokens(); |
| 36 | + $switchIndex = $this->getTargetToken($testMarker, [T_SWITCH]); |
| 37 | + $tokenArray = $tokens[$switchIndex]; |
| 38 | + |
| 39 | + $scopeCloserMarker = $testMarker; |
| 40 | + if (isset($testCloserMarker) === true) { |
| 41 | + $scopeCloserMarker = $testCloserMarker; |
| 42 | + } |
| 43 | + |
| 44 | + $expectedScopeCondition = $switchIndex; |
| 45 | + $expectedScopeOpener = $this->getTargetToken($testMarker, $expectedTokens['scope_opener']); |
| 46 | + $expectedScopeCloser = $this->getTargetToken($scopeCloserMarker, $expectedTokens['scope_closer']); |
| 47 | + |
| 48 | + $this->assertArrayHasKey('scope_condition', $tokenArray, 'Scope condition not set'); |
| 49 | + $this->assertSame( |
| 50 | + $expectedScopeCondition, |
| 51 | + $tokenArray['scope_condition'], |
| 52 | + sprintf( |
| 53 | + 'Scope condition not set correctly; expected T_SWITCH, found %s', |
| 54 | + $tokens[$tokenArray['scope_condition']]['type'] |
| 55 | + ) |
| 56 | + ); |
| 57 | + |
| 58 | + $this->assertArrayHasKey('scope_opener', $tokenArray, 'Scope opener not set'); |
| 59 | + $this->assertSame( |
| 60 | + $expectedScopeOpener, |
| 61 | + $tokenArray['scope_opener'], |
| 62 | + sprintf( |
| 63 | + 'Scope opener not set correctly; expected %s, found %s', |
| 64 | + $tokens[$expectedScopeOpener]['type'], |
| 65 | + $tokens[$tokenArray['scope_opener']]['type'] |
| 66 | + ) |
| 67 | + ); |
| 68 | + |
| 69 | + $this->assertArrayHasKey('scope_closer', $tokenArray, 'Scope closer not set'); |
| 70 | + $this->assertSame( |
| 71 | + $expectedScopeCloser, |
| 72 | + $tokenArray['scope_closer'], |
| 73 | + sprintf( |
| 74 | + 'Scope closer not set correctly; expected %s, found %s', |
| 75 | + $tokens[$expectedScopeCloser]['type'], |
| 76 | + $tokens[$tokenArray['scope_closer']]['type'] |
| 77 | + ) |
| 78 | + ); |
| 79 | + |
| 80 | + }//end testSwitchScope() |
| 81 | + |
| 82 | + |
| 83 | + /** |
| 84 | + * Data provider. |
| 85 | + * |
| 86 | + * @see testSwitchScope() |
| 87 | + * |
| 88 | + * @return array<string, array<string, string|array<string, int|string>>> |
| 89 | + */ |
| 90 | + public static function dataSwitchScope() |
| 91 | + { |
| 92 | + return [ |
| 93 | + 'switch normal syntax' => [ |
| 94 | + 'testMarker' => '/* testSwitchNormalSyntax */', |
| 95 | + 'expectedTokens' => [ |
| 96 | + 'scope_opener' => T_OPEN_CURLY_BRACKET, |
| 97 | + 'scope_closer' => T_CLOSE_CURLY_BRACKET, |
| 98 | + ], |
| 99 | + ], |
| 100 | + 'switch alternative syntax' => [ |
| 101 | + 'testMarker' => '/* testSwitchAlternativeSyntax */', |
| 102 | + 'expectedTokens' => [ |
| 103 | + 'scope_opener' => T_COLON, |
| 104 | + 'scope_closer' => T_ENDSWITCH, |
| 105 | + ], |
| 106 | + 'testCloserMarker' => '/* testSwitchAlternativeSyntaxEnd */', |
| 107 | + ], |
| 108 | + ]; |
| 109 | + |
| 110 | + }//end dataSwitchScope() |
| 111 | + |
| 112 | + |
| 113 | +}//end class |
0 commit comments