From 98e947e0cc8c15cc7ea6eb9f88af1df8309a4343 Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Thu, 5 Sep 2024 01:19:17 +0100 Subject: [PATCH] more fixes --- src/Functions.php | 4 +++- src/PendingCalls/TestCall.php | 15 ++++++++++----- tests/Features/Covers.php | 20 +++++++++++--------- 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/Functions.php b/src/Functions.php index c1fc766e..77a078d7 100644 --- a/src/Functions.php +++ b/src/Functions.php @@ -215,8 +215,10 @@ if (! function_exists('afterAll')) { if (! function_exists('covers')) { /** * Specifies which classes, or functions, a test method covers. + * + * @param array|string $classesOrFunctions */ - function covers(string ...$classesOrFunctions): void + function covers(array|string ...$classesOrFunctions): void { $filename = Backtrace::file(); diff --git a/src/PendingCalls/TestCall.php b/src/PendingCalls/TestCall.php index 7b839daf..1a325d34 100644 --- a/src/PendingCalls/TestCall.php +++ b/src/PendingCalls/TestCall.php @@ -514,14 +514,19 @@ final class TestCall /** * Sets the covered classes or methods. + * + * @param array|string $classesOrFunctions */ - public function covers(string ...$classesOrFunctions): self + public function covers(array|string ...$classesOrFunctions): self { - foreach ($classesOrFunctions as $classOrFunction) { - $isClass = class_exists($classOrFunction) || trait_exists($classOrFunction); - $isMethod = function_exists($classOrFunction); + /** @var array $classesOrFunctions */ + $classesOrFunctions = array_reduce($classesOrFunctions, fn ($carry, $item): array => is_array($item) ? array_merge($carry, $item) : array_merge($carry, [$item]), []); - if (! $isClass && ! $isMethod) { + foreach ($classesOrFunctions as $classOrFunction) { + $isClass = class_exists($classOrFunction) || trait_exists($classOrFunction) || interface_exists($classOrFunction) || enum_exists($classOrFunction); + $isFunction = function_exists($classOrFunction); + + if (! $isClass && ! $isFunction) { throw new InvalidArgumentException(sprintf('No class or method named "%s" has been found.', $classOrFunction)); } diff --git a/tests/Features/Covers.php b/tests/Features/Covers.php index 4fc4f48a..719329a4 100644 --- a/tests/Features/Covers.php +++ b/tests/Features/Covers.php @@ -12,35 +12,37 @@ $runCounter = 0; function testCoversFunction() {} +covers([CoversClass1::class]); + it('uses the correct PHPUnit attribute for class', function () { $attributes = (new ReflectionClass($this))->getAttributes(); 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[2]->getName())->toBe('PHPUnit\Framework\Attributes\CoversFunction'); - expect($attributes[2]->getArguments()[0])->toBe('testCoversFunction'); + expect($attributes[3]->getName())->toBe('PHPUnit\Framework\Attributes\CoversFunction'); + expect($attributes[3]->getArguments()[0])->toBe('testCoversFunction'); })->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[5]->getName())->toBe(CoversClass::class); + expect($attributes[5]->getArguments()[0])->toBe(CoversClass3::class); - expect($attributes[4]->getName())->toBe(CoversFunction::class); - expect($attributes[4]->getArguments()[0])->toBe('testCoversFunction'); + expect($attributes[6]->getName())->toBe(CoversFunction::class); + expect($attributes[6]->getArguments()[0])->toBe('testCoversFunction'); })->covers(CoversClass3::class, 'testCoversFunction'); it('uses the correct PHPUnit attribute for trait', function () { $attributes = (new ReflectionClass($this))->getAttributes(); - expect($attributes[5]->getName())->toBe('PHPUnit\Framework\Attributes\CoversClass'); - expect($attributes[5]->getArguments()[0])->toBe('Tests\Fixtures\Covers\CoversTrait'); + expect($attributes[8]->getName())->toBe('PHPUnit\Framework\Attributes\CoversClass'); + expect($attributes[8]->getArguments()[0])->toBe('Tests\Fixtures\Covers\CoversTrait'); })->coversClass(CoversTrait::class); it('uses the correct PHPUnit attribute for covers nothing', function () {