|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * PHPCSUtils, utility functions and classes for PHP_CodeSniffer sniff developers. |
| 4 | + * |
| 5 | + * @package PHPCSUtils |
| 6 | + * @copyright 2019-2020 PHPCSUtils Contributors |
| 7 | + * @license https://opensource.org/licenses/LGPL-3.0 LGPL3 |
| 8 | + * @link https://github.com/PHPCSStandards/PHPCSUtils |
| 9 | + */ |
| 10 | + |
| 11 | +namespace PHPCSUtils\Tests\Tokens\TokenHelper; |
| 12 | + |
| 13 | +use PHPCSUtils\BackCompat\Helper; |
| 14 | +use PHPCSUtils\Tokens\TokenHelper; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | + |
| 17 | +/** |
| 18 | + * Test class. |
| 19 | + * |
| 20 | + * @covers \PHPCSUtils\Tokens\TokenHelper::tokenExists |
| 21 | + * |
| 22 | + * @since 1.0.0 |
| 23 | + */ |
| 24 | +class TokenExistsTest extends TestCase |
| 25 | +{ |
| 26 | + |
| 27 | + /** |
| 28 | + * Add select constants to allow testing this method. |
| 29 | + * |
| 30 | + * @beforeClass |
| 31 | + * |
| 32 | + * @return void |
| 33 | + */ |
| 34 | + public static function setUpConstants() |
| 35 | + { |
| 36 | + \define('T_FAKETOKEN', -5); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Test the method. |
| 41 | + * |
| 42 | + * @dataProvider dataTokenExists |
| 43 | + * |
| 44 | + * @param string $name Token name. |
| 45 | + * @param bool $expected Expected function return value. |
| 46 | + * |
| 47 | + * @return void |
| 48 | + */ |
| 49 | + public function testTokenExists($name, $expected) |
| 50 | + { |
| 51 | + $this->assertSame($expected, TokenHelper::tokenExists($name)); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Data provider. |
| 56 | + * |
| 57 | + * @return array |
| 58 | + */ |
| 59 | + public function dataTokenExists() |
| 60 | + { |
| 61 | + $phpcsVersion = Helper::getVersion(); |
| 62 | + |
| 63 | + return [ |
| 64 | + 'Token which doesn\'t exist either way' => [ |
| 65 | + 'name' => 'T_DOESNOTEXIST', |
| 66 | + 'expected' => false, |
| 67 | + ], |
| 68 | + 'PHP native token which always exists' => [ |
| 69 | + 'name' => 'T_FUNCTION', |
| 70 | + 'expected' => true, |
| 71 | + ], |
| 72 | + 'PHPCS native token which always exists (in the PHPCS versions supported)' => [ |
| 73 | + 'name' => 'T_CLOSURE', |
| 74 | + 'expected' => true, |
| 75 | + ], |
| 76 | + 'PHP native token which may not exist - PHP 7.4 T_FN' => [ |
| 77 | + 'name' => 'T_FN', |
| 78 | + 'expected' => (\version_compare($phpcsVersion, '3.5.3', '>=') |
| 79 | + || \version_compare(\PHP_VERSION_ID, '70399', '>=')), |
| 80 | + ], |
| 81 | + 'PHP native token which may not exist - PHP 8.0 T_NULLSAFE_OBJECT_OPERATOR' => [ |
| 82 | + 'name' => 'T_NULLSAFE_OBJECT_OPERATOR', |
| 83 | + 'expected' => (\version_compare($phpcsVersion, '3.5.7', '>=') |
| 84 | + || \version_compare(\PHP_VERSION_ID, '79999', '>=')), |
| 85 | + ], |
| 86 | + 'PHP native token which may not exist - PHP 8.1 T_ENUM' => [ |
| 87 | + 'name' => 'T_ENUM', |
| 88 | + 'expected' => (\version_compare($phpcsVersion, '3.7.0', '>=') |
| 89 | + || \version_compare(\PHP_VERSION_ID, '80099', '>=')), |
| 90 | + ], |
| 91 | + 'Mocked polyfilled token' => [ |
| 92 | + 'name' => 'T_FAKETOKEN', |
| 93 | + 'expected' => false, |
| 94 | + ], |
| 95 | + ]; |
| 96 | + } |
| 97 | +} |
0 commit comments