forked from simpletest/simpletest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmock_objects.php
More file actions
1741 lines (1588 loc) · 56.9 KB
/
mock_objects.php
File metadata and controls
1741 lines (1588 loc) · 56.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
require_once dirname(__FILE__) . '/expectation.php';
require_once dirname(__FILE__) . '/simpletest.php';
require_once dirname(__FILE__) . '/dumper.php';
require_once dirname(__FILE__) . '/reflection.php';
/*
* Default character simpletest will substitute for any value
*/
if (! defined('MOCK_ANYTHING')) {
define('MOCK_ANYTHING', '*');
}
/**
* Parameter comparison assertion.
*/
class ParametersExpectation extends SimpleExpectation
{
private $expected;
/**
* Sets the expected parameter list.
*
* @param array $parameters Array of parameters including those that are wildcarded. If the
* value is not an array then it is considered to match any.
* @param string $message Customised message on failure.
*/
public function __construct($expected = false, $message = '%s')
{
parent::__construct($message);
$this->expected = $expected;
}
/**
* Tests the assertion. True if correct.
*
* @param array $parameters Comparison values.
*
* @return bool True if correct.
*/
public function test($parameters)
{
if (! is_array($this->expected)) {
return true;
}
if (count($this->expected) != count($parameters)) {
return false;
}
for ($i = 0; $i < count($this->expected); $i++) {
if (! $this->testParameter($parameters[$i], $this->expected[$i])) {
return false;
}
}
return true;
}
/**
* Tests an individual parameter.
*
* @param mixed $parameter Value to test.
* @param mixed $expected Comparison value.
*
* @return bool True if expectation fulfilled.
*/
protected function testParameter($parameter, $expected)
{
$comparison = $this->forceToExpectation($expected);
return $comparison->test($parameter);
}
/**
* Returns a human readable test message.
*
* @param array $comparison Incoming parameter list.
*
* @return string Description of success or failure.
*/
public function testMessage($parameters)
{
if ($this->test($parameters)) {
return sprintf(
'Expectation of %s arguments of [%s] is correct',
count($this->expected),
$this->renderArguments($this->expected)
);
} else {
return $this->describeDifference($this->expected, $parameters);
}
}
/**
* Message to display if expectation differs from the parameters actually received.
*
* @param array $expected Expected parameters as list.
* @param array $parameters Actual parameters received.
*
* @return string Description of difference.
*/
protected function describeDifference($expected, $parameters)
{
if (count($expected) != count($parameters)) {
return sprintf(
'Expected %s arguments of [%s], but got %s arguments of [%s]',
count($expected),
$this->renderArguments($expected),
count($parameters),
$this->renderArguments($parameters)
);
}
$messages = array();
for ($i = 0; $i < count($expected); $i++) {
$comparison = $this->forceToExpectation($expected[$i]);
if (! $comparison->test($parameters[$i])) {
$messages[] = 'parameter ' . ($i + 1) . ' with [' .
$comparison->overlayMessage($parameters[$i], $this->getDumper()) . ']';
}
}
return 'Parameter expectation differs at ' . implode(' and ', $messages);
}
/**
* Creates an identical expectation if the object/value is not already some type of expectation.
*
* @param mixed $expected Expected value.
*
* @return SimpleExpectation Expectation object.
*/
protected function forceToExpectation($expected)
{
if (SimpleExpectation::isExpectation($expected)) {
return $expected;
}
return new IdenticalExpectation($expected);
}
/**
* Renders the argument list as a string for messages.
*
* @param array $args Incoming arguments.
*
* @return string Simple description of type and value.
*/
protected function renderArguments($args)
{
$descriptions = array();
if (is_array($args)) {
foreach ($args as $arg) {
$dumper = new SimpleDumper();
$descriptions[] = $dumper->describeValue($arg);
}
}
return implode(', ', $descriptions);
}
}
/**
* Confirms that the number of calls on a method is as expected.
*/
class CallCountExpectation extends SimpleExpectation
{
private $method;
private $count;
/**
* Stashes the method and expected count for later reporting.
*
* @param string $method Name of method to confirm against.
* @param int $count Expected number of calls.
* @param string $message Custom error message.
*/
public function __construct($method, $count, $message = '%s')
{
$this->method = $method;
$this->count = $count;
parent::__construct($message);
}
/**
* Tests the assertion. True if correct.
*
* @param int $compare Measured call count.
*
* @return bool True if expected.
*/
public function test($compare)
{
return ($this->count == $compare);
}
/**
* Reports the comparison.
*
* @param int $compare Measured call count.
*
* @return string Message to show.
*/
public function testMessage($compare)
{
return sprintf(
'Expected call count for [%s] was [%s] got [%s]', $this->method, $this->count, $compare
);
}
}
/**
* Confirms that the number of calls on a method is as expected.
*/
class MinimumCallCountExpectation extends SimpleExpectation
{
private $method;
private $count;
/**
* Stashes the method and expected count for later reporting.
*
* @param string $method Name of method to confirm against.
* @param int $count Minimum number of calls.
* @param string $message Custom error message.
*/
public function __construct($method, $count, $message = '%s')
{
$this->method = $method;
$this->count = $count;
parent::__construct($message);
}
/**
* Tests the assertion. True if correct.
*
* @param int $compare Measured call count.
*
* @return bool True if enough.
*/
public function test($compare)
{
return ($this->count <= $compare);
}
/**
* Reports the comparison.
*
* @param int $compare Measured call count.
*
* @return string Message to show.
*/
public function testMessage($compare)
{
return sprintf(
'Minimum call count for [%s] was [%s] got [%s]', $this->method, $this->count, $compare
);
}
}
/**
* Confirms that the number of calls on a method is as expected.
*/
class MaximumCallCountExpectation extends SimpleExpectation
{
private $method;
private $count;
/**
* Stashes the method and expected count for later reporting.
*
* @param string $method Name of method to confirm against.
* @param int $count Minimum number of calls.
* @param string $message Custom error message.
*/
public function __construct($method, $count, $message = '%s')
{
$this->method = $method;
$this->count = $count;
parent::__construct($message);
}
/**
* Tests the assertion. True if correct.
*
* @param int $compare Measured call count.
*
* @return bool True if not over.
*/
public function test($compare)
{
return ($this->count >= $compare);
}
/**
* Reports the comparison.
*
* @param int $compare Measured call count.
*
* @return string Message to show.
*/
public function testMessage($compare)
{
return sprintf(
'Maximum call count for [%s] was [%s] got [%s]', $this->method, $this->count, $compare
);
}
}
/**
* Retrieves method actions by searching the parameter lists until an expected match is found.
*/
class SimpleSignatureMap
{
private $map;
/**
* Creates an empty call map.
*/
public function __construct()
{
$this->map = array();
}
/**
* Stashes a reference against a method call.
*
* @param array $parameters Array of arguments (including wildcards).
* @param mixed $action Reference placed in the map.
*/
public function add($parameters, $action)
{
$place = count($this->map);
$this->map[$place] = array();
$this->map[$place]['params'] = new ParametersExpectation($parameters);
$this->map[$place]['content'] = $action;
}
/**
* Searches the call list for a matching parameter set. Returned by reference.
*
* @param array $parameters Parameters to search by without wildcards.
*
* @return object Object held in the first matching slot, otherwise null.
*/
public function findFirstAction($parameters)
{
$slot = $this->findFirstSlot($parameters);
if (isset($slot) && isset($slot['content'])) {
return $slot['content'];
}
return;
}
/**
* Searches the call list for a matching parameter set. True if successful.
*
* @param array $parameters Parameters to search by without wildcards.
*
* @return bool True if a match is present.
*/
public function isMatch($parameters)
{
return ($this->findFirstSlot($parameters) != null);
}
/**
* Compares the incoming parameters with the internal expectation.
* Uses the incoming $test to dispatch the test message.
*
* @param SimpleTestCase $test Test to dispatch to.
* @param array $parameters The actual calling arguments.
* @param string $message The message to overlay.
*/
public function test($test, $parameters, $message)
{
}
/**
* Searches the map for a matching item.
*
* @param array $parameters Parameters to search by without wildcards.
*
* @return array Reference to slot or null.
*/
public function findFirstSlot($parameters)
{
$count = count($this->map);
for ($i = 0; $i < $count; $i++) {
if ($this->map[$i]['params']->test($parameters)) {
return $this->map[$i];
}
}
return;
}
}
/**
* Allows setting of actions against call signatures
* either at a specific time, or always.
* Specific time settings trump lasting ones,
* otherwise the most recently added will mask an earlier match.
*/
class SimpleCallSchedule
{
private $wildcard = MOCK_ANYTHING;
private $always;
private $at;
/**
* Sets up an empty response schedule. Creates an empty call map.
*/
public function __construct()
{
$this->always = array();
$this->at = array();
}
/**
* Stores an action against a signature
* that will always fire unless masked by a time specific one.
*
* @param string $method Method name.
* @param array $args Calling parameters.
* @param SimpleAction $action Actually simpleByValue, etc.
*/
public function register($method, $args, $action)
{
$args = $this->replaceWildcards($args);
$method = strtolower($method);
if (! isset($this->always[$method])) {
$this->always[$method] = new SimpleSignatureMap();
}
$this->always[$method]->add($args, $action);
}
/**
* Stores an action against a signature that will fire at a specific time in the future.
*
* @param int $step delay of calls to this method, 0 is next.
* @param string $method Method name.
* @param array $args Calling parameters.
* @param SimpleAction $action Actually SimpleByValue, etc.
*/
public function registerAt($step, $method, $args, $action)
{
$args = $this->replaceWildcards($args);
$method = strtolower($method);
if (! isset($this->at[$method])) {
$this->at[$method] = array();
}
if (! isset($this->at[$method][$step])) {
$this->at[$method][$step] = new SimpleSignatureMap();
}
$this->at[$method][$step]->add($args, $action);
}
/**
* Sets up an expectation on the argument list.
*
* @param string $method Method to test.
* @param array $args Bare arguments or list of expectation objects.
* @param string $message Failure message.
*/
public function expectArguments($method, $args, $message)
{
$args = $this->replaceWildcards($args);
$message .= Mock::getExpectationLine();
$this->expected_args[strtolower($method)] =
new ParametersExpectation($args, $message);
}
/**
* Actually carry out the action stored previously, if the parameters match.
*
* @param int $step Time of call.
* @param string $method Method name.
* @param array $args The parameters making up the rest of the call.
*
* @return mixed The result of the action.
*/
public function respond($step, $method, $args)
{
$method = strtolower($method);
if (isset($this->at[$method][$step])) {
if ($this->at[$method][$step]->isMatch($args)) {
$action = $this->at[$method][$step]->findFirstAction($args);
if (isset($action)) {
return $action->act();
}
}
}
if (isset($this->always[$method])) {
$action = $this->always[$method]->findFirstAction($args);
if (isset($action)) {
return $action->act();
}
}
return;
}
/**
* Replaces wildcard matches with wildcard expectations in the argument list.
*
* @param array $args Raw argument list.
*
* @return array Argument list with expectations.
*/
protected function replaceWildcards($args)
{
if ($args === false) {
return false;
}
for ($i = 0; $i < count($args); $i++) {
if ($args[$i] === $this->wildcard) {
$args[$i] = new AnythingExpectation();
}
}
return $args;
}
}
/**
* A type of SimpleMethodAction.
* Stashes a value for returning later.
* Follows usual PHP5 semantics of objects being returned by reference.
*/
class SimpleReturn
{
private $value;
/**
* Stashes it for later.
*
* @param mixed $value You need to clone objects if you want copy semantics for these.
*/
public function __construct($value)
{
$this->value = $value;
}
/**
* Returns the value stored earlier.
*
* @return mixed Whatever was stashed.
*/
public function act()
{
return $this->value;
}
}
/**
* A type of SimpleMethodAction.
* Stashes a reference for returning later.
*/
class SimpleByReference
{
private $reference;
/**
* Stashes it for later.
*
* @param mixed $reference Actual PHP4 style reference.
*/
public function __construct(&$reference)
{
$this->reference = &$reference;
}
/**
* Returns the reference stored earlier.
*
* @return mixed Whatever was stashed.
*/
public function &act()
{
return $this->reference;
}
}
/**
* A type of SimpleMethodAction.
* Stashes a value for returning later.
*/
class SimpleByValue
{
private $value;
/**
* Stashes it for later.
*
* @param mixed $value You need to clone objects if you want copy semantics for these.
*/
public function __construct($value)
{
$this->value = $value;
}
/**
* Returns the value stored earlier.
*
* @return mixed Whatever was stashed.
*/
public function act()
{
return $this->value;
}
}
/**
* A type of SimpleMethodAction. Stashes an exception for throwing later.
*/
class SimpleThrower
{
private $exception;
/**
* Stashes it for later.
*
* @param Exception $exception The exception object to throw.
*/
public function __construct($exception)
{
$this->exception = $exception;
}
/**
* Throws the exceptions stashed earlier.
*/
public function act()
{
throw $this->exception;
}
}
/**
* A type of SimpleMethodAction.
* Stashes an error for emitting later.
*/
class SimpleErrorThrower
{
private $error;
private $severity;
/**
* Stashes an error to throw later.
*
* @param string $error Error message.
* @param int $severity PHP error constant, e.g E_USER_ERROR.
*/
public function __construct($error, $severity)
{
$this->error = $error;
$this->severity = $severity;
}
/**
* Triggers the stashed error.
*/
public function act()
{
trigger_error($this->error, $this->severity);
return;
}
}
/**
* A base class or delegate that extends an empty collection of methods
* that can have their return values set and expectations made of the calls upon them.
*
* The mock will assert the expectations against it's attached test case
* in addition to the server stub behaviour or returning preprogrammed responses.
*/
class SimpleMock
{
private $actions;
private $expectations;
private $wildcard = MOCK_ANYTHING;
private $is_strict = true;
private $call_counts;
private $expected_counts;
private $max_counts;
private $expected_args;
private $expected_args_at;
/**
* Creates an empty action list and expectation list.
* All call counts are set to zero.
*/
public function __construct()
{
$this->actions = new SimpleCallSchedule();
$this->expectations = new SimpleCallSchedule();
$this->call_counts = array();
$this->expected_counts = array();
$this->max_counts = array();
$this->expected_args = array();
$this->expected_args_at = array();
$current_test_case = $this->getCurrentTestCase();
if ($current_test_case) {
$current_test_case->tell($this);
}
}
/**
* Disables a name check when setting expectations.
* This hack is needed for the partial mocks.
*/
public function disableExpectationNameChecks()
{
$this->is_strict = false;
}
/**
* Finds currently running test.
*
* @return SimpeTestCase Current test case.
*/
protected function getCurrentTestCase()
{
return SimpleTest::getContext()->getTest();
}
/**
* Die if bad arguments array is passed.
*
* @param mixed $args The arguments value to be checked.
* @param string $task Description of task attempt.
*
* @return bool Valid arguments
*/
protected function checkArgumentsIsArray($args, $task)
{
if (! is_array($args)) {
$errormsg = sprintf('Cannot %s. Parameter %s is not an array.', $task, $args);
trigger_error($errormsg, E_USER_ERROR);
}
}
/**
* Triggers a PHP error if the method is not part of this object.
*
* @param string $method Name of method.
* @param string $task Description of task attempt.
*/
protected function dieOnNoMethod($method, $task)
{
if ($this->is_strict && ! method_exists($this, $method)) {
$errormsg = sprintf('Cannot %s. Method %s() not in class %s.', $task, $method, get_class($this));
trigger_error($errormsg, E_USER_ERROR);
}
}
/**
* Replaces wildcard matches with wildcard expectations in the argument list.
*
* @param array $args Raw argument list.
*
* @return array Argument list with expectations.
*/
public function replaceWildcards($args)
{
if ($args === false) {
return false;
}
for ($i = 0; $i < count($args); $i++) {
if ($args[$i] === $this->wildcard) {
$args[$i] = new AnythingExpectation();
}
}
return $args;
}
/**
* Adds one to the call count of a method.
*
* @param string $method Method called.
* @param array $args Arguments as an array.
*/
protected function addCall($method, $args)
{
if (! isset($this->call_counts[$method])) {
$this->call_counts[$method] = 0;
}
$this->call_counts[$method]++;
}
/**
* Fetches the call count of a method so far.
*
* @param string $method Method name called.
*
* @return int Number of calls so far.
*/
public function getCallCount($method)
{
$this->dieOnNoMethod($method, 'get call count');
$method = strtolower($method);
if (! isset($this->call_counts[$method])) {
return 0;
}
return $this->call_counts[$method];
}
/**
* Sets a return for a parameter list that
* will be passed on by all calls to this method that match.
*
* @param string $method Method name.
* @param mixed $value Result of call by value/handle.
* @param array $args List of parameters to match including wildcards.
*/
public function returns($method, $value, $args = false)
{
$this->dieOnNoMethod($method, 'set return');
$this->actions->register($method, $args, new SimpleReturn($value));
}
/**
* Sets a return for a parameter list that
* will be passed only when the required call count is reached.
*
* @param int $timing Number of calls in the future to which the result applies.
* If not set then all calls will return the value.
* @param string $method Method name.
* @param mixed $value Result of call passed.
* @param array $args List of parameters to match including wildcards.
*/
public function returnsAt($timing, $method, $value, $args = false)
{
$this->dieOnNoMethod($method, 'set return value sequence');
$this->actions->registerAt($timing, $method, $args, new SimpleReturn($value));
}
/**
* Sets a return for a parameter list that
* will be passed by value for all calls to this method.
*
* @param string $method Method name.
* @param mixed $value Result of call passed by value.
* @param array $args List of parameters to match including wildcards.
*/
public function returnsByValue($method, $value, $args = false)
{
$this->dieOnNoMethod($method, 'set return value');
$this->actions->register($method, $args, new SimpleByValue($value));
}
/**
* Sets a return for a parameter list that
* will be passed by value only when the required call count is reached.
*
* @param int $timing Number of calls in the future to which the result applies.
* If not set then all calls will return the value.
* @param string $method Method name.
* @param mixed $value Result of call passed by value.
* @param array $args List of parameters to match including wildcards.
*/
public function returnsByValueAt($timing, $method, $value, $args = false)
{
$this->dieOnNoMethod($method, 'set return value sequence');
$this->actions->registerAt($timing, $method, $args, new SimpleByValue($value));
}
/**
* Sets a return for a parameter list that will be passed by reference for all calls.
*
* @param string $method Method name.
* @param mixed $reference Result of the call will be this object.
* @param array $args List of parameters to match including wildcards.
*/
public function returnsByReference($method, &$reference, $args = false)
{
$this->dieOnNoMethod($method, 'set return reference');
$this->actions->register($method, $args, new SimpleByReference($reference));
}
/**
* Sets a return for a parameter list that
* will be passed by value only when the required call count is reached.
*
* @param int $timing Number of calls in the future to which the result applies.
* If not set then all calls will return the value.
* @param string $method Method name.
* @param mixed $reference Result of the call will be this object.
* @param array $args List of parameters to match including wildcards.
*/
public function returnsByReferenceAt($timing, $method, &$reference, $args = false)
{
$this->dieOnNoMethod($method, 'set return reference sequence');
$this->actions->registerAt($timing, $method, $args, new SimpleByReference($reference));
}
/**
* Sets up an expected call with a set of expected parameters in that call.
* All calls will be compared to these expectations regardless of when the call is made.
*
* @param string $method Method call to test.
* @param array $args Expected parameters for the call including wildcards.
* @param string $message Overridden message.
*/
public function expect($method, $args, $message = '%s')
{
$this->dieOnNoMethod($method, 'set expected arguments');
$this->checkArgumentsIsArray($args, 'set expected arguments');
$this->expectations->expectArguments($method, $args, $message);
$args = $this->replaceWildcards($args);
$message .= Mock::getExpectationLine();
$this->expected_args[strtolower($method)] =
new ParametersExpectation($args, $message);
}
/**
* Sets up an expected call with a set of expected parameters in that call.
* The expected call count will be adjusted if it is set too low to reach this call.
*
* @param int $timing Number of calls in the future at which to test. Next call is 0.
* @param string $method Method call to test.
* @param array $args Expected parameters for the call including wildcards.
* @param string $message Overridden message.
*/
public function expectAt($timing, $method, $args, $message = '%s')
{
$this->dieOnNoMethod($method, 'set expected arguments at time');
$this->checkArgumentsIsArray($args, 'set expected arguments at time');
$args = $this->replaceWildcards($args);
if (! isset($this->expected_args_at[$timing])) {
$this->expected_args_at[$timing] = array();
}
$method = strtolower($method);
$message .= Mock::getExpectationLine();
$this->expected_args_at[$timing][$method] =
new ParametersExpectation($args, $message);
}
/**
* Sets an expectation for the number of times a method will be called.
* The tally method is used to check this.
*
* @param string $method Method call to test.
* @param int $count Number of times it should have been called at tally.
* @param string $message Overridden message.
*/
public function expectCallCount($method, $count, $message = '%s')
{
$this->dieOnNoMethod($method, 'set expected call count');
$message .= Mock::getExpectationLine();
$this->expected_counts[strtolower($method)] =
new CallCountExpectation($method, $count, $message);
}
/**
* Sets the number of times a method may be called before a test failure is triggered.
*
* @param string $method Method call to test.
* @param int $count Most number of times it should have been called.
* @param string $message Overridden message.
*/
public function expectMaximumCallCount($method, $count, $message = '%s')
{
$this->dieOnNoMethod($method, 'set maximum call count');
$message .= Mock::getExpectationLine();
$this->max_counts[strtolower($method)] =
new MaximumCallCountExpectation($method, $count, $message);
}
/**
* Sets the number of times to call a method to prevent a failure on the tally.
*
* @param string $method Method call to test.
* @param int $count Least number of times it should have been called.
* @param string $message Overridden message.
*/
public function expectMinimumCallCount($method, $count, $message = '%s')
{
$this->dieOnNoMethod($method, 'set minimum call count');
$message .= Mock::getExpectationLine();
$this->expected_counts[strtolower($method)] =
new MinimumCallCountExpectation($method, $count, $message);
}
/**
* Convenience method for barring a method call.
*
* @param string $method Method call to ban.
* @param string $message Overridden message.
*/
public function expectNever($method, $message = '%s')
{
$this->expectMaximumCallCount($method, 0, $message);