|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Rector\Tests\FileSystem\FilePathFilter; |
| 6 | + |
| 7 | +use Iterator; |
| 8 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | +use Rector\FileSystem\FilePathFilter; |
| 11 | + |
| 12 | +final class FilePathFilterTest extends TestCase |
| 13 | +{ |
| 14 | + private FilePathFilter $filePathFilter; |
| 15 | + |
| 16 | + protected function setUp(): void |
| 17 | + { |
| 18 | + $this->filePathFilter = new FilePathFilter(); |
| 19 | + } |
| 20 | + |
| 21 | + /** |
| 22 | + * @param string[] $patterns |
| 23 | + * @param string[] $expectedFilePaths |
| 24 | + */ |
| 25 | + #[DataProvider('provideData')] |
| 26 | + public function test(array $patterns, array $expectedFilePaths): void |
| 27 | + { |
| 28 | + $filePaths = [ |
| 29 | + '/project/src/Controller/HomeController.php', |
| 30 | + '/project/src/Repository/UserRepository.php', |
| 31 | + '/project/tests/Unit/SomeTest.php', |
| 32 | + '/project/tests/AbstractTestCase.php', |
| 33 | + ]; |
| 34 | + |
| 35 | + $this->assertSame($expectedFilePaths, $this->filePathFilter->filter($filePaths, $patterns)); |
| 36 | + } |
| 37 | + |
| 38 | + public static function provideData(): Iterator |
| 39 | + { |
| 40 | + yield 'no patterns keeps everything' => [[], [ |
| 41 | + '/project/src/Controller/HomeController.php', |
| 42 | + '/project/src/Repository/UserRepository.php', |
| 43 | + '/project/tests/Unit/SomeTest.php', |
| 44 | + '/project/tests/AbstractTestCase.php', |
| 45 | + ]]; |
| 46 | + |
| 47 | + yield 'path substring' => [['/Controller/'], ['/project/src/Controller/HomeController.php']]; |
| 48 | + |
| 49 | + yield 'path glob matches same as substring' => [['*/Controller/*'], ['/project/src/Controller/HomeController.php']]; |
| 50 | + |
| 51 | + yield 'basename glob' => [['*Repository.php'], ['/project/src/Repository/UserRepository.php']]; |
| 52 | + |
| 53 | + yield 'tests keyword' => [['tests'], [ |
| 54 | + '/project/tests/Unit/SomeTest.php', |
| 55 | + '/project/tests/AbstractTestCase.php', |
| 56 | + ]]; |
| 57 | + |
| 58 | + yield 'patterns combine with AND' => [['/tests/', '*Test.php'], ['/project/tests/Unit/SomeTest.php']]; |
| 59 | + |
| 60 | + yield 'no match yields empty' => [['*Missing.php'], []]; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * @param string[] $expectedPatterns |
| 65 | + */ |
| 66 | + #[DataProvider('provideParseData')] |
| 67 | + public function testParsePatterns(string $rawFilter, array $expectedPatterns): void |
| 68 | + { |
| 69 | + $this->assertSame($expectedPatterns, $this->filePathFilter->parsePatterns($rawFilter)); |
| 70 | + } |
| 71 | + |
| 72 | + public static function provideParseData(): Iterator |
| 73 | + { |
| 74 | + yield 'empty string yields no patterns' => ['', []]; |
| 75 | + yield 'single pattern' => ['/Controller/', ['/Controller/']]; |
| 76 | + yield 'comma separated, trimmed' => [' /Controller/ , *Repository.php ', ['/Controller/', '*Repository.php']]; |
| 77 | + yield 'blank parts dropped' => ['tests,,', ['tests']]; |
| 78 | + } |
| 79 | +} |
0 commit comments