Skip to content

Commit 3edfe71

Browse files
Initial work on #6519
1 parent 90c0803 commit 3edfe71

5 files changed

Lines changed: 156 additions & 21 deletions

File tree

composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"issues": "https://github.com/sebastianbergmann/phpunit/issues",
2121
"security": "https://github.com/sebastianbergmann/phpunit/security/policy"
2222
},
23+
"minimum-stability": "dev",
2324
"prefer-stable": true,
2425
"require": {
2526
"php": ">=8.4.1",
@@ -42,6 +43,7 @@
4243
"sebastian/diff": "^8.0.0",
4344
"sebastian/environment": "^9.0.0",
4445
"sebastian/exporter": "^8.0.0",
46+
"sebastian/file-filter": "^1.0@dev",
4547
"sebastian/global-state": "^9.0.0",
4648
"sebastian/object-enumerator": "^8.0.0",
4749
"sebastian/recursion-context": "^8.0.0",

composer.lock

Lines changed: 75 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <sebastian@phpunit.de>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace PHPUnit\TextUI\Configuration;
11+
12+
use SebastianBergmann\FileFilter\Builder as FilterBuilder;
13+
use SebastianBergmann\FileFilter\Filter as FileFilter;
14+
15+
/**
16+
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
17+
*
18+
* @internal This class is not covered by the backward compatibility promise for PHPUnit
19+
*/
20+
final class FileFilterMapper
21+
{
22+
public function map(Source $source): FileFilter
23+
{
24+
return (new FilterBuilder)->build(
25+
$this->directories($source->includeDirectories()),
26+
$this->files($source->includeFiles()),
27+
$this->directories($source->excludeDirectories()),
28+
$this->files($source->excludeFiles()),
29+
);
30+
}
31+
32+
/**
33+
* @return list<array{path: non-empty-string, prefix: string, suffix: string}>
34+
*/
35+
private function directories(FilterDirectoryCollection $directories): array
36+
{
37+
$result = [];
38+
39+
foreach ($directories as $directory) {
40+
$result[] = [
41+
'path' => $directory->path(),
42+
'prefix' => $directory->prefix(),
43+
'suffix' => $directory->suffix(),
44+
];
45+
}
46+
47+
return $result;
48+
}
49+
50+
/**
51+
* @return list<non-empty-string>
52+
*/
53+
private function files(FileCollection $files): array
54+
{
55+
$result = [];
56+
57+
foreach ($files as $file) {
58+
$result[] = $file->path();
59+
}
60+
61+
return $result;
62+
}
63+
}

src/TextUI/Configuration/SourceFilter.php

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
*/
1010
namespace PHPUnit\TextUI\Configuration;
1111

12+
use SebastianBergmann\FileFilter\Filter;
13+
1214
/**
1315
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
1416
*
@@ -17,38 +19,33 @@
1719
final class SourceFilter
1820
{
1921
private static ?self $instance = null;
20-
21-
/**
22-
* @var array<non-empty-string, true>
23-
*/
24-
private readonly array $map;
22+
private readonly Filter $filter;
2523

2624
public static function instance(): self
2725
{
28-
if (self::$instance === null) {
29-
self::$instance = new self(
30-
(new SourceMapper)->map(
31-
Registry::get()->source(),
32-
),
33-
);
26+
if (self::$instance !== null) {
27+
return self::$instance;
3428
}
3529

30+
self::$instance = new self(
31+
(new FileFilterMapper)->map(
32+
Registry::get()->source(),
33+
),
34+
);
35+
3636
return self::$instance;
3737
}
3838

39-
/**
40-
* @param array<non-empty-string, true> $map
41-
*/
42-
public function __construct(array $map)
39+
public function __construct(Filter $filter)
4340
{
44-
$this->map = $map;
41+
$this->filter = $filter;
4542
}
4643

4744
/**
4845
* @param non-empty-string $path
4946
*/
5047
public function includes(string $path): bool
5148
{
52-
return isset($this->map[$path]);
49+
return $this->filter->accepts($path);
5350
}
5451
}

tests/unit/TextUI/SourceFilterTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use PHPUnit\Framework\Attributes\Small;
1818

1919
#[CoversClass(SourceFilter::class)]
20+
#[CoversClass(FileFilterMapper::class)]
2021
#[Small]
2122
#[Group('textui')]
2223
#[Group('textui/configuration')]
@@ -425,7 +426,7 @@ public function testDeterminesWhetherFileIsIncluded(array $expectations, Source
425426
$this->assertFileExists($file);
426427
$this->assertSame(
427428
$shouldInclude,
428-
new SourceFilter((new SourceMapper)->map($source))->includes($file),
429+
new SourceFilter((new FileFilterMapper)->map($source))->includes($file),
429430
sprintf('expected match to return %s for: %s', json_encode($shouldInclude), $file),
430431
);
431432
}

0 commit comments

Comments
 (0)