| 148 |
lars |
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;
|
|
|
11 |
|
|
|
12 |
use const PHP_VERSION;
|
|
|
13 |
use function explode;
|
|
|
14 |
use function in_array;
|
|
|
15 |
use function is_dir;
|
|
|
16 |
use function is_file;
|
|
|
17 |
use function strpos;
|
|
|
18 |
use function version_compare;
|
|
|
19 |
use PHPUnit\Framework\Exception as FrameworkException;
|
|
|
20 |
use PHPUnit\Framework\TestSuite as TestSuiteObject;
|
|
|
21 |
use PHPUnit\TextUI\XmlConfiguration\TestSuiteCollection;
|
|
|
22 |
use SebastianBergmann\FileIterator\Facade;
|
|
|
23 |
|
|
|
24 |
/**
|
|
|
25 |
* @internal This class is not covered by the backward compatibility promise for PHPUnit
|
|
|
26 |
*/
|
|
|
27 |
final class TestSuiteMapper
|
|
|
28 |
{
|
|
|
29 |
/**
|
|
|
30 |
* @throws RuntimeException
|
|
|
31 |
* @throws TestDirectoryNotFoundException
|
|
|
32 |
* @throws TestFileNotFoundException
|
|
|
33 |
*/
|
|
|
34 |
public function map(TestSuiteCollection $configuration, string $filter): TestSuiteObject
|
|
|
35 |
{
|
|
|
36 |
try {
|
|
|
37 |
$filterAsArray = $filter ? explode(',', $filter) : [];
|
|
|
38 |
$result = new TestSuiteObject;
|
|
|
39 |
|
|
|
40 |
foreach ($configuration as $testSuiteConfiguration) {
|
|
|
41 |
if (!empty($filterAsArray) && !in_array($testSuiteConfiguration->name(), $filterAsArray, true)) {
|
|
|
42 |
continue;
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
$testSuite = new TestSuiteObject($testSuiteConfiguration->name());
|
|
|
46 |
$testSuiteEmpty = true;
|
|
|
47 |
|
|
|
48 |
$exclude = [];
|
|
|
49 |
|
|
|
50 |
foreach ($testSuiteConfiguration->exclude()->asArray() as $file) {
|
|
|
51 |
$exclude[] = $file->path();
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
foreach ($testSuiteConfiguration->directories() as $directory) {
|
|
|
55 |
if (!version_compare(PHP_VERSION, $directory->phpVersion(), $directory->phpVersionOperator()->asString())) {
|
|
|
56 |
continue;
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
$files = (new Facade)->getFilesAsArray(
|
|
|
60 |
$directory->path(),
|
|
|
61 |
$directory->suffix(),
|
|
|
62 |
$directory->prefix(),
|
|
|
63 |
$exclude
|
|
|
64 |
);
|
|
|
65 |
|
|
|
66 |
if (!empty($files)) {
|
|
|
67 |
$testSuite->addTestFiles($files);
|
|
|
68 |
|
|
|
69 |
$testSuiteEmpty = false;
|
|
|
70 |
} elseif (strpos($directory->path(), '*') === false && !is_dir($directory->path())) {
|
|
|
71 |
throw new TestDirectoryNotFoundException($directory->path());
|
|
|
72 |
}
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
foreach ($testSuiteConfiguration->files() as $file) {
|
|
|
76 |
if (!is_file($file->path())) {
|
|
|
77 |
throw new TestFileNotFoundException($file->path());
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
if (!version_compare(PHP_VERSION, $file->phpVersion(), $file->phpVersionOperator()->asString())) {
|
|
|
81 |
continue;
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
$testSuite->addTestFile($file->path());
|
|
|
85 |
|
|
|
86 |
$testSuiteEmpty = false;
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
if (!$testSuiteEmpty) {
|
|
|
90 |
$result->addTest($testSuite);
|
|
|
91 |
}
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
return $result;
|
|
|
95 |
} catch (FrameworkException $e) {
|
|
|
96 |
throw new RuntimeException(
|
|
|
97 |
$e->getMessage(),
|
|
|
98 |
$e->getCode(),
|
|
|
99 |
$e
|
|
|
100 |
);
|
|
|
101 |
}
|
|
|
102 |
}
|
|
|
103 |
}
|