Skip to content

Commit 7f39f60

Browse files
authored
Merge pull request #655 from PHPCSStandards/feature/test-util-tokens-class
Util\Tokens: add tests
2 parents 0b81673 + 7bdd340 commit 7f39f60

File tree

3 files changed

+247
-2
lines changed

3 files changed

+247
-2
lines changed

src/Util/Tokens.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -777,12 +777,14 @@ public static function tokenName($token)
777777
* For example T_CLASS tokens appear very infrequently in a file, and
778778
* therefore have a high weighting.
779779
*
780-
* Returns false if there are no weightings for any of the specified tokens.
780+
* If there are no weightings for any of the specified tokens, the first token
781+
* seen in the passed array will be returned.
781782
*
782783
* @param array<int|string> $tokens The token types to get the highest weighted
783784
* type for.
784785
*
785-
* @return int|false The highest weighted token.
786+
* @return int The highest weighted token.
787+
* On equal "weight", returns the first token of that particular weight.
786788
*/
787789
public static function getHighestWeightedToken(array $tokens)
788790
{
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
<?php
2+
/**
3+
* Tests for the \PHP_CodeSniffer\Util\Tokens::getHighestWeightedToken() method.
4+
*
5+
* @author Juliette Reinders Folmer <[email protected]>
6+
* @copyright 2024 PHPCSStandards and contributors
7+
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8+
*/
9+
10+
namespace PHP_CodeSniffer\Tests\Core\Util\Tokens;
11+
12+
use PHP_CodeSniffer\Util\Tokens;
13+
use PHPUnit\Framework\TestCase;
14+
15+
/**
16+
* Tests for the \PHP_CodeSniffer\Util\Tokens::getHighestWeightedToken() method.
17+
*
18+
* @covers \PHP_CodeSniffer\Util\Tokens::getHighestWeightedToken
19+
*/
20+
final class GetHighestWeightedTokenTest extends TestCase
21+
{
22+
23+
24+
/**
25+
* Test the method.
26+
*
27+
* @param array<int|string> $tokens The tokens to find the heighest weighted one.
28+
* @param int|false $expected The expected function return value.
29+
*
30+
* @dataProvider dataGetHighestWeightedToken
31+
*
32+
* @return void
33+
*/
34+
public function testGetHighestWeightedToken($tokens, $expected)
35+
{
36+
$this->assertSame($expected, Tokens::getHighestWeightedToken($tokens));
37+
38+
}//end testGetHighestWeightedToken()
39+
40+
41+
/**
42+
* Data provider.
43+
*
44+
* @return array<string, array<string, int|false|array<int|string>>
45+
*/
46+
public static function dataGetHighestWeightedToken()
47+
{
48+
$data = [
49+
'Array of non-tokens passed, returns first' => [
50+
'tokens' => [
51+
PHP_SAPI,
52+
PHP_MAJOR_VERSION,
53+
PHP_OS,
54+
],
55+
'expected' => PHP_SAPI,
56+
],
57+
'No weightings available for any of the selected tokens, first one wins' => [
58+
'tokens' => [
59+
T_VARIABLE,
60+
T_STRING,
61+
T_EXTENDS,
62+
T_IMPLEMENTS,
63+
],
64+
'expected' => T_VARIABLE,
65+
],
66+
'single token always returns that token' => [
67+
'tokens' => [T_VARIABLE],
68+
'expected' => T_VARIABLE,
69+
],
70+
'Unknown and known token, known token wins' => [
71+
'tokens' => [
72+
T_VARIABLE,
73+
T_SELF,
74+
],
75+
'expected' => T_SELF,
76+
],
77+
'Known and unknown token, known token wins' => [
78+
'tokens' => [
79+
T_CLOSURE,
80+
T_STRING,
81+
],
82+
'expected' => T_CLOSURE,
83+
],
84+
'Two tokens with equal weights passed, first one wins' => [
85+
'tokens' => [
86+
T_CLOSURE,
87+
T_FUNCTION,
88+
],
89+
'expected' => T_CLOSURE,
90+
],
91+
'Five tokens with equal weights passed, first one wins' => [
92+
'tokens' => [
93+
T_NAMESPACE,
94+
T_TRAIT,
95+
T_ENUM,
96+
T_CLASS,
97+
T_INTERFACE,
98+
],
99+
'expected' => T_NAMESPACE,
100+
],
101+
'Tokens with different weights passed, heightest (25) wins' => [
102+
'tokens' => [
103+
T_BITWISE_OR,
104+
T_SELF,
105+
T_MUL_EQUAL,
106+
],
107+
'expected' => T_SELF,
108+
],
109+
'Tokens with different weights passed, heightest (50) wins' => [
110+
'tokens' => [
111+
T_BITWISE_XOR,
112+
T_CATCH,
113+
T_SPACESHIP,
114+
T_PARENT,
115+
],
116+
'expected' => T_CATCH,
117+
],
118+
];
119+
120+
$high100 = [
121+
T_MULTIPLY,
122+
T_BITWISE_AND,
123+
T_SELF,
124+
T_FOREACH,
125+
T_CLOSURE,
126+
];
127+
$data['Tokens with different weights passed, ordered low-high, heightest (100) wins'] = [
128+
'tokens' => $high100,
129+
'expected' => T_CLOSURE,
130+
];
131+
132+
shuffle($high100);
133+
$data['Tokens with different weights passed, order random, heightest (100) wins'] = [
134+
'tokens' => $high100,
135+
'expected' => T_CLOSURE,
136+
];
137+
138+
$high1000 = [
139+
T_ENUM,
140+
T_FUNCTION,
141+
T_ELSEIF,
142+
T_PARENT,
143+
T_BITWISE_OR,
144+
T_MODULUS,
145+
];
146+
$data['Tokens with different weights passed, ordered low-high, heightest (1000) wins'] = [
147+
'tokens' => $high1000,
148+
'expected' => T_ENUM,
149+
];
150+
151+
shuffle($high1000);
152+
$data['Tokens with different weights passed, order random, heightest (1000) wins'] = [
153+
'tokens' => $high1000,
154+
'expected' => T_ENUM,
155+
];
156+
157+
return $data;
158+
159+
}//end dataGetHighestWeightedToken()
160+
161+
162+
}//end class
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
/**
3+
* Tests for the \PHP_CodeSniffer\Util\Tokens::tokenName() method.
4+
*
5+
* @author Juliette Reinders Folmer <[email protected]>
6+
* @copyright 2024 PHPCSStandards and contributors
7+
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8+
*/
9+
10+
namespace PHP_CodeSniffer\Tests\Core\Util\Tokens;
11+
12+
use PHP_CodeSniffer\Util\Tokens;
13+
use PHPUnit\Framework\TestCase;
14+
15+
/**
16+
* Tests for the \PHP_CodeSniffer\Util\Tokens::tokenName() method.
17+
*
18+
* @covers \PHP_CodeSniffer\Util\Tokens::tokenName
19+
*/
20+
final class TokenNameTest extends TestCase
21+
{
22+
23+
24+
/**
25+
* Test the method.
26+
*
27+
* @param int|string $tokenCode The PHP/PHPCS token code to get the name for.
28+
* @param string $expected The expected token name.
29+
*
30+
* @dataProvider dataTokenName
31+
*
32+
* @return void
33+
*/
34+
public function testTokenName($tokenCode, $expected)
35+
{
36+
$this->assertSame($expected, Tokens::tokenName($tokenCode));
37+
38+
}//end testTokenName()
39+
40+
41+
/**
42+
* Data provider.
43+
*
44+
* @return array<string, array<string, int|string>>
45+
*/
46+
public static function dataTokenName()
47+
{
48+
return [
49+
'PHP native token: T_ECHO' => [
50+
'tokenCode' => T_ECHO,
51+
'expected' => 'T_ECHO',
52+
],
53+
'PHP native token: T_FUNCTION' => [
54+
'tokenCode' => T_FUNCTION,
55+
'expected' => 'T_FUNCTION',
56+
],
57+
'PHPCS native token: T_CLOSURE' => [
58+
'tokenCode' => T_CLOSURE,
59+
'expected' => 'T_CLOSURE',
60+
],
61+
'PHPCS native token: T_STRING_CONCAT' => [
62+
'tokenCode' => T_STRING_CONCAT,
63+
'expected' => 'T_STRING_CONCAT',
64+
],
65+
66+
// Document the current behaviour for invalid input.
67+
// This behaviour is subject to change.
68+
'Non-token integer passed' => [
69+
'tokenCode' => 100000,
70+
'expected' => 'UNKNOWN',
71+
],
72+
'Non-token string passed' => [
73+
'tokenCode' => 'something',
74+
'expected' => 'ing',
75+
],
76+
];
77+
78+
}//end dataTokenName()
79+
80+
81+
}//end class

0 commit comments

Comments
 (0)