|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace ParaTest\TestDox; |
| 6 | + |
| 7 | +use PHPUnit\Logging\TestDox\TestResult as TestDoxTestMethod; |
| 8 | +use PHPUnit\Logging\TestDox\TestResultCollection; |
| 9 | +use ReflectionException; |
| 10 | +use ReflectionMethod; |
| 11 | +use SplFileInfo; |
| 12 | + |
| 13 | +use function array_merge; |
| 14 | +use function assert; |
| 15 | +use function file_get_contents; |
| 16 | +use function is_subclass_of; |
| 17 | +use function ksort; |
| 18 | +use function uksort; |
| 19 | +use function unserialize; |
| 20 | +use function usort; |
| 21 | + |
| 22 | +/** @internal */ |
| 23 | +final readonly class TestDoxResultsMerger |
| 24 | +{ |
| 25 | + /** |
| 26 | + * @param list<SplFileInfo> $testdoxFiles |
| 27 | + * |
| 28 | + * @return array<string, TestResultCollection> |
| 29 | + */ |
| 30 | + public function getResultsFromTestdoxFiles(array $testdoxFiles): array |
| 31 | + { |
| 32 | + /** @var array<string, TestResultCollection> $testMethodsGroupedByClass */ |
| 33 | + $testMethodsGroupedByClass = []; |
| 34 | + foreach ($testdoxFiles as $testdoxFile) { |
| 35 | + if (! $testdoxFile->isFile()) { |
| 36 | + continue; |
| 37 | + } |
| 38 | + |
| 39 | + $testdoxFileContents = file_get_contents($testdoxFile->getPathname()); |
| 40 | + assert($testdoxFileContents !== false); |
| 41 | + |
| 42 | + /** @var array<string, TestResultCollection> $testMethodsGroupedByClassInTestdoxFile */ |
| 43 | + $testMethodsGroupedByClassInTestdoxFile = unserialize($testdoxFileContents); |
| 44 | + foreach ($testMethodsGroupedByClassInTestdoxFile as $className => $testResultCollection) { |
| 45 | + if (! isset($testMethodsGroupedByClass[$className])) { |
| 46 | + $testMethodsGroupedByClass[$className] = $testResultCollection; |
| 47 | + } else { |
| 48 | + $combinedTestResultCollection = TestResultCollection::fromArray([ |
| 49 | + ...$testMethodsGroupedByClass[$className]->asArray(), |
| 50 | + ...$testResultCollection->asArray(), |
| 51 | + ]); |
| 52 | + $testMethodsGroupedByClass[$className] = $combinedTestResultCollection; |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + return $this->orderTestdoxResults($testMethodsGroupedByClass); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @param array<string, TestResultCollection> $testdoxResults |
| 62 | + * |
| 63 | + * @return array<string, TestResultCollection> |
| 64 | + */ |
| 65 | + private function orderTestdoxResults(array $testdoxResults): array |
| 66 | + { |
| 67 | + // @see \PHPUnit\Logging\TestDox\TestResultCollector::testMethodsGroupedByClass |
| 68 | + $orderedTestdoxResults = []; |
| 69 | + |
| 70 | + foreach ($testdoxResults as $prettifiedClassName => $tests) { |
| 71 | + $testsByDeclaringClass = []; |
| 72 | + |
| 73 | + foreach ($tests as $test) { |
| 74 | + try { |
| 75 | + $declaringClassName = (new ReflectionMethod($test->test()->className(), $test->test()->methodName()))->getDeclaringClass()->getName(); |
| 76 | + } catch (ReflectionException) { |
| 77 | + $declaringClassName = $test->test()->className(); |
| 78 | + } |
| 79 | + |
| 80 | + if (! isset($testsByDeclaringClass[$declaringClassName])) { |
| 81 | + $testsByDeclaringClass[$declaringClassName] = []; |
| 82 | + } |
| 83 | + |
| 84 | + $testsByDeclaringClass[$declaringClassName][] = $test; |
| 85 | + } |
| 86 | + |
| 87 | + foreach ($testsByDeclaringClass as $declaringClassName) { |
| 88 | + usort( |
| 89 | + $declaringClassName, |
| 90 | + static function (TestDoxTestMethod $a, TestDoxTestMethod $b): int { |
| 91 | + return $a->test()->line() <=> $b->test()->line(); |
| 92 | + }, |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | + uksort( |
| 97 | + $testsByDeclaringClass, |
| 98 | + /** |
| 99 | + * @param class-string $a |
| 100 | + * @param class-string $b |
| 101 | + */ |
| 102 | + static function (string $a, string $b): int { |
| 103 | + if (is_subclass_of($b, $a)) { |
| 104 | + return -1; |
| 105 | + } |
| 106 | + |
| 107 | + if (is_subclass_of($a, $b)) { |
| 108 | + return 1; |
| 109 | + } |
| 110 | + |
| 111 | + return 0; |
| 112 | + }, |
| 113 | + ); |
| 114 | + |
| 115 | + $tests = []; |
| 116 | + |
| 117 | + foreach ($testsByDeclaringClass as $_tests) { |
| 118 | + $tests = array_merge($tests, $_tests); |
| 119 | + } |
| 120 | + |
| 121 | + $orderedTestdoxResults[$prettifiedClassName] = TestResultCollection::fromArray($tests); |
| 122 | + } |
| 123 | + |
| 124 | + ksort($orderedTestdoxResults); |
| 125 | + |
| 126 | + return $orderedTestdoxResults; |
| 127 | + } |
| 128 | +} |
0 commit comments