|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Tests for the \PHP_CodeSniffer\Config --extensions argument. |
| 4 | + * |
| 5 | + * @copyright 2025 PHPCSStandards and contributors |
| 6 | + * @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence |
| 7 | + */ |
| 8 | + |
| 9 | +namespace PHP_CodeSniffer\Tests\Core\Config; |
| 10 | + |
| 11 | +use PHP_CodeSniffer\Tests\ConfigDouble; |
| 12 | +use PHPUnit\Framework\TestCase; |
| 13 | + |
| 14 | +/** |
| 15 | + * Tests for the \PHP_CodeSniffer\Config --extensions argument. |
| 16 | + * |
| 17 | + * @covers \PHP_CodeSniffer\Config::processLongArgument |
| 18 | + */ |
| 19 | +final class ExtensionsArgTest extends TestCase |
| 20 | +{ |
| 21 | + |
| 22 | + |
| 23 | + /** |
| 24 | + * Ensure that the extension property is set when the parameter is passed a valid value. |
| 25 | + * |
| 26 | + * @param string $passedValue Extensions as passed on the command line. |
| 27 | + * @param string $expected Expected value for the extensions property. |
| 28 | + * |
| 29 | + * @dataProvider dataValidExtensions |
| 30 | + * |
| 31 | + * @return void |
| 32 | + */ |
| 33 | + public function testValidExtensions($passedValue, $expected) |
| 34 | + { |
| 35 | + $config = new ConfigDouble(["--extensions=$passedValue"]); |
| 36 | + |
| 37 | + $this->assertSame($expected, $config->extensions); |
| 38 | + |
| 39 | + }//end testValidExtensions() |
| 40 | + |
| 41 | + |
| 42 | + /** |
| 43 | + * Data provider. |
| 44 | + * |
| 45 | + * @see self::testValidExtensions() |
| 46 | + * |
| 47 | + * @return array<int, array<string>> |
| 48 | + */ |
| 49 | + public static function dataValidExtensions() |
| 50 | + { |
| 51 | + return [ |
| 52 | + // Passing an empty extensions list is not useful, as it will result in no files being scanned, |
| 53 | + // but that's the responsibility of the user. |
| 54 | + 'Empty extensions list' => [ |
| 55 | + 'passedValue' => '', |
| 56 | + 'expected' => [], |
| 57 | + ], |
| 58 | + 'Single extension passed: php' => [ |
| 59 | + 'passedValue' => 'php', |
| 60 | + 'expected' => [ |
| 61 | + 'php' => 'PHP', |
| 62 | + ], |
| 63 | + ], |
| 64 | + // This would cause PHPCS to scan python files as PHP, which will probably cause very weird scan results, |
| 65 | + // but that's the responsibility of the user. |
| 66 | + 'Single extension passed: py' => [ |
| 67 | + 'passedValue' => 'py', |
| 68 | + 'expected' => [ |
| 69 | + 'py' => 'PHP', |
| 70 | + ], |
| 71 | + ], |
| 72 | + // This would likely result in a problem when PHPCS can't find a "PY" tokenizer class, |
| 73 | + // but that's not our concern at this moment. Support for non-PHP tokenizers is being dropped soon anyway. |
| 74 | + 'Single extension passed with language: py/py' => [ |
| 75 | + 'passedValue' => 'py/py', |
| 76 | + 'expected' => [ |
| 77 | + 'py' => 'PY', |
| 78 | + ], |
| 79 | + ], |
| 80 | + 'Multiple extensions passed: php,js,css' => [ |
| 81 | + 'passedValue' => 'php,js,css', |
| 82 | + 'expected' => [ |
| 83 | + 'php' => 'PHP', |
| 84 | + 'js' => 'JS', |
| 85 | + 'css' => 'CSS', |
| 86 | + ], |
| 87 | + ], |
| 88 | + 'Multiple extensions passed, some with language: php,inc/php,phpt/php,js' => [ |
| 89 | + 'passedValue' => 'php,inc/php,phpt/php,js', |
| 90 | + 'expected' => [ |
| 91 | + 'php' => 'PHP', |
| 92 | + 'inc' => 'PHP', |
| 93 | + 'phpt' => 'PHP', |
| 94 | + 'js' => 'JS', |
| 95 | + ], |
| 96 | + ], |
| 97 | + 'File extensions are set case sensitively (and filtering is case sensitive too)' => [ |
| 98 | + 'passedValue' => 'PHP,php', |
| 99 | + 'expected' => [ |
| 100 | + 'PHP' => 'PHP', |
| 101 | + 'php' => 'PHP', |
| 102 | + ], |
| 103 | + ], |
| 104 | + ]; |
| 105 | + |
| 106 | + }//end dataValidExtensions() |
| 107 | + |
| 108 | + |
| 109 | + /** |
| 110 | + * Ensure that only the first argument is processed and others are ignored. |
| 111 | + * |
| 112 | + * @return void |
| 113 | + */ |
| 114 | + public function testOnlySetOnce() |
| 115 | + { |
| 116 | + $config = new ConfigDouble( |
| 117 | + [ |
| 118 | + '--extensions=php', |
| 119 | + '--extensions=inc,module', |
| 120 | + ] |
| 121 | + ); |
| 122 | + |
| 123 | + $this->assertSame(['php' => 'PHP'], $config->extensions); |
| 124 | + |
| 125 | + }//end testOnlySetOnce() |
| 126 | + |
| 127 | + |
| 128 | +}//end class |
0 commit comments