mirror of
https://github.com/pestphp/pest.git
synced 2026-03-12 18:57:22 +01:00
feat(mock-plugin): moves mock stuff to their own plugin
This commit is contained in:
@ -46,7 +46,6 @@
|
|||||||
"illuminate/console": "^8.32.1",
|
"illuminate/console": "^8.32.1",
|
||||||
"illuminate/support": "^8.32.1",
|
"illuminate/support": "^8.32.1",
|
||||||
"laravel/dusk": "^6.13.0",
|
"laravel/dusk": "^6.13.0",
|
||||||
"mockery/mockery": "^1.4.3",
|
|
||||||
"pestphp/pest-dev-tools": "dev-master"
|
"pestphp/pest-dev-tools": "dev-master"
|
||||||
},
|
},
|
||||||
"minimum-stability": "dev",
|
"minimum-stability": "dev",
|
||||||
|
|||||||
@ -3,7 +3,6 @@
|
|||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
use Pest\Datasets;
|
use Pest\Datasets;
|
||||||
use Pest\Mock;
|
|
||||||
use Pest\PendingObjects\AfterEachCall;
|
use Pest\PendingObjects\AfterEachCall;
|
||||||
use Pest\PendingObjects\BeforeEachCall;
|
use Pest\PendingObjects\BeforeEachCall;
|
||||||
use Pest\PendingObjects\TestCall;
|
use Pest\PendingObjects\TestCall;
|
||||||
@ -105,15 +104,3 @@ function afterAll(Closure $closure): void
|
|||||||
{
|
{
|
||||||
TestSuite::getInstance()->afterAll->set($closure);
|
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
76
src/Mock.php
76
src/Mock.php
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -41,7 +41,6 @@ final class AfterEachRepository
|
|||||||
|
|
||||||
return ChainableClosure::from(function (): void {
|
return ChainableClosure::from(function (): void {
|
||||||
if (class_exists(Mockery::class)) {
|
if (class_exists(Mockery::class)) {
|
||||||
/* @phpstan-ignore-next-line */
|
|
||||||
if ($container = Mockery::getContainer()) {
|
if ($container = Mockery::getContainer()) {
|
||||||
/* @phpstan-ignore-next-line */
|
/* @phpstan-ignore-next-line */
|
||||||
$this->addToAssertionCount($container->mockery_getExpectationCount());
|
$this->addToAssertionCount($container->mockery_getExpectationCount());
|
||||||
|
|||||||
@ -77,10 +77,6 @@
|
|||||||
✓ it can call chained macro method
|
✓ it can call chained macro method
|
||||||
✓ it will throw exception from call if no macro exists
|
✓ 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
|
PASS Tests\Features\PendingHigherOrderTests
|
||||||
✓ get 'foo' → get 'bar' → expect true → toBeTrue
|
✓ get 'foo' → get 'bar' → expect true → toBeTrue
|
||||||
✓ get 'foo' → expect true → toBeTrue
|
✓ get 'foo' → expect true → toBeTrue
|
||||||
@ -223,5 +219,5 @@
|
|||||||
✓ it is a test
|
✓ it is a test
|
||||||
✓ it uses correct parent class
|
✓ it uses correct parent class
|
||||||
|
|
||||||
Tests: 7 skipped, 121 passed
|
Tests: 7 skipped, 119 passed
|
||||||
|
|
||||||
@ -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);
|
|
||||||
Reference in New Issue
Block a user