|
| 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 PHPUnit\Framework\TestCase; |
| 12 | +use PHP_CodeSniffer\Tests\ConfigDouble; |
| 13 | + |
| 14 | +/** |
| 15 | + * Tests for the \PHP_CodeSniffer\Config --sniffs and --exclude arguments. |
| 16 | + * |
| 17 | + * @covers \PHP_CodeSniffer\Config::processLongArgument |
| 18 | + */ |
| 19 | +final class SniffListTest extends TestCase |
| 20 | +{ |
| 21 | + |
| 22 | + |
| 23 | + /** |
| 24 | + * Ensure that the expected error message is returned for invalid arguments |
| 25 | + * |
| 26 | + * @param string $argument sniffs or exclude |
| 27 | + * @param string $value list of sniffs to include / exclude |
| 28 | + * @param string $message expected error message text |
| 29 | + * |
| 30 | + * @return void |
| 31 | + * @dataProvider dataInvalidSniffs |
| 32 | + */ |
| 33 | + public function testInvalid($argument, $value, $message) |
| 34 | + { |
| 35 | + $config = new ConfigDouble(); |
| 36 | + $exception = 'PHP_CodeSniffer\Exceptions\DeepExitException'; |
| 37 | + |
| 38 | + if (method_exists($this, 'expectException') === true) { |
| 39 | + // PHPUnit 5+. |
| 40 | + $this->expectException($exception); |
| 41 | + $this->expectExceptionMessage($message); |
| 42 | + } else { |
| 43 | + // PHPUnit 4. |
| 44 | + $this->setExpectedException($exception, $message); |
| 45 | + } |
| 46 | + |
| 47 | + $config->processLongArgument($argument.'='.$value, 0); |
| 48 | + |
| 49 | + }//end testInvalid() |
| 50 | + |
| 51 | + |
| 52 | + /** |
| 53 | + * Date provider for testInvalid() |
| 54 | + * |
| 55 | + * @see self::testInvalid() |
| 56 | + * @return array |
| 57 | + */ |
| 58 | + public static function dataInvalidSniffs() |
| 59 | + { |
| 60 | + $arguments = [ |
| 61 | + 'sniffs', |
| 62 | + 'exclude', |
| 63 | + ]; |
| 64 | + $result = []; |
| 65 | + |
| 66 | + $sniffs = []; |
| 67 | + |
| 68 | + $types = [ |
| 69 | + 'Standard', |
| 70 | + 'Standard.Category', |
| 71 | + 'Standard.Category.Sniff.Code', |
| 72 | + ]; |
| 73 | + foreach ($types as $value) { |
| 74 | + $sniffs[$value] = $value; |
| 75 | + $sniffs['Standard.Category.Sniff,B'.$value] = 'B'.$value; |
| 76 | + foreach ($types as $extra) { |
| 77 | + $sniffs['A'.$value.',B'.$extra] = 'A'.$value; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + $messageTemplate = 'ERROR: The specified sniff code "%s" is invalid'.PHP_EOL.PHP_EOL; |
| 82 | + foreach ($arguments as $argument) { |
| 83 | + foreach ($sniffs as $input => $output) { |
| 84 | + $result[] = [ |
| 85 | + 'argument' => $argument, |
| 86 | + 'value' => $input, |
| 87 | + 'message' => sprintf($messageTemplate, $output), |
| 88 | + ]; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + return $result; |
| 93 | + |
| 94 | + }//end dataInvalidSniffs() |
| 95 | + |
| 96 | + |
| 97 | + /** |
| 98 | + * Ensure that the valid data does not throw an exception |
| 99 | + * |
| 100 | + * @param string $argument sniffs or exclude |
| 101 | + * @param string $value list of sniffs to include or exclude |
| 102 | + * |
| 103 | + * @return void |
| 104 | + * @dataProvider dataValidSniffs |
| 105 | + */ |
| 106 | + public function testValid($argument, $value) |
| 107 | + { |
| 108 | + $config = new ConfigDouble(); |
| 109 | + $config->processLongArgument($argument.'='.$value, 0); |
| 110 | + |
| 111 | + }//end testValid() |
| 112 | + |
| 113 | + |
| 114 | + /** |
| 115 | + * Data provider for testValid() |
| 116 | + * |
| 117 | + * @see self::testValid() |
| 118 | + * @return array |
| 119 | + */ |
| 120 | + public static function dataValidSniffs() |
| 121 | + { |
| 122 | + $arguments = [ |
| 123 | + 'sniffs', |
| 124 | + 'exclude', |
| 125 | + ]; |
| 126 | + $result = []; |
| 127 | + |
| 128 | + foreach ($arguments as $argument) { |
| 129 | + $result[] = [ |
| 130 | + 'argument' => $argument, |
| 131 | + 'value' => 'Standard.Category.Sniff', |
| 132 | + ]; |
| 133 | + } |
| 134 | + |
| 135 | + return $result; |
| 136 | + |
| 137 | + }//end dataValidSniffs() |
| 138 | + |
| 139 | + |
| 140 | +}//end class |
0 commit comments