Skip to content

Commit

Permalink
Merge pull request #804 from titantwentyone/2.x
Browse files Browse the repository at this point in the history
Allow traits to be covered
  • Loading branch information
nunomaduro committed May 6, 2023
2 parents a34767f + 477492f commit cada5c5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/PendingCalls/TestCall.php
Expand Up @@ -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) {
Expand Down
8 changes: 8 additions & 0 deletions tests/Features/Covers.php
Expand Up @@ -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;

Expand Down Expand Up @@ -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());

Expand Down
8 changes: 8 additions & 0 deletions tests/Fixtures/Covers/CoversTrait.php
@@ -0,0 +1,8 @@
<?php

namespace Tests\Fixtures\Covers;

trait CoversTrait
{

}

0 comments on commit cada5c5

Please sign in to comment.