first commit

This commit is contained in:
kicap
2023-07-11 17:28:33 +08:00
commit 8b945c8750
3343 changed files with 975054 additions and 0 deletions

View File

@ -0,0 +1,522 @@
<?php
/*
* This file is part of the Prophecy.
* (c) Konstantin Kudryashov <ever.zet@gmail.com>
* Marcello Duarte <marcello.duarte@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Prophecy\Prophecy;
use Prophecy\Argument;
use Prophecy\Prophet;
use Prophecy\Promise;
use Prophecy\Prediction;
use Prophecy\Exception\Doubler\MethodNotFoundException;
use Prophecy\Exception\InvalidArgumentException;
use Prophecy\Exception\Prophecy\MethodProphecyException;
/**
* Method prophecy.
*
* @author Konstantin Kudryashov <ever.zet@gmail.com>
*/
class MethodProphecy
{
private $objectProphecy;
private $methodName;
private $argumentsWildcard;
private $promise;
private $prediction;
private $checkedPredictions = array();
private $bound = false;
private $voidReturnType = false;
/**
* Initializes method prophecy.
*
* @param ObjectProphecy $objectProphecy
* @param string $methodName
* @param null|Argument\ArgumentsWildcard|array $arguments
*
* @throws \Prophecy\Exception\Doubler\MethodNotFoundException If method not found
*/
public function __construct(ObjectProphecy $objectProphecy, $methodName, $arguments = null)
{
$double = $objectProphecy->reveal();
if (!method_exists($double, $methodName)) {
throw new MethodNotFoundException(sprintf(
'Method `%s::%s()` is not defined.', get_class($double), $methodName
), get_class($double), $methodName, $arguments);
}
$this->objectProphecy = $objectProphecy;
$this->methodName = $methodName;
$reflectedMethod = new \ReflectionMethod($double, $methodName);
if ($reflectedMethod->isFinal()) {
throw new MethodProphecyException(sprintf(
"Can not add prophecy for a method `%s::%s()`\n".
"as it is a final method.",
get_class($double),
$methodName
), $this);
}
if (null !== $arguments) {
$this->withArguments($arguments);
}
if (version_compare(PHP_VERSION, '7.0', '>=') && true === $reflectedMethod->hasReturnType()) {
$type = PHP_VERSION_ID >= 70100 ? $reflectedMethod->getReturnType()->getName() : (string) $reflectedMethod->getReturnType();
if ('void' === $type) {
$this->voidReturnType = true;
}
$this->will(function () use ($type) {
switch ($type) {
case 'void': return;
case 'string': return '';
case 'float': return 0.0;
case 'int': return 0;
case 'bool': return false;
case 'array': return array();
case 'callable':
case 'Closure':
return function () {};
case 'Traversable':
case 'Generator':
// Remove eval() when minimum version >=5.5
/** @var callable $generator */
$generator = eval('return function () { yield; };');
return $generator();
default:
$prophet = new Prophet;
return $prophet->prophesize($type)->reveal();
}
});
}
}
/**
* Sets argument wildcard.
*
* @param array|Argument\ArgumentsWildcard $arguments
*
* @return $this
*
* @throws \Prophecy\Exception\InvalidArgumentException
*/
public function withArguments($arguments)
{
if (is_array($arguments)) {
$arguments = new Argument\ArgumentsWildcard($arguments);
}
if (!$arguments instanceof Argument\ArgumentsWildcard) {
throw new InvalidArgumentException(sprintf(
"Either an array or an instance of ArgumentsWildcard expected as\n".
'a `MethodProphecy::withArguments()` argument, but got %s.',
gettype($arguments)
));
}
$this->argumentsWildcard = $arguments;
return $this;
}
/**
* Sets custom promise to the prophecy.
*
* @param callable|Promise\PromiseInterface $promise
*
* @return $this
*
* @throws \Prophecy\Exception\InvalidArgumentException
*/
public function will($promise)
{
if (is_callable($promise)) {
$promise = new Promise\CallbackPromise($promise);
}
if (!$promise instanceof Promise\PromiseInterface) {
throw new InvalidArgumentException(sprintf(
'Expected callable or instance of PromiseInterface, but got %s.',
gettype($promise)
));
}
$this->bindToObjectProphecy();
$this->promise = $promise;
return $this;
}
/**
* Sets return promise to the prophecy.
*
* @see \Prophecy\Promise\ReturnPromise
*
* @return $this
*/
public function willReturn()
{
if ($this->voidReturnType) {
throw new MethodProphecyException(
"The method \"$this->methodName\" has a void return type, and so cannot return anything",
$this
);
}
return $this->will(new Promise\ReturnPromise(func_get_args()));
}
/**
* @param array $items
*
* @return $this
*
* @throws \Prophecy\Exception\InvalidArgumentException
*/
public function willYield($items)
{
if ($this->voidReturnType) {
throw new MethodProphecyException(
"The method \"$this->methodName\" has a void return type, and so cannot yield anything",
$this
);
}
if (!is_array($items)) {
throw new InvalidArgumentException(sprintf(
'Expected array, but got %s.',
gettype($items)
));
}
// Remove eval() when minimum version >=5.5
/** @var callable $generator */
$generator = eval('return function() use ($items) {
foreach ($items as $key => $value) {
yield $key => $value;
}
};');
return $this->will($generator);
}
/**
* Sets return argument promise to the prophecy.
*
* @param int $index The zero-indexed number of the argument to return
*
* @see \Prophecy\Promise\ReturnArgumentPromise
*
* @return $this
*/
public function willReturnArgument($index = 0)
{
if ($this->voidReturnType) {
throw new MethodProphecyException("The method \"$this->methodName\" has a void return type", $this);
}
return $this->will(new Promise\ReturnArgumentPromise($index));
}
/**
* Sets throw promise to the prophecy.
*
* @see \Prophecy\Promise\ThrowPromise
*
* @param string|\Exception $exception Exception class or instance
*
* @return $this
*/
public function willThrow($exception)
{
return $this->will(new Promise\ThrowPromise($exception));
}
/**
* Sets custom prediction to the prophecy.
*
* @param callable|Prediction\PredictionInterface $prediction
*
* @return $this
*
* @throws \Prophecy\Exception\InvalidArgumentException
*/
public function should($prediction)
{
if (is_callable($prediction)) {
$prediction = new Prediction\CallbackPrediction($prediction);
}
if (!$prediction instanceof Prediction\PredictionInterface) {
throw new InvalidArgumentException(sprintf(
'Expected callable or instance of PredictionInterface, but got %s.',
gettype($prediction)
));
}
$this->bindToObjectProphecy();
$this->prediction = $prediction;
return $this;
}
/**
* Sets call prediction to the prophecy.
*
* @see \Prophecy\Prediction\CallPrediction
*
* @return $this
*/
public function shouldBeCalled()
{
return $this->should(new Prediction\CallPrediction);
}
/**
* Sets no calls prediction to the prophecy.
*
* @see \Prophecy\Prediction\NoCallsPrediction
*
* @return $this
*/
public function shouldNotBeCalled()
{
return $this->should(new Prediction\NoCallsPrediction);
}
/**
* Sets call times prediction to the prophecy.
*
* @see \Prophecy\Prediction\CallTimesPrediction
*
* @param $count
*
* @return $this
*/
public function shouldBeCalledTimes($count)
{
return $this->should(new Prediction\CallTimesPrediction($count));
}
/**
* Sets call times prediction to the prophecy.
*
* @see \Prophecy\Prediction\CallTimesPrediction
*
* @return $this
*/
public function shouldBeCalledOnce()
{
return $this->shouldBeCalledTimes(1);
}
/**
* Checks provided prediction immediately.
*
* @param callable|Prediction\PredictionInterface $prediction
*
* @return $this
*
* @throws \Prophecy\Exception\InvalidArgumentException
*/
public function shouldHave($prediction)
{
if (is_callable($prediction)) {
$prediction = new Prediction\CallbackPrediction($prediction);
}
if (!$prediction instanceof Prediction\PredictionInterface) {
throw new InvalidArgumentException(sprintf(
'Expected callable or instance of PredictionInterface, but got %s.',
gettype($prediction)
));
}
if (null === $this->promise && !$this->voidReturnType) {
$this->willReturn();
}
$calls = $this->getObjectProphecy()->findProphecyMethodCalls(
$this->getMethodName(),
$this->getArgumentsWildcard()
);
try {
$prediction->check($calls, $this->getObjectProphecy(), $this);
$this->checkedPredictions[] = $prediction;
} catch (\Exception $e) {
$this->checkedPredictions[] = $prediction;
throw $e;
}
return $this;
}
/**
* Checks call prediction.
*
* @see \Prophecy\Prediction\CallPrediction
*
* @return $this
*/
public function shouldHaveBeenCalled()
{
return $this->shouldHave(new Prediction\CallPrediction);
}
/**
* Checks no calls prediction.
*
* @see \Prophecy\Prediction\NoCallsPrediction
*
* @return $this
*/
public function shouldNotHaveBeenCalled()
{
return $this->shouldHave(new Prediction\NoCallsPrediction);
}
/**
* Checks no calls prediction.
*
* @see \Prophecy\Prediction\NoCallsPrediction
* @deprecated
*
* @return $this
*/
public function shouldNotBeenCalled()
{
return $this->shouldNotHaveBeenCalled();
}
/**
* Checks call times prediction.
*
* @see \Prophecy\Prediction\CallTimesPrediction
*
* @param int $count
*
* @return $this
*/
public function shouldHaveBeenCalledTimes($count)
{
return $this->shouldHave(new Prediction\CallTimesPrediction($count));
}
/**
* Checks call times prediction.
*
* @see \Prophecy\Prediction\CallTimesPrediction
*
* @return $this
*/
public function shouldHaveBeenCalledOnce()
{
return $this->shouldHaveBeenCalledTimes(1);
}
/**
* Checks currently registered [with should(...)] prediction.
*/
public function checkPrediction()
{
if (null === $this->prediction) {
return;
}
$this->shouldHave($this->prediction);
}
/**
* Returns currently registered promise.
*
* @return null|Promise\PromiseInterface
*/
public function getPromise()
{
return $this->promise;
}
/**
* Returns currently registered prediction.
*
* @return null|Prediction\PredictionInterface
*/
public function getPrediction()
{
return $this->prediction;
}
/**
* Returns predictions that were checked on this object.
*
* @return Prediction\PredictionInterface[]
*/
public function getCheckedPredictions()
{
return $this->checkedPredictions;
}
/**
* Returns object prophecy this method prophecy is tied to.
*
* @return ObjectProphecy
*/
public function getObjectProphecy()
{
return $this->objectProphecy;
}
/**
* Returns method name.
*
* @return string
*/
public function getMethodName()
{
return $this->methodName;
}
/**
* Returns arguments wildcard.
*
* @return Argument\ArgumentsWildcard
*/
public function getArgumentsWildcard()
{
return $this->argumentsWildcard;
}
/**
* @return bool
*/
public function hasReturnVoid()
{
return $this->voidReturnType;
}
private function bindToObjectProphecy()
{
if ($this->bound) {
return;
}
$this->getObjectProphecy()->addMethodProphecy($this);
$this->bound = true;
}
}

View File

@ -0,0 +1,286 @@
<?php
/*
* This file is part of the Prophecy.
* (c) Konstantin Kudryashov <ever.zet@gmail.com>
* Marcello Duarte <marcello.duarte@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Prophecy\Prophecy;
use SebastianBergmann\Comparator\ComparisonFailure;
use Prophecy\Comparator\Factory as ComparatorFactory;
use Prophecy\Call\Call;
use Prophecy\Doubler\LazyDouble;
use Prophecy\Argument\ArgumentsWildcard;
use Prophecy\Call\CallCenter;
use Prophecy\Exception\Prophecy\ObjectProphecyException;
use Prophecy\Exception\Prophecy\MethodProphecyException;
use Prophecy\Exception\Prediction\AggregateException;
use Prophecy\Exception\Prediction\PredictionException;
/**
* Object prophecy.
*
* @author Konstantin Kudryashov <ever.zet@gmail.com>
*/
class ObjectProphecy implements ProphecyInterface
{
private $lazyDouble;
private $callCenter;
private $revealer;
private $comparatorFactory;
/**
* @var MethodProphecy[][]
*/
private $methodProphecies = array();
/**
* Initializes object prophecy.
*
* @param LazyDouble $lazyDouble
* @param CallCenter $callCenter
* @param RevealerInterface $revealer
* @param ComparatorFactory $comparatorFactory
*/
public function __construct(
LazyDouble $lazyDouble,
CallCenter $callCenter = null,
RevealerInterface $revealer = null,
ComparatorFactory $comparatorFactory = null
) {
$this->lazyDouble = $lazyDouble;
$this->callCenter = $callCenter ?: new CallCenter;
$this->revealer = $revealer ?: new Revealer;
$this->comparatorFactory = $comparatorFactory ?: ComparatorFactory::getInstance();
}
/**
* Forces double to extend specific class.
*
* @param string $class
*
* @return $this
*/
public function willExtend($class)
{
$this->lazyDouble->setParentClass($class);
return $this;
}
/**
* Forces double to implement specific interface.
*
* @param string $interface
*
* @return $this
*/
public function willImplement($interface)
{
$this->lazyDouble->addInterface($interface);
return $this;
}
/**
* Sets constructor arguments.
*
* @param array $arguments
*
* @return $this
*/
public function willBeConstructedWith(array $arguments = null)
{
$this->lazyDouble->setArguments($arguments);
return $this;
}
/**
* Reveals double.
*
* @return object
*
* @throws \Prophecy\Exception\Prophecy\ObjectProphecyException If double doesn't implement needed interface
*/
public function reveal()
{
$double = $this->lazyDouble->getInstance();
if (null === $double || !$double instanceof ProphecySubjectInterface) {
throw new ObjectProphecyException(
"Generated double must implement ProphecySubjectInterface, but it does not.\n".
'It seems you have wrongly configured doubler without required ClassPatch.',
$this
);
}
$double->setProphecy($this);
return $double;
}
/**
* Adds method prophecy to object prophecy.
*
* @param MethodProphecy $methodProphecy
*
* @throws \Prophecy\Exception\Prophecy\MethodProphecyException If method prophecy doesn't
* have arguments wildcard
*/
public function addMethodProphecy(MethodProphecy $methodProphecy)
{
$argumentsWildcard = $methodProphecy->getArgumentsWildcard();
if (null === $argumentsWildcard) {
throw new MethodProphecyException(sprintf(
"Can not add prophecy for a method `%s::%s()`\n".
"as you did not specify arguments wildcard for it.",
get_class($this->reveal()),
$methodProphecy->getMethodName()
), $methodProphecy);
}
$methodName = strtolower($methodProphecy->getMethodName());
if (!isset($this->methodProphecies[$methodName])) {
$this->methodProphecies[$methodName] = array();
}
$this->methodProphecies[$methodName][] = $methodProphecy;
}
/**
* Returns either all or related to single method prophecies.
*
* @param null|string $methodName
*
* @return MethodProphecy[]
*/
public function getMethodProphecies($methodName = null)
{
if (null === $methodName) {
return $this->methodProphecies;
}
$methodName = strtolower($methodName);
if (!isset($this->methodProphecies[$methodName])) {
return array();
}
return $this->methodProphecies[$methodName];
}
/**
* Makes specific method call.
*
* @param string $methodName
* @param array $arguments
*
* @return mixed
*/
public function makeProphecyMethodCall($methodName, array $arguments)
{
$arguments = $this->revealer->reveal($arguments);
$return = $this->callCenter->makeCall($this, $methodName, $arguments);
return $this->revealer->reveal($return);
}
/**
* Finds calls by method name & arguments wildcard.
*
* @param string $methodName
* @param ArgumentsWildcard $wildcard
*
* @return Call[]
*/
public function findProphecyMethodCalls($methodName, ArgumentsWildcard $wildcard)
{
return $this->callCenter->findCalls($methodName, $wildcard);
}
/**
* Checks that registered method predictions do not fail.
*
* @throws \Prophecy\Exception\Prediction\AggregateException If any of registered predictions fail
* @throws \Prophecy\Exception\Call\UnexpectedCallException
*/
public function checkProphecyMethodsPredictions()
{
$exception = new AggregateException(sprintf("%s:\n", get_class($this->reveal())));
$exception->setObjectProphecy($this);
$this->callCenter->checkUnexpectedCalls();
foreach ($this->methodProphecies as $prophecies) {
foreach ($prophecies as $prophecy) {
try {
$prophecy->checkPrediction();
} catch (PredictionException $e) {
$exception->append($e);
}
}
}
if (count($exception->getExceptions())) {
throw $exception;
}
}
/**
* Creates new method prophecy using specified method name and arguments.
*
* @param string $methodName
* @param array $arguments
*
* @return MethodProphecy
*/
public function __call($methodName, array $arguments)
{
$arguments = new ArgumentsWildcard($this->revealer->reveal($arguments));
foreach ($this->getMethodProphecies($methodName) as $prophecy) {
$argumentsWildcard = $prophecy->getArgumentsWildcard();
$comparator = $this->comparatorFactory->getComparatorFor(
$argumentsWildcard, $arguments
);
try {
$comparator->assertEquals($argumentsWildcard, $arguments);
return $prophecy;
} catch (ComparisonFailure $failure) {}
}
return new MethodProphecy($this, $methodName, $arguments);
}
/**
* Tries to get property value from double.
*
* @param string $name
*
* @return mixed
*/
public function __get($name)
{
return $this->reveal()->$name;
}
/**
* Tries to set property value to double.
*
* @param string $name
* @param mixed $value
*/
public function __set($name, $value)
{
$this->reveal()->$name = $this->revealer->reveal($value);
}
}

View File

@ -0,0 +1,27 @@
<?php
/*
* This file is part of the Prophecy.
* (c) Konstantin Kudryashov <ever.zet@gmail.com>
* Marcello Duarte <marcello.duarte@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Prophecy\Prophecy;
/**
* Core Prophecy interface.
*
* @author Konstantin Kudryashov <ever.zet@gmail.com>
*/
interface ProphecyInterface
{
/**
* Reveals prophecy object (double) .
*
* @return object
*/
public function reveal();
}

View File

@ -0,0 +1,34 @@
<?php
/*
* This file is part of the Prophecy.
* (c) Konstantin Kudryashov <ever.zet@gmail.com>
* Marcello Duarte <marcello.duarte@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Prophecy\Prophecy;
/**
* Controllable doubles interface.
*
* @author Konstantin Kudryashov <ever.zet@gmail.com>
*/
interface ProphecySubjectInterface
{
/**
* Sets subject prophecy.
*
* @param ProphecyInterface $prophecy
*/
public function setProphecy(ProphecyInterface $prophecy);
/**
* Returns subject prophecy.
*
* @return ProphecyInterface
*/
public function getProphecy();
}

View File

@ -0,0 +1,44 @@
<?php
/*
* This file is part of the Prophecy.
* (c) Konstantin Kudryashov <ever.zet@gmail.com>
* Marcello Duarte <marcello.duarte@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Prophecy\Prophecy;
/**
* Basic prophecies revealer.
*
* @author Konstantin Kudryashov <ever.zet@gmail.com>
*/
class Revealer implements RevealerInterface
{
/**
* Unwraps value(s).
*
* @param mixed $value
*
* @return mixed
*/
public function reveal($value)
{
if (is_array($value)) {
return array_map(array($this, __FUNCTION__), $value);
}
if (!is_object($value)) {
return $value;
}
if ($value instanceof ProphecyInterface) {
$value = $value->reveal();
}
return $value;
}
}

View File

@ -0,0 +1,29 @@
<?php
/*
* This file is part of the Prophecy.
* (c) Konstantin Kudryashov <ever.zet@gmail.com>
* Marcello Duarte <marcello.duarte@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Prophecy\Prophecy;
/**
* Prophecies revealer interface.
*
* @author Konstantin Kudryashov <ever.zet@gmail.com>
*/
interface RevealerInterface
{
/**
* Unwraps value(s).
*
* @param mixed $value
*
* @return mixed
*/
public function reveal($value);
}