Completed
Pull Request — master (#566)
by
unknown
03:50
created

ExpectationDirector   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 199
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 5
dl 0
loc 199
ccs 70
cts 70
cp 1
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A addExpectation() 0 4 1
A call() 0 20 2
A verify() 0 12 4
B findExpectation() 0 17 5
A makeExpectationDefault() 0 12 2
B _findExpectationIn() 0 13 6
A getExpectations() 0 4 1
A getDefaultExpectations() 0 4 1
A getExpectationCount() 0 4 1
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 Expectation[]
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 304
    public function __construct($name, \Mockery\MockInterface $mock)
67
    {
68 304
        $this->_name = $name;
69 304
        $this->_mock = $mock;
70 304
    }
71
72
    /**
73
     * Add a new expectation to the director
74
     *
75
     * @param \Mockery\Expectation $expectation
76
     */
77 304
    public function addExpectation(\Mockery\Expectation $expectation)
78
    {
79 304
        $this->_expectations[] = $expectation;
80 304
    }
81
82
    /**
83
     * Handle a method call being directed by this instance
84
     *
85
     * @param array $args
86
     * @return mixed
87
     */
88 279
    public function call(array $args)
89
    {
90 279
        $expectation = $this->findExpectation($args);
91 279
        if (is_null($expectation)) {
92 49
            $exception = new \Mockery\Exception\NoMatchingExpectationException(
93
                'No matching handler found for '
94 49
                . $this->_mock->mockery_getName() . '::'
95 49
                . \Mockery::formatArgs($this->_name, $args)
96 49
                . '. Either the method was unexpected or its arguments matched'
97 49
                . ' no expected argument list for this method'
98 49
                . PHP_EOL . PHP_EOL
99 49
                . \Mockery::formatObjects($args)
100 49
            );
101 49
            $exception->setMock($this->_mock)
102 49
                ->setMethodName($this->_name)
103 49
                ->setActualArguments($args);
104 49
            throw $exception;
105
        }
106 233
        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 142
    public function verify()
116
    {
117 142
        if (!empty($this->_expectations)) {
118 138
            foreach ($this->_expectations as $exp) {
119 138
                $exp->verify();
120 120
            }
121 119
        } else {
122 6
            foreach ($this->_defaults as $exp) {
123 6
                $exp->verify();
124 6
            }
125
        }
126 123
    }
127
128
    /**
129
     * Attempt to locate an expectation matching the provided args
130
     *
131
     * @param array $args
132
     * @return mixed
133
     */
134 279
    public function findExpectation(array $args)
135
    {
136 279
        if (!empty($this->_expectations)) {
137 262
            $orderedExpectations = array();
138 262
            $reversedUnordered = array();
139 262
            foreach ($this->_expectations as $exp) {
140 262
                if ($exp->getOrderNumber() || $exp->isExpected()) {
141 150
                    array_push($orderedExpectations, $exp);
142 150
                } else {
143 162
                    array_unshift($reversedUnordered, $exp);
144
                }
145 262
            }
146 262
            return $this->_findExpectationIn(array_merge($orderedExpectations, $reversedUnordered), $args);
147
        } else {
148 18
            return $this->_findExpectationIn($this->_defaults, $args);
149
        }
150
    }
151
152
    /**
153
     * Make the given expectation a default for all others assuming it was
154
     * correctly created last
155
     *
156
     * @param \Mockery\Expectation
157
     */
158 25
    public function makeExpectationDefault(\Mockery\Expectation $expectation)
159
    {
160 25
        $last = end($this->_expectations);
161 25
        if ($last === $expectation) {
162 24
            array_pop($this->_expectations);
163 24
            array_unshift($this->_defaults, $expectation);
164 24
        } else {
165 1
            throw new \Mockery\Exception(
166
                'Cannot turn a previously defined expectation into a default'
167 1
            );
168
        }
169 24
    }
170
171
    /**
172
     * Search current array of expectations for a match
173
     *
174
     * @param array $expectations
175
     * @param array $args
176
     * @return mixed
177
     */
178 279
    protected function _findExpectationIn(array $expectations, array $args)
179
    {
180 279
        foreach ($expectations as $exp) {
181 279
            if ($exp->isEligible() && $exp->matchArgs($args)) {
182 230
                return $exp;
183
            }
184 68
        }
185 60
        foreach ($expectations as $exp) {
186 60
            if ($exp->matchArgs($args)) {
187 11
                return $exp;
188
            }
189 49
        }
190 49
    }
191
192
    /**
193
     * Return all expectations assigned to this director
194
     *
195
     * @return array
196
     */
197 24
    public function getExpectations()
198
    {
199 24
        return $this->_expectations;
200
    }
201
202
    /**
203
     * Return all expectations assigned to this director
204
     *
205
     * @return array
206
     */
207 6
    public function getDefaultExpectations()
208
    {
209 6
        return $this->_defaults;
210
    }
211
212
    /**
213
     * Return the number of expectations assigned to this director.
214
     *
215
     * @return int
216
     */
217 19
    public function getExpectationCount()
218
    {
219 19
        return count($this->getExpectations());
220
    }
221
}
222