From 963b7f43ab9de01ff708da09cf70b629119a53e7 Mon Sep 17 00:00:00 2001 From: Tim Smith Date: Fri, 5 May 2023 17:59:52 +0100 Subject: [PATCH 1/2] Allow traits to be covered --- src/PendingCalls/TestCall.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PendingCalls/TestCall.php b/src/PendingCalls/TestCall.php index 79885fbfd..a4c45a7bc 100644 --- a/src/PendingCalls/TestCall.php +++ b/src/PendingCalls/TestCall.php @@ -227,7 +227,7 @@ public function todo(): self public function covers(string ...$classesOrFunctions): self { foreach ($classesOrFunctions as $classOrFunction) { - $isClass = class_exists($classOrFunction); + $isClass = class_exists($classOrFunction) || trait_exists($classOrFunction); $isMethod = function_exists($classOrFunction); if (! $isClass && ! $isMethod) { From 477492fdd2fa6e220aaac77d3a2f2fb9ab2af9c3 Mon Sep 17 00:00:00 2001 From: Tim Smith Date: Sat, 6 May 2023 08:43:19 +0100 Subject: [PATCH 2/2] Adding tests --- tests/Features/Covers.php | 8 ++++++++ tests/Fixtures/Covers/CoversTrait.php | 8 ++++++++ 2 files changed, 16 insertions(+) create mode 100644 tests/Fixtures/Covers/CoversTrait.php diff --git a/tests/Features/Covers.php b/tests/Features/Covers.php index 3ee6ae817..12ec4ac5b 100644 --- a/tests/Features/Covers.php +++ b/tests/Features/Covers.php @@ -6,6 +6,7 @@ use Tests\Fixtures\Covers\CoversClass1; use Tests\Fixtures\Covers\CoversClass2; use Tests\Fixtures\Covers\CoversClass3; +use Tests\Fixtures\Covers\CoversTrait; $runCounter = 0; @@ -43,6 +44,13 @@ function testCoversFunction() expect($attributes[3]->getArguments()[0])->toBe(CoversClass3::class); })->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'); +})->coversClass(CoversTrait::class); + it('appends CoversNothing to method attributes', function () { $phpDoc = (new ReflectionClass($this))->getMethod($this->name()); diff --git a/tests/Fixtures/Covers/CoversTrait.php b/tests/Fixtures/Covers/CoversTrait.php new file mode 100644 index 000000000..9d660edb9 --- /dev/null +++ b/tests/Fixtures/Covers/CoversTrait.php @@ -0,0 +1,8 @@ +