mirror of
https://github.com/pestphp/pest.git
synced 2026-03-12 10:47:25 +01:00
feat(mock): updates tests
This commit is contained in:
13
src/Mock.php
13
src/Mock.php
@ -4,6 +4,7 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Pest;
|
namespace Pest;
|
||||||
|
|
||||||
|
use InvalidArgumentException;
|
||||||
use Mockery;
|
use Mockery;
|
||||||
use Pest\Exceptions\MissingDependency;
|
use Pest\Exceptions\MissingDependency;
|
||||||
|
|
||||||
@ -44,11 +45,17 @@ final class Mock
|
|||||||
*/
|
*/
|
||||||
public function expect(...$methods)
|
public function expect(...$methods)
|
||||||
{
|
{
|
||||||
foreach ($methods as $method => $result) {
|
foreach ($methods as $method => $expectation) {
|
||||||
/* @phpstan-ignore-next-line */
|
/* @phpstan-ignore-next-line */
|
||||||
$this->mock
|
$method = $this->mock
|
||||||
->shouldReceive((string) $method)
|
->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;
|
return $this->mock;
|
||||||
|
|||||||
@ -79,7 +79,7 @@
|
|||||||
|
|
||||||
PASS Tests\Features\Mocks
|
PASS Tests\Features\Mocks
|
||||||
✓ it can mock methods
|
✓ it can mock methods
|
||||||
✓ access to the mock object
|
✓ it allows access to the underlying mockery mock
|
||||||
|
|
||||||
PASS Tests\Features\PendingHigherOrderTests
|
PASS Tests\Features\PendingHigherOrderTests
|
||||||
✓ get 'foo' → get 'bar' → expect true → toBeTrue
|
✓ get 'foo' → get 'bar' → expect true → toBeTrue
|
||||||
|
|||||||
@ -10,15 +10,15 @@ interface Http
|
|||||||
|
|
||||||
it('can mock methods', function () {
|
it('can mock methods', function () {
|
||||||
$mock = mock(Http::class)->expect(
|
$mock = mock(Http::class)->expect(
|
||||||
get: 'foo',
|
get: fn () => 'foo',
|
||||||
);
|
);
|
||||||
|
|
||||||
expect($mock->get())->toBe('foo');
|
expect($mock->get())->toBe('foo');
|
||||||
})->skip(((float) phpversion()) < 8.0);
|
})->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);
|
$mock = mock(Http::class);
|
||||||
expect($mock->expect())->toBeInstanceOf(MockInterface::class);
|
|
||||||
|
|
||||||
|
expect($mock->expect())->toBeInstanceOf(MockInterface::class);
|
||||||
expect($mock->shouldReceive())->toBeInstanceOf(CompositeExpectation::class);
|
expect($mock->shouldReceive())->toBeInstanceOf(CompositeExpectation::class);
|
||||||
})->skip(((float) phpversion()) < 8.0);
|
})->skip(((float) phpversion()) < 8.0);
|
||||||
|
|||||||
Reference in New Issue
Block a user