summaryrefslogtreecommitdiff
path: root/vendor/phpspec/prophecy/src/Prophecy/Promise
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/phpspec/prophecy/src/Prophecy/Promise')
-rw-r--r--vendor/phpspec/prophecy/src/Prophecy/Promise/CallbackPromise.php67
-rw-r--r--vendor/phpspec/prophecy/src/Prophecy/Promise/PromiseInterface.php35
-rw-r--r--vendor/phpspec/prophecy/src/Prophecy/Promise/ReturnArgumentPromise.php61
-rw-r--r--vendor/phpspec/prophecy/src/Prophecy/Promise/ReturnPromise.php55
-rw-r--r--vendor/phpspec/prophecy/src/Prophecy/Promise/ThrowPromise.php100
5 files changed, 318 insertions, 0 deletions
diff --git a/vendor/phpspec/prophecy/src/Prophecy/Promise/CallbackPromise.php b/vendor/phpspec/prophecy/src/Prophecy/Promise/CallbackPromise.php
new file mode 100644
index 000000000..f766e6e67
--- /dev/null
+++ b/vendor/phpspec/prophecy/src/Prophecy/Promise/CallbackPromise.php
@@ -0,0 +1,67 @@
+<?php
+
+/*
+ * This file is part of the Prophecy.
+ * (c) Konstantin Kudryashov <[email protected]>
+ * Marcello Duarte <[email protected]>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Prophecy\Promise;
+
+use Prophecy\Prophecy\ObjectProphecy;
+use Prophecy\Prophecy\MethodProphecy;
+use Prophecy\Exception\InvalidArgumentException;
+use Closure;
+use ReflectionFunction;
+
+/**
+ * Callback promise.
+ *
+ * @author Konstantin Kudryashov <[email protected]>
+ */
+class CallbackPromise implements PromiseInterface
+{
+ private $callback;
+
+ /**
+ * Initializes callback promise.
+ *
+ * @param callable $callback Custom callback
+ *
+ * @throws \Prophecy\Exception\InvalidArgumentException
+ */
+ public function __construct($callback)
+ {
+ if (!is_callable($callback)) {
+ throw new InvalidArgumentException(sprintf(
+ 'Callable expected as an argument to CallbackPromise, but got %s.',
+ gettype($callback)
+ ));
+ }
+
+ $this->callback = $callback;
+ }
+
+ /**
+ * Evaluates promise callback.
+ *
+ * @param array $args
+ * @param ObjectProphecy $object
+ * @param MethodProphecy $method
+ *
+ * @return mixed
+ */
+ public function execute(array $args, ObjectProphecy $object, MethodProphecy $method)
+ {
+ $callback = $this->callback;
+
+ if ($callback instanceof Closure && method_exists('Closure', 'bind') && (new ReflectionFunction($callback))->getClosureThis() !== null) {
+ $callback = Closure::bind($callback, $object);
+ }
+
+ return call_user_func($callback, $args, $object, $method);
+ }
+}
diff --git a/vendor/phpspec/prophecy/src/Prophecy/Promise/PromiseInterface.php b/vendor/phpspec/prophecy/src/Prophecy/Promise/PromiseInterface.php
new file mode 100644
index 000000000..382537b47
--- /dev/null
+++ b/vendor/phpspec/prophecy/src/Prophecy/Promise/PromiseInterface.php
@@ -0,0 +1,35 @@
+<?php
+
+/*
+ * This file is part of the Prophecy.
+ * (c) Konstantin Kudryashov <[email protected]>
+ * Marcello Duarte <[email protected]>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Prophecy\Promise;
+
+use Prophecy\Prophecy\ObjectProphecy;
+use Prophecy\Prophecy\MethodProphecy;
+
+/**
+ * Promise interface.
+ * Promises are logical blocks, tied to `will...` keyword.
+ *
+ * @author Konstantin Kudryashov <[email protected]>
+ */
+interface PromiseInterface
+{
+ /**
+ * Evaluates promise.
+ *
+ * @param array $args
+ * @param ObjectProphecy $object
+ * @param MethodProphecy $method
+ *
+ * @return mixed
+ */
+ public function execute(array $args, ObjectProphecy $object, MethodProphecy $method);
+}
diff --git a/vendor/phpspec/prophecy/src/Prophecy/Promise/ReturnArgumentPromise.php b/vendor/phpspec/prophecy/src/Prophecy/Promise/ReturnArgumentPromise.php
new file mode 100644
index 000000000..39bfeea07
--- /dev/null
+++ b/vendor/phpspec/prophecy/src/Prophecy/Promise/ReturnArgumentPromise.php
@@ -0,0 +1,61 @@
+<?php
+
+/*
+ * This file is part of the Prophecy.
+ * (c) Konstantin Kudryashov <[email protected]>
+ * Marcello Duarte <[email protected]>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Prophecy\Promise;
+
+use Prophecy\Exception\InvalidArgumentException;
+use Prophecy\Prophecy\ObjectProphecy;
+use Prophecy\Prophecy\MethodProphecy;
+
+/**
+ * Return argument promise.
+ *
+ * @author Konstantin Kudryashov <[email protected]>
+ */
+class ReturnArgumentPromise implements PromiseInterface
+{
+ /**
+ * @var int
+ */
+ private $index;
+
+ /**
+ * Initializes callback promise.
+ *
+ * @param int $index The zero-indexed number of the argument to return
+ *
+ * @throws \Prophecy\Exception\InvalidArgumentException
+ */
+ public function __construct($index = 0)
+ {
+ if (!is_int($index) || $index < 0) {
+ throw new InvalidArgumentException(sprintf(
+ 'Zero-based index expected as argument to ReturnArgumentPromise, but got %s.',
+ $index
+ ));
+ }
+ $this->index = $index;
+ }
+
+ /**
+ * Returns nth argument if has one, null otherwise.
+ *
+ * @param array $args
+ * @param ObjectProphecy $object
+ * @param MethodProphecy $method
+ *
+ * @return null|mixed
+ */
+ public function execute(array $args, ObjectProphecy $object, MethodProphecy $method)
+ {
+ return count($args) > $this->index ? $args[$this->index] : null;
+ }
+}
diff --git a/vendor/phpspec/prophecy/src/Prophecy/Promise/ReturnPromise.php b/vendor/phpspec/prophecy/src/Prophecy/Promise/ReturnPromise.php
new file mode 100644
index 000000000..c7d5ac598
--- /dev/null
+++ b/vendor/phpspec/prophecy/src/Prophecy/Promise/ReturnPromise.php
@@ -0,0 +1,55 @@
+<?php
+
+/*
+ * This file is part of the Prophecy.
+ * (c) Konstantin Kudryashov <[email protected]>
+ * Marcello Duarte <[email protected]>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Prophecy\Promise;
+
+use Prophecy\Prophecy\ObjectProphecy;
+use Prophecy\Prophecy\MethodProphecy;
+
+/**
+ * Return promise.
+ *
+ * @author Konstantin Kudryashov <[email protected]>
+ */
+class ReturnPromise implements PromiseInterface
+{
+ private $returnValues = array();
+
+ /**
+ * Initializes promise.
+ *
+ * @param array $returnValues Array of values
+ */
+ public function __construct(array $returnValues)
+ {
+ $this->returnValues = $returnValues;
+ }
+
+ /**
+ * Returns saved values one by one until last one, then continuously returns last value.
+ *
+ * @param array $args
+ * @param ObjectProphecy $object
+ * @param MethodProphecy $method
+ *
+ * @return mixed
+ */
+ public function execute(array $args, ObjectProphecy $object, MethodProphecy $method)
+ {
+ $value = array_shift($this->returnValues);
+
+ if (!count($this->returnValues)) {
+ $this->returnValues[] = $value;
+ }
+
+ return $value;
+ }
+}
diff --git a/vendor/phpspec/prophecy/src/Prophecy/Promise/ThrowPromise.php b/vendor/phpspec/prophecy/src/Prophecy/Promise/ThrowPromise.php
new file mode 100644
index 000000000..26ec19edf
--- /dev/null
+++ b/vendor/phpspec/prophecy/src/Prophecy/Promise/ThrowPromise.php
@@ -0,0 +1,100 @@
+<?php
+
+/*
+ * This file is part of the Prophecy.
+ * (c) Konstantin Kudryashov <[email protected]>
+ * Marcello Duarte <[email protected]>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Prophecy\Promise;
+
+use Doctrine\Instantiator\Instantiator;
+use Prophecy\Prophecy\ObjectProphecy;
+use Prophecy\Prophecy\MethodProphecy;
+use Prophecy\Exception\InvalidArgumentException;
+use ReflectionClass;
+
+/**
+ * Throw promise.
+ *
+ * @author Konstantin Kudryashov <[email protected]>
+ */
+class ThrowPromise implements PromiseInterface
+{
+ private $exception;
+
+ /**
+ * @var \Doctrine\Instantiator\Instantiator
+ */
+ private $instantiator;
+
+ /**
+ * Initializes promise.
+ *
+ * @param string|\Exception|\Throwable $exception Exception class name or instance
+ *
+ * @throws \Prophecy\Exception\InvalidArgumentException
+ */
+ public function __construct($exception)
+ {
+ if (is_string($exception)) {
+ if ((!class_exists($exception) && !interface_exists($exception)) || !$this->isAValidThrowable($exception)) {
+ throw new InvalidArgumentException(sprintf(
+ 'Exception / Throwable class or instance expected as argument to ThrowPromise, but got %s.',
+ $exception
+ ));
+ }
+ } elseif (!$exception instanceof \Exception && !$exception instanceof \Throwable) {
+ throw new InvalidArgumentException(sprintf(
+ 'Exception / Throwable class or instance expected as argument to ThrowPromise, but got %s.',
+ is_object($exception) ? get_class($exception) : gettype($exception)
+ ));
+ }
+
+ $this->exception = $exception;
+ }
+
+ /**
+ * Throws predefined exception.
+ *
+ * @param array $args
+ * @param ObjectProphecy $object
+ * @param MethodProphecy $method
+ *
+ * @throws object
+ */
+ public function execute(array $args, ObjectProphecy $object, MethodProphecy $method)
+ {
+ if (is_string($this->exception)) {
+ $classname = $this->exception;
+ $reflection = new ReflectionClass($classname);
+ $constructor = $reflection->getConstructor();
+
+ if ($constructor->isPublic() && 0 == $constructor->getNumberOfRequiredParameters()) {
+ throw $reflection->newInstance();
+ }
+
+ if (!$this->instantiator) {
+ $this->instantiator = new Instantiator();
+ }
+
+ throw $this->instantiator->instantiate($classname);
+ }
+
+ throw $this->exception;
+ }
+
+ /**
+ * @param string $exception
+ *
+ * @return bool
+ */
+ private function isAValidThrowable($exception)
+ {
+ return is_a($exception, 'Exception', true)
+ || is_a($exception, 'Throwable', true);
+ }
+}