diff --git a/tests/_files/Metadata/Attribute/tests/CoversNothingTest.php b/tests/_files/Metadata/Attribute/tests/CoversNothingTest.php new file mode 100644 index 0000000000..bd5b02a1e9 --- /dev/null +++ b/tests/_files/Metadata/Attribute/tests/CoversNothingTest.php @@ -0,0 +1,22 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PHPUnit\TestFixture\Metadata\Attribute; + +use PHPUnit\Framework\Attributes\CoversNothing; +use PHPUnit\Framework\TestCase; + +#[CoversNothing] +final class CoversNothingTest extends TestCase +{ + #[CoversNothing] + public function testOne(): void + { + } +} diff --git a/tests/_files/Metadata/Attribute/tests/CoversTest.php b/tests/_files/Metadata/Attribute/tests/CoversTest.php index bd50f4bb1e..44b82d2e40 100644 --- a/tests/_files/Metadata/Attribute/tests/CoversTest.php +++ b/tests/_files/Metadata/Attribute/tests/CoversTest.php @@ -15,7 +15,6 @@ use PHPUnit\Framework\Attributes\CoversFunction; use PHPUnit\Framework\Attributes\CoversMethod; use PHPUnit\Framework\Attributes\CoversNamespace; -use PHPUnit\Framework\Attributes\CoversNothing; use PHPUnit\Framework\TestCase; #[CoversNamespace('PHPUnit\TestFixture\Metadata\Attribute')] @@ -24,10 +23,8 @@ #[CoversClassesThatImplementInterface(Example::class)] #[CoversMethod(Example::class, 'method')] #[CoversFunction('f')] -#[CoversNothing] final class CoversTest extends TestCase { - #[CoversNothing] public function testOne(): void { } diff --git a/tests/_files/Metadata/Attribute/tests/UsesTest.php b/tests/_files/Metadata/Attribute/tests/UsesTest.php index b0da2366b1..7ac437e876 100644 --- a/tests/_files/Metadata/Attribute/tests/UsesTest.php +++ b/tests/_files/Metadata/Attribute/tests/UsesTest.php @@ -25,4 +25,7 @@ #[UsesFunction('f')] final class UsesTest extends TestCase { + public function testOne(): void + { + } } diff --git a/tests/unit/Metadata/Api/CodeCoverageTest.php b/tests/unit/Metadata/Api/CodeCoverageTest.php index 31a96cb2d9..240e259713 100644 --- a/tests/unit/Metadata/Api/CodeCoverageTest.php +++ b/tests/unit/Metadata/Api/CodeCoverageTest.php @@ -9,15 +9,25 @@ */ namespace PHPUnit\Metadata\Api; +use function array_shift; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Group; use PHPUnit\Framework\Attributes\Small; +use PHPUnit\Framework\Attributes\TestDox; use PHPUnit\Framework\TestCase; use PHPUnit\TestFixture\CoversClassOnClassTest; use PHPUnit\TestFixture\CoversNothingOnClassTest; use PHPUnit\TestFixture\CoversNothingOnMethodTest; +use PHPUnit\TestFixture\Metadata\Attribute\CoversTest; +use PHPUnit\TestFixture\Metadata\Attribute\UsesTest; use PHPUnit\TestFixture\NoCoverageAttributesTest; +use SebastianBergmann\CodeCoverage\Test\Target\Class_; +use SebastianBergmann\CodeCoverage\Test\Target\ClassesThatExtendClass; +use SebastianBergmann\CodeCoverage\Test\Target\ClassesThatImplementInterface; +use SebastianBergmann\CodeCoverage\Test\Target\Function_; +use SebastianBergmann\CodeCoverage\Test\Target\Method; +use SebastianBergmann\CodeCoverage\Test\Target\Namespace_; #[CoversClass(CodeCoverage::class)] #[Small] @@ -37,6 +47,78 @@ public static function canSkipCoverageProvider(): array ]; } + #[TestDox('Maps #[Covers*()] metadata to phpunit/php-code-coverage TargetCollection')] + public function testMapsCoversMetadataToCodeCoverageTargetCollection(): void + { + $targets = (new CodeCoverage)->coversTargets(CoversTest::class, 'testOne'); + + $this->assertNotFalse($targets); + $this->assertCount(6, $targets); + + $targets = $targets->asArray(); + + $target = array_shift($targets); + $this->assertInstanceOf(Namespace_::class, $target); + $this->assertSame('PHPUnit\TestFixture\Metadata\Attribute', $target->namespace()); + + $target = array_shift($targets); + $this->assertInstanceOf(Class_::class, $target); + $this->assertSame('PHPUnit\TestFixture\Metadata\Attribute\Example', $target->className()); + + $target = array_shift($targets); + $this->assertInstanceOf(ClassesThatExtendClass::class, $target); + $this->assertSame('PHPUnit\TestFixture\Metadata\Attribute\Example', $target->className()); + + $target = array_shift($targets); + $this->assertInstanceOf(ClassesThatImplementInterface::class, $target); + $this->assertSame('PHPUnit\TestFixture\Metadata\Attribute\Example', $target->interfaceName()); + + $target = array_shift($targets); + $this->assertInstanceOf(Method::class, $target); + $this->assertSame('PHPUnit\TestFixture\Metadata\Attribute\Example', $target->className()); + $this->assertSame('method', $target->methodName()); + + $target = array_shift($targets); + $this->assertInstanceOf(Function_::class, $target); + $this->assertSame('f', $target->functionName()); + } + + #[TestDox('Maps #[Uses*()] metadata to phpunit/php-code-coverage TargetCollection')] + public function testMapsUsesMetadataToCodeCoverageTargetCollection(): void + { + $targets = (new CodeCoverage)->usesTargets(UsesTest::class, 'testOne'); + + $this->assertNotFalse($targets); + $this->assertCount(6, $targets); + + $targets = $targets->asArray(); + + $target = array_shift($targets); + $this->assertInstanceOf(Namespace_::class, $target); + $this->assertSame('PHPUnit\TestFixture\Metadata\Attribute', $target->namespace()); + + $target = array_shift($targets); + $this->assertInstanceOf(Class_::class, $target); + $this->assertSame('PHPUnit\TestFixture\Metadata\Attribute\Example', $target->className()); + + $target = array_shift($targets); + $this->assertInstanceOf(ClassesThatExtendClass::class, $target); + $this->assertSame('PHPUnit\TestFixture\Metadata\Attribute\Example', $target->className()); + + $target = array_shift($targets); + $this->assertInstanceOf(ClassesThatImplementInterface::class, $target); + $this->assertSame('PHPUnit\TestFixture\Metadata\Attribute\Example', $target->interfaceName()); + + $target = array_shift($targets); + $this->assertInstanceOf(Method::class, $target); + $this->assertSame('PHPUnit\TestFixture\Metadata\Attribute\Example', $target->className()); + $this->assertSame('method', $target->methodName()); + + $target = array_shift($targets); + $this->assertInstanceOf(Function_::class, $target); + $this->assertSame('f', $target->functionName()); + } + /** * @param class-string $testCase */ diff --git a/tests/unit/Metadata/Parser/AttributeParserTestCase.php b/tests/unit/Metadata/Parser/AttributeParserTestCase.php index acbccf6b27..80953f8282 100644 --- a/tests/unit/Metadata/Parser/AttributeParserTestCase.php +++ b/tests/unit/Metadata/Parser/AttributeParserTestCase.php @@ -25,6 +25,7 @@ use PHPUnit\TestFixture\Metadata\Attribute\AnotherTest; use PHPUnit\TestFixture\Metadata\Attribute\BackupGlobalsTest; use PHPUnit\TestFixture\Metadata\Attribute\BackupStaticPropertiesTest; +use PHPUnit\TestFixture\Metadata\Attribute\CoversNothingTest; use PHPUnit\TestFixture\Metadata\Attribute\CoversTest; use PHPUnit\TestFixture\Metadata\Attribute\DependencyTest; use PHPUnit\TestFixture\Metadata\Attribute\DisableReturnValueGenerationForTestDoublesTest; @@ -143,7 +144,7 @@ public function test_parses_CoversMethod_attribute_on_class(): void #[TestDox('Parses #[CoversNothing] attribute on class')] public function test_parses_CoversNothing_attribute_on_class(): void { - $metadata = $this->parser()->forClass(CoversTest::class)->isCoversNothing(); + $metadata = $this->parser()->forClass(CoversNothingTest::class)->isCoversNothing(); $this->assertCount(1, $metadata); $this->assertTrue($metadata->asArray()[0]->isCoversNothing()); @@ -557,7 +558,7 @@ public function test_parses_BeforeClass_attribute_on_method(): void #[TestDox('Parses #[CoversNothing] attribute on method')] public function test_parses_CoversNothing_attribute_on_method(): void { - $metadata = $this->parser()->forMethod(CoversTest::class, 'testOne')->isCoversNothing(); + $metadata = $this->parser()->forMethod(CoversNothingTest::class, 'testOne')->isCoversNothing(); $this->assertCount(1, $metadata); $this->assertTrue($metadata->asArray()[0]->isCoversNothing()); @@ -1100,7 +1101,6 @@ public function test_parses_attributes_for_class_and_method(): void $this->assertCount(1, $metadata->isCoversClass()); $this->assertCount(1, $metadata->isCoversFunction()); - $this->assertCount(2, $metadata->isCoversNothing()); } public function test_ignores_attributes_not_owned_by_PHPUnit(): void