|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Tests for the \PHP_CodeSniffer\Config --sniffs and --exclude arguments. |
| 4 | + * |
| 5 | + * @author Dan Wallis <[email protected]> |
| 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\Exceptions\DeepExitException; |
| 12 | +use PHP_CodeSniffer\Tests\ConfigDouble; |
| 13 | +use PHPUnit\Framework\TestCase; |
| 14 | + |
| 15 | +/** |
| 16 | + * Tests for the \PHP_CodeSniffer\Config --sniffs and --exclude arguments. |
| 17 | + * |
| 18 | + * @covers \PHP_CodeSniffer\Config::processLongArgument |
| 19 | + */ |
| 20 | +final class SniffsExcludeArgsTest extends TestCase |
| 21 | +{ |
| 22 | + |
| 23 | + |
| 24 | + /** |
| 25 | + * Ensure that the expected error message is returned for invalid arguments. |
| 26 | + * |
| 27 | + * @param string $argument 'sniffs' or 'exclude'. |
| 28 | + * @param string $value List of sniffs to include / exclude. |
| 29 | + * @param string $message Expected error message text. |
| 30 | + * |
| 31 | + * @return void |
| 32 | + * @dataProvider dataInvalidSniffs |
| 33 | + */ |
| 34 | + public function testInvalid($argument, $value, $message) |
| 35 | + { |
| 36 | + $this->expectException(DeepExitException::class); |
| 37 | + $this->expectExceptionMessage($message); |
| 38 | + |
| 39 | + new ConfigDouble(["--$argument=$value"]); |
| 40 | + |
| 41 | + }//end testInvalid() |
| 42 | + |
| 43 | + |
| 44 | + /** |
| 45 | + * Data provider for testInvalid(). |
| 46 | + * |
| 47 | + * @see self::testInvalid() |
| 48 | + * @return array<string, array<string, string>> |
| 49 | + */ |
| 50 | + public static function dataInvalidSniffs() |
| 51 | + { |
| 52 | + $arguments = [ |
| 53 | + 'sniffs', |
| 54 | + 'exclude', |
| 55 | + ]; |
| 56 | + $data = []; |
| 57 | + |
| 58 | + $messageTemplate = 'ERROR: The specified sniff code "%s" is invalid'.PHP_EOL.PHP_EOL; |
| 59 | + |
| 60 | + foreach ($arguments as $argument) { |
| 61 | + // An empty string is not a valid sniff. |
| 62 | + $data[$argument.'; empty string'] = [ |
| 63 | + 'argument' => $argument, |
| 64 | + 'value' => '', |
| 65 | + 'message' => sprintf($messageTemplate, ''), |
| 66 | + ]; |
| 67 | + |
| 68 | + // A standard is not a valid sniff. |
| 69 | + $data[$argument.'; standard'] = [ |
| 70 | + 'argument' => $argument, |
| 71 | + 'value' => 'Standard', |
| 72 | + 'message' => sprintf($messageTemplate, 'Standard'), |
| 73 | + ]; |
| 74 | + |
| 75 | + // A category is not a valid sniff. |
| 76 | + $data[$argument.'; category'] = [ |
| 77 | + 'argument' => $argument, |
| 78 | + 'value' => 'Standard.Category', |
| 79 | + 'message' => sprintf($messageTemplate, 'Standard.Category'), |
| 80 | + ]; |
| 81 | + |
| 82 | + // An error-code is not a valid sniff. |
| 83 | + $data[$argument.'; error-code'] = [ |
| 84 | + 'argument' => $argument, |
| 85 | + 'value' => 'Standard.Category', |
| 86 | + 'message' => sprintf($messageTemplate, 'Standard.Category'), |
| 87 | + ]; |
| 88 | + |
| 89 | + // Only the first error is reported. |
| 90 | + $data[$argument.'; two errors'] = [ |
| 91 | + 'argument' => $argument, |
| 92 | + 'value' => 'StandardOne,StandardTwo', |
| 93 | + 'message' => sprintf($messageTemplate, 'StandardOne'), |
| 94 | + ]; |
| 95 | + $data[$argument.'; valid followed by invalid'] = [ |
| 96 | + 'argument' => $argument, |
| 97 | + 'value' => 'StandardOne.Category.Sniff,StandardTwo.Category', |
| 98 | + 'message' => sprintf($messageTemplate, 'StandardTwo.Category'), |
| 99 | + ]; |
| 100 | + }//end foreach |
| 101 | + |
| 102 | + return $data; |
| 103 | + |
| 104 | + }//end dataInvalidSniffs() |
| 105 | + |
| 106 | + |
| 107 | + /** |
| 108 | + * Ensure that the valid data does not throw an exception, and the value is stored. |
| 109 | + * |
| 110 | + * @param string $argument 'sniffs' or 'exclude'. |
| 111 | + * @param string $value List of sniffs to include or exclude. |
| 112 | + * |
| 113 | + * @return void |
| 114 | + * @dataProvider dataValidSniffs |
| 115 | + */ |
| 116 | + public function testValid($argument, $value) |
| 117 | + { |
| 118 | + $config = new ConfigDouble(["--$argument=$value"]); |
| 119 | + |
| 120 | + $this->assertSame(explode(',', $value), $config->$argument); |
| 121 | + |
| 122 | + }//end testValid() |
| 123 | + |
| 124 | + |
| 125 | + /** |
| 126 | + * Data provider for testValid(). |
| 127 | + * |
| 128 | + * @see self::testValid() |
| 129 | + * @return array<string, array<string, string>> |
| 130 | + */ |
| 131 | + public static function dataValidSniffs() |
| 132 | + { |
| 133 | + $arguments = [ |
| 134 | + 'sniffs', |
| 135 | + 'exclude', |
| 136 | + ]; |
| 137 | + $data = []; |
| 138 | + |
| 139 | + foreach ($arguments as $argument) { |
| 140 | + $data[$argument.'; one valid sniff'] = [ |
| 141 | + 'argument' => $argument, |
| 142 | + 'value' => 'Standard.Category.Sniff', |
| 143 | + ]; |
| 144 | + $data[$argument.'; two valid sniffs'] = [ |
| 145 | + 'argument' => $argument, |
| 146 | + 'value' => 'StandardOne.Category.Sniff,StandardTwo.Category.Sniff', |
| 147 | + ]; |
| 148 | + } |
| 149 | + |
| 150 | + return $data; |
| 151 | + |
| 152 | + }//end dataValidSniffs() |
| 153 | + |
| 154 | + |
| 155 | + /** |
| 156 | + * Ensure that only the first argument is processed and others are ignored. |
| 157 | + * |
| 158 | + * @param string $argument 'sniffs' or 'exclude'. |
| 159 | + * |
| 160 | + * @return void |
| 161 | + * @dataProvider dataOnlySetOnce |
| 162 | + */ |
| 163 | + public function testOnlySetOnce($argument) |
| 164 | + { |
| 165 | + $config = new ConfigDouble( |
| 166 | + [ |
| 167 | + "--$argument=StandardOne.Category.Sniff", |
| 168 | + "--$argument=StandardTwo.Category.Sniff", |
| 169 | + "--$argument=Standard.AnotherCategory.Sniff", |
| 170 | + ] |
| 171 | + ); |
| 172 | + |
| 173 | + $this->assertSame(['StandardOne.Category.Sniff'], $config->$argument); |
| 174 | + |
| 175 | + }//end testOnlySetOnce() |
| 176 | + |
| 177 | + |
| 178 | + /** |
| 179 | + * Data provider for testOnlySetOnce(). |
| 180 | + * |
| 181 | + * @return array<string, array<string>> |
| 182 | + */ |
| 183 | + public static function dataOnlySetOnce() |
| 184 | + { |
| 185 | + return [ |
| 186 | + 'sniffs' => ['sniffs'], |
| 187 | + 'exclude' => ['exclude'], |
| 188 | + ]; |
| 189 | + |
| 190 | + }//end dataOnlySetOnce() |
| 191 | + |
| 192 | + |
| 193 | +}//end class |
0 commit comments