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 Closure; |
24
|
|
|
use Mockery\Matcher\MultiArgumentClosure; |
25
|
|
|
use Mockery\Matcher\ArgumentListMatcher; |
26
|
|
|
use Mockery\Matcher\AnyArgs; |
27
|
|
|
use Mockery\Matcher\NoArgs; |
28
|
|
|
|
29
|
|
|
class Expectation implements ExpectationInterface |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* Mock object to which this expectation belongs |
33
|
|
|
* |
34
|
|
|
* @var object |
35
|
|
|
*/ |
36
|
|
|
protected $_mock = null; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Method name |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $_name = null; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Arguments expected by this expectation |
47
|
|
|
* |
48
|
|
|
* @var array |
49
|
|
|
*/ |
50
|
|
|
protected $_expectedArgs = array(); |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Count validator store |
54
|
|
|
* |
55
|
|
|
* @var array |
56
|
|
|
*/ |
57
|
|
|
protected $_countValidators = array(); |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* The count validator class to use |
61
|
|
|
* |
62
|
|
|
* @var string |
63
|
|
|
*/ |
64
|
|
|
protected $_countValidatorClass = 'Mockery\CountValidator\Exact'; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Actual count of calls to this expectation |
68
|
|
|
* |
69
|
|
|
* @var int |
70
|
|
|
*/ |
71
|
|
|
protected $_actualCount = 0; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Value to return from this expectation |
75
|
|
|
* |
76
|
|
|
* @var mixed |
77
|
|
|
*/ |
78
|
|
|
protected $_returnValue = null; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Array of return values as a queue for multiple return sequence |
82
|
|
|
* |
83
|
|
|
* @var array |
84
|
|
|
*/ |
85
|
|
|
protected $_returnQueue = array(); |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Array of closures executed with given arguments to generate a result |
89
|
|
|
* to be returned |
90
|
|
|
* |
91
|
|
|
* @var array |
92
|
|
|
*/ |
93
|
|
|
protected $_closureQueue = array(); |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Array of values to be set when this expectation matches |
97
|
|
|
* |
98
|
|
|
* @var array |
99
|
|
|
*/ |
100
|
|
|
protected $_setQueue = array(); |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Integer representing the call order of this expectation |
104
|
|
|
* |
105
|
|
|
* @var int |
106
|
|
|
*/ |
107
|
|
|
protected $_orderNumber = null; |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Integer representing the call order of this expectation on a global basis |
111
|
|
|
* |
112
|
|
|
* @var int |
113
|
|
|
*/ |
114
|
|
|
protected $_globalOrderNumber = null; |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Flag indicating that an exception is expected to be throw (not returned) |
118
|
|
|
* |
119
|
|
|
* @var bool |
120
|
|
|
*/ |
121
|
|
|
protected $_throw = false; |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Flag indicating whether the order of calling is determined locally or |
125
|
|
|
* globally |
126
|
|
|
* |
127
|
|
|
* @var bool |
128
|
|
|
*/ |
129
|
|
|
protected $_globally = false; |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Flag indicating if the return value should be obtained from the original |
133
|
|
|
* class method instead of returning predefined values from the return queue |
134
|
|
|
* |
135
|
|
|
* @var bool |
136
|
|
|
*/ |
137
|
|
|
protected $_passthru = false; |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Constructor |
141
|
|
|
* |
142
|
|
|
* @param \Mockery\MockInterface $mock |
143
|
|
|
* @param string $name |
144
|
|
|
*/ |
145
|
310 |
|
public function __construct(\Mockery\MockInterface $mock, $name) |
146
|
|
|
{ |
147
|
310 |
|
$this->_mock = $mock; |
148
|
310 |
|
$this->_name = $name; |
149
|
310 |
|
$this->withAnyArgs(); |
150
|
310 |
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Return a string with the method name and arguments formatted |
154
|
|
|
* |
155
|
|
|
* @param string $name Name of the expected method |
156
|
|
|
* @param array $args List of arguments to the method |
157
|
|
|
* @return string |
158
|
|
|
*/ |
159
|
46 |
|
public function __toString() |
160
|
|
|
{ |
161
|
46 |
|
return \Mockery::formatArgs($this->_name, $this->_expectedArgs); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Verify the current call, i.e. that the given arguments match those |
166
|
|
|
* of this expectation |
167
|
|
|
* |
168
|
|
|
* @param array $args |
169
|
|
|
* @return mixed |
170
|
|
|
*/ |
171
|
238 |
|
public function verifyCall(array $args) |
172
|
|
|
{ |
173
|
238 |
|
$this->validateOrder(); |
174
|
238 |
|
$this->_actualCount++; |
175
|
238 |
|
if (true === $this->_passthru) { |
176
|
3 |
|
return $this->_mock->mockery_callSubjectMethod($this->_name, $args); |
177
|
|
|
} |
178
|
|
|
|
179
|
236 |
|
$return = $this->_getReturnValue($args); |
180
|
236 |
|
$this->throwAsNecessary($return); |
181
|
230 |
|
$this->_setValues(); |
182
|
|
|
|
183
|
230 |
|
return $return; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Throws an exception if the expectation has been configured to do so |
188
|
|
|
* |
189
|
|
|
* @throws \Exception|\Throwable |
190
|
|
|
* @return void |
191
|
|
|
*/ |
192
|
236 |
|
private function throwAsNecessary($return) |
193
|
|
|
{ |
194
|
236 |
|
if (!$this->_throw) { |
195
|
230 |
|
return; |
196
|
|
|
} |
197
|
|
|
|
198
|
7 |
|
$type = version_compare(PHP_VERSION, '7.0.0') >= 0 |
199
|
7 |
|
? "\Throwable" |
200
|
7 |
|
: "\Exception"; |
201
|
|
|
|
202
|
7 |
|
if ($return instanceof $type) { |
203
|
7 |
|
throw $return; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
return; |
|
|
|
|
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Sets public properties with queued values to the mock object |
211
|
|
|
* |
212
|
|
|
* @param array $args |
|
|
|
|
213
|
|
|
* @return mixed |
214
|
|
|
*/ |
215
|
230 |
|
protected function _setValues() |
216
|
|
|
{ |
217
|
230 |
|
foreach ($this->_setQueue as $name => &$values) { |
218
|
8 |
|
if (count($values) > 0) { |
219
|
8 |
|
$value = array_shift($values); |
220
|
8 |
|
$this->_mock->{$name} = $value; |
221
|
8 |
|
} |
222
|
230 |
|
} |
223
|
230 |
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Fetch the return value for the matching args |
227
|
|
|
* |
228
|
|
|
* @param array $args |
229
|
|
|
* @return mixed |
230
|
|
|
*/ |
231
|
236 |
|
protected function _getReturnValue(array $args) |
232
|
|
|
{ |
233
|
236 |
|
if (count($this->_closureQueue) > 1) { |
234
|
|
|
return call_user_func_array(array_shift($this->_closureQueue), $args); |
235
|
236 |
|
} elseif (count($this->_closureQueue) > 0) { |
236
|
1 |
|
return call_user_func_array(current($this->_closureQueue), $args); |
237
|
235 |
|
} elseif (count($this->_returnQueue) > 1) { |
238
|
5 |
|
return array_shift($this->_returnQueue); |
239
|
234 |
|
} elseif (count($this->_returnQueue) > 0) { |
240
|
96 |
|
return current($this->_returnQueue); |
241
|
|
|
} |
242
|
|
|
|
243
|
139 |
|
return $this->_mock->mockery_returnValueForMethod($this->_name); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Checks if this expectation is eligible for additional calls |
248
|
|
|
* |
249
|
|
|
* @return bool |
250
|
|
|
*/ |
251
|
275 |
|
public function isEligible() |
252
|
|
|
{ |
253
|
275 |
|
foreach ($this->_countValidators as $validator) { |
254
|
139 |
|
if (!$validator->isEligible($this->_actualCount)) { |
255
|
22 |
|
return false; |
256
|
|
|
} |
257
|
272 |
|
} |
258
|
272 |
|
return true; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Check if there is a constraint on call count |
263
|
|
|
* |
264
|
|
|
* @return bool |
265
|
|
|
*/ |
266
|
|
|
public function isCallCountConstrained() |
267
|
|
|
{ |
268
|
|
|
return (count($this->_countValidators) > 0); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Verify call order |
273
|
|
|
* |
274
|
|
|
* @return void |
275
|
|
|
*/ |
276
|
238 |
|
public function validateOrder() |
277
|
|
|
{ |
278
|
238 |
|
if ($this->_orderNumber) { |
279
|
15 |
|
$this->_mock->mockery_validateOrder((string) $this, $this->_orderNumber, $this->_mock); |
280
|
15 |
|
} |
281
|
238 |
|
if ($this->_globalOrderNumber) { |
282
|
1 |
|
$this->_mock->mockery_getContainer() |
283
|
1 |
|
->mockery_validateOrder((string) $this, $this->_globalOrderNumber, $this->_mock); |
284
|
1 |
|
} |
285
|
238 |
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Verify this expectation |
289
|
|
|
* |
290
|
|
|
* @return bool |
291
|
|
|
*/ |
292
|
148 |
|
public function verify() |
293
|
|
|
{ |
294
|
148 |
|
foreach ($this->_countValidators as $validator) { |
295
|
123 |
|
$validator->validate($this->_actualCount); |
296
|
132 |
|
} |
297
|
129 |
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* Check if the registered expectation is an ArgumentListMatcher |
301
|
|
|
* @return bool |
302
|
|
|
*/ |
303
|
282 |
|
private function isArgumentListMatcher() |
304
|
|
|
{ |
305
|
282 |
|
return (count($this->_expectedArgs) === 1 && ($this->_expectedArgs[0] instanceof ArgumentListMatcher)); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* Check if passed arguments match an argument expectation |
310
|
|
|
* |
311
|
|
|
* @param array $args |
312
|
|
|
* @return bool |
313
|
|
|
*/ |
314
|
282 |
|
public function matchArgs(array $args) |
315
|
|
|
{ |
316
|
282 |
|
if ($this->isArgumentListMatcher()) { |
317
|
182 |
|
return $this->_matchArg($this->_expectedArgs[0], $args); |
318
|
|
|
} |
319
|
105 |
|
$argCount = count($args); |
320
|
105 |
|
if ($argCount !== count($this->_expectedArgs)) { |
321
|
5 |
|
return false; |
322
|
|
|
} |
323
|
102 |
|
for ($i=0; $i<$argCount; $i++) { |
324
|
102 |
|
$param =& $args[$i]; |
325
|
102 |
|
if (!$this->_matchArg($this->_expectedArgs[$i], $param)) { |
326
|
45 |
|
return false; |
327
|
|
|
} |
328
|
66 |
|
} |
329
|
|
|
|
330
|
66 |
|
return true; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* Check if passed argument matches an argument expectation |
335
|
|
|
* |
336
|
|
|
* @param mixed $expected |
337
|
|
|
* @param mixed &$actual |
338
|
|
|
* @return bool |
339
|
|
|
*/ |
340
|
279 |
|
protected function _matchArg($expected, &$actual) |
341
|
|
|
{ |
342
|
279 |
|
if ($expected === $actual) { |
343
|
30 |
|
return true; |
344
|
|
|
} |
345
|
264 |
|
if (!is_object($expected) && !is_object($actual) && $expected == $actual) { |
346
|
|
|
return true; |
347
|
|
|
} |
348
|
264 |
|
if (is_string($expected) && !is_array($actual) && !is_object($actual)) { |
349
|
|
|
# push/pop an error handler here to to make sure no error/exception thrown if $expected is not a regex |
350
|
6 |
|
set_error_handler(function () { |
351
|
6 |
|
}); |
352
|
6 |
|
$result = preg_match($expected, (string) $actual); |
353
|
6 |
|
restore_error_handler(); |
354
|
|
|
|
355
|
6 |
|
if ($result) { |
356
|
3 |
|
return true; |
357
|
|
|
} |
358
|
3 |
|
} |
359
|
263 |
|
if (is_string($expected) && is_object($actual)) { |
360
|
1 |
|
$result = $actual instanceof $expected; |
361
|
1 |
|
if ($result) { |
362
|
1 |
|
return true; |
363
|
|
|
} |
364
|
|
|
} |
365
|
262 |
|
if ($expected instanceof \Mockery\Matcher\MatcherAbstract) { |
366
|
243 |
|
return $expected->match($actual); |
367
|
|
|
} |
368
|
21 |
|
if ($expected instanceof \Hamcrest\Matcher || $expected instanceof \Hamcrest_Matcher) { |
|
|
|
|
369
|
3 |
|
return $expected->matches($actual); |
370
|
|
|
} |
371
|
18 |
|
return false; |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* Expected argument setter for the expectation |
376
|
|
|
* |
377
|
|
|
* @param mixed[] ... |
378
|
|
|
* @return self |
379
|
|
|
*/ |
380
|
130 |
|
public function with(...$args) |
381
|
|
|
{ |
382
|
130 |
|
return $this->withArgs($args); |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
/** |
386
|
|
|
* Expected arguments for the expectation passed as an array |
387
|
|
|
* |
388
|
|
|
* @param array $arguments |
389
|
|
|
* @return self |
390
|
|
|
*/ |
391
|
141 |
|
private function withArgsInArray(array $arguments) |
392
|
|
|
{ |
393
|
141 |
|
if (empty($arguments)) { |
394
|
3 |
|
return $this->withNoArgs(); |
395
|
|
|
} |
396
|
138 |
|
$this->_expectedArgs = $arguments; |
397
|
138 |
|
return $this; |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
/** |
401
|
|
|
* Expected arguments have to be matched by the given closure. |
402
|
|
|
* |
403
|
|
|
* @param Closure $closure |
404
|
|
|
* @return self |
405
|
|
|
*/ |
406
|
6 |
|
private function withArgsMatchedByClosure(Closure $closure) |
407
|
|
|
{ |
408
|
6 |
|
$this->_expectedArgs = [new MultiArgumentClosure($closure)]; |
409
|
6 |
|
return $this; |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
/** |
413
|
|
|
* Expected arguments for the expectation passed as an array or a closure that matches each passed argument on |
414
|
|
|
* each function call. |
415
|
|
|
* |
416
|
|
|
* @param array|Closure $argsOrClosure |
417
|
|
|
* @return self |
418
|
|
|
*/ |
419
|
148 |
|
public function withArgs($argsOrClosure) |
420
|
|
|
{ |
421
|
148 |
|
if (is_array($argsOrClosure)) { |
422
|
141 |
|
$this->withArgsInArray($argsOrClosure); |
423
|
148 |
|
} elseif ($argsOrClosure instanceof Closure) { |
424
|
6 |
|
$this->withArgsMatchedByClosure($argsOrClosure); |
425
|
6 |
|
} else { |
426
|
1 |
|
throw new \InvalidArgumentException(sprintf('Call to %s with an invalid argument (%s), only array and '. |
427
|
1 |
|
'closure are allowed', __METHOD__, $argsOrClosure)); |
428
|
|
|
} |
429
|
147 |
|
return $this; |
430
|
|
|
} |
431
|
|
|
|
432
|
|
|
/** |
433
|
|
|
* Set with() as no arguments expected |
434
|
|
|
* |
435
|
|
|
* @return self |
436
|
|
|
*/ |
437
|
7 |
|
public function withNoArgs() |
438
|
|
|
{ |
439
|
7 |
|
$this->_expectedArgs = [new NoArgs()]; |
440
|
7 |
|
return $this; |
441
|
|
|
} |
442
|
|
|
|
443
|
|
|
/** |
444
|
|
|
* Set expectation that any arguments are acceptable |
445
|
|
|
* |
446
|
|
|
* @return self |
447
|
|
|
*/ |
448
|
310 |
|
public function withAnyArgs() |
449
|
|
|
{ |
450
|
310 |
|
$this->_expectedArgs = [new AnyArgs()]; |
451
|
310 |
|
return $this; |
452
|
|
|
} |
453
|
|
|
|
454
|
|
|
/** |
455
|
|
|
* Set a return value, or sequential queue of return values |
456
|
|
|
* |
457
|
|
|
* @param mixed[] ... |
458
|
|
|
* @return self |
459
|
|
|
*/ |
460
|
109 |
|
public function andReturn(...$args) |
461
|
|
|
{ |
462
|
109 |
|
$this->_returnQueue = $args; |
463
|
109 |
|
return $this; |
464
|
|
|
} |
465
|
|
|
|
466
|
|
|
/** |
467
|
|
|
* Set a return value, or sequential queue of return values |
468
|
|
|
* |
469
|
|
|
* @param mixed[] ... |
470
|
|
|
* @return self |
471
|
|
|
*/ |
472
|
|
|
public function andReturns(...$args) |
473
|
|
|
{ |
474
|
|
|
return call_user_func_array([$this, 'andReturn'], $args); |
475
|
|
|
} |
476
|
|
|
|
477
|
|
|
/** |
478
|
|
|
* Return this mock, like a fluent interface |
479
|
|
|
* |
480
|
|
|
* @return self |
481
|
|
|
*/ |
482
|
1 |
|
public function andReturnSelf() |
483
|
|
|
{ |
484
|
1 |
|
return $this->andReturn($this->_mock); |
485
|
|
|
} |
486
|
|
|
|
487
|
|
|
/** |
488
|
|
|
* Set a sequential queue of return values with an array |
489
|
|
|
* |
490
|
|
|
* @param array $values |
491
|
|
|
* @return self |
492
|
|
|
*/ |
493
|
2 |
|
public function andReturnValues(array $values) |
494
|
|
|
{ |
495
|
2 |
|
call_user_func_array(array($this, 'andReturn'), $values); |
496
|
2 |
|
return $this; |
497
|
|
|
} |
498
|
|
|
|
499
|
|
|
/** |
500
|
|
|
* Set a closure or sequence of closures with which to generate return |
501
|
|
|
* values. The arguments passed to the expected method are passed to the |
502
|
|
|
* closures as parameters. |
503
|
|
|
* |
504
|
|
|
* @param callable[] $args |
505
|
|
|
* @return self |
506
|
|
|
*/ |
507
|
1 |
|
public function andReturnUsing(...$args) |
508
|
|
|
{ |
509
|
1 |
|
$this->_closureQueue = $args; |
510
|
1 |
|
return $this; |
511
|
|
|
} |
512
|
|
|
|
513
|
|
|
/** |
514
|
|
|
* Return a self-returning black hole object. |
515
|
|
|
* |
516
|
|
|
* @return self |
517
|
|
|
*/ |
518
|
1 |
|
public function andReturnUndefined() |
519
|
|
|
{ |
520
|
1 |
|
$this->andReturn(new \Mockery\Undefined); |
521
|
1 |
|
return $this; |
522
|
|
|
} |
523
|
|
|
|
524
|
|
|
/** |
525
|
|
|
* Return null. This is merely a language construct for Mock describing. |
526
|
|
|
* |
527
|
|
|
* @return self |
528
|
|
|
*/ |
529
|
1 |
|
public function andReturnNull() |
530
|
|
|
{ |
531
|
1 |
|
return $this->andReturn(null); |
532
|
|
|
} |
533
|
|
|
|
534
|
1 |
|
public function andReturnFalse() |
535
|
|
|
{ |
536
|
1 |
|
return $this->andReturn(false); |
537
|
|
|
} |
538
|
|
|
|
539
|
1 |
|
public function andReturnTrue() |
540
|
|
|
{ |
541
|
1 |
|
return $this->andReturn(true); |
542
|
|
|
} |
543
|
|
|
|
544
|
|
|
/** |
545
|
|
|
* Set Exception class and arguments to that class to be thrown |
546
|
|
|
* |
547
|
|
|
* @param string|\Exception $exception |
548
|
|
|
* @param string $message |
549
|
|
|
* @param int $code |
550
|
|
|
* @param \Exception $previous |
551
|
|
|
* @return self |
552
|
|
|
*/ |
553
|
6 |
|
public function andThrow($exception, $message = '', $code = 0, \Exception $previous = null) |
554
|
|
|
{ |
555
|
6 |
|
$this->_throw = true; |
556
|
6 |
|
if (is_object($exception)) { |
557
|
2 |
|
$this->andReturn($exception); |
558
|
2 |
|
} else { |
559
|
4 |
|
$this->andReturn(new $exception($message, $code, $previous)); |
560
|
|
|
} |
561
|
6 |
|
return $this; |
562
|
|
|
} |
563
|
|
|
|
564
|
|
|
/** |
565
|
|
|
* Set Exception classes to be thrown |
566
|
|
|
* |
567
|
|
|
* @param array $exceptions |
568
|
|
|
* @return self |
569
|
|
|
*/ |
570
|
2 |
|
public function andThrowExceptions(array $exceptions) |
571
|
|
|
{ |
572
|
2 |
|
$this->_throw = true; |
573
|
2 |
|
foreach ($exceptions as $exception) { |
574
|
2 |
|
if (!is_object($exception)) { |
575
|
1 |
|
throw new Exception('You must pass an array of exception objects to andThrowExceptions'); |
576
|
|
|
} |
577
|
1 |
|
} |
578
|
1 |
|
return $this->andReturnValues($exceptions); |
579
|
|
|
} |
580
|
|
|
|
581
|
|
|
/** |
582
|
|
|
* Register values to be set to a public property each time this expectation occurs |
583
|
|
|
* |
584
|
|
|
* @param string $name |
585
|
|
|
* @param array $values |
586
|
|
|
* @return self |
587
|
|
|
*/ |
588
|
8 |
|
public function andSet($name, ...$values) |
589
|
|
|
{ |
590
|
8 |
|
$this->_setQueue[$name] = $values; |
591
|
8 |
|
return $this; |
592
|
|
|
} |
593
|
|
|
|
594
|
|
|
/** |
595
|
|
|
* Alias to andSet(). Allows the natural English construct |
596
|
|
|
* - set('foo', 'bar')->andReturn('bar') |
597
|
|
|
* |
598
|
|
|
* @param string $name |
599
|
|
|
* @param mixed $value |
600
|
|
|
* @return self |
601
|
|
|
*/ |
602
|
3 |
|
public function set($name, $value) |
603
|
|
|
{ |
604
|
3 |
|
return call_user_func_array(array($this, 'andSet'), func_get_args()); |
605
|
|
|
} |
606
|
|
|
|
607
|
|
|
/** |
608
|
|
|
* Indicates this expectation should occur zero or more times |
609
|
|
|
* |
610
|
|
|
* @return self |
611
|
|
|
*/ |
612
|
2 |
|
public function zeroOrMoreTimes() |
613
|
|
|
{ |
614
|
2 |
|
$this->atLeast()->never(); |
615
|
2 |
|
} |
616
|
|
|
|
617
|
|
|
/** |
618
|
|
|
* Indicates the number of times this expectation should occur |
619
|
|
|
* |
620
|
|
|
* @param int $limit |
621
|
|
|
* @throws \InvalidArgumentException |
622
|
|
|
* @return self |
623
|
|
|
*/ |
624
|
160 |
|
public function times($limit = null) |
625
|
|
|
{ |
626
|
160 |
|
if (is_null($limit)) { |
627
|
|
|
return $this; |
628
|
|
|
} |
629
|
160 |
|
if (!is_int($limit)) { |
630
|
1 |
|
throw new \InvalidArgumentException('The passed Times limit should be an integer value'); |
631
|
|
|
} |
632
|
159 |
|
$this->_countValidators[] = new $this->_countValidatorClass($this, $limit); |
633
|
159 |
|
$this->_countValidatorClass = 'Mockery\CountValidator\Exact'; |
634
|
159 |
|
return $this; |
635
|
|
|
} |
636
|
|
|
|
637
|
|
|
/** |
638
|
|
|
* Indicates that this expectation is never expected to be called |
639
|
|
|
* |
640
|
|
|
* @return self |
641
|
|
|
*/ |
642
|
38 |
|
public function never() |
643
|
|
|
{ |
644
|
38 |
|
return $this->times(0); |
645
|
|
|
} |
646
|
|
|
|
647
|
|
|
/** |
648
|
|
|
* Indicates that this expectation is expected exactly once |
649
|
|
|
* |
650
|
|
|
* @return self |
651
|
|
|
*/ |
652
|
110 |
|
public function once() |
653
|
|
|
{ |
654
|
110 |
|
return $this->times(1); |
655
|
|
|
} |
656
|
|
|
|
657
|
|
|
/** |
658
|
|
|
* Indicates that this expectation is expected exactly twice |
659
|
|
|
* |
660
|
|
|
* @return self |
661
|
|
|
*/ |
662
|
19 |
|
public function twice() |
663
|
|
|
{ |
664
|
19 |
|
return $this->times(2); |
665
|
|
|
} |
666
|
|
|
|
667
|
|
|
/** |
668
|
|
|
* Sets next count validator to the AtLeast instance |
669
|
|
|
* |
670
|
|
|
* @return self |
671
|
|
|
*/ |
672
|
15 |
|
public function atLeast() |
673
|
|
|
{ |
674
|
15 |
|
$this->_countValidatorClass = 'Mockery\CountValidator\AtLeast'; |
675
|
15 |
|
return $this; |
676
|
|
|
} |
677
|
|
|
|
678
|
|
|
/** |
679
|
|
|
* Sets next count validator to the AtMost instance |
680
|
|
|
* |
681
|
|
|
* @return self |
682
|
|
|
*/ |
683
|
7 |
|
public function atMost() |
684
|
|
|
{ |
685
|
7 |
|
$this->_countValidatorClass = 'Mockery\CountValidator\AtMost'; |
686
|
7 |
|
return $this; |
687
|
|
|
} |
688
|
|
|
|
689
|
|
|
/** |
690
|
|
|
* Shorthand for setting minimum and maximum constraints on call counts |
691
|
|
|
* |
692
|
|
|
* @param int $minimum |
693
|
|
|
* @param int $maximum |
694
|
|
|
*/ |
695
|
|
|
public function between($minimum, $maximum) |
696
|
|
|
{ |
697
|
|
|
return $this->atLeast()->times($minimum)->atMost()->times($maximum); |
698
|
|
|
} |
699
|
|
|
|
700
|
|
|
/** |
701
|
|
|
* Indicates that this expectation must be called in a specific given order |
702
|
|
|
* |
703
|
|
|
* @param string $group Name of the ordered group |
704
|
|
|
* @return self |
705
|
|
|
*/ |
706
|
17 |
|
public function ordered($group = null) |
707
|
|
|
{ |
708
|
17 |
|
if ($this->_globally) { |
709
|
1 |
|
$this->_globalOrderNumber = $this->_defineOrdered($group, $this->_mock->mockery_getContainer()); |
710
|
1 |
|
} else { |
711
|
16 |
|
$this->_orderNumber = $this->_defineOrdered($group, $this->_mock); |
712
|
|
|
} |
713
|
17 |
|
$this->_globally = false; |
714
|
17 |
|
return $this; |
715
|
|
|
} |
716
|
|
|
|
717
|
|
|
/** |
718
|
|
|
* Indicates call order should apply globally |
719
|
|
|
* |
720
|
|
|
* @return self |
721
|
|
|
*/ |
722
|
1 |
|
public function globally() |
723
|
|
|
{ |
724
|
1 |
|
$this->_globally = true; |
725
|
1 |
|
return $this; |
726
|
|
|
} |
727
|
|
|
|
728
|
|
|
/** |
729
|
|
|
* Setup the ordering tracking on the mock or mock container |
730
|
|
|
* |
731
|
|
|
* @param string $group |
732
|
|
|
* @param object $ordering |
733
|
|
|
* @return int |
734
|
|
|
*/ |
735
|
17 |
|
protected function _defineOrdered($group, $ordering) |
736
|
|
|
{ |
737
|
17 |
|
$groups = $ordering->mockery_getGroups(); |
738
|
17 |
|
if (is_null($group)) { |
739
|
16 |
|
$result = $ordering->mockery_allocateOrder(); |
740
|
17 |
|
} elseif (isset($groups[$group])) { |
741
|
2 |
|
$result = $groups[$group]; |
742
|
2 |
|
} else { |
743
|
4 |
|
$result = $ordering->mockery_allocateOrder(); |
744
|
4 |
|
$ordering->mockery_setGroup($group, $result); |
745
|
|
|
} |
746
|
17 |
|
return $result; |
747
|
|
|
} |
748
|
|
|
|
749
|
|
|
/** |
750
|
|
|
* Return order number |
751
|
|
|
* |
752
|
|
|
* @return int |
753
|
|
|
*/ |
754
|
1 |
|
public function getOrderNumber() |
755
|
|
|
{ |
756
|
1 |
|
return $this->_orderNumber; |
757
|
|
|
} |
758
|
|
|
|
759
|
|
|
/** |
760
|
|
|
* Mark this expectation as being a default |
761
|
|
|
* |
762
|
|
|
* @return self |
763
|
|
|
*/ |
764
|
27 |
|
public function byDefault() |
765
|
|
|
{ |
766
|
27 |
|
$director = $this->_mock->mockery_getExpectationsFor($this->_name); |
767
|
27 |
|
if (!empty($director)) { |
768
|
27 |
|
$director->makeExpectationDefault($this); |
769
|
26 |
|
} |
770
|
26 |
|
return $this; |
771
|
|
|
} |
772
|
|
|
|
773
|
|
|
/** |
774
|
|
|
* Return the parent mock of the expectation |
775
|
|
|
* |
776
|
|
|
* @return \Mockery\MockInterface |
777
|
|
|
*/ |
778
|
30 |
|
public function getMock() |
779
|
|
|
{ |
780
|
30 |
|
return $this->_mock; |
781
|
|
|
} |
782
|
|
|
|
783
|
|
|
/** |
784
|
|
|
* Flag this expectation as calling the original class method with the |
785
|
|
|
* any provided arguments instead of using a return value queue. |
786
|
|
|
* |
787
|
|
|
* @return self |
788
|
|
|
*/ |
789
|
3 |
|
public function passthru() |
790
|
|
|
{ |
791
|
3 |
|
if ($this->_mock instanceof Mock) { |
792
|
|
|
throw new Exception( |
793
|
|
|
'Mock Objects not created from a loaded/existing class are ' |
794
|
|
|
. 'incapable of passing method calls through to a parent class' |
795
|
|
|
); |
796
|
|
|
} |
797
|
3 |
|
$this->_passthru = true; |
798
|
3 |
|
return $this; |
799
|
|
|
} |
800
|
|
|
|
801
|
|
|
/** |
802
|
|
|
* Cloning logic |
803
|
|
|
* |
804
|
|
|
*/ |
805
|
10 |
|
public function __clone() |
806
|
|
|
{ |
807
|
10 |
|
$newValidators = array(); |
808
|
10 |
|
$countValidators = $this->_countValidators; |
809
|
10 |
|
foreach ($countValidators as $validator) { |
810
|
7 |
|
$newValidators[] = clone $validator; |
811
|
10 |
|
} |
812
|
10 |
|
$this->_countValidators = $newValidators; |
813
|
10 |
|
} |
814
|
|
|
|
815
|
7 |
|
public function getName() |
816
|
|
|
{ |
817
|
7 |
|
return $this->_name; |
818
|
|
|
} |
819
|
|
|
} |
820
|
|
|
|