chore: adjusts tests

This commit is contained in:
Nuno Maduro
2021-08-26 21:14:56 +01:00
parent dbf3c0a8cf
commit 2125bf9668
4 changed files with 23 additions and 11 deletions

View File

@ -6,7 +6,7 @@ namespace Pest;
use BadMethodCallException; use BadMethodCallException;
use Closure; use Closure;
use LogicException; use InvalidArgumentException;
use Pest\Concerns\Extendable; use Pest\Concerns\Extendable;
use Pest\Concerns\RetrievesValues; use Pest\Concerns\RetrievesValues;
use Pest\Support\Arr; use Pest\Support\Arr;
@ -758,8 +758,7 @@ final class Expectation
/** /**
* Asserts that executing value throws an exception. * Asserts that executing value throws an exception.
* *
* @param string|Closure $exception string: the exception class * @param (Closure(Throwable): mixed)|string $exception
* Closure: first parameter = exception class
*/ */
public function toThrow($exception, string $exceptionMessage = null): Expectation public function toThrow($exception, string $exceptionMessage = null): Expectation
{ {
@ -770,11 +769,11 @@ final class Expectation
$parameters = (new ReflectionFunction($exception))->getParameters(); $parameters = (new ReflectionFunction($exception))->getParameters();
if (1 !== count($parameters)) { 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) { 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(); $exception = $type->getName();
@ -782,14 +781,14 @@ final class Expectation
try { try {
($this->value)(); ($this->value)();
} catch (Throwable $e) { } catch (Throwable $e) { // @phpstan-ignore-line
if (!class_exists($exception)) { if (!class_exists($exception)) {
Assert::assertStringContainsString($exception, $e->getMessage()); Assert::assertStringContainsString($exception, $e->getMessage());
return $this; return $this;
} }
if ($exceptionMessage) { if ($exceptionMessage !== null) {
Assert::assertStringContainsString($exceptionMessage, $e->getMessage()); Assert::assertStringContainsString($exceptionMessage, $e->getMessage());
} }

View File

@ -22,7 +22,7 @@ use PHPUnit\Framework\TestCase;
final class TestRepository final class TestRepository
{ {
/** /**
* @var string * @var non-empty-string
*/ */
private const SEPARATOR = '>>>'; private const SEPARATOR = '>>>';

View File

@ -449,6 +449,19 @@
✓ failures ✓ failures
✓ not 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 PASS Tests\Features\Helpers
✓ it can set/get properties on $this ✓ it can set/get properties on $this
✓ it throws error if property do not exist ✓ it throws error if property do not exist
@ -649,5 +662,5 @@
✓ it is a test ✓ it is a test
✓ it uses correct parent class ✓ it uses correct parent class
Tests: 4 incompleted, 9 skipped, 421 passed Tests: 4 incompleted, 9 skipped, 432 passed

View File

@ -53,8 +53,8 @@ test('not failures', function () {
test('closure missing parameter', function () { test('closure missing parameter', function () {
expect(function () {})->toThrow(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 () { test('closure missing type-hint', function () {
expect(function () {})->toThrow(function ($e) {}); 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.');