|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Tests for the \PHP_CodeSniffer\Files\File::getDeclarationName method. |
| 4 | + * |
| 5 | + * @author Juliette Reinders Folmer <[email protected]> |
| 6 | + * @copyright 2022-2024 PHPCSStandards Contributors |
| 7 | + * @license https://github.com/PHPCSStandards/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 | +/** |
| 15 | + * Tests for the \PHP_CodeSniffer\Files\File:getDeclarationName method. |
| 16 | + * |
| 17 | + * @covers \PHP_CodeSniffer\Files\File::getDeclarationName |
| 18 | + */ |
| 19 | +class GetDeclarationNameJSTest extends AbstractMethodUnitTest |
| 20 | +{ |
| 21 | + |
| 22 | + /** |
| 23 | + * The file extension of the test case file (without leading dot). |
| 24 | + * |
| 25 | + * @var string |
| 26 | + */ |
| 27 | + protected static $fileExtension = 'js'; |
| 28 | + |
| 29 | + |
| 30 | + /** |
| 31 | + * Test receiving an expected exception when a non-supported token is passed. |
| 32 | + * |
| 33 | + * @return void |
| 34 | + */ |
| 35 | + public function testInvalidTokenPassed() |
| 36 | + { |
| 37 | + $this->expectRunTimeException('Token type "T_STRING" is not T_FUNCTION, T_CLASS, T_INTERFACE, T_TRAIT or T_ENUM'); |
| 38 | + |
| 39 | + $target = $this->getTargetToken('/* testInvalidTokenPassed */', T_STRING); |
| 40 | + self::$phpcsFile->getDeclarationName($target); |
| 41 | + |
| 42 | + }//end testInvalidTokenPassed() |
| 43 | + |
| 44 | + |
| 45 | + /** |
| 46 | + * Test receiving "null" when passed an anonymous construct or in case of a parse error. |
| 47 | + * |
| 48 | + * @param string $testMarker The comment which prefaces the target token in the test file. |
| 49 | + * @param int|string $targetType Token type of the token to get as stackPtr. |
| 50 | + * |
| 51 | + * @dataProvider dataGetDeclarationNameNull |
| 52 | + * |
| 53 | + * @return void |
| 54 | + */ |
| 55 | + public function testGetDeclarationNameNull($testMarker, $targetType) |
| 56 | + { |
| 57 | + $target = $this->getTargetToken($testMarker, $targetType); |
| 58 | + $result = self::$phpcsFile->getDeclarationName($target); |
| 59 | + $this->assertNull($result); |
| 60 | + |
| 61 | + }//end testGetDeclarationNameNull() |
| 62 | + |
| 63 | + |
| 64 | + /** |
| 65 | + * Data provider. |
| 66 | + * |
| 67 | + * @see GetDeclarationNameTest::testGetDeclarationNameNull() |
| 68 | + * |
| 69 | + * @return array<string, array<string, int|string>> |
| 70 | + */ |
| 71 | + public static function dataGetDeclarationNameNull() |
| 72 | + { |
| 73 | + return [ |
| 74 | + 'closure' => [ |
| 75 | + 'testMarker' => '/* testClosure */', |
| 76 | + 'targetType' => T_CLOSURE, |
| 77 | + ], |
| 78 | + ]; |
| 79 | + |
| 80 | + }//end dataGetDeclarationNameNull() |
| 81 | + |
| 82 | + |
| 83 | + /** |
| 84 | + * Test retrieving the name of a function or OO structure. |
| 85 | + * |
| 86 | + * @param string $testMarker The comment which prefaces the target token in the test file. |
| 87 | + * @param string $expected Expected function output. |
| 88 | + * @param array<int|string>|null $targetType Token type of the token to get as stackPtr. |
| 89 | + * |
| 90 | + * @dataProvider dataGetDeclarationName |
| 91 | + * |
| 92 | + * @return void |
| 93 | + */ |
| 94 | + public function testGetDeclarationName($testMarker, $expected, $targetType=null) |
| 95 | + { |
| 96 | + if (isset($targetType) === false) { |
| 97 | + $targetType = [ |
| 98 | + T_CLASS, |
| 99 | + T_INTERFACE, |
| 100 | + T_TRAIT, |
| 101 | + T_ENUM, |
| 102 | + T_FUNCTION, |
| 103 | + ]; |
| 104 | + } |
| 105 | + |
| 106 | + $target = $this->getTargetToken($testMarker, $targetType); |
| 107 | + $result = self::$phpcsFile->getDeclarationName($target); |
| 108 | + $this->assertSame($expected, $result); |
| 109 | + |
| 110 | + }//end testGetDeclarationName() |
| 111 | + |
| 112 | + |
| 113 | + /** |
| 114 | + * Data provider. |
| 115 | + * |
| 116 | + * @see GetDeclarationNameTest::testGetDeclarationName() |
| 117 | + * |
| 118 | + * @return array<string, array<string, string|array<int|string>>> |
| 119 | + */ |
| 120 | + public static function dataGetDeclarationName() |
| 121 | + { |
| 122 | + return [ |
| 123 | + 'function' => [ |
| 124 | + 'testMarker' => '/* testFunction */', |
| 125 | + 'expected' => 'functionName', |
| 126 | + ], |
| 127 | + 'class' => [ |
| 128 | + 'testMarker' => '/* testClass */', |
| 129 | + 'expected' => 'ClassName', |
| 130 | + 'targetType' => [ |
| 131 | + T_CLASS, |
| 132 | + T_STRING, |
| 133 | + ], |
| 134 | + ], |
| 135 | + 'function-unicode-name' => [ |
| 136 | + 'testMarker' => '/* testFunctionUnicode */', |
| 137 | + 'expected' => 'π', |
| 138 | + ], |
| 139 | + ]; |
| 140 | + |
| 141 | + }//end dataGetDeclarationName() |
| 142 | + |
| 143 | + |
| 144 | + /** |
| 145 | + * Test retrieving the name of JS ES6 class method. |
| 146 | + * |
| 147 | + * @return void |
| 148 | + */ |
| 149 | + public function testGetDeclarationNameES6Method() |
| 150 | + { |
| 151 | + $target = $this->getTargetToken('/* testMethod */', [T_CLASS, T_INTERFACE, T_TRAIT, T_FUNCTION]); |
| 152 | + $result = self::$phpcsFile->getDeclarationName($target); |
| 153 | + $this->assertSame('methodName', $result); |
| 154 | + |
| 155 | + }//end testGetDeclarationNameES6Method() |
| 156 | + |
| 157 | + |
| 158 | +}//end class |
0 commit comments