1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
<?php
/**
* Tests for the \PHP_CodeSniffer\Util\Tokens::getHighestWeightedToken() method.
*
* @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
* @copyright 2024 PHPCSStandards and contributors
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/
namespace PHP_CodeSniffer\Tests\Core\Util\Tokens;
use PHP_CodeSniffer\Util\Tokens;
use PHPUnit\Framework\TestCase;
/**
* Tests for the \PHP_CodeSniffer\Util\Tokens::getHighestWeightedToken() method.
*
* @covers \PHP_CodeSniffer\Util\Tokens::getHighestWeightedToken
*/
final class GetHighestWeightedTokenTest extends TestCase
{
/**
* Test the method.
*
* @param array<int|string> $tokens The tokens to find the heighest weighted one.
* @param int|false $expected The expected function return value.
*
* @dataProvider dataGetHighestWeightedToken
*
* @return void
*/
public function testGetHighestWeightedToken($tokens, $expected)
{
$this->assertSame($expected, Tokens::getHighestWeightedToken($tokens));
}//end testGetHighestWeightedToken()
/**
* Data provider.
*
* @return array<string, array<string, int|false|array<int|string>>
*/
public static function dataGetHighestWeightedToken()
{
$data = [
'Array of non-tokens passed, returns first' => [
'tokens' => [
PHP_SAPI,
PHP_MAJOR_VERSION,
PHP_OS,
],
'expected' => PHP_SAPI,
],
'No weightings available for any of the selected tokens, first one wins' => [
'tokens' => [
T_VARIABLE,
T_STRING,
T_EXTENDS,
T_IMPLEMENTS,
],
'expected' => T_VARIABLE,
],
'single token always returns that token' => [
'tokens' => [T_VARIABLE],
'expected' => T_VARIABLE,
],
'Unknown and known token, known token wins' => [
'tokens' => [
T_VARIABLE,
T_SELF,
],
'expected' => T_SELF,
],
'Known and unknown token, known token wins' => [
'tokens' => [
T_CLOSURE,
T_STRING,
],
'expected' => T_CLOSURE,
],
'Two tokens with equal weights passed, first one wins' => [
'tokens' => [
T_CLOSURE,
T_FUNCTION,
],
'expected' => T_CLOSURE,
],
'Five tokens with equal weights passed, first one wins' => [
'tokens' => [
T_NAMESPACE,
T_TRAIT,
T_ENUM,
T_CLASS,
T_INTERFACE,
],
'expected' => T_NAMESPACE,
],
'Tokens with different weights passed, heightest (25) wins' => [
'tokens' => [
T_BITWISE_OR,
T_SELF,
T_MUL_EQUAL,
],
'expected' => T_SELF,
],
'Tokens with different weights passed, heightest (50) wins' => [
'tokens' => [
T_BITWISE_XOR,
T_CATCH,
T_SPACESHIP,
T_PARENT,
],
'expected' => T_CATCH,
],
];
$high100 = [
T_MULTIPLY,
T_BITWISE_AND,
T_SELF,
T_FOREACH,
T_CLOSURE,
];
$data['Tokens with different weights passed, ordered low-high, heightest (100) wins'] = [
'tokens' => $high100,
'expected' => T_CLOSURE,
];
shuffle($high100);
$data['Tokens with different weights passed, order random, heightest (100) wins'] = [
'tokens' => $high100,
'expected' => T_CLOSURE,
];
$high1000 = [
T_ENUM,
T_FUNCTION,
T_ELSEIF,
T_PARENT,
T_BITWISE_OR,
T_MODULUS,
];
$data['Tokens with different weights passed, ordered low-high, heightest (1000) wins'] = [
'tokens' => $high1000,
'expected' => T_ENUM,
];
shuffle($high1000);
$data['Tokens with different weights passed, order random, heightest (1000) wins'] = [
'tokens' => $high1000,
'expected' => T_ENUM,
];
return $data;
}//end dataGetHighestWeightedToken()
}//end class
|