1<?php
2
3/*
4 * This file is part of the Prophecy.
5 * (c) Konstantin Kudryashov <ever.zet@gmail.com>
6 *     Marcello Duarte <marcello.duarte@gmail.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Prophecy\Prediction;
13
14use Prophecy\Call\Call;
15use Prophecy\Prophecy\ObjectProphecy;
16use Prophecy\Prophecy\MethodProphecy;
17use Prophecy\Argument\ArgumentsWildcard;
18use Prophecy\Argument\Token\AnyValuesToken;
19use Prophecy\Util\StringUtil;
20use Prophecy\Exception\Prediction\NoCallsException;
21
22/**
23 * Call prediction.
24 *
25 * @author Konstantin Kudryashov <ever.zet@gmail.com>
26 */
27class CallPrediction implements PredictionInterface
28{
29    private $util;
30
31    /**
32     * Initializes prediction.
33     *
34     * @param StringUtil $util
35     */
36    public function __construct(StringUtil $util = null)
37    {
38        $this->util = $util ?: new StringUtil;
39    }
40
41    /**
42     * Tests that there was at least one call.
43     *
44     * @param Call[]         $calls
45     * @param ObjectProphecy $object
46     * @param MethodProphecy $method
47     *
48     * @throws \Prophecy\Exception\Prediction\NoCallsException
49     */
50    public function check(array $calls, ObjectProphecy $object, MethodProphecy $method)
51    {
52        if (count($calls)) {
53            return;
54        }
55
56        $methodCalls = $object->findProphecyMethodCalls(
57            $method->getMethodName(),
58            new ArgumentsWildcard(array(new AnyValuesToken))
59        );
60
61        if (count($methodCalls)) {
62            throw new NoCallsException(sprintf(
63                "No calls have been made that match:\n".
64                "  %s->%s(%s)\n".
65                "but expected at least one.\n".
66                "Recorded `%s(...)` calls:\n%s",
67
68                get_class($object->reveal()),
69                $method->getMethodName(),
70                $method->getArgumentsWildcard(),
71                $method->getMethodName(),
72                $this->util->stringifyCalls($methodCalls)
73            ), $method);
74        }
75
76        throw new NoCallsException(sprintf(
77            "No calls have been made that match:\n".
78            "  %s->%s(%s)\n".
79            "but expected at least one.",
80
81            get_class($object->reveal()),
82            $method->getMethodName(),
83            $method->getArgumentsWildcard()
84        ), $method);
85    }
86}
87