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
|
|
|
use Mockery\HigherOrderMessage; |
24
|
|
|
use Mockery\MockInterface; |
25
|
|
|
|
26
|
|
|
class Mock implements MockInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* Stores an array of all expectation directors for this mock |
30
|
|
|
* |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
protected $_mockery_expectations = array(); |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Stores an inital number of expectations that can be manipulated |
37
|
|
|
* while using the getter method. |
38
|
|
|
* |
39
|
|
|
* @var int |
40
|
|
|
*/ |
41
|
|
|
protected $_mockery_expectations_count = 0; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Flag to indicate whether we can ignore method calls missing from our |
45
|
|
|
* expectations |
46
|
|
|
* |
47
|
|
|
* @var bool |
48
|
|
|
*/ |
49
|
|
|
protected $_mockery_ignoreMissing = false; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Flag to indicate whether we can defer method calls missing from our |
53
|
|
|
* expectations |
54
|
|
|
* |
55
|
|
|
* @var bool |
56
|
|
|
*/ |
57
|
|
|
protected $_mockery_deferMissing = false; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Flag to indicate whether this mock was verified |
61
|
|
|
* |
62
|
|
|
* @var bool |
63
|
|
|
*/ |
64
|
|
|
protected $_mockery_verified = false; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Given name of the mock |
68
|
|
|
* |
69
|
|
|
* @var string |
70
|
|
|
*/ |
71
|
|
|
protected $_mockery_name = null; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Order number of allocation |
75
|
|
|
* |
76
|
|
|
* @var int |
77
|
|
|
*/ |
78
|
|
|
protected $_mockery_allocatedOrder = 0; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Current ordered number |
82
|
|
|
* |
83
|
|
|
* @var int |
84
|
|
|
*/ |
85
|
|
|
protected $_mockery_currentOrder = 0; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Ordered groups |
89
|
|
|
* |
90
|
|
|
* @var array |
91
|
|
|
*/ |
92
|
|
|
protected $_mockery_groups = array(); |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Mock container containing this mock object |
96
|
|
|
* |
97
|
|
|
* @var \Mockery\Container |
98
|
|
|
*/ |
99
|
|
|
protected $_mockery_container = null; |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Instance of a core object on which methods are called in the event |
103
|
|
|
* it has been set, and an expectation for one of the object's methods |
104
|
|
|
* does not exist. This implements a simple partial mock proxy system. |
105
|
|
|
* |
106
|
|
|
* @var object |
107
|
|
|
*/ |
108
|
|
|
protected $_mockery_partial = null; |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Flag to indicate we should ignore all expectations temporarily. Used |
112
|
|
|
* mainly to prevent expectation matching when in the middle of a mock |
113
|
|
|
* object recording session. |
114
|
|
|
* |
115
|
|
|
* @var bool |
116
|
|
|
*/ |
117
|
|
|
protected $_mockery_disableExpectationMatching = false; |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Stores all stubbed public methods separate from any on-object public |
121
|
|
|
* properties that may exist. |
122
|
|
|
* |
123
|
|
|
* @var array |
124
|
|
|
*/ |
125
|
|
|
protected $_mockery_mockableProperties = array(); |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @var array |
129
|
|
|
*/ |
130
|
|
|
protected $_mockery_mockableMethods = array(); |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Just a local cache for this mock's target's methods |
134
|
|
|
* |
135
|
|
|
* @var ReflectionMethod[] |
136
|
|
|
*/ |
137
|
|
|
protected static $_mockery_methods; |
138
|
|
|
|
139
|
|
|
protected $_mockery_allowMockingProtectedMethods = false; |
140
|
|
|
|
141
|
|
|
protected $_mockery_receivedMethodCalls; |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* If shouldIgnoreMissing is called, this value will be returned on all calls to missing methods |
145
|
|
|
* @var mixed |
146
|
|
|
*/ |
147
|
|
|
protected $_mockery_defaultReturnValue = null; |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* We want to avoid constructors since class is copied to Generator.php |
151
|
|
|
* for inclusion on extending class definitions. |
152
|
|
|
* |
153
|
|
|
* @param \Mockery\Container $container |
154
|
|
|
* @param object $partialObject |
155
|
|
|
* @return void |
156
|
|
|
*/ |
157
|
|
|
public function mockery_init(\Mockery\Container $container = null, $partialObject = null) |
158
|
|
|
{ |
159
|
|
|
if (is_null($container)) { |
160
|
|
|
$container = new \Mockery\Container; |
161
|
|
|
} |
162
|
|
|
$this->_mockery_container = $container; |
163
|
|
|
if (!is_null($partialObject)) { |
164
|
|
|
$this->_mockery_partial = $partialObject; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
if (!\Mockery::getConfiguration()->mockingNonExistentMethodsAllowed()) { |
168
|
|
|
foreach ($this->mockery_getMethods() as $method) { |
169
|
|
|
if ($method->isPublic() && !$method->isStatic()) { |
170
|
|
|
$this->_mockery_mockableMethods[] = $method->getName(); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Set expected method calls |
178
|
|
|
* |
179
|
|
|
* @param null|string $methodName,... one or many methods that are expected to be called in this mock |
180
|
|
|
* @return \Mockery\ExpectationInterface|\Mockery\HigherOrderMessage |
181
|
|
|
*/ |
182
|
|
|
public function shouldReceive($methodName = null) |
183
|
|
|
{ |
184
|
|
|
if ($methodName === null) { |
185
|
|
|
return new HigherOrderMessage($this, "shouldReceive"); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
foreach (func_get_args() as $method) { |
189
|
|
|
if ("" == $method) { |
190
|
|
|
throw new \InvalidArgumentException("Received empty method name"); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** @var array $nonPublicMethods */ |
195
|
|
|
$nonPublicMethods = $this->getNonPublicMethods(); |
196
|
|
|
|
197
|
|
|
$self = $this; |
198
|
|
|
$allowMockingProtectedMethods = $this->_mockery_allowMockingProtectedMethods; |
199
|
|
|
|
200
|
|
|
$lastExpectation = \Mockery::parseShouldReturnArgs( |
201
|
|
|
$this, func_get_args(), function ($method) use ($self, $nonPublicMethods, $allowMockingProtectedMethods) { |
202
|
|
|
$rm = $self->mockery_getMethod($method); |
203
|
|
|
if ($rm) { |
204
|
|
|
if ($rm->isPrivate()) { |
205
|
|
|
throw new \InvalidArgumentException("$method() cannot be mocked as it is a private method"); |
206
|
|
|
} |
207
|
|
|
if (!$allowMockingProtectedMethods && $rm->isProtected()) { |
208
|
|
|
throw new \InvalidArgumentException("$method() cannot be mocked as it is a protected method and mocking protected methods is not enabled for the currently used mock object."); |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
$director = $self->mockery_getExpectationsFor($method); |
213
|
|
|
if (!$director) { |
214
|
|
|
$director = new \Mockery\ExpectationDirector($method, $self); |
215
|
|
|
$self->mockery_setExpectationsFor($method, $director); |
216
|
|
|
} |
217
|
|
|
$expectation = new \Mockery\Expectation($self, $method); |
218
|
|
|
$director->addExpectation($expectation); |
219
|
|
|
return $expectation; |
220
|
|
|
} |
221
|
|
|
); |
222
|
|
|
return $lastExpectation; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Shortcut method for setting an expectation that a method should not be called. |
227
|
|
|
* |
228
|
|
|
* @param null|string $methodName,... one or many methods that are expected not to be called in this mock |
229
|
|
|
* @return \Mockery\Expectation|\Mockery\HigherOrderMessage |
230
|
|
|
*/ |
231
|
|
|
public function shouldNotReceive($methodName = null) |
232
|
|
|
{ |
233
|
|
|
if ($methodName === null) { |
234
|
|
|
return new HigherOrderMessage($this, "shouldNotReceive"); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
$expectation = call_user_func_array(array($this, 'shouldReceive'), func_get_args()); |
238
|
|
|
$expectation->never(); |
239
|
|
|
return $expectation; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Allows additional methods to be mocked that do not explicitly exist on mocked class |
244
|
|
|
* @param String $method name of the method to be mocked |
245
|
|
|
* @return Mock |
246
|
|
|
*/ |
247
|
|
|
public function shouldAllowMockingMethod($method) |
248
|
|
|
{ |
249
|
|
|
$this->_mockery_mockableMethods[] = $method; |
250
|
|
|
return $this; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Set mock to ignore unexpected methods and return Undefined class |
255
|
|
|
* @param mixed $returnValue the default return value for calls to missing functions on this mock |
256
|
|
|
* @return Mock |
257
|
|
|
*/ |
258
|
|
|
public function shouldIgnoreMissing($returnValue = null) |
259
|
|
|
{ |
260
|
|
|
$this->_mockery_ignoreMissing = true; |
261
|
|
|
$this->_mockery_defaultReturnValue = $returnValue; |
262
|
|
|
return $this; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
public function asUndefined() |
266
|
|
|
{ |
267
|
|
|
$this->_mockery_ignoreMissing = true; |
268
|
|
|
$this->_mockery_defaultReturnValue = new \Mockery\Undefined; |
269
|
|
|
return $this; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* @return Mock |
274
|
|
|
*/ |
275
|
|
|
public function shouldAllowMockingProtectedMethods() |
276
|
|
|
{ |
277
|
|
|
$this->_mockery_allowMockingProtectedMethods = true; |
278
|
|
|
return $this; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Set mock to defer unexpected methods to it's parent |
284
|
|
|
* |
285
|
|
|
* This is particularly useless for this class, as it doesn't have a parent, |
286
|
|
|
* but included for completeness |
287
|
|
|
* |
288
|
|
|
* @return Mock |
289
|
|
|
*/ |
290
|
|
|
public function shouldDeferMissing() |
291
|
|
|
{ |
292
|
|
|
$this->_mockery_deferMissing = true; |
293
|
|
|
return $this; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Create an obviously worded alias to shouldDeferMissing() |
298
|
|
|
* |
299
|
|
|
* @return Mock |
300
|
|
|
*/ |
301
|
|
|
public function makePartial() |
302
|
|
|
{ |
303
|
|
|
return $this->shouldDeferMissing(); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* In the event shouldReceive() accepting one or more methods/returns, |
308
|
|
|
* this method will switch them from normal expectations to default |
309
|
|
|
* expectations |
310
|
|
|
* |
311
|
|
|
* @return self |
312
|
|
|
*/ |
313
|
|
|
public function byDefault() |
314
|
|
|
{ |
315
|
|
|
foreach ($this->_mockery_expectations as $director) { |
316
|
|
|
$exps = $director->getExpectations(); |
317
|
|
|
foreach ($exps as $exp) { |
318
|
|
|
$exp->byDefault(); |
319
|
|
|
} |
320
|
|
|
} |
321
|
|
|
return $this; |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* Capture calls to this mock |
326
|
|
|
*/ |
327
|
|
|
public function __call($method, array $args) |
328
|
|
|
{ |
329
|
|
|
return $this->_mockery_handleMethodCall($method, $args); |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
public static function __callStatic($method, array $args) |
333
|
|
|
{ |
334
|
|
|
return self::_mockery_handleStaticMethodCall($method, $args); |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* Forward calls to this magic method to the __call method |
339
|
|
|
*/ |
340
|
|
|
public function __toString() |
341
|
|
|
{ |
342
|
|
|
return $this->__call('__toString', array()); |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
/** |
346
|
|
|
* Iterate across all expectation directors and validate each |
347
|
|
|
* |
348
|
|
|
* @throws \Mockery\CountValidator\Exception |
349
|
|
|
* @return void |
350
|
|
|
*/ |
351
|
|
|
public function mockery_verify() |
352
|
|
|
{ |
353
|
|
|
if ($this->_mockery_verified) { |
354
|
|
|
return; |
355
|
|
|
} |
356
|
|
|
if (isset($this->_mockery_ignoreVerification) |
357
|
|
|
&& $this->_mockery_ignoreVerification == true) { |
358
|
|
|
return; |
359
|
|
|
} |
360
|
|
|
$this->_mockery_verified = true; |
361
|
|
|
foreach ($this->_mockery_expectations as $director) { |
362
|
|
|
$director->verify(); |
363
|
|
|
} |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* Tear down tasks for this mock |
368
|
|
|
* |
369
|
|
|
* @return void |
370
|
|
|
*/ |
371
|
|
|
public function mockery_teardown() |
372
|
|
|
{ |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
/** |
376
|
|
|
* Fetch the next available allocation order number |
377
|
|
|
* |
378
|
|
|
* @return int |
379
|
|
|
*/ |
380
|
|
|
public function mockery_allocateOrder() |
381
|
|
|
{ |
382
|
|
|
$this->_mockery_allocatedOrder += 1; |
383
|
|
|
return $this->_mockery_allocatedOrder; |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
/** |
387
|
|
|
* Set ordering for a group |
388
|
|
|
* |
389
|
|
|
* @param mixed $group |
390
|
|
|
* @param int $order |
391
|
|
|
*/ |
392
|
|
|
public function mockery_setGroup($group, $order) |
393
|
|
|
{ |
394
|
|
|
$this->_mockery_groups[$group] = $order; |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
/** |
398
|
|
|
* Fetch array of ordered groups |
399
|
|
|
* |
400
|
|
|
* @return array |
401
|
|
|
*/ |
402
|
|
|
public function mockery_getGroups() |
403
|
|
|
{ |
404
|
|
|
return $this->_mockery_groups; |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
/** |
408
|
|
|
* Set current ordered number |
409
|
|
|
* |
410
|
|
|
* @param int $order |
411
|
|
|
*/ |
412
|
|
|
public function mockery_setCurrentOrder($order) |
413
|
|
|
{ |
414
|
|
|
$this->_mockery_currentOrder = $order; |
415
|
|
|
return $this->_mockery_currentOrder; |
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
/** |
419
|
|
|
* Get current ordered number |
420
|
|
|
* |
421
|
|
|
* @return int |
422
|
|
|
*/ |
423
|
|
|
public function mockery_getCurrentOrder() |
424
|
|
|
{ |
425
|
|
|
return $this->_mockery_currentOrder; |
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
/** |
429
|
|
|
* Validate the current mock's ordering |
430
|
|
|
* |
431
|
|
|
* @param string $method |
432
|
|
|
* @param int $order |
433
|
|
|
* @throws \Mockery\Exception |
434
|
|
|
* @return void |
435
|
|
|
*/ |
436
|
|
|
public function mockery_validateOrder($method, $order) |
437
|
|
|
{ |
438
|
|
|
if ($order < $this->_mockery_currentOrder) { |
439
|
|
|
$exception = new \Mockery\Exception\InvalidOrderException( |
440
|
|
|
'Method ' . __CLASS__ . '::' . $method . '()' |
441
|
|
|
. ' called out of order: expected order ' |
442
|
|
|
. $order . ', was ' . $this->_mockery_currentOrder |
443
|
|
|
); |
444
|
|
|
$exception->setMock($this) |
445
|
|
|
->setMethodName($method) |
446
|
|
|
->setExpectedOrder($order) |
447
|
|
|
->setActualOrder($this->_mockery_currentOrder); |
448
|
|
|
throw $exception; |
449
|
|
|
} |
450
|
|
|
$this->mockery_setCurrentOrder($order); |
451
|
|
|
} |
452
|
|
|
|
453
|
|
|
/** |
454
|
|
|
* Gets the count of expectations for this mock |
455
|
|
|
* |
456
|
|
|
* @return int |
457
|
|
|
*/ |
458
|
|
|
public function mockery_getExpectationCount() |
459
|
|
|
{ |
460
|
|
|
$count = $this->_mockery_expectations_count; |
461
|
|
|
foreach ($this->_mockery_expectations as $director) { |
462
|
|
|
$count += $director->getExpectationCount(); |
463
|
|
|
} |
464
|
|
|
return $count; |
465
|
|
|
} |
466
|
|
|
|
467
|
|
|
/** |
468
|
|
|
* Return the expectations director for the given method |
469
|
|
|
* |
470
|
|
|
* @var string $method |
471
|
|
|
* @return \Mockery\ExpectationDirector|null |
472
|
|
|
*/ |
473
|
|
|
public function mockery_setExpectationsFor($method, \Mockery\ExpectationDirector $director) |
474
|
|
|
{ |
475
|
|
|
$this->_mockery_expectations[$method] = $director; |
476
|
|
|
} |
477
|
|
|
|
478
|
|
|
/** |
479
|
|
|
* Return the expectations director for the given method |
480
|
|
|
* |
481
|
|
|
* @var string $method |
482
|
|
|
* @return \Mockery\ExpectationDirector|null |
483
|
|
|
*/ |
484
|
|
|
public function mockery_getExpectationsFor($method) |
485
|
|
|
{ |
486
|
|
|
if (isset($this->_mockery_expectations[$method])) { |
487
|
|
|
return $this->_mockery_expectations[$method]; |
488
|
|
|
} |
489
|
|
|
} |
490
|
|
|
|
491
|
|
|
/** |
492
|
|
|
* Find an expectation matching the given method and arguments |
493
|
|
|
* |
494
|
|
|
* @var string $method |
495
|
|
|
* @var array $args |
496
|
|
|
* @return \Mockery\Expectation|null |
497
|
|
|
*/ |
498
|
|
|
public function mockery_findExpectation($method, array $args) |
499
|
|
|
{ |
500
|
|
|
if (!isset($this->_mockery_expectations[$method])) { |
501
|
|
|
return null; |
502
|
|
|
} |
503
|
|
|
$director = $this->_mockery_expectations[$method]; |
504
|
|
|
|
505
|
|
|
return $director->findExpectation($args); |
506
|
|
|
} |
507
|
|
|
|
508
|
|
|
/** |
509
|
|
|
* Return the container for this mock |
510
|
|
|
* |
511
|
|
|
* @return \Mockery\Container |
512
|
|
|
*/ |
513
|
|
|
public function mockery_getContainer() |
514
|
|
|
{ |
515
|
|
|
return $this->_mockery_container; |
516
|
|
|
} |
517
|
|
|
|
518
|
|
|
/** |
519
|
|
|
* Return the name for this mock |
520
|
|
|
* |
521
|
|
|
* @return string |
522
|
|
|
*/ |
523
|
|
|
public function mockery_getName() |
524
|
|
|
{ |
525
|
|
|
return __CLASS__; |
526
|
|
|
} |
527
|
|
|
|
528
|
|
|
/** |
529
|
|
|
* @return array |
530
|
|
|
*/ |
531
|
|
|
public function mockery_getMockableProperties() |
532
|
|
|
{ |
533
|
|
|
return $this->_mockery_mockableProperties; |
534
|
|
|
} |
535
|
|
|
|
536
|
|
|
public function __isset($name) |
537
|
|
|
{ |
538
|
|
|
if (false === stripos($name, '_mockery_') && method_exists(get_parent_class($this), '__isset')) { |
539
|
|
|
return parent::__isset($name); |
540
|
|
|
} |
541
|
|
|
|
542
|
|
|
return false; |
543
|
|
|
} |
544
|
|
|
|
545
|
|
|
public function mockery_getExpectations() |
546
|
|
|
{ |
547
|
|
|
return $this->_mockery_expectations; |
548
|
|
|
} |
549
|
|
|
|
550
|
|
|
/** |
551
|
|
|
* Calls a parent class method and returns the result. Used in a passthru |
552
|
|
|
* expectation where a real return value is required while still taking |
553
|
|
|
* advantage of expectation matching and call count verification. |
554
|
|
|
* |
555
|
|
|
* @param string $name |
556
|
|
|
* @param array $args |
557
|
|
|
* @return mixed |
558
|
|
|
*/ |
559
|
|
|
public function mockery_callSubjectMethod($name, array $args) |
560
|
|
|
{ |
561
|
|
|
return call_user_func_array('parent::' . $name, $args); |
562
|
|
|
} |
563
|
|
|
|
564
|
|
|
/** |
565
|
|
|
* @return string[] |
566
|
|
|
*/ |
567
|
|
|
public function mockery_getMockableMethods() |
568
|
|
|
{ |
569
|
|
|
return $this->_mockery_mockableMethods; |
570
|
|
|
} |
571
|
|
|
|
572
|
|
|
/** |
573
|
|
|
* @return bool |
574
|
|
|
*/ |
575
|
|
|
public function mockery_isAnonymous() |
576
|
|
|
{ |
577
|
|
|
$rfc = new \ReflectionClass($this); |
578
|
|
|
$onlyImplementsMock = count($rfc->getInterfaces()) == 1; |
579
|
|
|
return (false === $rfc->getParentClass()) && $onlyImplementsMock; |
580
|
|
|
} |
581
|
|
|
|
582
|
|
|
public function __wakeup() |
583
|
|
|
{ |
584
|
|
|
/** |
585
|
|
|
* This does not add __wakeup method support. It's a blind method and any |
586
|
|
|
* expected __wakeup work will NOT be performed. It merely cuts off |
587
|
|
|
* annoying errors where a __wakeup exists but is not essential when |
588
|
|
|
* mocking |
589
|
|
|
*/ |
590
|
|
|
} |
591
|
|
|
|
592
|
|
|
public function __destruct() |
593
|
|
|
{ |
594
|
|
|
/** |
595
|
|
|
* Overrides real class destructor in case if class was created without original constructor |
596
|
|
|
*/ |
597
|
|
|
} |
598
|
|
|
|
599
|
|
|
public function mockery_getMethod($name) |
600
|
|
|
{ |
601
|
|
|
foreach ($this->mockery_getMethods() as $method) { |
602
|
|
|
if ($method->getName() == $name) { |
603
|
|
|
return $method; |
604
|
|
|
} |
605
|
|
|
} |
606
|
|
|
|
607
|
|
|
return null; |
608
|
|
|
} |
609
|
|
|
|
610
|
|
|
/** |
611
|
|
|
* @param string $name Method name. |
612
|
|
|
* |
613
|
|
|
* @return mixed Generated return value based on the declared return value of the named method. |
614
|
|
|
*/ |
615
|
|
|
public function mockery_returnValueForMethod($name) |
616
|
|
|
{ |
617
|
|
|
if (version_compare(PHP_VERSION, '7.0.0-dev') < 0) { |
618
|
|
|
return; |
619
|
|
|
} |
620
|
|
|
|
621
|
|
|
$rm = $this->mockery_getMethod($name); |
622
|
|
|
if (!$rm || !$rm->hasReturnType()) { |
623
|
|
|
return; |
624
|
|
|
} |
625
|
|
|
|
626
|
|
|
$type = (string) $rm->getReturnType(); |
627
|
|
|
switch ($type) { |
628
|
|
|
case '': return; |
629
|
|
|
case 'string': return ''; |
630
|
|
|
case 'int': return 0; |
631
|
|
|
case 'float': return 0.0; |
632
|
|
|
case 'bool': return false; |
633
|
|
|
case 'array': return []; |
634
|
|
|
|
635
|
|
|
case 'callable': |
636
|
|
|
case 'Closure': |
637
|
|
|
return function () { |
638
|
|
|
}; |
639
|
|
|
|
640
|
|
|
case 'Traversable': |
641
|
|
|
case 'Generator': |
642
|
|
|
// Remove eval() when minimum version >=5.5 |
643
|
|
|
$generator = eval('return function () { yield; };'); |
644
|
|
|
return $generator(); |
645
|
|
|
|
646
|
|
|
case 'self': |
647
|
|
|
return \Mockery::mock($rm->getDeclaringClass()->getName()); |
648
|
|
|
|
649
|
|
|
case 'void': |
650
|
|
|
return null; |
651
|
|
|
|
652
|
|
|
default: |
653
|
|
|
return \Mockery::mock($type); |
654
|
|
|
} |
655
|
|
|
} |
656
|
|
|
|
657
|
|
|
public function shouldHaveReceived($method = null, $args = null) |
658
|
|
|
{ |
659
|
|
|
if ($method === null) { |
660
|
|
|
return new HigherOrderMessage($this, "shouldHaveReceived"); |
661
|
|
|
} |
662
|
|
|
|
663
|
|
|
$expectation = new \Mockery\VerificationExpectation($this, $method); |
664
|
|
|
if (null !== $args) { |
665
|
|
|
$expectation->withArgs($args); |
|
|
|
|
666
|
|
|
} |
667
|
|
|
$expectation->atLeast()->once(); |
668
|
|
|
$director = new \Mockery\VerificationDirector($this->_mockery_getReceivedMethodCalls(), $expectation); |
669
|
|
|
$this->_mockery_expectations_count++; |
670
|
|
|
$director->verify(); |
671
|
|
|
return $director; |
672
|
|
|
} |
673
|
|
|
|
674
|
|
|
public function shouldNotHaveReceived($method = null, $args = null) |
675
|
|
|
{ |
676
|
|
|
if ($method === null) { |
677
|
|
|
return new HigherOrderMessage($this, "shouldNotHaveReceived"); |
678
|
|
|
} |
679
|
|
|
|
680
|
|
|
$expectation = new \Mockery\VerificationExpectation($this, $method); |
681
|
|
|
if (null !== $args) { |
682
|
|
|
$expectation->withArgs($args); |
|
|
|
|
683
|
|
|
} |
684
|
|
|
$expectation->never(); |
685
|
|
|
$director = new \Mockery\VerificationDirector($this->_mockery_getReceivedMethodCalls(), $expectation); |
686
|
|
|
$this->_mockery_expectations_count++; |
687
|
|
|
$director->verify(); |
688
|
|
|
return null; |
689
|
|
|
} |
690
|
|
|
|
691
|
|
|
protected static function _mockery_handleStaticMethodCall($method, array $args) |
692
|
|
|
{ |
693
|
|
|
try { |
694
|
|
|
$associatedRealObject = \Mockery::fetchMock(__CLASS__); |
695
|
|
|
return $associatedRealObject->__call($method, $args); |
696
|
|
|
} catch (\BadMethodCallException $e) { |
697
|
|
|
throw new \BadMethodCallException( |
698
|
|
|
'Static method ' . $associatedRealObject->mockery_getName() . '::' . $method |
699
|
|
|
. '() does not exist on this mock object' |
700
|
|
|
); |
701
|
|
|
} |
702
|
|
|
} |
703
|
|
|
|
704
|
|
|
protected function _mockery_getReceivedMethodCalls() |
705
|
|
|
{ |
706
|
|
|
return $this->_mockery_receivedMethodCalls ?: $this->_mockery_receivedMethodCalls = new \Mockery\ReceivedMethodCalls(); |
707
|
|
|
} |
708
|
|
|
|
709
|
|
|
protected function _mockery_handleMethodCall($method, array $args) |
710
|
|
|
{ |
711
|
|
|
$this->_mockery_getReceivedMethodCalls()->push(new \Mockery\MethodCall($method, $args)); |
712
|
|
|
|
713
|
|
|
$rm = $this->mockery_getMethod($method); |
714
|
|
|
if ($rm && $rm->isProtected() && !$this->_mockery_allowMockingProtectedMethods) { |
715
|
|
|
if ($rm->isAbstract()) { |
716
|
|
|
return; |
717
|
|
|
} |
718
|
|
|
|
719
|
|
|
try { |
720
|
|
|
$prototype = $rm->getPrototype(); |
721
|
|
|
if ($prototype->isAbstract()) { |
722
|
|
|
return; |
723
|
|
|
} |
724
|
|
|
} catch (\ReflectionException $re) { |
725
|
|
|
// noop - there is no hasPrototype method |
726
|
|
|
} |
727
|
|
|
|
728
|
|
|
return call_user_func_array("parent::$method", $args); |
729
|
|
|
} |
730
|
|
|
|
731
|
|
|
if (isset($this->_mockery_expectations[$method]) |
732
|
|
|
&& !$this->_mockery_disableExpectationMatching) { |
733
|
|
|
$handler = $this->_mockery_expectations[$method]; |
734
|
|
|
|
735
|
|
|
try { |
736
|
|
|
return $handler->call($args); |
737
|
|
|
} catch (\Mockery\Exception\NoMatchingExpectationException $e) { |
738
|
|
|
if (!$this->_mockery_ignoreMissing && !$this->_mockery_deferMissing) { |
739
|
|
|
throw $e; |
740
|
|
|
} |
741
|
|
|
} |
742
|
|
|
} |
743
|
|
|
|
744
|
|
|
if (!is_null($this->_mockery_partial) && method_exists($this->_mockery_partial, $method)) { |
745
|
|
|
return call_user_func_array(array($this->_mockery_partial, $method), $args); |
746
|
|
|
} elseif ($this->_mockery_deferMissing && is_callable("parent::$method") |
747
|
|
|
&& (!$this->hasMethodOverloadingInParentClass() || method_exists(get_parent_class($this), $method))) { |
748
|
|
|
return call_user_func_array("parent::$method", $args); |
749
|
|
|
} elseif ($method == '__toString') { |
750
|
|
|
// __toString is special because we force its addition to the class API regardless of the |
751
|
|
|
// original implementation. Thus, we should always return a string rather than honor |
752
|
|
|
// _mockery_ignoreMissing and break the API with an error. |
753
|
|
|
return sprintf("%s#%s", __CLASS__, spl_object_hash($this)); |
754
|
|
|
} elseif ($this->_mockery_ignoreMissing) { |
755
|
|
|
if (\Mockery::getConfiguration()->mockingNonExistentMethodsAllowed() || (method_exists($this->_mockery_partial, $method) || is_callable("parent::$method"))) { |
756
|
|
|
if ($this->_mockery_defaultReturnValue instanceof \Mockery\Undefined) { |
757
|
|
|
return call_user_func_array(array($this->_mockery_defaultReturnValue, $method), $args); |
758
|
|
|
} elseif (null === $this->_mockery_defaultReturnValue) { |
759
|
|
|
return $this->mockery_returnValueForMethod($method); |
760
|
|
|
} else { |
761
|
|
|
return $this->_mockery_defaultReturnValue; |
762
|
|
|
} |
763
|
|
|
} |
764
|
|
|
} |
765
|
|
|
|
766
|
|
|
$message = 'Method ' . __CLASS__ . '::' . $method . |
767
|
|
|
'() does not exist on this mock object'; |
768
|
|
|
|
769
|
|
|
if (!is_null($rm)) { |
770
|
|
|
$message = 'Received ' . __CLASS__ . |
771
|
|
|
'::' . $method . '(), but no expectations were specified'; |
772
|
|
|
} |
773
|
|
|
|
774
|
|
|
throw new \BadMethodCallException( |
775
|
|
|
$message |
776
|
|
|
); |
777
|
|
|
} |
778
|
|
|
|
779
|
|
|
/** |
780
|
|
|
* Uses reflection to get the list of all |
781
|
|
|
* methods within the current mock object |
782
|
|
|
* |
783
|
|
|
* @return array |
784
|
|
|
*/ |
785
|
|
|
protected function mockery_getMethods() |
786
|
|
|
{ |
787
|
|
|
if (static::$_mockery_methods) { |
788
|
|
|
return static::$_mockery_methods; |
789
|
|
|
} |
790
|
|
|
|
791
|
|
|
if (isset($this->_mockery_partial)) { |
792
|
|
|
$reflected = new \ReflectionObject($this->_mockery_partial); |
793
|
|
|
} else { |
794
|
|
|
$reflected = new \ReflectionClass($this); |
795
|
|
|
} |
796
|
|
|
|
797
|
|
|
return static::$_mockery_methods = $reflected->getMethods(); |
|
|
|
|
798
|
|
|
} |
799
|
|
|
|
800
|
|
|
private function hasMethodOverloadingInParentClass() |
801
|
|
|
{ |
802
|
|
|
// if there's __call any name would be callable |
803
|
|
|
return is_callable('parent::' . uniqid(__FUNCTION__)); |
804
|
|
|
} |
805
|
|
|
|
806
|
|
|
/** |
807
|
|
|
* @return array |
808
|
|
|
*/ |
809
|
|
|
private function getNonPublicMethods() |
810
|
|
|
{ |
811
|
|
|
return array_map( |
812
|
|
|
function ($method) { |
813
|
|
|
return $method->getName(); |
814
|
|
|
}, |
815
|
|
|
array_filter($this->mockery_getMethods(), function ($method) { |
816
|
|
|
return !$method->isPublic(); |
817
|
|
|
}) |
818
|
|
|
); |
819
|
|
|
} |
820
|
|
|
} |
821
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: