|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Tests the adding of the "parenthesis" keys to an anonymous class token. |
| 4 | + * |
| 5 | + * @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl> |
| 6 | + * @copyright 2019 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\PHP; |
| 11 | + |
| 12 | +use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest; |
| 13 | + |
| 14 | +class T_AnonClassParenthesisOwnerTest extends AbstractMethodUnitTest |
| 15 | +{ |
| 16 | + |
| 17 | + |
| 18 | + /** |
| 19 | + * Test that anonymous class tokens without parenthesis do not get assigned a parenthesis owner. |
| 20 | + * |
| 21 | + * @param string $testMarker The comment which prefaces the target token in the test file. |
| 22 | + * |
| 23 | + * @dataProvider dataAnonClassNoParentheses |
| 24 | + * @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional |
| 25 | + * |
| 26 | + * @return void |
| 27 | + */ |
| 28 | + public function testAnonClassNoParentheses($testMarker) |
| 29 | + { |
| 30 | + $tokens = self::$phpcsFile->getTokens(); |
| 31 | + |
| 32 | + $anonClass = $this->getTargetToken($testMarker, T_ANON_CLASS); |
| 33 | + $this->assertFalse(array_key_exists('parenthesis_owner', $tokens[$anonClass])); |
| 34 | + $this->assertFalse(array_key_exists('parenthesis_opener', $tokens[$anonClass])); |
| 35 | + $this->assertFalse(array_key_exists('parenthesis_closer', $tokens[$anonClass])); |
| 36 | + |
| 37 | + }//end testAnonClassNoParentheses() |
| 38 | + |
| 39 | + |
| 40 | + /** |
| 41 | + * Test that the next open/close parenthesis after an anonymous class without parenthesis |
| 42 | + * do not get assigned the anonymous class as a parenthesis owner. |
| 43 | + * |
| 44 | + * @param string $testMarker The comment which prefaces the target token in the test file. |
| 45 | + * |
| 46 | + * @dataProvider dataAnonClassNoParentheses |
| 47 | + * @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional |
| 48 | + * |
| 49 | + * @return void |
| 50 | + */ |
| 51 | + public function testAnonClassNoParenthesesNextOpenClose($testMarker) |
| 52 | + { |
| 53 | + $tokens = self::$phpcsFile->getTokens(); |
| 54 | + $function = $this->getTargetToken($testMarker, T_FUNCTION); |
| 55 | + |
| 56 | + $opener = $this->getTargetToken($testMarker, T_OPEN_PARENTHESIS); |
| 57 | + $this->assertTrue(array_key_exists('parenthesis_owner', $tokens[$opener])); |
| 58 | + $this->assertSame($function, $tokens[$opener]['parenthesis_owner']); |
| 59 | + |
| 60 | + $closer = $this->getTargetToken($testMarker, T_CLOSE_PARENTHESIS); |
| 61 | + $this->assertTrue(array_key_exists('parenthesis_owner', $tokens[$closer])); |
| 62 | + $this->assertSame($function, $tokens[$closer]['parenthesis_owner']); |
| 63 | + |
| 64 | + }//end testAnonClassNoParenthesesNextOpenClose() |
| 65 | + |
| 66 | + |
| 67 | + /** |
| 68 | + * Data provider. |
| 69 | + * |
| 70 | + * @see testAnonClassNoParentheses() |
| 71 | + * @see testAnonClassNoParenthesesNextOpenClose() |
| 72 | + * |
| 73 | + * @return array |
| 74 | + */ |
| 75 | + public function dataAnonClassNoParentheses() |
| 76 | + { |
| 77 | + return [ |
| 78 | + ['/* testNoParentheses */'], |
| 79 | + ['/* testNoParenthesesAndEmptyTokens */'], |
| 80 | + ]; |
| 81 | + |
| 82 | + }//end dataAnonClassNoParentheses() |
| 83 | + |
| 84 | + |
| 85 | + /** |
| 86 | + * Test that anonymous class tokens with parenthesis get assigned a parenthesis owner, |
| 87 | + * opener and closer; and that the opener/closer get the anonymous class assigned as owner. |
| 88 | + * |
| 89 | + * @param string $testMarker The comment which prefaces the target token in the test file. |
| 90 | + * |
| 91 | + * @dataProvider dataAnonClassWithParentheses |
| 92 | + * @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional |
| 93 | + * |
| 94 | + * @return void |
| 95 | + */ |
| 96 | + public function testAnonClassWithParentheses($testMarker) |
| 97 | + { |
| 98 | + $tokens = self::$phpcsFile->getTokens(); |
| 99 | + $anonClass = $this->getTargetToken($testMarker, T_ANON_CLASS); |
| 100 | + $opener = $this->getTargetToken($testMarker, T_OPEN_PARENTHESIS); |
| 101 | + $closer = $this->getTargetToken($testMarker, T_CLOSE_PARENTHESIS); |
| 102 | + |
| 103 | + $this->assertTrue(array_key_exists('parenthesis_owner', $tokens[$anonClass])); |
| 104 | + $this->assertTrue(array_key_exists('parenthesis_opener', $tokens[$anonClass])); |
| 105 | + $this->assertTrue(array_key_exists('parenthesis_closer', $tokens[$anonClass])); |
| 106 | + $this->assertSame($anonClass, $tokens[$anonClass]['parenthesis_owner']); |
| 107 | + $this->assertSame($opener, $tokens[$anonClass]['parenthesis_opener']); |
| 108 | + $this->assertSame($closer, $tokens[$anonClass]['parenthesis_closer']); |
| 109 | + |
| 110 | + $this->assertTrue(array_key_exists('parenthesis_owner', $tokens[$opener])); |
| 111 | + $this->assertTrue(array_key_exists('parenthesis_opener', $tokens[$opener])); |
| 112 | + $this->assertTrue(array_key_exists('parenthesis_closer', $tokens[$opener])); |
| 113 | + $this->assertSame($anonClass, $tokens[$opener]['parenthesis_owner']); |
| 114 | + $this->assertSame($opener, $tokens[$opener]['parenthesis_opener']); |
| 115 | + $this->assertSame($closer, $tokens[$opener]['parenthesis_closer']); |
| 116 | + |
| 117 | + $this->assertTrue(array_key_exists('parenthesis_owner', $tokens[$closer])); |
| 118 | + $this->assertTrue(array_key_exists('parenthesis_opener', $tokens[$closer])); |
| 119 | + $this->assertTrue(array_key_exists('parenthesis_closer', $tokens[$closer])); |
| 120 | + $this->assertSame($anonClass, $tokens[$closer]['parenthesis_owner']); |
| 121 | + $this->assertSame($opener, $tokens[$closer]['parenthesis_opener']); |
| 122 | + $this->assertSame($closer, $tokens[$closer]['parenthesis_closer']); |
| 123 | + |
| 124 | + }//end testAnonClassWithParentheses() |
| 125 | + |
| 126 | + |
| 127 | + /** |
| 128 | + * Data provider. |
| 129 | + * |
| 130 | + * @see testAnonClassWithParentheses() |
| 131 | + * |
| 132 | + * @return array |
| 133 | + */ |
| 134 | + public function dataAnonClassWithParentheses() |
| 135 | + { |
| 136 | + return [ |
| 137 | + ['/* testWithParentheses */'], |
| 138 | + ['/* testWithParenthesesAndEmptyTokens */'], |
| 139 | + ]; |
| 140 | + |
| 141 | + }//end dataAnonClassWithParentheses() |
| 142 | + |
| 143 | + |
| 144 | +}//end class |
0 commit comments