feat: always use attributes instead of annotations

This commit is contained in:
Nuno Maduro
2024-01-05 18:00:14 +00:00
parent 04d2fa5ce8
commit 53dc9ffa06
20 changed files with 152 additions and 328 deletions

View File

@ -3,8 +3,8 @@
use Pest\PendingCalls\TestCall;
use Pest\TestSuite;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\CoversFunction;
use Tests\Fixtures\Covers\CoversClass1;
use Tests\Fixtures\Covers\CoversClass2;
use Tests\Fixtures\Covers\CoversClass3;
use Tests\Fixtures\Covers\CoversTrait;
@ -17,45 +17,39 @@ function testCoversFunction()
it('uses the correct PHPUnit attribute for class', function () {
$attributes = (new ReflectionClass($this))->getAttributes();
expect($attributes[0]->getName())->toBe('PHPUnit\Framework\Attributes\CoversClass');
expect($attributes[0]->getArguments()[0])->toBe('Tests\Fixtures\Covers\CoversClass1');
expect($attributes[1]->getName())->toBe('PHPUnit\Framework\Attributes\CoversClass');
expect($attributes[1]->getArguments()[0])->toBe('Tests\Fixtures\Covers\CoversClass1');
})->coversClass(CoversClass1::class);
it('uses the correct PHPUnit attribute for function', function () {
$attributes = (new ReflectionClass($this))->getAttributes();
expect($attributes[1]->getName())->toBe('PHPUnit\Framework\Attributes\CoversFunction');
expect($attributes[1]->getArguments()[0])->toBe('testCoversFunction');
expect($attributes[2]->getName())->toBe('PHPUnit\Framework\Attributes\CoversFunction');
expect($attributes[2]->getArguments()[0])->toBe('testCoversFunction');
})->coversFunction('testCoversFunction');
it('removes duplicated attributes', function () {
$attributes = (new ReflectionClass($this))->getAttributes();
expect($attributes[2]->getName())->toBe(CoversClass::class);
expect($attributes[2]->getArguments()[0])->toBe(CoversClass2::class);
})
->coversClass(CoversClass2::class, CoversClass1::class)
->coversFunction('testCoversFunction');
it('guesses if the given argument is a class or function', function () {
$attributes = (new ReflectionClass($this))->getAttributes();
expect($attributes[3]->getName())->toBe(CoversClass::class);
expect($attributes[3]->getArguments()[0])->toBe(CoversClass3::class);
expect($attributes[4]->getName())->toBe(CoversFunction::class);
expect($attributes[4]->getArguments()[0])->toBe('testCoversFunction');
})->covers(CoversClass3::class, 'testCoversFunction');
it('uses the correct PHPUnit attribute for trait', function () {
$attributes = (new ReflectionClass($this))->getAttributes();
expect($attributes[4]->getName())->toBe('PHPUnit\Framework\Attributes\CoversClass');
expect($attributes[4]->getArguments()[0])->toBe('Tests\Fixtures\Covers\CoversTrait');
expect($attributes[5]->getName())->toBe('PHPUnit\Framework\Attributes\CoversClass');
expect($attributes[5]->getArguments()[0])->toBe('Tests\Fixtures\Covers\CoversTrait');
})->coversClass(CoversTrait::class);
it('uses the correct PHPUnit attribute for covers nothing', function () {
$attributes = (new ReflectionClass($this))->getAttributes();
$attributes = (new ReflectionMethod($this, $this->name()))->getAttributes();
expect($attributes[5]->getName())->toBe('PHPUnit\Framework\Attributes\CoversNothing');
expect($attributes[5]->getArguments())->toHaveCount(0);
expect($attributes[2]->getName())->toBe('PHPUnit\Framework\Attributes\CoversNothing');
expect($attributes[2]->getArguments())->toHaveCount(0);
})->coversNothing();
it('throws exception if no class nor method has been found', function () {

View File

@ -12,7 +12,7 @@ class ExecutedTest extends TestCase
{
public static $executed = false;
/** @test */
#[Test]
public function testThatGetsExecuted(): void
{
self::$executed = true;

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Tests\CustomTestCase;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use function PHPUnit\Framework\assertTrue;
@ -15,7 +16,7 @@ class ParentTest extends TestCase
return false;
}
/** @test */
#[Test]
public function testOverrideMethod(): void
{
assertTrue($this->getEntity() || true);