|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Tests for the \PHP_CodeSniffer\Files\File:getClassProperties method. |
| 4 | + * |
| 5 | + * @author Juliette Reinders Folmer <[email protected]> |
| 6 | + * @copyright 2022 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\File; |
| 11 | + |
| 12 | +use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest; |
| 13 | + |
| 14 | +class GetClassPropertiesTest extends AbstractMethodUnitTest |
| 15 | +{ |
| 16 | + |
| 17 | + |
| 18 | + /** |
| 19 | + * Test receiving an expected exception when a non class token is passed. |
| 20 | + * |
| 21 | + * @param string $testMarker The comment which prefaces the target token in the test file. |
| 22 | + * @param array $tokenType The type of token to look for after the marker. |
| 23 | + * |
| 24 | + * @dataProvider dataNotAClassException |
| 25 | + * |
| 26 | + * @expectedException PHP_CodeSniffer\Exceptions\RuntimeException |
| 27 | + * @expectedExceptionMessage $stackPtr must be of type T_CLASS |
| 28 | + * |
| 29 | + * @return void |
| 30 | + */ |
| 31 | + public function testNotAClassException($testMarker, $tokenType) |
| 32 | + { |
| 33 | + $target = $this->getTargetToken($testMarker, $tokenType); |
| 34 | + self::$phpcsFile->getClassProperties($target); |
| 35 | + |
| 36 | + }//end testNotAClassException() |
| 37 | + |
| 38 | + |
| 39 | + /** |
| 40 | + * Data provider. |
| 41 | + * |
| 42 | + * @see testNotAClassException() For the array format. |
| 43 | + * |
| 44 | + * @return array |
| 45 | + */ |
| 46 | + public function dataNotAClassException() |
| 47 | + { |
| 48 | + return [ |
| 49 | + 'interface' => [ |
| 50 | + '/* testNotAClass */', |
| 51 | + \T_INTERFACE, |
| 52 | + ], |
| 53 | + 'anon-class' => [ |
| 54 | + '/* testAnonClass */', |
| 55 | + \T_ANON_CLASS, |
| 56 | + ], |
| 57 | + 'enum' => [ |
| 58 | + '/* testEnum */', |
| 59 | + \T_ENUM, |
| 60 | + ], |
| 61 | + ]; |
| 62 | + |
| 63 | + }//end dataNotAClassException() |
| 64 | + |
| 65 | + |
| 66 | + /** |
| 67 | + * Test retrieving the properties for a class declaration. |
| 68 | + * |
| 69 | + * @param string $testMarker The comment which prefaces the target token in the test file. |
| 70 | + * @param array $expected Expected function output. |
| 71 | + * |
| 72 | + * @dataProvider dataGetClassProperties |
| 73 | + * |
| 74 | + * @return void |
| 75 | + */ |
| 76 | + public function testGetClassProperties($testMarker, $expected) |
| 77 | + { |
| 78 | + $class = $this->getTargetToken($testMarker, \T_CLASS); |
| 79 | + $result = self::$phpcsFile->getClassProperties($class); |
| 80 | + $this->assertSame($expected, $result); |
| 81 | + |
| 82 | + }//end testGetClassProperties() |
| 83 | + |
| 84 | + |
| 85 | + /** |
| 86 | + * Data provider. |
| 87 | + * |
| 88 | + * @see testGetClassProperties() For the array format. |
| 89 | + * |
| 90 | + * @return array |
| 91 | + */ |
| 92 | + public function dataGetClassProperties() |
| 93 | + { |
| 94 | + return [ |
| 95 | + 'no-properties' => [ |
| 96 | + '/* testClassWithoutProperties */', |
| 97 | + [ |
| 98 | + 'is_abstract' => false, |
| 99 | + 'is_final' => false, |
| 100 | + 'is_readonly' => false, |
| 101 | + ], |
| 102 | + ], |
| 103 | + 'abstract' => [ |
| 104 | + '/* testAbstractClass */', |
| 105 | + [ |
| 106 | + 'is_abstract' => true, |
| 107 | + 'is_final' => false, |
| 108 | + 'is_readonly' => false, |
| 109 | + ], |
| 110 | + ], |
| 111 | + 'final' => [ |
| 112 | + '/* testFinalClass */', |
| 113 | + [ |
| 114 | + 'is_abstract' => false, |
| 115 | + 'is_final' => true, |
| 116 | + 'is_readonly' => false, |
| 117 | + ], |
| 118 | + ], |
| 119 | + 'readonly' => [ |
| 120 | + '/* testReadonlyClass */', |
| 121 | + [ |
| 122 | + 'is_abstract' => false, |
| 123 | + 'is_final' => false, |
| 124 | + 'is_readonly' => true, |
| 125 | + ], |
| 126 | + ], |
| 127 | + 'final-readonly' => [ |
| 128 | + '/* testFinalReadonlyClass */', |
| 129 | + [ |
| 130 | + 'is_abstract' => false, |
| 131 | + 'is_final' => true, |
| 132 | + 'is_readonly' => true, |
| 133 | + ], |
| 134 | + ], |
| 135 | + 'readonly-final' => [ |
| 136 | + '/* testReadonlyFinalClass */', |
| 137 | + [ |
| 138 | + 'is_abstract' => false, |
| 139 | + 'is_final' => true, |
| 140 | + 'is_readonly' => true, |
| 141 | + ], |
| 142 | + ], |
| 143 | + 'abstract-readonly' => [ |
| 144 | + '/* testAbstractReadonlyClass */', |
| 145 | + [ |
| 146 | + 'is_abstract' => true, |
| 147 | + 'is_final' => false, |
| 148 | + 'is_readonly' => true, |
| 149 | + ], |
| 150 | + ], |
| 151 | + 'readonly-abstract' => [ |
| 152 | + '/* testReadonlyAbstractClass */', |
| 153 | + [ |
| 154 | + 'is_abstract' => true, |
| 155 | + 'is_final' => false, |
| 156 | + 'is_readonly' => true, |
| 157 | + ], |
| 158 | + ], |
| 159 | + 'comments-and-new-lines' => [ |
| 160 | + '/* testWithCommentsAndNewLines */', |
| 161 | + [ |
| 162 | + 'is_abstract' => true, |
| 163 | + 'is_final' => false, |
| 164 | + 'is_readonly' => false, |
| 165 | + ], |
| 166 | + ], |
| 167 | + 'no-properties-with-docblock' => [ |
| 168 | + '/* testWithDocblockWithoutProperties */', |
| 169 | + [ |
| 170 | + 'is_abstract' => false, |
| 171 | + 'is_final' => false, |
| 172 | + 'is_readonly' => false, |
| 173 | + ], |
| 174 | + ], |
| 175 | + 'abstract-final-parse-error' => [ |
| 176 | + '/* testParseErrorAbstractFinal */', |
| 177 | + [ |
| 178 | + 'is_abstract' => true, |
| 179 | + 'is_final' => true, |
| 180 | + 'is_readonly' => false, |
| 181 | + ], |
| 182 | + ], |
| 183 | + ]; |
| 184 | + |
| 185 | + }//end dataGetClassProperties() |
| 186 | + |
| 187 | + |
| 188 | +}//end class |
0 commit comments