Chris@0
|
1 <?php
|
Chris@0
|
2 /*
|
Chris@0
|
3 * This file is part of the PHPUnit_MockObject package.
|
Chris@0
|
4 *
|
Chris@0
|
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
|
Chris@0
|
6 *
|
Chris@0
|
7 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
8 * file that was distributed with this source code.
|
Chris@0
|
9 */
|
Chris@0
|
10
|
Chris@0
|
11 /**
|
Chris@0
|
12 * Main matcher which defines a full expectation using method, parameter and
|
Chris@0
|
13 * invocation matchers.
|
Chris@0
|
14 * This matcher encapsulates all the other matchers and allows the builder to
|
Chris@0
|
15 * set the specific matchers when the appropriate methods are called (once(),
|
Chris@0
|
16 * where() etc.).
|
Chris@0
|
17 *
|
Chris@0
|
18 * All properties are public so that they can easily be accessed by the builder.
|
Chris@0
|
19 *
|
Chris@0
|
20 * @since Class available since Release 1.0.0
|
Chris@0
|
21 */
|
Chris@0
|
22 class PHPUnit_Framework_MockObject_Matcher implements PHPUnit_Framework_MockObject_Matcher_Invocation
|
Chris@0
|
23 {
|
Chris@0
|
24 /**
|
Chris@0
|
25 * @var PHPUnit_Framework_MockObject_Matcher_Invocation
|
Chris@0
|
26 */
|
Chris@0
|
27 public $invocationMatcher;
|
Chris@0
|
28
|
Chris@0
|
29 /**
|
Chris@0
|
30 * @var mixed
|
Chris@0
|
31 */
|
Chris@0
|
32 public $afterMatchBuilderId = null;
|
Chris@0
|
33
|
Chris@0
|
34 /**
|
Chris@0
|
35 * @var bool
|
Chris@0
|
36 */
|
Chris@0
|
37 public $afterMatchBuilderIsInvoked = false;
|
Chris@0
|
38
|
Chris@0
|
39 /**
|
Chris@0
|
40 * @var PHPUnit_Framework_MockObject_Matcher_MethodName
|
Chris@0
|
41 */
|
Chris@0
|
42 public $methodNameMatcher = null;
|
Chris@0
|
43
|
Chris@0
|
44 /**
|
Chris@0
|
45 * @var PHPUnit_Framework_MockObject_Matcher_Parameters
|
Chris@0
|
46 */
|
Chris@0
|
47 public $parametersMatcher = null;
|
Chris@0
|
48
|
Chris@0
|
49 /**
|
Chris@0
|
50 * @var PHPUnit_Framework_MockObject_Stub
|
Chris@0
|
51 */
|
Chris@0
|
52 public $stub = null;
|
Chris@0
|
53
|
Chris@0
|
54 /**
|
Chris@0
|
55 * @param PHPUnit_Framework_MockObject_Matcher_Invocation $invocationMatcher
|
Chris@0
|
56 */
|
Chris@0
|
57 public function __construct(PHPUnit_Framework_MockObject_Matcher_Invocation $invocationMatcher)
|
Chris@0
|
58 {
|
Chris@0
|
59 $this->invocationMatcher = $invocationMatcher;
|
Chris@0
|
60 }
|
Chris@0
|
61
|
Chris@0
|
62 /**
|
Chris@0
|
63 * @return string
|
Chris@0
|
64 */
|
Chris@0
|
65 public function toString()
|
Chris@0
|
66 {
|
Chris@0
|
67 $list = array();
|
Chris@0
|
68
|
Chris@0
|
69 if ($this->invocationMatcher !== null) {
|
Chris@0
|
70 $list[] = $this->invocationMatcher->toString();
|
Chris@0
|
71 }
|
Chris@0
|
72
|
Chris@0
|
73 if ($this->methodNameMatcher !== null) {
|
Chris@0
|
74 $list[] = 'where ' . $this->methodNameMatcher->toString();
|
Chris@0
|
75 }
|
Chris@0
|
76
|
Chris@0
|
77 if ($this->parametersMatcher !== null) {
|
Chris@0
|
78 $list[] = 'and ' . $this->parametersMatcher->toString();
|
Chris@0
|
79 }
|
Chris@0
|
80
|
Chris@0
|
81 if ($this->afterMatchBuilderId !== null) {
|
Chris@0
|
82 $list[] = 'after ' . $this->afterMatchBuilderId;
|
Chris@0
|
83 }
|
Chris@0
|
84
|
Chris@0
|
85 if ($this->stub !== null) {
|
Chris@0
|
86 $list[] = 'will ' . $this->stub->toString();
|
Chris@0
|
87 }
|
Chris@0
|
88
|
Chris@0
|
89 return implode(' ', $list);
|
Chris@0
|
90 }
|
Chris@0
|
91
|
Chris@0
|
92 /**
|
Chris@0
|
93 * @param PHPUnit_Framework_MockObject_Invocation $invocation
|
Chris@0
|
94 * @return mixed
|
Chris@0
|
95 */
|
Chris@0
|
96 public function invoked(PHPUnit_Framework_MockObject_Invocation $invocation)
|
Chris@0
|
97 {
|
Chris@0
|
98 if ($this->invocationMatcher === null) {
|
Chris@0
|
99 throw new PHPUnit_Framework_Exception(
|
Chris@0
|
100 'No invocation matcher is set'
|
Chris@0
|
101 );
|
Chris@0
|
102 }
|
Chris@0
|
103
|
Chris@0
|
104 if ($this->methodNameMatcher === null) {
|
Chris@0
|
105 throw new PHPUnit_Framework_Exception('No method matcher is set');
|
Chris@0
|
106 }
|
Chris@0
|
107
|
Chris@0
|
108 if ($this->afterMatchBuilderId !== null) {
|
Chris@0
|
109 $builder = $invocation->object
|
Chris@0
|
110 ->__phpunit_getInvocationMocker()
|
Chris@0
|
111 ->lookupId($this->afterMatchBuilderId);
|
Chris@0
|
112
|
Chris@0
|
113 if (!$builder) {
|
Chris@0
|
114 throw new PHPUnit_Framework_Exception(
|
Chris@0
|
115 sprintf(
|
Chris@0
|
116 'No builder found for match builder identification <%s>',
|
Chris@0
|
117 $this->afterMatchBuilderId
|
Chris@0
|
118 )
|
Chris@0
|
119 );
|
Chris@0
|
120 }
|
Chris@0
|
121
|
Chris@0
|
122 $matcher = $builder->getMatcher();
|
Chris@0
|
123
|
Chris@0
|
124 if ($matcher && $matcher->invocationMatcher->hasBeenInvoked()) {
|
Chris@0
|
125 $this->afterMatchBuilderIsInvoked = true;
|
Chris@0
|
126 }
|
Chris@0
|
127 }
|
Chris@0
|
128
|
Chris@0
|
129 $this->invocationMatcher->invoked($invocation);
|
Chris@0
|
130
|
Chris@0
|
131 try {
|
Chris@0
|
132 if ($this->parametersMatcher !== null &&
|
Chris@0
|
133 !$this->parametersMatcher->matches($invocation)) {
|
Chris@0
|
134 $this->parametersMatcher->verify();
|
Chris@0
|
135 }
|
Chris@0
|
136 } catch (PHPUnit_Framework_ExpectationFailedException $e) {
|
Chris@0
|
137 throw new PHPUnit_Framework_ExpectationFailedException(
|
Chris@0
|
138 sprintf(
|
Chris@0
|
139 "Expectation failed for %s when %s\n%s",
|
Chris@0
|
140 $this->methodNameMatcher->toString(),
|
Chris@0
|
141 $this->invocationMatcher->toString(),
|
Chris@0
|
142 $e->getMessage()
|
Chris@0
|
143 ),
|
Chris@0
|
144 $e->getComparisonFailure()
|
Chris@0
|
145 );
|
Chris@0
|
146 }
|
Chris@0
|
147
|
Chris@0
|
148 if ($this->stub) {
|
Chris@0
|
149 return $this->stub->invoke($invocation);
|
Chris@0
|
150 }
|
Chris@0
|
151
|
Chris@0
|
152 return;
|
Chris@0
|
153 }
|
Chris@0
|
154
|
Chris@0
|
155 /**
|
Chris@0
|
156 * @param PHPUnit_Framework_MockObject_Invocation $invocation
|
Chris@0
|
157 * @return bool
|
Chris@0
|
158 */
|
Chris@0
|
159 public function matches(PHPUnit_Framework_MockObject_Invocation $invocation)
|
Chris@0
|
160 {
|
Chris@0
|
161 if ($this->afterMatchBuilderId !== null) {
|
Chris@0
|
162 $builder = $invocation->object
|
Chris@0
|
163 ->__phpunit_getInvocationMocker()
|
Chris@0
|
164 ->lookupId($this->afterMatchBuilderId);
|
Chris@0
|
165
|
Chris@0
|
166 if (!$builder) {
|
Chris@0
|
167 throw new PHPUnit_Framework_Exception(
|
Chris@0
|
168 sprintf(
|
Chris@0
|
169 'No builder found for match builder identification <%s>',
|
Chris@0
|
170 $this->afterMatchBuilderId
|
Chris@0
|
171 )
|
Chris@0
|
172 );
|
Chris@0
|
173 }
|
Chris@0
|
174
|
Chris@0
|
175 $matcher = $builder->getMatcher();
|
Chris@0
|
176
|
Chris@0
|
177 if (!$matcher) {
|
Chris@0
|
178 return false;
|
Chris@0
|
179 }
|
Chris@0
|
180
|
Chris@0
|
181 if (!$matcher->invocationMatcher->hasBeenInvoked()) {
|
Chris@0
|
182 return false;
|
Chris@0
|
183 }
|
Chris@0
|
184 }
|
Chris@0
|
185
|
Chris@0
|
186 if ($this->invocationMatcher === null) {
|
Chris@0
|
187 throw new PHPUnit_Framework_Exception(
|
Chris@0
|
188 'No invocation matcher is set'
|
Chris@0
|
189 );
|
Chris@0
|
190 }
|
Chris@0
|
191
|
Chris@0
|
192 if ($this->methodNameMatcher === null) {
|
Chris@0
|
193 throw new PHPUnit_Framework_Exception('No method matcher is set');
|
Chris@0
|
194 }
|
Chris@0
|
195
|
Chris@0
|
196 if (!$this->invocationMatcher->matches($invocation)) {
|
Chris@0
|
197 return false;
|
Chris@0
|
198 }
|
Chris@0
|
199
|
Chris@0
|
200 try {
|
Chris@0
|
201 if (!$this->methodNameMatcher->matches($invocation)) {
|
Chris@0
|
202 return false;
|
Chris@0
|
203 }
|
Chris@0
|
204 } catch (PHPUnit_Framework_ExpectationFailedException $e) {
|
Chris@0
|
205 throw new PHPUnit_Framework_ExpectationFailedException(
|
Chris@0
|
206 sprintf(
|
Chris@0
|
207 "Expectation failed for %s when %s\n%s",
|
Chris@0
|
208 $this->methodNameMatcher->toString(),
|
Chris@0
|
209 $this->invocationMatcher->toString(),
|
Chris@0
|
210 $e->getMessage()
|
Chris@0
|
211 ),
|
Chris@0
|
212 $e->getComparisonFailure()
|
Chris@0
|
213 );
|
Chris@0
|
214 }
|
Chris@0
|
215
|
Chris@0
|
216 return true;
|
Chris@0
|
217 }
|
Chris@0
|
218
|
Chris@0
|
219 /**
|
Chris@0
|
220 * @throws PHPUnit_Framework_Exception
|
Chris@0
|
221 * @throws PHPUnit_Framework_ExpectationFailedException
|
Chris@0
|
222 */
|
Chris@0
|
223 public function verify()
|
Chris@0
|
224 {
|
Chris@0
|
225 if ($this->invocationMatcher === null) {
|
Chris@0
|
226 throw new PHPUnit_Framework_Exception(
|
Chris@0
|
227 'No invocation matcher is set'
|
Chris@0
|
228 );
|
Chris@0
|
229 }
|
Chris@0
|
230
|
Chris@0
|
231 if ($this->methodNameMatcher === null) {
|
Chris@0
|
232 throw new PHPUnit_Framework_Exception('No method matcher is set');
|
Chris@0
|
233 }
|
Chris@0
|
234
|
Chris@0
|
235 try {
|
Chris@0
|
236 $this->invocationMatcher->verify();
|
Chris@0
|
237
|
Chris@0
|
238 if ($this->parametersMatcher === null) {
|
Chris@0
|
239 $this->parametersMatcher = new PHPUnit_Framework_MockObject_Matcher_AnyParameters;
|
Chris@0
|
240 }
|
Chris@0
|
241
|
Chris@0
|
242 $invocationIsAny = get_class($this->invocationMatcher) === 'PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount';
|
Chris@0
|
243 $invocationIsNever = get_class($this->invocationMatcher) === 'PHPUnit_Framework_MockObject_Matcher_InvokedCount' && $this->invocationMatcher->isNever();
|
Chris@0
|
244 if (!$invocationIsAny && !$invocationIsNever) {
|
Chris@0
|
245 $this->parametersMatcher->verify();
|
Chris@0
|
246 }
|
Chris@0
|
247 } catch (PHPUnit_Framework_ExpectationFailedException $e) {
|
Chris@0
|
248 throw new PHPUnit_Framework_ExpectationFailedException(
|
Chris@0
|
249 sprintf(
|
Chris@0
|
250 "Expectation failed for %s when %s.\n%s",
|
Chris@0
|
251 $this->methodNameMatcher->toString(),
|
Chris@0
|
252 $this->invocationMatcher->toString(),
|
Chris@0
|
253 PHPUnit_Framework_TestFailure::exceptionToString($e)
|
Chris@0
|
254 )
|
Chris@0
|
255 );
|
Chris@0
|
256 }
|
Chris@0
|
257 }
|
Chris@0
|
258
|
Chris@0
|
259 /**
|
Chris@0
|
260 * @since Method available since Release 1.2.4
|
Chris@0
|
261 */
|
Chris@0
|
262 public function hasMatchers()
|
Chris@0
|
263 {
|
Chris@0
|
264 if ($this->invocationMatcher !== null &&
|
Chris@0
|
265 !$this->invocationMatcher instanceof PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount) {
|
Chris@0
|
266 return true;
|
Chris@0
|
267 }
|
Chris@0
|
268
|
Chris@0
|
269 return false;
|
Chris@0
|
270 }
|
Chris@0
|
271 }
|