Mercurial > hg > isophonics-drupal-site
comparison vendor/phpunit/phpunit-mock-objects/src/Builder/InvocationMocker.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\Builder; | |
11 | |
12 use PHPUnit\Framework\Constraint\Constraint; | |
13 use PHPUnit\Framework\MockObject\Matcher; | |
14 use PHPUnit\Framework\MockObject\Matcher\Invocation; | |
15 use PHPUnit\Framework\MockObject\RuntimeException; | |
16 use PHPUnit\Framework\MockObject\Stub; | |
17 use PHPUnit\Framework\MockObject\Stub\MatcherCollection; | |
18 | |
19 /** | |
20 * Builder for mocked or stubbed invocations. | |
21 * | |
22 * Provides methods for building expectations without having to resort to | |
23 * instantiating the various matchers manually. These methods also form a | |
24 * more natural way of reading the expectation. This class should be together | |
25 * with the test case PHPUnit\Framework\MockObject\TestCase. | |
26 */ | |
27 class InvocationMocker implements MethodNameMatch | |
28 { | |
29 /** | |
30 * @var MatcherCollection | |
31 */ | |
32 private $collection; | |
33 | |
34 /** | |
35 * @var Matcher | |
36 */ | |
37 private $matcher; | |
38 | |
39 /** | |
40 * @var string[] | |
41 */ | |
42 private $configurableMethods = []; | |
43 | |
44 /** | |
45 * @param MatcherCollection $collection | |
46 * @param Invocation $invocationMatcher | |
47 * @param array $configurableMethods | |
48 */ | |
49 public function __construct(MatcherCollection $collection, Invocation $invocationMatcher, array $configurableMethods) | |
50 { | |
51 $this->collection = $collection; | |
52 $this->matcher = new Matcher($invocationMatcher); | |
53 | |
54 $this->collection->addMatcher($this->matcher); | |
55 | |
56 $this->configurableMethods = $configurableMethods; | |
57 } | |
58 | |
59 /** | |
60 * @return Matcher | |
61 */ | |
62 public function getMatcher() | |
63 { | |
64 return $this->matcher; | |
65 } | |
66 | |
67 /** | |
68 * @param mixed $id | |
69 * | |
70 * @return InvocationMocker | |
71 */ | |
72 public function id($id) | |
73 { | |
74 $this->collection->registerId($id, $this); | |
75 | |
76 return $this; | |
77 } | |
78 | |
79 /** | |
80 * @param Stub $stub | |
81 * | |
82 * @return InvocationMocker | |
83 */ | |
84 public function will(Stub $stub) | |
85 { | |
86 $this->matcher->setStub($stub); | |
87 | |
88 return $this; | |
89 } | |
90 | |
91 /** | |
92 * @param mixed $value | |
93 * @param mixed $nextValues, ... | |
94 * | |
95 * @return InvocationMocker | |
96 */ | |
97 public function willReturn($value, ...$nextValues) | |
98 { | |
99 if (\count($nextValues) === 0) { | |
100 $stub = new Stub\ReturnStub($value); | |
101 } else { | |
102 $stub = new Stub\ConsecutiveCalls( | |
103 \array_merge([$value], $nextValues) | |
104 ); | |
105 } | |
106 | |
107 return $this->will($stub); | |
108 } | |
109 | |
110 /** | |
111 * @param mixed $reference | |
112 * | |
113 * @return InvocationMocker | |
114 */ | |
115 public function willReturnReference(&$reference) | |
116 { | |
117 $stub = new Stub\ReturnReference($reference); | |
118 | |
119 return $this->will($stub); | |
120 } | |
121 | |
122 /** | |
123 * @param array $valueMap | |
124 * | |
125 * @return InvocationMocker | |
126 */ | |
127 public function willReturnMap(array $valueMap) | |
128 { | |
129 $stub = new Stub\ReturnValueMap($valueMap); | |
130 | |
131 return $this->will($stub); | |
132 } | |
133 | |
134 /** | |
135 * @param mixed $argumentIndex | |
136 * | |
137 * @return InvocationMocker | |
138 */ | |
139 public function willReturnArgument($argumentIndex) | |
140 { | |
141 $stub = new Stub\ReturnArgument($argumentIndex); | |
142 | |
143 return $this->will($stub); | |
144 } | |
145 | |
146 /** | |
147 * @param callable $callback | |
148 * | |
149 * @return InvocationMocker | |
150 */ | |
151 public function willReturnCallback($callback) | |
152 { | |
153 $stub = new Stub\ReturnCallback($callback); | |
154 | |
155 return $this->will($stub); | |
156 } | |
157 | |
158 /** | |
159 * @return InvocationMocker | |
160 */ | |
161 public function willReturnSelf() | |
162 { | |
163 $stub = new Stub\ReturnSelf; | |
164 | |
165 return $this->will($stub); | |
166 } | |
167 | |
168 /** | |
169 * @param mixed $values, ... | |
170 * | |
171 * @return InvocationMocker | |
172 */ | |
173 public function willReturnOnConsecutiveCalls(...$values) | |
174 { | |
175 $stub = new Stub\ConsecutiveCalls($values); | |
176 | |
177 return $this->will($stub); | |
178 } | |
179 | |
180 /** | |
181 * @param \Exception $exception | |
182 * | |
183 * @return InvocationMocker | |
184 */ | |
185 public function willThrowException(\Exception $exception) | |
186 { | |
187 $stub = new Stub\Exception($exception); | |
188 | |
189 return $this->will($stub); | |
190 } | |
191 | |
192 /** | |
193 * @param mixed $id | |
194 * | |
195 * @return InvocationMocker | |
196 */ | |
197 public function after($id) | |
198 { | |
199 $this->matcher->setAfterMatchBuilderId($id); | |
200 | |
201 return $this; | |
202 } | |
203 | |
204 /** | |
205 * @param array ...$arguments | |
206 * | |
207 * @return InvocationMocker | |
208 * | |
209 * @throws RuntimeException | |
210 */ | |
211 public function with(...$arguments) | |
212 { | |
213 $this->canDefineParameters(); | |
214 | |
215 $this->matcher->setParametersMatcher(new Matcher\Parameters($arguments)); | |
216 | |
217 return $this; | |
218 } | |
219 | |
220 /** | |
221 * @param array ...$arguments | |
222 * | |
223 * @return InvocationMocker | |
224 * | |
225 * @throws RuntimeException | |
226 */ | |
227 public function withConsecutive(...$arguments) | |
228 { | |
229 $this->canDefineParameters(); | |
230 | |
231 $this->matcher->setParametersMatcher(new Matcher\ConsecutiveParameters($arguments)); | |
232 | |
233 return $this; | |
234 } | |
235 | |
236 /** | |
237 * @return InvocationMocker | |
238 * | |
239 * @throws RuntimeException | |
240 */ | |
241 public function withAnyParameters() | |
242 { | |
243 $this->canDefineParameters(); | |
244 | |
245 $this->matcher->setParametersMatcher(new Matcher\AnyParameters); | |
246 | |
247 return $this; | |
248 } | |
249 | |
250 /** | |
251 * @param Constraint|string $constraint | |
252 * | |
253 * @return InvocationMocker | |
254 * | |
255 * @throws RuntimeException | |
256 */ | |
257 public function method($constraint) | |
258 { | |
259 if ($this->matcher->hasMethodNameMatcher()) { | |
260 throw new RuntimeException( | |
261 'Method name matcher is already defined, cannot redefine' | |
262 ); | |
263 } | |
264 | |
265 if (\is_string($constraint) && !\in_array(\strtolower($constraint), $this->configurableMethods)) { | |
266 throw new RuntimeException( | |
267 \sprintf( | |
268 'Trying to configure method "%s" which cannot be configured because it does not exist, has not been specified, is final, or is static', | |
269 $constraint | |
270 ) | |
271 ); | |
272 } | |
273 | |
274 $this->matcher->setMethodNameMatcher(new Matcher\MethodName($constraint)); | |
275 | |
276 return $this; | |
277 } | |
278 | |
279 /** | |
280 * Validate that a parameters matcher can be defined, throw exceptions otherwise. | |
281 * | |
282 * @throws RuntimeException | |
283 */ | |
284 private function canDefineParameters() | |
285 { | |
286 if (!$this->matcher->hasMethodNameMatcher()) { | |
287 throw new RuntimeException( | |
288 'Method name matcher is not defined, cannot define parameter ' . | |
289 'matcher without one' | |
290 ); | |
291 } | |
292 | |
293 if ($this->matcher->hasParametersMatcher()) { | |
294 throw new RuntimeException( | |
295 'Parameter matcher is already defined, cannot redefine' | |
296 ); | |
297 } | |
298 } | |
299 } |