ExpectationDirector::call()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 20
ccs 16
cts 16
cp 1
rs 9.4285
cc 2
eloc 16
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * Mockery
4
 *
5
 * LICENSE
6
 *
7
 * This source file is subject to the new BSD license that is bundled
8
 * with this package in the file LICENSE.txt.
9
 * It is also available through the world-wide-web at this URL:
10
 * http://github.com/padraic/mockery/blob/master/LICENSE
11
 * If you did not receive a copy of the license and are unable to
12
 * obtain it through the world-wide-web, please send an email
13
 * to [email protected] so we can send you a copy immediately.
14
 *
15
 * @category   Mockery
16
 * @package    Mockery
17
 * @copyright  Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com)
18
 * @license    http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
19
 */
20
21
namespace Mockery;
22
23
class ExpectationDirector
24
{
25
    /**
26
     * Method name the director is directing
27
     *
28
     * @var string
29
     */
30
    protected $_name = null;
31
32
    /**
33
     * Mock object the director is attached to
34
     *
35
     * @var \Mockery\MockInterface
36
     */
37
    protected $_mock = null;
38
39
    /**
40
     * Stores an array of all expectations for this mock
41
     *
42
     * @var array
43
     */
44
    protected $_expectations = array();
45
46
    /**
47
     * The expected order of next call
48
     *
49
     * @var int
50
     */
51
    protected $_expectedOrder = null;
52
53
    /**
54
     * Stores an array of all default expectations for this mock
55
     *
56
     * @var array
57
     */
58
    protected $_defaults = array();
59
60
    /**
61
     * Constructor
62
     *
63
     * @param string $name
64
     * @param \Mockery\MockInterface $mock
65
     */
66 302
    public function __construct($name, \Mockery\MockInterface $mock)
67
    {
68 302
        $this->_name = $name;
69 302
        $this->_mock = $mock;
70 302
    }
71
72
    /**
73
     * Add a new expectation to the director
74
     *
75
     * @param \Mockery\Expectation $expectation
76
     */
77 302
    public function addExpectation(\Mockery\Expectation $expectation)
78
    {
79 302
        $this->_expectations[] = $expectation;
80 302
    }
81
82
    /**
83
     * Handle a method call being directed by this instance
84
     *
85
     * @param array $args
86
     * @return mixed
87
     */
88 275
    public function call(array $args)
89
    {
90 275
        $expectation = $this->findExpectation($args);
91 275
        if (is_null($expectation)) {
92 47
            $exception = new \Mockery\Exception\NoMatchingExpectationException(
93
                'No matching handler found for '
94 47
                . $this->_mock->mockery_getName() . '::'
95 47
                . \Mockery::formatArgs($this->_name, $args)
96 47
                . '. Either the method was unexpected or its arguments matched'
97 47
                . ' no expected argument list for this method'
98 47
                . PHP_EOL . PHP_EOL
99 47
                . \Mockery::formatObjects($args)
100 47
            );
101 47
            $exception->setMock($this->_mock)
102 47
                ->setMethodName($this->_name)
103 47
                ->setActualArguments($args);
104 47
            throw $exception;
105
        }
106 231
        return $expectation->verifyCall($args);
107
    }
108
109
    /**
110
     * Verify all expectations of the director
111
     *
112
     * @throws \Mockery\CountValidator\Exception
113
     * @return void
114
     */
115 140
    public function verify()
116
    {
117 140
        if (!empty($this->_expectations)) {
118 134
            foreach ($this->_expectations as $exp) {
119 134
                $exp->verify();
120 115
            }
121 114
        } else {
122 8
            foreach ($this->_defaults as $exp) {
123 8
                $exp->verify();
124 8
            }
125
        }
126 120
    }
127
128
    /**
129
     * Attempt to locate an expectation matching the provided args
130
     *
131
     * @param array $args
132
     * @return mixed
133
     */
134 275
    public function findExpectation(array $args)
135
    {
136 275
        if (!empty($this->_expectations)) {
137 256
            return $this->_findExpectationIn($this->_expectations, $args);
138
        } else {
139 20
            return $this->_findExpectationIn($this->_defaults, $args);
140
        }
141
    }
142
143
    /**
144
     * Make the given expectation a default for all others assuming it was
145
     * correctly created last
146
     *
147
     * @param \Mockery\Expectation
148
     */
149 27
    public function makeExpectationDefault(\Mockery\Expectation $expectation)
150
    {
151 27
        $last = end($this->_expectations);
152 27
        if ($last === $expectation) {
153 26
            array_pop($this->_expectations);
154 26
            array_unshift($this->_defaults, $expectation);
155 26
        } else {
156 1
            throw new \Mockery\Exception(
157
                'Cannot turn a previously defined expectation into a default'
158 1
            );
159
        }
160 26
    }
161
162
    /**
163
     * Search current array of expectations for a match
164
     *
165
     * @param array $expectations
166
     * @param array $args
167
     * @return mixed
168
     */
169 275
    protected function _findExpectationIn(array $expectations, array $args)
170
    {
171 275
        foreach ($expectations as $exp) {
172 275
            if ($exp->isEligible() && $exp->matchArgs($args)) {
173 228
                return $exp;
174
            }
175 71
        }
176 57
        foreach ($expectations as $exp) {
177 57
            if ($exp->matchArgs($args)) {
178 10
                return $exp;
179
            }
180 47
        }
181 47
    }
182
183
    /**
184
     * Return all expectations assigned to this director
185
     *
186
     * @return array
187
     */
188 26
    public function getExpectations()
189
    {
190 26
        return $this->_expectations;
191
    }
192
193
    /**
194
     * Return all expectations assigned to this director
195
     *
196
     * @return array
197
     */
198 6
    public function getDefaultExpectations()
199
    {
200 6
        return $this->_defaults;
201
    }
202
203
    /**
204
     * Return the number of expectations assigned to this director.
205
     *
206
     * @return int
207
     */
208 21
    public function getExpectationCount()
209
    {
210 21
        return count($this->getExpectations());
211
    }
212
}
213