|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Tests for the \PHP_CodeSniffer\Filters\GitModified class. |
| 4 | + * |
| 5 | + * @author Juliette Reinders Folmer <[email protected]> |
| 6 | + * @copyright 2023 PHPCSStandards Contributors |
| 7 | + * @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence |
| 8 | + */ |
| 9 | + |
| 10 | +namespace PHP_CodeSniffer\Tests\Core\Filters; |
| 11 | + |
| 12 | +use PHP_CodeSniffer\Config; |
| 13 | +use PHP_CodeSniffer\Filters\Filter; |
| 14 | +use PHP_CodeSniffer\Ruleset; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | +use RecursiveIteratorIterator; |
| 17 | + |
| 18 | +/** |
| 19 | + * Base functionality and utilities for testing Filter classes. |
| 20 | + */ |
| 21 | +abstract class AbstractFilterTestCase extends TestCase |
| 22 | +{ |
| 23 | + |
| 24 | + /** |
| 25 | + * The Config object. |
| 26 | + * |
| 27 | + * @var \PHP_CodeSniffer\Config |
| 28 | + */ |
| 29 | + protected static $config; |
| 30 | + |
| 31 | + /** |
| 32 | + * The Ruleset object. |
| 33 | + * |
| 34 | + * @var \PHP_CodeSniffer\Ruleset |
| 35 | + */ |
| 36 | + protected static $ruleset; |
| 37 | + |
| 38 | + |
| 39 | + /** |
| 40 | + * Initialize the config and ruleset objects. |
| 41 | + * |
| 42 | + * @beforeClass |
| 43 | + * |
| 44 | + * @return void |
| 45 | + */ |
| 46 | + public static function initializeConfigAndRuleset() |
| 47 | + { |
| 48 | + self::$config = new Config(['--standard=PSR1', '--report-width=80']); |
| 49 | + self::$ruleset = new Ruleset(self::$config); |
| 50 | + |
| 51 | + }//end initializeConfigAndRuleset() |
| 52 | + |
| 53 | + |
| 54 | + /** |
| 55 | + * Retrieve an array of files which were accepted by a filter. |
| 56 | + * |
| 57 | + * @param \PHP_CodeSniffer\Filters\Filter $filter The Filter object under test. |
| 58 | + * |
| 59 | + * @return array<string> |
| 60 | + */ |
| 61 | + protected function getFilteredResultsAsArray(Filter $filter) |
| 62 | + { |
| 63 | + $iterator = new RecursiveIteratorIterator($filter); |
| 64 | + $files = []; |
| 65 | + foreach ($iterator as $file) { |
| 66 | + $files[] = $file; |
| 67 | + } |
| 68 | + |
| 69 | + return $files; |
| 70 | + |
| 71 | + }//end getFilteredResultsAsArray() |
| 72 | + |
| 73 | + |
| 74 | + /** |
| 75 | + * Translate Linux paths to Windows paths, when necessary. |
| 76 | + * |
| 77 | + * These type of tests should be able to run and pass on both *nix as well as Windows |
| 78 | + * based dev systems. This method is a helper to allow for this. |
| 79 | + * |
| 80 | + * @param array<string|array> $paths A single or multi-dimensional array containing |
| 81 | + * file paths. |
| 82 | + * |
| 83 | + * @return array<string|array> |
| 84 | + */ |
| 85 | + protected static function mapPathsToRuntimeOs(array $paths) |
| 86 | + { |
| 87 | + if (DIRECTORY_SEPARATOR !== '\\') { |
| 88 | + return $paths; |
| 89 | + } |
| 90 | + |
| 91 | + foreach ($paths as $key => $value) { |
| 92 | + if (is_string($value) === true) { |
| 93 | + $paths[$key] = strtr($value, '/', '\\\\'); |
| 94 | + } else if (is_array($value) === true) { |
| 95 | + $paths[$key] = self::mapPathsToRuntimeOs($value); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + return $paths; |
| 100 | + |
| 101 | + }//end mapPathsToRuntimeOs() |
| 102 | + |
| 103 | + |
| 104 | +}//end class |
0 commit comments