From 7023cec43251dce26f41d18b93b616ffe6649679 Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Sat, 15 May 2021 01:01:46 +0100 Subject: [PATCH] feat(mock): updates tests --- src/Mock.php | 13 ++++++++++--- tests/.snapshots/success.txt | 2 +- tests/Features/Mocks.php | 6 +++--- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/Mock.php b/src/Mock.php index 31d35fdb..12dfed94 100644 --- a/src/Mock.php +++ b/src/Mock.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace Pest; +use InvalidArgumentException; use Mockery; use Pest\Exceptions\MissingDependency; @@ -44,11 +45,17 @@ final class Mock */ public function expect(...$methods) { - foreach ($methods as $method => $result) { + foreach ($methods as $method => $expectation) { /* @phpstan-ignore-next-line */ - $this->mock + $method = $this->mock ->shouldReceive((string) $method) - ->andReturn($result); + ->once(); + + if (!is_callable($expectation)) { + throw new InvalidArgumentException(sprintf('Method %s from %s expects a callable as expectation.', $method, $method->mock()->mockery_getName(), )); + } + + $method->andReturnUsing($expectation); } return $this->mock; diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index 58d5baf5..b471823f 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -79,7 +79,7 @@ PASS Tests\Features\Mocks ✓ it can mock methods - ✓ access to the mock object + ✓ it allows access to the underlying mockery mock PASS Tests\Features\PendingHigherOrderTests ✓ get 'foo' → get 'bar' → expect true → toBeTrue diff --git a/tests/Features/Mocks.php b/tests/Features/Mocks.php index 51b896d5..ac2bb545 100644 --- a/tests/Features/Mocks.php +++ b/tests/Features/Mocks.php @@ -10,15 +10,15 @@ interface Http it('can mock methods', function () { $mock = mock(Http::class)->expect( - get: 'foo', + get: fn () => 'foo', ); expect($mock->get())->toBe('foo'); })->skip(((float) phpversion()) < 8.0); -test('access to the mock object', function () { +it('allows access to the underlying mockery mock', function () { $mock = mock(Http::class); - expect($mock->expect())->toBeInstanceOf(MockInterface::class); + expect($mock->expect())->toBeInstanceOf(MockInterface::class); expect($mock->shouldReceive())->toBeInstanceOf(CompositeExpectation::class); })->skip(((float) phpversion()) < 8.0);