mirror of
https://github.com/pestphp/pest.git
synced 2026-03-10 01:37:21 +01:00
feat(mock): adds work in progress
This commit is contained in:
24
src/Exceptions/MissingDependency.php
Normal file
24
src/Exceptions/MissingDependency.php
Normal file
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Pest\Exceptions;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use NunoMaduro\Collision\Contracts\RenderlessEditor;
|
||||
use NunoMaduro\Collision\Contracts\RenderlessTrace;
|
||||
use Symfony\Component\Console\Exception\ExceptionInterface;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
final class MissingDependency extends InvalidArgumentException implements ExceptionInterface, RenderlessEditor, RenderlessTrace
|
||||
{
|
||||
/**
|
||||
* Creates a new instance of missing dependency.
|
||||
*/
|
||||
public function __construct(string $feature, string $dependency)
|
||||
{
|
||||
parent::__construct(sprintf('The feature "%s" requires "%s".', $feature, $dependency));
|
||||
}
|
||||
}
|
||||
@ -3,6 +3,7 @@
|
||||
declare(strict_types=1);
|
||||
|
||||
use Pest\Datasets;
|
||||
use Pest\Mock;
|
||||
use Pest\PendingObjects\AfterEachCall;
|
||||
use Pest\PendingObjects\BeforeEachCall;
|
||||
use Pest\PendingObjects\TestCall;
|
||||
@ -104,3 +105,15 @@ 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);
|
||||
}
|
||||
}
|
||||
|
||||
69
src/Mock.php
Normal file
69
src/Mock.php
Normal file
@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Pest;
|
||||
|
||||
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 => $result) {
|
||||
/* @phpstan-ignore-next-line */
|
||||
$this->mock
|
||||
->shouldReceive((string) $method)
|
||||
->andReturn($result);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user