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 Mock implements MockInterface
24 : {
25 :
26 : /**
27 : * Stores an array of all expectation directors for this mock
28 : *
29 : * @var array
30 : */
31 : protected $_mockery_expectations = array();
32 :
33 : /**
34 : * Last expectation that was set
35 : *
36 : * @var object
37 : */
38 : protected $_mockery_lastExpectation = null;
39 :
40 : /**
41 : * Flag to indicate whether we can ignore method calls missing from our
42 : * expectations
43 : *
44 : * @var bool
45 : */
46 : protected $_mockery_ignoreMissing = false;
47 :
48 : /**
49 : * Flag to indicate whether this mock was verified
50 : *
51 : * @var bool
52 : */
53 : protected $_mockery_verified = false;
54 :
55 : /**
56 : * Given name of the mock
57 : *
58 : * @var string
59 : */
60 : protected $_mockery_name = null;
61 :
62 : /**
63 : * Order number of allocation
64 : *
65 : * @var int
66 : */
67 : protected $_mockery_allocatedOrder = 0;
68 :
69 : /**
70 : * Current ordered number
71 : *
72 : * @var int
73 : */
74 : protected $_mockery_currentOrder = 0;
75 :
76 : /**
77 : * Ordered groups
78 : *
79 : * @var array
80 : */
81 : protected $_mockery_groups = array();
82 :
83 : /**
84 : * Mock container containing this mock object
85 : *
86 : * @var \Mockery\Container
87 : */
88 : protected $_mockery_container = null;
89 :
90 : /**
91 : * Instance of a core object on which methods are called in the event
92 : * it has been set, and an expectation for one of the object's methods
93 : * does not exist. This implements a simple partial mock proxy system.
94 : *
95 : * @var object
96 : */
97 : protected $_mockery_partial = null;
98 :
99 : /**
100 : * Flag to indicate we should ignore all expectations temporarily. Used
101 : * mainly to prevent expectation matching when in the middle of a mock
102 : * object recording session.
103 : *
104 : * @var bool
105 : */
106 : protected $_mockery_disableExpectationMatching = false;
107 :
108 : /**
109 : * Stores all stubbed public methods separate from any on-object public
110 : * properties that may exist.
111 : *
112 : * @var array
113 : */
114 : protected $_mockery_mockableProperties = array();
115 :
116 : /**
117 : * We want to avoid constructors since class is copied to Generator.php
118 : * for inclusion on extending class definitions.
119 : *
120 : * @param string $name
121 : * @param \Mockery\Container $container
122 : * @param object $partialObject
123 : * @return void
124 : */
125 : public function mockery_init($name, \Mockery\Container $container = null, $partialObject = null)
126 : {
127 0 : $this->_mockery_name = $name;
128 0 : if(is_null($container)) {
129 0 : $container = new \Mockery\Container;
130 0 : }
131 0 : $this->_mockery_container = $container;
132 0 : if (!is_null($partialObject)) {
133 0 : $this->_mockery_partial = $partialObject;
134 0 : }
135 0 : }
136 :
137 : /**
138 : * Set expected method calls
139 : *
140 : * @param mixed
141 : * @return \Mockery\Expectation
142 : */
143 : public function shouldReceive()
144 : {
145 0 : $self = $this;
146 0 : $lastExpectation = \Mockery::parseShouldReturnArgs(
147 0 : $this, func_get_args(), function($method) use ($self) {
148 0 : $director = $self->mockery_getExpectationsFor($method);
149 0 : if (!$director) {
150 0 : $director = new \Mockery\ExpectationDirector($method, $self);
151 0 : $self->mockery_setExpectationsFor($method, $director);
152 0 : }
153 0 : $expectation = new \Mockery\Expectation($self, $method);
154 0 : $director->addExpectation($expectation);
155 0 : return $expectation;
156 : }
157 0 : );
158 0 : return $lastExpectation;
159 : }
160 :
161 : /**
162 : * Set mock to ignore unexpected methods and return Undefined class
163 : *
164 : * @return void
165 : */
166 : public function shouldIgnoreMissing()
167 : {
168 0 : $this->_mockery_ignoreMissing = true;
169 0 : }
170 :
171 : /**
172 : * Accepts a closure which is executed with an object recorder which proxies
173 : * to the partial source object. The intent being to record the
174 : * interactions of a concrete object as a set of expectations on the
175 : * current mock object. The partial may then be passed to a second process
176 : * to see if it fulfils the same (or exact same) contract as the original.
177 : *
178 : * @param Closure $closure
179 : */
180 : public function shouldExpect(Closure $closure)
181 : {
182 0 : $recorder = new \Mockery\Recorder($this, $this->_mockery_partial);
183 0 : $this->_mockery_disableExpectationMatching = true;
184 0 : $closure($recorder);
185 0 : $this->_mockery_disableExpectationMatching = false;
186 0 : return $this;
187 : }
188 :
189 : /**
190 : * In the event shouldReceive() accepting one or more methods/returns,
191 : * this method will switch them from normal expectations to default
192 : * expectations
193 : *
194 : * @return self
195 : */
196 : public function byDefault()
197 : {
198 0 : foreach ($this->_mockery_expectations as $director) {
199 0 : $exps = $director->getExpectations();
200 0 : foreach ($exps as $exp) {
201 0 : $exp->byDefault();
202 0 : }
203 0 : }
204 0 : return $this;
205 : }
206 :
207 : /**
208 : * Capture calls to this mock
209 : */
210 : public function __call($method, array $args)
211 : {
212 0 : if (isset($this->_mockery_expectations[$method])
213 0 : && !$this->_mockery_disableExpectationMatching) {
214 0 : $handler = $this->_mockery_expectations[$method];
215 0 : return $handler->call($args);
216 0 : } elseif (!is_null($this->_mockery_partial) && method_exists($this->_mockery_partial, $method)) {
217 0 : return call_user_func_array(array($this->_mockery_partial, $method), $args);
218 0 : } elseif ($this->_mockery_ignoreMissing) {
219 0 : $return = new \Mockery\Undefined;
220 0 : return $return;
221 : }
222 0 : throw new \BadMethodCallException(
223 0 : 'Method ' . $this->_mockery_name . '::' . $method . '() does not exist on this mock object'
224 0 : );
225 : }
226 :
227 : /**public function __set($name, $value)
228 : {
229 : $this->_mockery_mockableProperties[$name] = $value;
230 : return $this;
231 : }
232 :
233 : public function __get($name)
234 : {
235 : if (isset($this->_mockery_mockableProperties[$name])) {
236 : return $this->_mockery_mockableProperties[$name];
237 : } elseif(isset($this->{$name})) {
238 : return $this->{$name};
239 : }
240 : throw new \InvalidArgumentException (
241 : 'Property ' . $this->_mockery_name . '::' . $name . ' does not exist on this mock object'
242 : );
243 : }**/
244 :
245 : /**
246 : * Iterate across all expectation directors and validate each
247 : *
248 : * @throws \Mockery\CountValidator\Exception
249 : * @return void
250 : */
251 : public function mockery_verify()
252 : {
253 0 : if ($this->_mockery_verified) return true;
254 0 : $this->_mockery_verified = true;
255 0 : foreach($this->_mockery_expectations as $director) {
256 0 : $director->verify();
257 0 : }
258 0 : }
259 :
260 : /**
261 : * Tear down tasks for this mock
262 : *
263 : * @return void
264 : */
265 : public function mockery_teardown()
266 : {
267 :
268 0 : }
269 :
270 : /**
271 : * Fetch the next available allocation order number
272 : *
273 : * @return int
274 : */
275 : public function mockery_allocateOrder()
276 : {
277 0 : $this->_mockery_allocatedOrder += 1;
278 0 : return $this->_mockery_allocatedOrder;
279 : }
280 :
281 : /**
282 : * Set ordering for a group
283 : *
284 : * @param mixed $group
285 : * @param int $order
286 : */
287 : public function mockery_setGroup($group, $order)
288 : {
289 0 : $this->_mockery_groups[$group] = $order;
290 0 : }
291 :
292 : /**
293 : * Fetch array of ordered groups
294 : *
295 : * @return array
296 : */
297 : public function mockery_getGroups()
298 : {
299 0 : return $this->_mockery_groups;
300 : }
301 :
302 : /**
303 : * Set current ordered number
304 : *
305 : * @param int $order
306 : */
307 : public function mockery_setCurrentOrder($order)
308 : {
309 0 : $this->_mockery_currentOrder = $order;
310 0 : return $this->_mockery_currentOrder;
311 : }
312 :
313 : /**
314 : * Get current ordered number
315 : *
316 : * @return int
317 : */
318 : public function mockery_getCurrentOrder()
319 : {
320 0 : return $this->_mockery_currentOrder;
321 : }
322 :
323 : /**
324 : * Validate the current mock's ordering
325 : *
326 : * @param string $method
327 : * @param int $order
328 : * @throws \Mockery\Exception
329 : * @return void
330 : */
331 : public function mockery_validateOrder($method, $order)
332 : {
333 0 : if ($order < $this->_mockery_currentOrder) {
334 0 : throw new \Mockery\Exception(
335 0 : 'Method ' . $this->_mockery_name . '::' . $method . '()'
336 0 : . ' called out of order: expected order '
337 0 : . $order . ', was ' . $this->_mockery_currentOrder
338 0 : );
339 : }
340 0 : $this->mockery_setCurrentOrder($order);
341 0 : }
342 :
343 : /**
344 : * Return the expectations director for the given method
345 : *
346 : * @var string $method
347 : * @return \Mockery\ExpectationDirector|null
348 : */
349 : public function mockery_setExpectationsFor($method, \Mockery\ExpectationDirector $director)
350 : {
351 0 : $this->_mockery_expectations[$method] = $director;
352 0 : }
353 :
354 : /**
355 : * Return the expectations director for the given method
356 : *
357 : * @var string $method
358 : * @return \Mockery\ExpectationDirector|null
359 : */
360 : public function mockery_getExpectationsFor($method)
361 : {
362 0 : if (isset($this->_mockery_expectations[$method])) {
363 0 : return $this->_mockery_expectations[$method];
364 : }
365 0 : }
366 :
367 : /**
368 : * Find an expectation matching the given method and arguments
369 : *
370 : * @var string $method
371 : * @var array $args
372 : * @return \Mockery\Expectation|null
373 : */
374 : public function mockery_findExpectation($method, array $args)
375 : {
376 0 : if (!isset($this->_mockery_expectations[$method])) {
377 0 : return null;
378 : }
379 0 : $director = $this->_mockery_expectations[$method];
380 0 : return $director->findExpectation($args);
381 : }
382 :
383 : /**
384 : * Return the container for this mock
385 : *
386 : * @return \Mockery\Container
387 : */
388 : public function mockery_getContainer()
389 : {
390 0 : return $this->_mockery_container;
391 : }
392 :
393 : /**
394 : * Return the name for this mock
395 : *
396 : * @return string
397 : */
398 : public function mockery_getName()
399 : {
400 0 : return $this->_mockery_name;
401 : }
402 :
403 : public function mockery_getMockableProperties()
404 : {
405 0 : return $this->_mockery_mockableProperties;
406 : }
407 :
408 : }
|