Chris@14
|
1 <?php
|
Chris@14
|
2 /*
|
Chris@14
|
3 * This file is part of the phpunit-mock-objects package.
|
Chris@14
|
4 *
|
Chris@14
|
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
|
Chris@14
|
6 *
|
Chris@14
|
7 * For the full copyright and license information, please view the LICENSE
|
Chris@14
|
8 * file that was distributed with this source code.
|
Chris@14
|
9 */
|
Chris@14
|
10 namespace PHPUnit\Framework\MockObject\Matcher;
|
Chris@14
|
11
|
Chris@14
|
12 use PHPUnit\Framework\ExpectationFailedException;
|
Chris@14
|
13 use PHPUnit\Framework\MockObject\Invocation as BaseInvocation;
|
Chris@14
|
14
|
Chris@14
|
15 /**
|
Chris@14
|
16 * Invocation matcher which checks if a method has been invoked a certain amount
|
Chris@14
|
17 * of times.
|
Chris@14
|
18 * If the number of invocations exceeds the value it will immediately throw an
|
Chris@14
|
19 * exception,
|
Chris@14
|
20 * If the number is less it will later be checked in verify() and also throw an
|
Chris@14
|
21 * exception.
|
Chris@14
|
22 */
|
Chris@14
|
23 class InvokedCount extends InvokedRecorder
|
Chris@14
|
24 {
|
Chris@14
|
25 /**
|
Chris@14
|
26 * @var int
|
Chris@14
|
27 */
|
Chris@14
|
28 private $expectedCount;
|
Chris@14
|
29
|
Chris@14
|
30 /**
|
Chris@14
|
31 * @param int $expectedCount
|
Chris@14
|
32 */
|
Chris@14
|
33 public function __construct($expectedCount)
|
Chris@14
|
34 {
|
Chris@14
|
35 $this->expectedCount = $expectedCount;
|
Chris@14
|
36 }
|
Chris@14
|
37
|
Chris@14
|
38 /**
|
Chris@14
|
39 * @return bool
|
Chris@14
|
40 */
|
Chris@14
|
41 public function isNever()
|
Chris@14
|
42 {
|
Chris@14
|
43 return $this->expectedCount === 0;
|
Chris@14
|
44 }
|
Chris@14
|
45
|
Chris@14
|
46 /**
|
Chris@14
|
47 * @return string
|
Chris@14
|
48 */
|
Chris@14
|
49 public function toString()
|
Chris@14
|
50 {
|
Chris@14
|
51 return 'invoked ' . $this->expectedCount . ' time(s)';
|
Chris@14
|
52 }
|
Chris@14
|
53
|
Chris@14
|
54 /**
|
Chris@14
|
55 * @param BaseInvocation $invocation
|
Chris@14
|
56 *
|
Chris@14
|
57 * @throws ExpectationFailedException
|
Chris@14
|
58 */
|
Chris@14
|
59 public function invoked(BaseInvocation $invocation)
|
Chris@14
|
60 {
|
Chris@14
|
61 parent::invoked($invocation);
|
Chris@14
|
62
|
Chris@14
|
63 $count = $this->getInvocationCount();
|
Chris@14
|
64
|
Chris@14
|
65 if ($count > $this->expectedCount) {
|
Chris@14
|
66 $message = $invocation->toString() . ' ';
|
Chris@14
|
67
|
Chris@14
|
68 switch ($this->expectedCount) {
|
Chris@14
|
69 case 0:
|
Chris@14
|
70 $message .= 'was not expected to be called.';
|
Chris@14
|
71
|
Chris@14
|
72 break;
|
Chris@14
|
73
|
Chris@14
|
74 case 1:
|
Chris@14
|
75 $message .= 'was not expected to be called more than once.';
|
Chris@14
|
76
|
Chris@14
|
77 break;
|
Chris@14
|
78
|
Chris@14
|
79 default:
|
Chris@14
|
80 $message .= \sprintf(
|
Chris@14
|
81 'was not expected to be called more than %d times.',
|
Chris@14
|
82 $this->expectedCount
|
Chris@14
|
83 );
|
Chris@14
|
84 }
|
Chris@14
|
85
|
Chris@14
|
86 throw new ExpectationFailedException($message);
|
Chris@14
|
87 }
|
Chris@14
|
88 }
|
Chris@14
|
89
|
Chris@14
|
90 /**
|
Chris@14
|
91 * Verifies that the current expectation is valid. If everything is OK the
|
Chris@14
|
92 * code should just return, if not it must throw an exception.
|
Chris@14
|
93 *
|
Chris@14
|
94 * @throws ExpectationFailedException
|
Chris@14
|
95 */
|
Chris@14
|
96 public function verify()
|
Chris@14
|
97 {
|
Chris@14
|
98 $count = $this->getInvocationCount();
|
Chris@14
|
99
|
Chris@14
|
100 if ($count !== $this->expectedCount) {
|
Chris@14
|
101 throw new ExpectationFailedException(
|
Chris@14
|
102 \sprintf(
|
Chris@14
|
103 'Method was expected to be called %d times, ' .
|
Chris@14
|
104 'actually called %d times.',
|
Chris@14
|
105 $this->expectedCount,
|
Chris@14
|
106 $count
|
Chris@14
|
107 )
|
Chris@14
|
108 );
|
Chris@14
|
109 }
|
Chris@14
|
110 }
|
Chris@14
|
111 }
|