Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Dec 23, 2024
1 parent fe933b8 commit 035051a
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 6 deletions.
22 changes: 22 additions & 0 deletions tests/_files/Metadata/Attribute/tests/CoversNothingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* 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
{
}
}
3 changes: 0 additions & 3 deletions tests/_files/Metadata/Attribute/tests/CoversTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')]
Expand All @@ -24,10 +23,8 @@
#[CoversClassesThatImplementInterface(Example::class)]
#[CoversMethod(Example::class, 'method')]
#[CoversFunction('f')]
#[CoversNothing]
final class CoversTest extends TestCase
{
#[CoversNothing]
public function testOne(): void
{
}
Expand Down
3 changes: 3 additions & 0 deletions tests/_files/Metadata/Attribute/tests/UsesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,7 @@
#[UsesFunction('f')]
final class UsesTest extends TestCase
{
public function testOne(): void
{
}
}
82 changes: 82 additions & 0 deletions tests/unit/Metadata/Api/CodeCoverageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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
*/
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/Metadata/Parser/AttributeParserTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 035051a

Please sign in to comment.