feat(mock): adds work in progress

This commit is contained in:
Nuno Maduro
2021-04-10 21:11:57 +01:00
parent 3205b571b0
commit c9f723530d
7 changed files with 134 additions and 18 deletions

View 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));
}
}

View File

@ -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
View 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);
}
}