diff --git a/src/Factories/Attributes/Covers.php b/src/Factories/Attributes/Covers.php index 2c6234f3..946bd1fd 100644 --- a/src/Factories/Attributes/Covers.php +++ b/src/Factories/Attributes/Covers.php @@ -4,6 +4,8 @@ declare(strict_types=1); namespace Pest\Factories\Attributes; +use Pest\Factories\Covers\CoversClass; +use Pest\Factories\Covers\CoversFunction; use Pest\Factories\TestCaseMethodFactory; /** @@ -22,11 +24,16 @@ final class Covers public function __invoke(TestCaseMethodFactory $method, array $attributes): array { foreach ($method->covers as $covering) { - if (is_array($covering)) { - $attributes[] = "#[\PHPUnit\Framework\Attributes\CoversClass({$covering[0]}]"; - $attributes[] = "#[\PHPUnit\Framework\Attributes\CoversFunction({$covering[1]}]"; + if ($covering instanceof CoversClass) { + $attributes[] = "#[\PHPUnit\Framework\Attributes\CoversClass({$covering->class}]"; + + if (!is_null($covering->method)) { + $attributes[] = "#[\PHPUnit\Framework\Attributes\CoversFunction({$covering->method}]"; + } + } else if ($covering instanceof CoversFunction) { + $attributes[] = "#[\PHPUnit\Framework\Attributes\CoversFunction({$covering->function}]"; } else { - $attributes[] = "#[\PHPUnit\Framework\Attributes\CoversClass($covering)]"; + $attributes[] = "#[\PHPUnit\Framework\Attributes\CoversNothing]"; } } diff --git a/src/Factories/Covers/CoversClass.php b/src/Factories/Covers/CoversClass.php new file mode 100644 index 00000000..934660fd --- /dev/null +++ b/src/Factories/Covers/CoversClass.php @@ -0,0 +1,16 @@ + + */ + private static array $attributes = [ + Attributes\Covers::class, + ]; + /** * The FQN of the Test Case class. * @@ -142,7 +151,7 @@ final class TestCaseFactory } $methodsCode = implode('', array_map( - fn (TestCaseMethodFactory $methodFactory) => $methodFactory->buildForEvaluation($classFQN, self::$annotations), + fn (TestCaseMethodFactory $methodFactory) => $methodFactory->buildForEvaluation($classFQN, self::$annotations, self::$attributes), $methods )); diff --git a/src/Factories/TestCaseMethodFactory.php b/src/Factories/TestCaseMethodFactory.php index 2c029162..aac6c8dc 100644 --- a/src/Factories/TestCaseMethodFactory.php +++ b/src/Factories/TestCaseMethodFactory.php @@ -50,7 +50,7 @@ final class TestCaseMethodFactory /** * The covered classes and methods, if any. * - * @var array + * @var array */ public array $covers = []; diff --git a/src/PendingCalls/TestCall.php b/src/PendingCalls/TestCall.php index 53a0cd2f..a7c1621e 100644 --- a/src/PendingCalls/TestCall.php +++ b/src/PendingCalls/TestCall.php @@ -5,7 +5,9 @@ declare(strict_types=1); namespace Pest\PendingCalls; use Closure; -use InvalidArgumentException; +use Pest\Factories\Covers\CoversClass; +use Pest\Factories\Covers\CoversFunction; +use Pest\Factories\Covers\CoversNothing; use Pest\Factories\TestCaseMethodFactory; use Pest\Support\Backtrace; use Pest\Support\HigherOrderCallables; @@ -171,19 +173,35 @@ final class TestCall /** * Sets the covered class and method. + * + * @param class-string $class + * @param string|null $method */ - public function covers(string|array ...$classes): TestCall + public function covers(string $class, ?string $method = null): TestCall { - foreach ($classes as $i => $class) { - if (is_array($class) && count($class) !== 2) { - throw new InvalidArgumentException(sprintf( - 'The #%s covered class must be an array with exactly 2 items: class and method name.', - $i - )); - } + $this->testCaseMethod->covers[] = new CoversClass(...func_get_args()); - $this->testCaseMethod->covers[] = $class; - } + return $this; + } + + /** + * Sets the covered function. + * + * @param string $method + */ + public function coversFunction(string $method): TestCall + { + $this->testCaseMethod->covers[] = new CoversFunction(...func_get_args()); + + return $this; + } + + /** + * Sets that the current test covers nothing. + */ + public function coversNothing(): TestCall + { + $this->testCaseMethod->covers[] = new CoversNothing(); return $this; }