comparison vendor/phpunit/phpunit-mock-objects/src/Matcher.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents
children
comparison
equal deleted inserted replaced
13:5fb285c0d0e3 14:1fec387a4317
1 <?php
2 /*
3 * This file is part of the phpunit-mock-objects package.
4 *
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10 namespace PHPUnit\Framework\MockObject;
11
12 use PHPUnit\Framework\ExpectationFailedException;
13 use PHPUnit\Framework\MockObject\Matcher\AnyInvokedCount;
14 use PHPUnit\Framework\MockObject\Matcher\AnyParameters;
15 use PHPUnit\Framework\MockObject\Matcher\Invocation as MatcherInvocation;
16 use PHPUnit\Framework\MockObject\Matcher\InvokedCount;
17 use PHPUnit\Framework\MockObject\Matcher\MethodName;
18 use PHPUnit\Framework\MockObject\Matcher\Parameters;
19 use PHPUnit\Framework\TestFailure;
20
21 /**
22 * Main matcher which defines a full expectation using method, parameter and
23 * invocation matchers.
24 * This matcher encapsulates all the other matchers and allows the builder to
25 * set the specific matchers when the appropriate methods are called (once(),
26 * where() etc.).
27 *
28 * All properties are public so that they can easily be accessed by the builder.
29 */
30 class Matcher implements MatcherInvocation
31 {
32 /**
33 * @var MatcherInvocation
34 */
35 private $invocationMatcher;
36
37 /**
38 * @var mixed
39 */
40 private $afterMatchBuilderId = null;
41
42 /**
43 * @var bool
44 */
45 private $afterMatchBuilderIsInvoked = false;
46
47 /**
48 * @var MethodName
49 */
50 private $methodNameMatcher = null;
51
52 /**
53 * @var Parameters
54 */
55 private $parametersMatcher = null;
56
57 /**
58 * @var Stub
59 */
60 private $stub = null;
61
62 /**
63 * @param MatcherInvocation $invocationMatcher
64 */
65 public function __construct(MatcherInvocation $invocationMatcher)
66 {
67 $this->invocationMatcher = $invocationMatcher;
68 }
69
70 public function hasMatchers(): bool
71 {
72 return $this->invocationMatcher !== null && !$this->invocationMatcher instanceof AnyInvokedCount;
73 }
74
75 public function hasMethodNameMatcher(): bool
76 {
77 return $this->methodNameMatcher !== null;
78 }
79
80 public function getMethodNameMatcher(): MethodName
81 {
82 return $this->methodNameMatcher;
83 }
84
85 public function setMethodNameMatcher(MethodName $matcher)
86 {
87 $this->methodNameMatcher = $matcher;
88 }
89
90 public function hasParametersMatcher(): bool
91 {
92 return $this->parametersMatcher !== null;
93 }
94
95 public function getParametersMatcher(): Parameters
96 {
97 return $this->parametersMatcher;
98 }
99
100 public function setParametersMatcher($matcher)
101 {
102 $this->parametersMatcher = $matcher;
103 }
104
105 public function setStub($stub)
106 {
107 $this->stub = $stub;
108 }
109
110 public function setAfterMatchBuilderId($id)
111 {
112 $this->afterMatchBuilderId = $id;
113 }
114
115 /**
116 * @param Invocation $invocation
117 *
118 * @return mixed
119 *
120 * @throws \Exception
121 * @throws RuntimeException
122 * @throws ExpectationFailedException
123 */
124 public function invoked(Invocation $invocation)
125 {
126 if ($this->invocationMatcher === null) {
127 throw new RuntimeException(
128 'No invocation matcher is set'
129 );
130 }
131
132 if ($this->methodNameMatcher === null) {
133 throw new RuntimeException('No method matcher is set');
134 }
135
136 if ($this->afterMatchBuilderId !== null) {
137 $builder = $invocation->getObject()
138 ->__phpunit_getInvocationMocker()
139 ->lookupId($this->afterMatchBuilderId);
140
141 if (!$builder) {
142 throw new RuntimeException(
143 \sprintf(
144 'No builder found for match builder identification <%s>',
145 $this->afterMatchBuilderId
146 )
147 );
148 }
149
150 $matcher = $builder->getMatcher();
151
152 if ($matcher && $matcher->invocationMatcher->hasBeenInvoked()) {
153 $this->afterMatchBuilderIsInvoked = true;
154 }
155 }
156
157 $this->invocationMatcher->invoked($invocation);
158
159 try {
160 if ($this->parametersMatcher !== null &&
161 !$this->parametersMatcher->matches($invocation)) {
162 $this->parametersMatcher->verify();
163 }
164 } catch (ExpectationFailedException $e) {
165 throw new ExpectationFailedException(
166 \sprintf(
167 "Expectation failed for %s when %s\n%s",
168 $this->methodNameMatcher->toString(),
169 $this->invocationMatcher->toString(),
170 $e->getMessage()
171 ),
172 $e->getComparisonFailure()
173 );
174 }
175
176 if ($this->stub) {
177 return $this->stub->invoke($invocation);
178 }
179
180 return $invocation->generateReturnValue();
181 }
182
183 /**
184 * @param Invocation $invocation
185 *
186 * @return bool
187 *
188 * @throws RuntimeException
189 * @throws ExpectationFailedException
190 */
191 public function matches(Invocation $invocation)
192 {
193 if ($this->afterMatchBuilderId !== null) {
194 $builder = $invocation->getObject()
195 ->__phpunit_getInvocationMocker()
196 ->lookupId($this->afterMatchBuilderId);
197
198 if (!$builder) {
199 throw new RuntimeException(
200 \sprintf(
201 'No builder found for match builder identification <%s>',
202 $this->afterMatchBuilderId
203 )
204 );
205 }
206
207 $matcher = $builder->getMatcher();
208
209 if (!$matcher) {
210 return false;
211 }
212
213 if (!$matcher->invocationMatcher->hasBeenInvoked()) {
214 return false;
215 }
216 }
217
218 if ($this->invocationMatcher === null) {
219 throw new RuntimeException(
220 'No invocation matcher is set'
221 );
222 }
223
224 if ($this->methodNameMatcher === null) {
225 throw new RuntimeException('No method matcher is set');
226 }
227
228 if (!$this->invocationMatcher->matches($invocation)) {
229 return false;
230 }
231
232 try {
233 if (!$this->methodNameMatcher->matches($invocation)) {
234 return false;
235 }
236 } catch (ExpectationFailedException $e) {
237 throw new ExpectationFailedException(
238 \sprintf(
239 "Expectation failed for %s when %s\n%s",
240 $this->methodNameMatcher->toString(),
241 $this->invocationMatcher->toString(),
242 $e->getMessage()
243 ),
244 $e->getComparisonFailure()
245 );
246 }
247
248 return true;
249 }
250
251 /**
252 * @throws RuntimeException
253 * @throws ExpectationFailedException
254 */
255 public function verify()
256 {
257 if ($this->invocationMatcher === null) {
258 throw new RuntimeException(
259 'No invocation matcher is set'
260 );
261 }
262
263 if ($this->methodNameMatcher === null) {
264 throw new RuntimeException('No method matcher is set');
265 }
266
267 try {
268 $this->invocationMatcher->verify();
269
270 if ($this->parametersMatcher === null) {
271 $this->parametersMatcher = new AnyParameters;
272 }
273
274 $invocationIsAny = $this->invocationMatcher instanceof AnyInvokedCount;
275 $invocationIsNever = $this->invocationMatcher instanceof InvokedCount && $this->invocationMatcher->isNever();
276
277 if (!$invocationIsAny && !$invocationIsNever) {
278 $this->parametersMatcher->verify();
279 }
280 } catch (ExpectationFailedException $e) {
281 throw new ExpectationFailedException(
282 \sprintf(
283 "Expectation failed for %s when %s.\n%s",
284 $this->methodNameMatcher->toString(),
285 $this->invocationMatcher->toString(),
286 TestFailure::exceptionToString($e)
287 )
288 );
289 }
290 }
291
292 /**
293 * @return string
294 */
295 public function toString()
296 {
297 $list = [];
298
299 if ($this->invocationMatcher !== null) {
300 $list[] = $this->invocationMatcher->toString();
301 }
302
303 if ($this->methodNameMatcher !== null) {
304 $list[] = 'where ' . $this->methodNameMatcher->toString();
305 }
306
307 if ($this->parametersMatcher !== null) {
308 $list[] = 'and ' . $this->parametersMatcher->toString();
309 }
310
311 if ($this->afterMatchBuilderId !== null) {
312 $list[] = 'after ' . $this->afterMatchBuilderId;
313 }
314
315 if ($this->stub !== null) {
316 $list[] = 'will ' . $this->stub->toString();
317 }
318
319 return \implode(' ', $list);
320 }
321 }