move coversNothing to method annotations

This commit is contained in:
danilopolani
2022-03-09 11:05:10 +01:00
parent a027e24e3c
commit 3795870150
6 changed files with 83 additions and 31 deletions

View File

@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
namespace Pest\Factories\Annotations;
use Pest\Factories\Covers\CoversNothing as CoversNothingFactory;
use Pest\Factories\TestCaseMethodFactory;
/**
* @internal
*/
final class CoversNothing
{
/**
* Adds annotations regarding the "depends" feature.
*
* @param array<int, string> $annotations
*
* @return array<int, string>
*/
public function __invoke(TestCaseMethodFactory $method, array $annotations): array
{
if (($method->covers[0] ?? null) instanceof CoversNothingFactory) {
$annotations[] = '@coversNothing';
}
return $annotations;
}
}

View File

@ -39,8 +39,6 @@ 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

@ -33,6 +33,7 @@ final class TestCaseFactory
private static array $annotations = [ private static array $annotations = [
Annotations\Depends::class, Annotations\Depends::class,
Annotations\Groups::class, Annotations\Groups::class,
Annotations\CoversNothing::class,
]; ];
/** /**
@ -150,25 +151,31 @@ final class TestCaseFactory
$classFQN .= $className; $classFQN .= $className;
} }
$methodsCode = implode('', array_map( $classAvailableAttributes = array_filter(self::$attributes, fn (string $attribute) => $attribute::ABOVE_CLASS);
fn (TestCaseMethodFactory $methodFactory) => $methodFactory->buildForEvaluation($classFQN, self::$annotations), $methodAvailableAttributes = array_filter(self::$attributes, fn (string $attribute) => !$attribute::ABOVE_CLASS);
$methods
));
$classAttributesCode = []; $classAttributes = [];
$classAvailableAttributes = array_filter(self::$attributes, fn (string $attribute) => $attribute::ABOVE_CLASS);
foreach ($classAvailableAttributes as $attribute) { foreach ($classAvailableAttributes as $attribute) {
$classAttributesCode = array_reduce( $classAttributes = array_reduce(
$methods, $methods,
fn (array $carry, TestCaseMethodFactory $methodFactory) => (new $attribute())->__invoke($methodFactory, $carry), fn (array $carry, TestCaseMethodFactory $methodFactory) => (new $attribute())->__invoke($methodFactory, $carry),
$classAttributesCode $classAttributes
); );
} }
$methodsCode = implode('', array_map(
fn (TestCaseMethodFactory $methodFactory) => $methodFactory->buildForEvaluation(
$classFQN,
self::$annotations,
$methodAvailableAttributes
),
$methods
));
$classAttributesCode = implode('', array_map( $classAttributesCode = implode('', array_map(
static fn (string $attribute) => sprintf("\n %s", $attribute), static fn (string $attribute) => sprintf("\n %s", $attribute),
array_unique($classAttributesCode), array_unique($classAttributes),
)); ));
try { try {

View File

@ -114,8 +114,9 @@ final class TestCaseMethodFactory
* Creates a PHPUnit method as a string ready for evaluation. * Creates a PHPUnit method as a string ready for evaluation.
* *
* @param array<int, class-string> $annotationsToUse * @param array<int, class-string> $annotationsToUse
* @param array<int, class-string<\Pest\Factories\Attributes\Attribute>> $attributesToUse
*/ */
public function buildForEvaluation(string $classFQN, array $annotationsToUse): string public function buildForEvaluation(string $classFQN, array $annotationsToUse, array $attributesToUse): string
{ {
if ($this->description === null) { if ($this->description === null) {
throw ShouldNotHappen::fromMessage('The test description may not be empty.'); throw ShouldNotHappen::fromMessage('The test description may not be empty.');
@ -129,12 +130,18 @@ final class TestCaseMethodFactory
$datasetsCode = ''; $datasetsCode = '';
$annotations = ['@test']; $annotations = ['@test'];
$attributes = [];
foreach ($annotationsToUse as $annotation) { foreach ($annotationsToUse as $annotation) {
/** @phpstan-ignore-next-line */ /** @phpstan-ignore-next-line */
$annotations = (new $annotation())->__invoke($this, $annotations); $annotations = (new $annotation())->__invoke($this, $annotations);
} }
foreach ($attributesToUse as $attribute) {
/** @phpstan-ignore-next-line */
$attributes = (new $attribute())->__invoke($this, $attributes);
}
if (count($this->datasets) > 0) { if (count($this->datasets) > 0) {
$dataProviderName = $methodName . '_dataset'; $dataProviderName = $methodName . '_dataset';
$annotations[] = "@dataProvider $dataProviderName"; $annotations[] = "@dataProvider $dataProviderName";
@ -145,10 +152,15 @@ final class TestCaseMethodFactory
static fn ($annotation) => sprintf("\n * %s", $annotation), $annotations, static fn ($annotation) => sprintf("\n * %s", $annotation), $annotations,
)); ));
$attributes = implode('', array_map(
static fn ($attribute) => sprintf("\n %s", $attribute), $attributes,
));
return <<<EOF return <<<EOF
/**$annotations /**$annotations
*/ */
$attributes
public function $methodName() public function $methodName()
{ {
return \$this->__runTest( return \$this->__runTest(

View File

@ -224,7 +224,7 @@ final class TestCall
*/ */
public function coversNothing(): TestCall public function coversNothing(): TestCall
{ {
$this->testCaseMethod->covers[] = new CoversNothing(); $this->testCaseMethod->covers = [new CoversNothing()];
return $this; return $this;
} }

View File

@ -35,37 +35,42 @@ it('uses the correct PHPUnit attribute for function', function () {
expect($attributes[1]->getArguments()[0])->toBe('foo'); expect($attributes[1]->getArguments()[0])->toBe('foo');
})->coversFunction('foo'); })->coversFunction('foo');
it('uses the correct PHPUnit attribute for nothing', function () {
$attributes = (new ReflectionClass($this))->getAttributes();
expect($attributes[2]->getName())->toBe('PHPUnit\Framework\Attributes\CoversNothing');
})->coversNothing();
it('removes duplicated attributes', function () { it('removes duplicated attributes', function () {
$attributes = (new ReflectionClass($this))->getAttributes(); $attributes = (new ReflectionClass($this))->getAttributes();
expect($attributes[2]->getName())->toBe('PHPUnit\Framework\Attributes\CoversClass');
expect($attributes[2]->getArguments()[0])->toBe('P\Tests\Features\TestCoversClass2');
expect($attributes[3]->getName())->toBe('PHPUnit\Framework\Attributes\CoversClass'); expect($attributes[3]->getName())->toBe('PHPUnit\Framework\Attributes\CoversClass');
expect($attributes[3]->getArguments()[0])->toBe('P\Tests\Features\TestCoversClass2'); expect($attributes[3]->getArguments()[0])->toBe('Pest\Factories\Attributes\Covers');
expect($attributes[4]->getName())->toBe('PHPUnit\Framework\Attributes\CoversClass'); expect($attributes[4]->getName())->toBe('PHPUnit\Framework\Attributes\CoversFunction');
expect($attributes[4]->getArguments()[0])->toBe('Pest\Factories\Attributes\Covers'); expect($attributes[4]->getArguments()[0])->toBe('bar');
expect($attributes[5]->getName())->toBe('PHPUnit\Framework\Attributes\CoversFunction'); expect($attributes[5]->getName())->toBe('PHPUnit\Framework\Attributes\CoversFunction');
expect($attributes[5]->getArguments()[0])->toBe('bar'); expect($attributes[5]->getArguments()[0])->toBe('baz');
expect($attributes[6]->getName())->toBe('PHPUnit\Framework\Attributes\CoversFunction');
expect($attributes[6]->getArguments()[0])->toBe('baz');
}) })
->coversClass(TestCoversClass2::class, TestCoversClass1::class, Covers::class) ->coversClass(TestCoversClass2::class, TestCoversClass1::class, Covers::class)
->coversNothing()
->coversFunction('bar', 'foo', 'baz'); ->coversFunction('bar', 'foo', 'baz');
it('guesses if the given argument is a class or function', function () { it('guesses if the given argument is a class or function', function () {
$attributes = (new ReflectionClass($this))->getAttributes(); $attributes = (new ReflectionClass($this))->getAttributes();
expect($attributes[7]->getName())->toBe('PHPUnit\Framework\Attributes\CoversClass'); expect($attributes[6]->getName())->toBe('PHPUnit\Framework\Attributes\CoversClass');
expect($attributes[7]->getArguments()[0])->toBe('P\Tests\Features\TestCoversClass3'); expect($attributes[6]->getArguments()[0])->toBe('P\Tests\Features\TestCoversClass3');
expect($attributes[8]->getName())->toBe('PHPUnit\Framework\Attributes\CoversFunction'); expect($attributes[7]->getName())->toBe('PHPUnit\Framework\Attributes\CoversFunction');
expect($attributes[8]->getArguments()[0])->toBe('testCoversFunction'); expect($attributes[7]->getArguments()[0])->toBe('testCoversFunction');
})->covers(TestCoversClass3::class, 'testCoversFunction'); })->covers(TestCoversClass3::class, 'testCoversFunction');
it('appends CoversNothing to method attributes', function () {
$phpDoc = (new ReflectionClass($this))->getMethod($this->getName());
expect(str_contains($phpDoc->getDocComment(), '* @coversNothing'))->toBeTrue();
})->coversNothing();
it('does not append CoversNothing to other methods', function () {
$phpDoc = (new ReflectionClass($this))->getMethod($this->getName());
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');