4 * This file is part of the Prophecy.
5 * (c) Konstantin Kudryashov <ever.zet@gmail.com>
6 * Marcello Duarte <marcello.duarte@gmail.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Prophecy\Prediction;
14 use Prophecy\Call\Call;
15 use Prophecy\Prophecy\ObjectProphecy;
16 use Prophecy\Prophecy\MethodProphecy;
17 use Prophecy\Exception\InvalidArgumentException;
21 * Callback prediction.
23 * @author Konstantin Kudryashov <ever.zet@gmail.com>
25 class CallbackPrediction implements PredictionInterface
30 * Initializes callback prediction.
32 * @param callable $callback Custom callback
34 * @throws \Prophecy\Exception\InvalidArgumentException
36 public function __construct($callback)
38 if (!is_callable($callback)) {
39 throw new InvalidArgumentException(sprintf(
40 'Callable expected as an argument to CallbackPrediction, but got %s.',
45 $this->callback = $callback;
49 * Executes preset callback.
51 * @param Call[] $calls
52 * @param ObjectProphecy $object
53 * @param MethodProphecy $method
55 public function check(array $calls, ObjectProphecy $object, MethodProphecy $method)
57 $callback = $this->callback;
59 if ($callback instanceof Closure && method_exists('Closure', 'bind')) {
60 $callback = Closure::bind($callback, $object);
63 call_user_func($callback, $calls, $object, $method);