feat(mock-plugin): moves mock stuff to their own plugin

This commit is contained in:
Nuno Maduro
2021-05-15 23:43:37 +01:00
parent 9394aa4649
commit 9027411004
6 changed files with 1 additions and 128 deletions

View File

@ -46,7 +46,6 @@
"illuminate/console": "^8.32.1",
"illuminate/support": "^8.32.1",
"laravel/dusk": "^6.13.0",
"mockery/mockery": "^1.4.3",
"pestphp/pest-dev-tools": "dev-master"
},
"minimum-stability": "dev",

View File

@ -3,7 +3,6 @@
declare(strict_types=1);
use Pest\Datasets;
use Pest\Mock;
use Pest\PendingObjects\AfterEachCall;
use Pest\PendingObjects\BeforeEachCall;
use Pest\PendingObjects\TestCall;
@ -105,15 +104,3 @@ function afterAll(Closure $closure): void
{
TestSuite::getInstance()->afterAll->set($closure);
}
if (!function_exists('mock')) {
/**
* Creates a new mock with the given class or object.
*
* @param string|object $object
*/
function mock($object): Mock
{
return new Mock($object);
}
}

View File

@ -1,76 +0,0 @@
<?php
declare(strict_types=1);
namespace Pest;
use InvalidArgumentException;
use Mockery;
use Pest\Exceptions\MissingDependency;
/**
* @mixin \Mockery\MockInterface
*/
final class Mock
{
/**
* The object being mocked.
*
* @readonly
*
* @var \Mockery\MockInterface|\Mockery\LegacyMockInterface
*/
private $mock;
/**
* Creates a new mock instance.
*
* @param string|object $object
*/
public function __construct($object)
{
if (!class_exists(Mockery::class)) {
throw new MissingDependency('Mocking', 'mockery/mockery');
}
$this->mock = Mockery::mock($object);
}
/**
* Define mock expectations.
*
* @param mixed ...$methods
*
* @return \Mockery\MockInterface|\Mockery\LegacyMockInterface
*/
public function expect(...$methods)
{
foreach ($methods as $method => $expectation) {
/* @phpstan-ignore-next-line */
$method = $this->mock
->shouldReceive((string) $method)
->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;
}
/**
* Proxies calls to the original mock object.
*
* @param array<int, mixed> $arguments
*
* @return mixed
*/
public function __call(string $method, array $arguments)
{
/* @phpstan-ignore-next-line */
return $this->mock->{$method}($arguments);
}
}

View File

@ -41,7 +41,6 @@ final class AfterEachRepository
return ChainableClosure::from(function (): void {
if (class_exists(Mockery::class)) {
/* @phpstan-ignore-next-line */
if ($container = Mockery::getContainer()) {
/* @phpstan-ignore-next-line */
$this->addToAssertionCount($container->mockery_getExpectationCount());

View File

@ -77,10 +77,6 @@
✓ it can call chained macro method
✓ it will throw exception from call if no macro exists
PASS Tests\Features\Mocks
✓ it can mock methods
✓ it allows access to the underlying mockery mock
PASS Tests\Features\PendingHigherOrderTests
✓ get 'foo' → get 'bar' → expect true → toBeTrue
✓ get 'foo' → expect true → toBeTrue
@ -223,5 +219,5 @@
✓ it is a test
✓ it uses correct parent class
Tests: 7 skipped, 121 passed
Tests: 7 skipped, 119 passed

View File

@ -1,32 +0,0 @@
<?php
use Mockery\CompositeExpectation;
use Mockery\MockInterface;
interface Http
{
public function get(): string;
}
it('can mock methods', function () {
$mock = mock(Http::class)->expect(
get: fn () => 'foo',
);
expect($mock->get())->toBe('foo');
})->skip(((float) phpversion()) < 8.0);
it('can access to arguments', function () {
$mock = mock(Http::class)->expect(
get: fn ($foo) => $foo,
);
expect($mock->get('foo'))->toBe('foo');
})->skip(((float) phpversion()) < 8.0);
it('allows access to the underlying mockery mock', function () {
$mock = mock(Http::class);
expect($mock->expect())->toBeInstanceOf(MockInterface::class);
expect($mock->shouldReceive())->toBeInstanceOf(CompositeExpectation::class);
})->skip(((float) phpversion()) < 8.0);