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\Argument\ArgumentsWildcard;
18 use Prophecy\Argument\Token\AnyValuesToken;
19 use Prophecy\Util\StringUtil;
20 use Prophecy\Exception\Prediction\UnexpectedCallsCountException;
23 * Prediction interface.
24 * Predictions are logical test blocks, tied to `should...` keyword.
26 * @author Konstantin Kudryashov <ever.zet@gmail.com>
28 class CallTimesPrediction implements PredictionInterface
34 * Initializes prediction.
37 * @param StringUtil $util
39 public function __construct($times, StringUtil $util = null)
41 $this->times = intval($times);
42 $this->util = $util ?: new StringUtil;
46 * Tests that there was exact amount of calls made.
48 * @param Call[] $calls
49 * @param ObjectProphecy $object
50 * @param MethodProphecy $method
52 * @throws \Prophecy\Exception\Prediction\UnexpectedCallsCountException
54 public function check(array $calls, ObjectProphecy $object, MethodProphecy $method)
56 if ($this->times == count($calls)) {
60 $methodCalls = $object->findProphecyMethodCalls(
61 $method->getMethodName(),
62 new ArgumentsWildcard(array(new AnyValuesToken))
67 "Expected exactly %d calls that match:\n".
69 "but %d were made:\n%s",
72 get_class($object->reveal()),
73 $method->getMethodName(),
74 $method->getArgumentsWildcard(),
76 $this->util->stringifyCalls($calls)
78 } elseif (count($methodCalls)) {
80 "Expected exactly %d calls that match:\n".
82 "but none were made.\n".
83 "Recorded `%s(...)` calls:\n%s",
86 get_class($object->reveal()),
87 $method->getMethodName(),
88 $method->getArgumentsWildcard(),
89 $method->getMethodName(),
90 $this->util->stringifyCalls($methodCalls)
94 "Expected exactly %d calls that match:\n".
96 "but none were made.",
99 get_class($object->reveal()),
100 $method->getMethodName(),
101 $method->getArgumentsWildcard()
105 throw new UnexpectedCallsCountException($message, $method, $this->times, $calls);