Skip to content

Commit 516c847

Browse files
Extract class
1 parent 5ed521f commit 516c847

File tree

4 files changed

+118
-21
lines changed

4 files changed

+118
-21
lines changed

src/CodeCoverage.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
use SebastianBergmann\CodeCoverage\Test\Target\MapBuilder;
3232
use SebastianBergmann\CodeCoverage\Test\Target\Mapper;
3333
use SebastianBergmann\CodeCoverage\Test\Target\TargetCollection;
34+
use SebastianBergmann\CodeCoverage\Test\Target\TargetCollectionValidator;
3435
use SebastianBergmann\CodeCoverage\Test\Target\ValidationResult;
3536
use SebastianBergmann\CodeCoverage\Test\TestSize\TestSize;
3637
use SebastianBergmann\CodeCoverage\Test\TestStatus\TestStatus;
@@ -368,7 +369,7 @@ public function detectsDeadCode(): bool
368369

369370
public function validate(TargetCollection $targets): ValidationResult
370371
{
371-
return $targets->validate($this->targetMapper());
372+
return (new TargetCollectionValidator)->validate($this->targetMapper(), $targets);
372373
}
373374

374375
/**

src/Target/TargetCollection.php

-20
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
namespace SebastianBergmann\CodeCoverage\Test\Target;
1111

1212
use function count;
13-
use function implode;
1413
use Countable;
1514
use IteratorAggregate;
1615

@@ -68,23 +67,4 @@ public function getIterator(): TargetCollectionIterator
6867
{
6968
return new TargetCollectionIterator($this);
7069
}
71-
72-
public function validate(Mapper $mapper): ValidationResult
73-
{
74-
$errors = [];
75-
76-
foreach ($this->targets as $target) {
77-
try {
78-
$mapper->mapTarget($target);
79-
} catch (InvalidCodeCoverageTargetException $e) {
80-
$errors[] = $e->getMessage();
81-
}
82-
}
83-
84-
if ($errors === []) {
85-
return ValidationResult::success();
86-
}
87-
88-
return ValidationResult::failure(implode("\n", $errors));
89-
}
9070
}
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of phpunit/php-code-coverage.
4+
*
5+
* (c) Sebastian Bergmann <[email protected]>
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 SebastianBergmann\CodeCoverage\Test\Target;
11+
12+
use function implode;
13+
14+
/**
15+
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for phpunit/php-code-coverage
16+
*
17+
* @internal This class is not covered by the backward compatibility promise for phpunit/php-code-coverage
18+
*/
19+
final readonly class TargetCollectionValidator
20+
{
21+
public function validate(Mapper $mapper, TargetCollection $targets): ValidationResult
22+
{
23+
$errors = [];
24+
25+
foreach ($targets as $target) {
26+
try {
27+
$mapper->mapTarget($target);
28+
} catch (InvalidCodeCoverageTargetException $e) {
29+
$errors[] = $e->getMessage();
30+
}
31+
}
32+
33+
if ($errors === []) {
34+
return ValidationResult::success();
35+
}
36+
37+
return ValidationResult::failure(implode("\n", $errors));
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of phpunit/php-code-coverage.
4+
*
5+
* (c) Sebastian Bergmann <[email protected]>
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 SebastianBergmann\CodeCoverage\Test\Target;
11+
12+
use function assert;
13+
use PHPUnit\Framework\Attributes\CoversClass;
14+
use PHPUnit\Framework\Attributes\Small;
15+
use PHPUnit\Framework\TestCase;
16+
use SebastianBergmann\CodeCoverage\Filter;
17+
use SebastianBergmann\CodeCoverage\StaticAnalysis\ParsingFileAnalyser;
18+
use SebastianBergmann\CodeCoverage\TestFixture\Target\TargetClass;
19+
20+
/**
21+
* @phpstan-import-type TargetMap from Mapper
22+
*/
23+
#[CoversClass(TargetCollectionValidator::class)]
24+
#[Small]
25+
final class TargetCollectionValidatorTest extends TestCase
26+
{
27+
public function test_TargetCollection_is_valid_when_all_targets_can_be_mapped(): void
28+
{
29+
$targets = TargetCollection::fromArray([Target::forClass(TargetClass::class)]);
30+
$mapper = $this->mapper([__DIR__ . '/../../_files/Target/TargetClass.php']);
31+
$validator = new TargetCollectionValidator;
32+
33+
$result = $validator->validate($mapper, $targets);
34+
35+
$this->assertTrue($result->isSuccess());
36+
}
37+
38+
public function test_TargetCollection_is_invalid_when_target_cannot_be_mapped(): void
39+
{
40+
$targets = TargetCollection::fromArray([Target::forClass(TargetClass::class)]);
41+
$mapper = $this->mapper([]);
42+
$validator = new TargetCollectionValidator;
43+
44+
$result = $validator->validate($mapper, $targets);
45+
46+
$this->assertTrue($result->isFailure());
47+
48+
assert($result instanceof ValidationFailure);
49+
50+
$this->assertSame(
51+
'Class SebastianBergmann\CodeCoverage\TestFixture\Target\TargetClass is not a valid target for code coverage',
52+
$result->message(),
53+
);
54+
}
55+
56+
/**
57+
* @param list<non-empty-string> $files
58+
*/
59+
private function mapper(array $files): Mapper
60+
{
61+
return new Mapper($this->map($files));
62+
}
63+
64+
/**
65+
* @param list<non-empty-string> $files
66+
*
67+
* @return TargetMap
68+
*/
69+
private function map(array $files): array
70+
{
71+
$filter = new Filter;
72+
73+
$filter->includeFiles($files);
74+
75+
return (new MapBuilder)->build($filter, new ParsingFileAnalyser(false, false));
76+
}
77+
}

0 commit comments

Comments
 (0)