Pest php coverage annotations

57 views Asked by At

In phpunit you can @covers annotations in docblocks above test methods. Is this also possible with pest php?

Whatever object the test and it functions in pest php return, allow me to call a covers method on it. But it is not documented on how that should work. I cannot pass in a docblock. I can pass in a class name. But I don’t know how to reference a method.

1

There are 1 answers

0
IGP On

You can either cover a class or a function but I don't think it's possible to do anything equivalent to covering a specific method at the moment.

This is the implementation of the covers() method in Pest (found at pes-up/pest/src/PendingCalls/TestCall.php)

/**
 * Sets the covered classes or methods.
 */
public function covers(string ...$classesOrFunctions): self
{
    foreach ($classesOrFunctions as $classOrFunction) {
        $isClass = class_exists($classOrFunction) || trait_exists($classOrFunction);
        $isMethod = function_exists($classOrFunction);

        if (! $isClass && ! $isMethod) {
            throw new InvalidArgumentException(sprintf('No class or method named "%s" has been found.', $classOrFunction));
        }

        if ($isClass) {
            $this->coversClass($classOrFunction);
        } else {
            $this->coversFunction($classOrFunction);
        }
    }

    return $this;
}

This means you can do something like:

use Some\Namespace\YourClass;

test('...', function () { ... })->covers(YourClass::class);

or


test('...', function () { ... })->covers('someGloballyAvailableFunction');

but not

use Some\Namespace\YourClass;

test('...', function () { ... })->covers(YourClass::class . '@someMethod');