|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Tests converting enum "case" to T_ENUM_CASE. |
| 4 | + * |
| 5 | + * @author Jaroslav Hanslík <[email protected]> |
| 6 | + * @copyright 2021 Squiz Pty Ltd (ABN 77 084 670 600) |
| 7 | + * @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence |
| 8 | + */ |
| 9 | + |
| 10 | +namespace PHP_CodeSniffer\Tests\Core\Tokenizer\Tokenizer; |
| 11 | + |
| 12 | +use PHP_CodeSniffer\Tests\Core\Tokenizer\AbstractTokenizerTestCase; |
| 13 | + |
| 14 | +final class RecurseScopeMapCaseKeywordConditionsTest extends AbstractTokenizerTestCase |
| 15 | +{ |
| 16 | + |
| 17 | + |
| 18 | + /** |
| 19 | + * Test that the enum "case" is converted to T_ENUM_CASE. |
| 20 | + * |
| 21 | + * @param string $testMarker The comment which prefaces the target token in the test file. |
| 22 | + * |
| 23 | + * @dataProvider dataEnumCases |
| 24 | + * @covers PHP_CodeSniffer\Tokenizers\Tokenizer::recurseScopeMap |
| 25 | + * |
| 26 | + * @return void |
| 27 | + */ |
| 28 | + public function testEnumCases($testMarker) |
| 29 | + { |
| 30 | + $tokens = $this->phpcsFile->getTokens(); |
| 31 | + $enumCase = $this->getTargetToken($testMarker, [T_ENUM_CASE, T_CASE]); |
| 32 | + $tokenArray = $tokens[$enumCase]; |
| 33 | + |
| 34 | + // Make sure we're looking at the right token. |
| 35 | + $this->assertSame(T_ENUM_CASE, $tokenArray['code'], 'Token tokenized as '.$tokenArray['type'].', not T_ENUM_CASE (code)'); |
| 36 | + |
| 37 | + $this->assertArrayNotHasKey('scope_condition', $tokenArray, 'Scope condition is set'); |
| 38 | + $this->assertArrayNotHasKey('scope_opener', $tokenArray, 'Scope opener is set'); |
| 39 | + $this->assertArrayNotHasKey('scope_closer', $tokenArray, 'Scope closer is set'); |
| 40 | + |
| 41 | + }//end testEnumCases() |
| 42 | + |
| 43 | + |
| 44 | + /** |
| 45 | + * Data provider. |
| 46 | + * |
| 47 | + * @see testEnumCases() |
| 48 | + * |
| 49 | + * @return array<string, array<string>> |
| 50 | + */ |
| 51 | + public static function dataEnumCases() |
| 52 | + { |
| 53 | + return [ |
| 54 | + 'enum case, no value' => ['/* testPureEnumCase */'], |
| 55 | + 'enum case, integer value' => ['/* testBackingIntegerEnumCase */'], |
| 56 | + 'enum case, string value' => ['/* testBackingStringEnumCase */'], |
| 57 | + 'enum case, integer value in more complex enum' => ['/* testEnumCaseInComplexEnum */'], |
| 58 | + 'enum case, keyword in mixed case' => ['/* testEnumCaseIsCaseInsensitive */'], |
| 59 | + 'enum case, after switch statement' => ['/* testEnumCaseAfterSwitch */'], |
| 60 | + 'enum case, after switch statement using alternative syntax' => ['/* testEnumCaseAfterSwitchWithEndSwitch */'], |
| 61 | + ]; |
| 62 | + |
| 63 | + }//end dataEnumCases() |
| 64 | + |
| 65 | + |
| 66 | + /** |
| 67 | + * Test that "case" that is not enum case is still tokenized as `T_CASE`. |
| 68 | + * |
| 69 | + * @param string $testMarker The comment which prefaces the target token in the test file. |
| 70 | + * |
| 71 | + * @dataProvider dataNotEnumCases |
| 72 | + * @covers PHP_CodeSniffer\Tokenizers\Tokenizer::recurseScopeMap |
| 73 | + * |
| 74 | + * @return void |
| 75 | + */ |
| 76 | + public function testNotEnumCases($testMarker) |
| 77 | + { |
| 78 | + $tokens = $this->phpcsFile->getTokens(); |
| 79 | + $case = $this->getTargetToken($testMarker, [T_ENUM_CASE, T_CASE]); |
| 80 | + $tokenArray = $tokens[$case]; |
| 81 | + |
| 82 | + // Make sure we're looking at the right token. |
| 83 | + $this->assertSame(T_CASE, $tokenArray['code'], 'Token tokenized as '.$tokenArray['type'].', not T_CASE (code)'); |
| 84 | + |
| 85 | + $this->assertArrayHasKey('scope_condition', $tokenArray, 'Scope condition is not set'); |
| 86 | + $this->assertArrayHasKey('scope_opener', $tokenArray, 'Scope opener is not set'); |
| 87 | + $this->assertArrayHasKey('scope_closer', $tokenArray, 'Scope closer is not set'); |
| 88 | + |
| 89 | + }//end testNotEnumCases() |
| 90 | + |
| 91 | + |
| 92 | + /** |
| 93 | + * Data provider. |
| 94 | + * |
| 95 | + * @see testNotEnumCases() |
| 96 | + * |
| 97 | + * @return array<string, array<string>> |
| 98 | + */ |
| 99 | + public static function dataNotEnumCases() |
| 100 | + { |
| 101 | + return [ |
| 102 | + 'switch case with constant, semicolon condition end' => ['/* testCaseWithSemicolonIsNotEnumCase */'], |
| 103 | + 'switch case with constant, colon condition end' => ['/* testCaseWithConstantIsNotEnumCase */'], |
| 104 | + 'switch case with constant, comparison' => ['/* testCaseWithConstantAndIdenticalIsNotEnumCase */'], |
| 105 | + 'switch case with constant, assignment' => ['/* testCaseWithAssigmentToConstantIsNotEnumCase */'], |
| 106 | + 'switch case with constant, keyword in mixed case' => ['/* testIsNotEnumCaseIsCaseInsensitive */'], |
| 107 | + 'switch case, body in curlies declares enum' => ['/* testCaseInSwitchWhenCreatingEnumInSwitch1 */'], |
| 108 | + 'switch case, body after semicolon declares enum' => ['/* testCaseInSwitchWhenCreatingEnumInSwitch2 */'], |
| 109 | + ]; |
| 110 | + |
| 111 | + }//end dataNotEnumCases() |
| 112 | + |
| 113 | + |
| 114 | + /** |
| 115 | + * Test that "case" that is not enum case is still tokenized as `T_CASE`. |
| 116 | + * |
| 117 | + * @param string $testMarker The comment which prefaces the target token in the test file. |
| 118 | + * |
| 119 | + * @dataProvider dataKeywordAsEnumCaseNameShouldBeString |
| 120 | + * @covers PHP_CodeSniffer\Tokenizers\Tokenizer::recurseScopeMap |
| 121 | + * |
| 122 | + * @return void |
| 123 | + */ |
| 124 | + public function testKeywordAsEnumCaseNameShouldBeString($testMarker) |
| 125 | + { |
| 126 | + $tokens = $this->phpcsFile->getTokens(); |
| 127 | + $enumCaseName = $this->getTargetToken($testMarker, [T_STRING, T_INTERFACE, T_TRAIT, T_ENUM, T_FUNCTION, T_FALSE, T_DEFAULT, T_ARRAY]); |
| 128 | + $tokenArray = $tokens[$enumCaseName]; |
| 129 | + |
| 130 | + // Make sure we're looking at the right token. |
| 131 | + $this->assertSame(T_STRING, $tokenArray['code'], 'Token tokenized as '.$tokenArray['type'].', not T_STRING (code)'); |
| 132 | + |
| 133 | + $this->assertArrayNotHasKey('scope_condition', $tokenArray, 'Scope condition is set'); |
| 134 | + $this->assertArrayNotHasKey('scope_opener', $tokenArray, 'Scope opener is set'); |
| 135 | + $this->assertArrayNotHasKey('scope_closer', $tokenArray, 'Scope closer is set'); |
| 136 | + |
| 137 | + }//end testKeywordAsEnumCaseNameShouldBeString() |
| 138 | + |
| 139 | + |
| 140 | + /** |
| 141 | + * Data provider. |
| 142 | + * |
| 143 | + * @see testKeywordAsEnumCaseNameShouldBeString() |
| 144 | + * |
| 145 | + * @return array<string, array<string>> |
| 146 | + */ |
| 147 | + public static function dataKeywordAsEnumCaseNameShouldBeString() |
| 148 | + { |
| 149 | + return [ |
| 150 | + '"interface" as case name' => ['/* testKeywordAsEnumCaseNameShouldBeString1 */'], |
| 151 | + '"trait" as case name' => ['/* testKeywordAsEnumCaseNameShouldBeString2 */'], |
| 152 | + '"enum" as case name' => ['/* testKeywordAsEnumCaseNameShouldBeString3 */'], |
| 153 | + '"function" as case name' => ['/* testKeywordAsEnumCaseNameShouldBeString4 */'], |
| 154 | + '"false" as case name' => ['/* testKeywordAsEnumCaseNameShouldBeString5 */'], |
| 155 | + '"default" as case name' => ['/* testKeywordAsEnumCaseNameShouldBeString6 */'], |
| 156 | + '"array" as case name' => ['/* testKeywordAsEnumCaseNameShouldBeString7 */'], |
| 157 | + ]; |
| 158 | + |
| 159 | + }//end dataKeywordAsEnumCaseNameShouldBeString() |
| 160 | + |
| 161 | + |
| 162 | +}//end class |
0 commit comments