|
| 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/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 EnumCaseTest extends AbstractMethodUnitTest |
| 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\PHP::tokenize |
| 25 | + * @covers PHP_CodeSniffer\Tokenizers\Tokenizer::recurseScopeMap |
| 26 | + * |
| 27 | + * @return void |
| 28 | + */ |
| 29 | + public function testEnumCases($testMarker) |
| 30 | + { |
| 31 | + $tokens = self::$phpcsFile->getTokens(); |
| 32 | + |
| 33 | + $enumCase = $this->getTargetToken($testMarker, [T_ENUM_CASE, T_CASE]); |
| 34 | + |
| 35 | + $this->assertSame(T_ENUM_CASE, $tokens[$enumCase]['code']); |
| 36 | + $this->assertSame('T_ENUM_CASE', $tokens[$enumCase]['type']); |
| 37 | + |
| 38 | + $this->assertArrayNotHasKey('scope_condition', $tokens[$enumCase], 'Scope condition is set'); |
| 39 | + $this->assertArrayNotHasKey('scope_opener', $tokens[$enumCase], 'Scope opener is set'); |
| 40 | + $this->assertArrayNotHasKey('scope_closer', $tokens[$enumCase], 'Scope closer is set'); |
| 41 | + |
| 42 | + }//end testEnumCases() |
| 43 | + |
| 44 | + |
| 45 | + /** |
| 46 | + * Data provider. |
| 47 | + * |
| 48 | + * @see testEnumCases() |
| 49 | + * |
| 50 | + * @return array |
| 51 | + */ |
| 52 | + public function dataEnumCases() |
| 53 | + { |
| 54 | + return [ |
| 55 | + ['/* testPureEnumCase */'], |
| 56 | + ['/* testBackingIntegerEnumCase */'], |
| 57 | + ['/* testBackingStringEnumCase */'], |
| 58 | + ['/* testEnumCaseInComplexEnum */'], |
| 59 | + ['/* testEnumCaseIsCaseInsensitive */'], |
| 60 | + ['/* testEnumCaseAfterSwitch */'], |
| 61 | + ['/* testEnumCaseAfterSwitchWithEndSwitch */'], |
| 62 | + ]; |
| 63 | + |
| 64 | + }//end dataEnumCases() |
| 65 | + |
| 66 | + |
| 67 | + /** |
| 68 | + * Test that "case" that is not enum case is still tokenized as `T_CASE`. |
| 69 | + * |
| 70 | + * @param string $testMarker The comment which prefaces the target token in the test file. |
| 71 | + * |
| 72 | + * @dataProvider dataNotEnumCases |
| 73 | + * @covers PHP_CodeSniffer\Tokenizers\PHP::tokenize |
| 74 | + * @covers PHP_CodeSniffer\Tokenizers\Tokenizer::recurseScopeMap |
| 75 | + * |
| 76 | + * @return void |
| 77 | + */ |
| 78 | + public function testNotEnumCases($testMarker) |
| 79 | + { |
| 80 | + $tokens = self::$phpcsFile->getTokens(); |
| 81 | + |
| 82 | + $case = $this->getTargetToken($testMarker, [T_ENUM_CASE, T_CASE]); |
| 83 | + |
| 84 | + $this->assertSame(T_CASE, $tokens[$case]['code']); |
| 85 | + $this->assertSame('T_CASE', $tokens[$case]['type']); |
| 86 | + |
| 87 | + $this->assertArrayHasKey('scope_condition', $tokens[$case], 'Scope condition is not set'); |
| 88 | + $this->assertArrayHasKey('scope_opener', $tokens[$case], 'Scope opener is not set'); |
| 89 | + $this->assertArrayHasKey('scope_closer', $tokens[$case], 'Scope closer is not set'); |
| 90 | + |
| 91 | + }//end testNotEnumCases() |
| 92 | + |
| 93 | + |
| 94 | + /** |
| 95 | + * Data provider. |
| 96 | + * |
| 97 | + * @see testNotEnumCases() |
| 98 | + * |
| 99 | + * @return array |
| 100 | + */ |
| 101 | + public function dataNotEnumCases() |
| 102 | + { |
| 103 | + return [ |
| 104 | + ['/* testCaseWithSemicolonIsNotEnumCase */'], |
| 105 | + ['/* testCaseWithConstantIsNotEnumCase */'], |
| 106 | + ['/* testCaseWithConstantAndIdenticalIsNotEnumCase */'], |
| 107 | + ['/* testCaseWithAssigmentToConstantIsNotEnumCase */'], |
| 108 | + ['/* testIsNotEnumCaseIsCaseInsensitive */'], |
| 109 | + ['/* testCaseInSwitchWhenCreatingEnumInSwitch1 */'], |
| 110 | + ['/* testCaseInSwitchWhenCreatingEnumInSwitch2 */'], |
| 111 | + ]; |
| 112 | + |
| 113 | + }//end dataNotEnumCases() |
| 114 | + |
| 115 | + |
| 116 | + /** |
| 117 | + * Test that "case" that is not enum case is still tokenized as `T_CASE`. |
| 118 | + * |
| 119 | + * @param string $testMarker The comment which prefaces the target token in the test file. |
| 120 | + * |
| 121 | + * @dataProvider dataKeywordAsEnumCaseNameShouldBeString |
| 122 | + * @covers PHP_CodeSniffer\Tokenizers\PHP::tokenize |
| 123 | + * |
| 124 | + * @return void |
| 125 | + */ |
| 126 | + public function testKeywordAsEnumCaseNameShouldBeString($testMarker) |
| 127 | + { |
| 128 | + $tokens = self::$phpcsFile->getTokens(); |
| 129 | + |
| 130 | + $enumCaseName = $this->getTargetToken($testMarker, [T_STRING, T_INTERFACE, T_TRAIT, T_ENUM, T_FUNCTION, T_FALSE, T_DEFAULT, T_ARRAY]); |
| 131 | + |
| 132 | + $this->assertSame(T_STRING, $tokens[$enumCaseName]['code']); |
| 133 | + $this->assertSame('T_STRING', $tokens[$enumCaseName]['type']); |
| 134 | + |
| 135 | + }//end testKeywordAsEnumCaseNameShouldBeString() |
| 136 | + |
| 137 | + |
| 138 | + /** |
| 139 | + * Data provider. |
| 140 | + * |
| 141 | + * @see testKeywordAsEnumCaseNameShouldBeString() |
| 142 | + * |
| 143 | + * @return array |
| 144 | + */ |
| 145 | + public function dataKeywordAsEnumCaseNameShouldBeString() |
| 146 | + { |
| 147 | + return [ |
| 148 | + ['/* testKeywordAsEnumCaseNameShouldBeString1 */'], |
| 149 | + ['/* testKeywordAsEnumCaseNameShouldBeString2 */'], |
| 150 | + ['/* testKeywordAsEnumCaseNameShouldBeString3 */'], |
| 151 | + ['/* testKeywordAsEnumCaseNameShouldBeString4 */'], |
| 152 | + ]; |
| 153 | + |
| 154 | + }//end dataKeywordAsEnumCaseNameShouldBeString() |
| 155 | + |
| 156 | + |
| 157 | +}//end class |
0 commit comments