feat: moves covers nothing to attribute

This commit is contained in:
Nuno Maduro
2024-01-05 14:37:33 +00:00
parent 7764a7a162
commit 04d2fa5ce8
4 changed files with 6 additions and 45 deletions

View File

@ -1,27 +0,0 @@
<?php
declare(strict_types=1);
namespace Pest\Factories\Annotations;
use Pest\Contracts\AddsAnnotations;
use Pest\Factories\Covers\CoversNothing as CoversNothingFactory;
use Pest\Factories\TestCaseMethodFactory;
/**
* @internal
*/
final class CoversNothing implements AddsAnnotations
{
/**
* {@inheritdoc}
*/
public function __invoke(TestCaseMethodFactory $method, array $annotations): array
{
if (($method->covers[0] ?? null) instanceof CoversNothingFactory) {
$annotations[] = '@coversNothing';
}
return $annotations;
}
}

View File

@ -36,6 +36,8 @@ final class Covers extends Attribute
$attributes[] = "#[\PHPUnit\Framework\Attributes\CoversClass({$covering->class}::class)]"; $attributes[] = "#[\PHPUnit\Framework\Attributes\CoversClass({$covering->class}::class)]";
} elseif ($covering instanceof CoversFunction) { } elseif ($covering instanceof CoversFunction) {
$attributes[] = "#[\PHPUnit\Framework\Attributes\CoversFunction('{$covering->function}')]"; $attributes[] = "#[\PHPUnit\Framework\Attributes\CoversFunction('{$covering->function}')]";
} else {
$attributes[] = "#[\PHPUnit\Framework\Attributes\CoversNothing()]";
} }
} }

View File

@ -34,7 +34,6 @@ final class TestCaseFactory
private const ANNOTATIONS = [ private const ANNOTATIONS = [
Annotations\Depends::class, Annotations\Depends::class,
Annotations\Groups::class, Annotations\Groups::class,
Annotations\CoversNothing::class,
Annotations\TestDox::class, Annotations\TestDox::class,
]; ];

View File

@ -51,28 +51,15 @@ it('uses the correct PHPUnit attribute for trait', function () {
expect($attributes[4]->getArguments()[0])->toBe('Tests\Fixtures\Covers\CoversTrait'); expect($attributes[4]->getArguments()[0])->toBe('Tests\Fixtures\Covers\CoversTrait');
})->coversClass(CoversTrait::class); })->coversClass(CoversTrait::class);
it('appends CoversNothing to method attributes', function () { it('uses the correct PHPUnit attribute for covers nothing', function () {
$phpDoc = (new ReflectionClass($this))->getMethod($this->name()); $attributes = (new ReflectionClass($this))->getAttributes();
expect(str_contains($phpDoc->getDocComment(), '* @coversNothing'))->toBeTrue(); expect($attributes[5]->getName())->toBe('PHPUnit\Framework\Attributes\CoversNothing');
expect($attributes[5]->getArguments())->toHaveCount(0);
})->coversNothing(); })->coversNothing();
it('does not append CoversNothing to other methods', function () {
$phpDoc = (new ReflectionClass($this))->getMethod($this->name());
expect(str_contains($phpDoc->getDocComment(), '* @coversNothing'))->toBeFalse();
});
it('throws exception if no class nor method has been found', function () { it('throws exception if no class nor method has been found', function () {
$testCall = new TestCall(TestSuite::getInstance(), 'filename', 'description', fn () => 'closure'); $testCall = new TestCall(TestSuite::getInstance(), 'filename', 'description', fn () => 'closure');
$testCall->covers('fakeName'); $testCall->covers('fakeName');
})->throws(InvalidArgumentException::class, 'No class or method named "fakeName" has been found.'); })->throws(InvalidArgumentException::class, 'No class or method named "fakeName" has been found.');
describe('a "describe" group of tests', function () {
it('does not append CoversNothing to method attributes', function () {
$phpDoc = (new ReflectionClass($this))->getMethod($this->name());
expect(str_contains($phpDoc->getDocComment(), '* @coversNothing'))->toBeTrue();
});
})->coversNothing();