Skip to content

Commit 9011920

Browse files
authored
Merge pull request #217 from PHPCSStandards/feature/file-getdeclarationname-add-tests
File::getDeclarationName(): add tests
2 parents e3f58eb + 2feff6b commit 9011920

File tree

4 files changed

+508
-0
lines changed

4 files changed

+508
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/* testInvalidTokenPassed */
2+
print something;
3+
4+
var object =
5+
{
6+
/* testClosure */
7+
propertyName: function () {}
8+
}
9+
10+
/* testFunction */
11+
function functionName() {}
12+
13+
/* testClass */
14+
class ClassName
15+
{
16+
/* testMethod */
17+
methodName() {
18+
return false;
19+
}
20+
}
21+
22+
/* testFunctionUnicode */
23+
function π() {}
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
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
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
3+
/* testInvalidTokenPassed */
4+
echo MY_CONSTANT;
5+
6+
/* testClosure */
7+
$closure = function() {};
8+
9+
/* testAnonClassWithParens */
10+
$anonClass = new class() {};
11+
12+
/* testAnonClassWithParens2 */
13+
$class = new class() {
14+
private $property = 'test';
15+
public function test() {}
16+
};
17+
18+
/* testAnonClassWithoutParens */
19+
$anonClass = new class {};
20+
21+
/* testAnonClassExtendsWithoutParens */
22+
$class = new class extends SomeClass {
23+
private $property = 'test';
24+
public function test() {}
25+
};
26+
27+
/* testFunction */
28+
function functionName() {}
29+
30+
/* testFunctionReturnByRef */
31+
function & functionNameByRef() {}
32+
33+
/* testClass */
34+
abstract class ClassName {
35+
/* testMethod */
36+
public function methodName() {}
37+
38+
/* testAbstractMethod */
39+
abstract protected function abstractMethodName();
40+
41+
/* testMethodReturnByRef */
42+
private function &MethodNameByRef();
43+
}
44+
45+
/* testExtendedClass */
46+
class ExtendedClass extends Foo {}
47+
48+
/* testInterface */
49+
interface InterfaceName {}
50+
51+
/* testTrait */
52+
trait TraitName {
53+
/* testFunctionEndingWithNumber */
54+
function ValidNameEndingWithNumber5(){}
55+
}
56+
57+
/* testClassWithNumber */
58+
class ClassWith1Number implements SomeInterface {}
59+
60+
/* testInterfaceWithNumbers */
61+
interface InterfaceWith12345Numbers extends AnotherInterface {}
62+
63+
/* testClassWithCommentsAndNewLines */
64+
class /* comment */
65+
66+
// phpcs:ignore Standard.Cat.SniffName -- for reasons
67+
ClassWithCommentsAndNewLines {}
68+
69+
/* testFunctionFn */
70+
function fn() {}
71+
72+
/* testPureEnum */
73+
enum Foo
74+
{
75+
case SOME_CASE;
76+
}
77+
78+
/* testBackedEnumSpaceBetweenNameAndColon */
79+
enum Hoo : string
80+
{
81+
case ONE = 'one';
82+
case TWO = 'two';
83+
}
84+
85+
/* testBackedEnumNoSpaceBetweenNameAndColon */
86+
enum Suit: int implements Colorful, CardGame {}
87+
88+
/* testFunctionReturnByRefWithReservedKeywordEach */
89+
function &each() {}
90+
91+
/* testFunctionReturnByRefWithReservedKeywordParent */
92+
function &parent() {}
93+
94+
/* testFunctionReturnByRefWithReservedKeywordSelf */
95+
function &self() {}
96+
97+
/* testFunctionReturnByRefWithReservedKeywordStatic */
98+
function &static() {}
99+
100+
/* testLiveCoding */
101+
// Intentional parse error. This has to be the last test in the file.
102+
function // Comment.

0 commit comments

Comments
 (0)