|
3 | 3 | * Tests for the \PHP_CodeSniffer\Filters\Filter::accept method.
|
4 | 4 | *
|
5 | 5 | * @author Willington Vega <wvega@wvega.com>
|
6 |
| - * @copyright 2006-2018 Squiz Pty Ltd (ABN 77 084 670 600) |
| 6 | + * @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl> |
| 7 | + * @copyright 2019 Squiz Pty Ltd (ABN 77 084 670 600) |
7 | 8 | * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
|
8 | 9 | */
|
9 | 10 |
|
|
17 | 18 | class AcceptTest extends TestCase
|
18 | 19 | {
|
19 | 20 |
|
| 21 | + /** |
| 22 | + * The Config object. |
| 23 | + * |
| 24 | + * @var \PHP_CodeSniffer\Config |
| 25 | + */ |
| 26 | + protected static $config; |
| 27 | + |
| 28 | + /** |
| 29 | + * The Ruleset object. |
| 30 | + * |
| 31 | + * @var \PHP_CodeSniffer\Ruleset |
| 32 | + */ |
| 33 | + protected static $ruleset; |
| 34 | + |
20 | 35 |
|
21 | 36 | /**
|
22 |
| - * Test paths that include the name of a standard with associated |
23 |
| - * exclude-patterns are still accepted. |
| 37 | + * Initialize the config and ruleset objects based on the `AcceptTest.xml` ruleset file. |
24 | 38 | *
|
25 | 39 | * @return void
|
26 | 40 | */
|
27 |
| - public function testExcludePatternsForStandards() |
| 41 | + public static function setUpBeforeClass() |
28 | 42 | {
|
29 |
| - $standard = __DIR__.'/'.basename(__FILE__, '.php').'.inc'; |
30 |
| - $config = new Config(["--standard=$standard"]); |
31 |
| - $ruleset = new Ruleset($config); |
| 43 | + $standard = __DIR__.'/'.basename(__FILE__, '.php').'.xml'; |
| 44 | + self::$config = new Config(["--standard=$standard"]); |
| 45 | + self::$ruleset = new Ruleset(self::$config); |
| 46 | + |
| 47 | + }//end setUpBeforeClass() |
32 | 48 |
|
33 |
| - $paths = ['/path/to/generic-project/src/Main.php']; |
34 | 49 |
|
35 |
| - $fakeDI = new \RecursiveArrayIterator($paths); |
36 |
| - $filter = new Filter($fakeDI, '/path/to/generic-project/src', $config, $ruleset); |
| 50 | + /** |
| 51 | + * Test filtering a file list for excluded paths. |
| 52 | + * |
| 53 | + * @param array $inputPaths List of file paths to be filtered. |
| 54 | + * @param array $expectedOutput Expected filtering result. |
| 55 | + * |
| 56 | + * @dataProvider dataExcludePatterns |
| 57 | + * |
| 58 | + * @return void |
| 59 | + */ |
| 60 | + public function testExcludePatterns($inputPaths, $expectedOutput) |
| 61 | + { |
| 62 | + $fakeDI = new \RecursiveArrayIterator($inputPaths); |
| 63 | + $filter = new Filter($fakeDI, '/', self::$config, self::$ruleset); |
37 | 64 | $iterator = new \RecursiveIteratorIterator($filter);
|
38 | 65 | $files = [];
|
39 | 66 |
|
40 | 67 | foreach ($iterator as $file) {
|
41 | 68 | $files[] = $file;
|
42 | 69 | }
|
43 | 70 |
|
44 |
| - $this->assertEquals($paths, $files); |
| 71 | + $this->assertEquals($expectedOutput, $files); |
| 72 | + |
| 73 | + }//end testExcludePatterns() |
| 74 | + |
| 75 | + |
| 76 | + /** |
| 77 | + * Data provider. |
| 78 | + * |
| 79 | + * @see testExcludePatterns |
| 80 | + * |
| 81 | + * @return array |
| 82 | + */ |
| 83 | + public function dataExcludePatterns() |
| 84 | + { |
| 85 | + $testCases = [ |
| 86 | + // Test top-level exclude patterns. |
| 87 | + [ |
| 88 | + [ |
| 89 | + '/path/to/src/Main.php', |
| 90 | + '/path/to/src/Something/Main.php', |
| 91 | + '/path/to/src/Other/Main.php', |
| 92 | + ], |
| 93 | + ['/path/to/src/Main.php'], |
| 94 | + ], |
| 95 | + |
| 96 | + // Test ignoring standard/sniff specific exclude patterns. |
| 97 | + [ |
| 98 | + [ |
| 99 | + '/path/to/src/generic-project/Main.php', |
| 100 | + '/path/to/src/generic/Main.php', |
| 101 | + '/path/to/src/anything-generic/Main.php', |
| 102 | + ], |
| 103 | + [ |
| 104 | + '/path/to/src/generic-project/Main.php', |
| 105 | + '/path/to/src/generic/Main.php', |
| 106 | + '/path/to/src/anything-generic/Main.php', |
| 107 | + ], |
| 108 | + ], |
| 109 | + ]; |
| 110 | + |
| 111 | + // Allow these tests to work on Windows as well. |
| 112 | + if (DIRECTORY_SEPARATOR === '\\') { |
| 113 | + foreach ($testCases as $key => $case) { |
| 114 | + foreach ($case as $nr => $param) { |
| 115 | + foreach ($param as $file => $value) { |
| 116 | + $testCases[$key][$nr][$file] = strtr($value, '/', '\\'); |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + return $testCases; |
45 | 123 |
|
46 |
| - }//end testExcludePatternsForStandards() |
| 124 | + }//end dataExcludePatterns() |
47 | 125 |
|
48 | 126 |
|
49 | 127 | }//end class
|
0 commit comments