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 padraic@php.net 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 : /**
27 : * Method name the director is directing
28 : *
29 : * @var string
30 : */
31 : protected $_name = null;
32 :
33 : /**
34 : * Mock object the director is attached to
35 : *
36 : * @var \Mockery\MockInterface
37 : */
38 : protected $_mock = null;
39 :
40 : /**
41 : * Stores an array of all expectations for this mock
42 : *
43 : * @var array
44 : */
45 : protected $_expectations = array();
46 :
47 : /**
48 : * The expected order of next call
49 : *
50 : * @var int
51 : */
52 : protected $_expectedOrder = null;
53 :
54 : /**
55 : * Stores an array of all default expectations for this mock
56 : *
57 : * @var array
58 : */
59 : protected $_defaults = array();
60 :
61 : /**
62 : * Constructor
63 : *
64 : * @param string $name
65 : * @param \Mockery\MockInterface $mock
66 : */
67 : public function __construct($name, \Mockery\MockInterface $mock)
68 : {
69 26 : $this->_name = $name;
70 26 : $this->_mock = $mock;
71 26 : }
72 :
73 : /**
74 : * Add a new expectation to the director
75 : *
76 : * @param Mutateme\Expectation $expectation
77 : */
78 : public function addExpectation(\Mockery\Expectation $expectation)
79 : {
80 26 : $this->_expectations[] = $expectation;
81 26 : }
82 :
83 : /**
84 : * Handle a method call being directed by this instance
85 : *
86 : * @param array $args
87 : * @return mixed
88 : */
89 : public function call(array $args)
90 : {
91 25 : $expectation = $this->findExpectation($args);
92 25 : if (is_null($expectation)) {
93 0 : throw new \Mockery\Exception(
94 : 'No matching handler found for '
95 0 : . $this->_mock->mockery_getName() . '::'
96 0 : . \Mockery::formatArgs($this->_name, $args)
97 0 : . '. Either the method was unexpected or its arguments matched'
98 0 : . ' no expected argument list for this method'
99 0 : );
100 : }
101 25 : return $expectation->verifyCall($args);
102 : }
103 :
104 : /**
105 : * Verify all expectations of the director
106 : *
107 : * @throws \Mockery\CountValidator\Exception
108 : * @return void
109 : */
110 : public function verify()
111 : {
112 24 : if (!empty($this->_expectations)) {
113 24 : foreach ($this->_expectations as $exp) {
114 24 : $exp->verify();
115 24 : }
116 24 : } else {
117 0 : foreach ($this->_defaults as $exp) {
118 0 : $exp->verify();
119 0 : }
120 : }
121 24 : }
122 :
123 : /**
124 : * Attempt to locate an expecatation matching the provided args
125 : *
126 : * @param array $args
127 : * @return mixed
128 : */
129 : public function findExpectation(array $args)
130 : {
131 25 : if (!empty($this->_expectations)) {
132 25 : return $this->_findExpectationIn($this->_expectations, $args);
133 : } else {
134 0 : return $this->_findExpectationIn($this->_defaults, $args);
135 : }
136 : }
137 :
138 : /**
139 : * Make the given expectation a default for all others assuming it was
140 : * correctly created last
141 : *
142 : * @param \Mockery\Expectation
143 : */
144 : public function makeExpectationDefault(\Mockery\Expectation $expectation)
145 : {
146 0 : $last = end($this->_expectations);
147 0 : if ($last === $expectation) {
148 0 : array_pop($this->_expectations);
149 0 : $this->_defaults[] = $expectation;
150 0 : } else {
151 0 : throw new \Mockery\Exception(
152 : 'Cannot turn a previously defined expectation into a default'
153 0 : );
154 : }
155 0 : }
156 :
157 : /**
158 : * Search current array of expectations for a match
159 : *
160 : * @param array $expectations
161 : * @param array $args
162 : * @return mixed
163 : */
164 : protected function _findExpectationIn(array $expectations, array $args)
165 : {
166 25 : foreach ($expectations as $exp) {
167 25 : if ($exp->matchArgs($args) && $exp->isEligible()) {
168 25 : return $exp;
169 : }
170 2 : }
171 0 : foreach ($expectations as $exp) {
172 0 : if ($exp->matchArgs($args)) {
173 0 : return $exp;
174 : }
175 0 : }
176 0 : }
177 :
178 : /**
179 : * Return all expectations assigned to this director
180 : *
181 : * @return array
182 : */
183 : public function getExpectations()
184 : {
185 0 : return $this->_expectations;
186 : }
187 :
188 : }
|