mirror of
https://github.com/pestphp/pest.git
synced 2026-03-06 15:57:21 +01:00
feat(mock-plugin): moves mock stuff to their own plugin
This commit is contained in:
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
if (class_exists(Mockery::class)) {
|
||||
/* @phpstan-ignore-next-line */
|
||||
if ($container = Mockery::getContainer()) {
|
||||
/* @phpstan-ignore-next-line */
|
||||
$this->addToAssertionCount($container->mockery_getExpectationCount());
|
||||
|
||||
Reference in New Issue
Block a user