From 2125bf9668a20c9cef89add6114f6742af9be8eb Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Thu, 26 Aug 2021 21:14:56 +0100 Subject: [PATCH] chore: adjusts tests --- src/Expectation.php | 13 ++++++------- src/Repositories/TestRepository.php | 2 +- tests/.snapshots/success.txt | 15 ++++++++++++++- tests/Features/Expect/toThrow.php | 4 ++-- 4 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/Expectation.php b/src/Expectation.php index 5d32c195..9491fa6b 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -6,7 +6,7 @@ namespace Pest; use BadMethodCallException; use Closure; -use LogicException; +use InvalidArgumentException; use Pest\Concerns\Extendable; use Pest\Concerns\RetrievesValues; use Pest\Support\Arr; @@ -758,8 +758,7 @@ final class Expectation /** * Asserts that executing value throws an exception. * - * @param string|Closure $exception string: the exception class - * Closure: first parameter = exception class + * @param (Closure(Throwable): mixed)|string $exception */ public function toThrow($exception, string $exceptionMessage = null): Expectation { @@ -770,11 +769,11 @@ final class Expectation $parameters = (new ReflectionFunction($exception))->getParameters(); if (1 !== count($parameters)) { - throw new LogicException('The "toThrow" closure must have a single parameter type-hinted as the class string'); + throw new InvalidArgumentException('The given closure must have a single parameter type-hinted as the class string.'); } if (!($type = $parameters[0]->getType()) instanceof ReflectionNamedType) { - throw new LogicException('The "toThrow" closure\'s parameter must be type-hinted as the class string'); + throw new InvalidArgumentException('The given closure\'s parameter must be type-hinted as the class string.'); } $exception = $type->getName(); @@ -782,14 +781,14 @@ final class Expectation try { ($this->value)(); - } catch (Throwable $e) { + } catch (Throwable $e) { // @phpstan-ignore-line if (!class_exists($exception)) { Assert::assertStringContainsString($exception, $e->getMessage()); return $this; } - if ($exceptionMessage) { + if ($exceptionMessage !== null) { Assert::assertStringContainsString($exceptionMessage, $e->getMessage()); } diff --git a/src/Repositories/TestRepository.php b/src/Repositories/TestRepository.php index c3fe2877..e522677a 100644 --- a/src/Repositories/TestRepository.php +++ b/src/Repositories/TestRepository.php @@ -22,7 +22,7 @@ use PHPUnit\Framework\TestCase; final class TestRepository { /** - * @var string + * @var non-empty-string */ private const SEPARATOR = '>>>'; diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index 30b28483..3ff086f1 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -449,6 +449,19 @@ ✓ failures ✓ not failures + PASS Tests\Features\Expect\toThrow + ✓ passes + ✓ failures 1 + ✓ failures 2 + ✓ failures 3 + ✓ failures 4 + ✓ failures 5 + ✓ failures 6 + ✓ failures 7 + ✓ not failures + ✓ closure missing parameter + ✓ closure missing type-hint + PASS Tests\Features\Helpers ✓ it can set/get properties on $this ✓ it throws error if property do not exist @@ -649,5 +662,5 @@ ✓ it is a test ✓ it uses correct parent class - Tests: 4 incompleted, 9 skipped, 421 passed + Tests: 4 incompleted, 9 skipped, 432 passed \ No newline at end of file diff --git a/tests/Features/Expect/toThrow.php b/tests/Features/Expect/toThrow.php index 290003eb..434ced44 100644 --- a/tests/Features/Expect/toThrow.php +++ b/tests/Features/Expect/toThrow.php @@ -53,8 +53,8 @@ test('not failures', function () { test('closure missing parameter', function () { expect(function () {})->toThrow(function () {}); -})->throws(LogicException::class, 'The "toThrow" closure must have a single parameter type-hinted as the class string'); +})->throws(InvalidArgumentException::class, 'The given closure must have a single parameter type-hinted as the class string.'); test('closure missing type-hint', function () { expect(function () {})->toThrow(function ($e) {}); -})->throws(LogicException::class, 'The "toThrow" closure\'s parameter must be type-hinted as the class string'); +})->throws(InvalidArgumentException::class, 'The given closure\'s parameter must be type-hinted as the class string.');