CallPrediction::check()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 36

Duplication

Lines 14
Ratio 38.89 %

Importance

Changes 0
Metric Value
dl 14
loc 36
rs 9.344
c 0
b 0
f 0
cc 3
nc 3
nop 3
1
<?php
2
3
/*
4
 * This file is part of the Prophecy.
5
 * (c) Konstantin Kudryashov <[email protected]>
6
 *     Marcello Duarte <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Prophecy\Prediction;
13
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\NoCallsException;
21
22
/**
23
 * Call prediction.
24
 *
25
 * @author Konstantin Kudryashov <[email protected]>
26
 */
27
class 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 View Code Duplication
        if (count($methodCalls)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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