|
| 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