comparison vendor/phpunit/phpunit-mock-objects/src/MockBuilder.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\TestCase;
13
14 /**
15 * Implementation of the Builder pattern for Mock objects.
16 */
17 class MockBuilder
18 {
19 /**
20 * @var TestCase
21 */
22 private $testCase;
23
24 /**
25 * @var string
26 */
27 private $type;
28
29 /**
30 * @var array
31 */
32 private $methods = [];
33
34 /**
35 * @var array
36 */
37 private $methodsExcept = [];
38
39 /**
40 * @var string
41 */
42 private $mockClassName = '';
43
44 /**
45 * @var array
46 */
47 private $constructorArgs = [];
48
49 /**
50 * @var bool
51 */
52 private $originalConstructor = true;
53
54 /**
55 * @var bool
56 */
57 private $originalClone = true;
58
59 /**
60 * @var bool
61 */
62 private $autoload = true;
63
64 /**
65 * @var bool
66 */
67 private $cloneArguments = false;
68
69 /**
70 * @var bool
71 */
72 private $callOriginalMethods = false;
73
74 /**
75 * @var object
76 */
77 private $proxyTarget = null;
78
79 /**
80 * @var bool
81 */
82 private $allowMockingUnknownTypes = true;
83
84 /**
85 * @var Generator
86 */
87 private $generator;
88
89 /**
90 * @param TestCase $testCase
91 * @param array|string $type
92 */
93 public function __construct(TestCase $testCase, $type)
94 {
95 $this->testCase = $testCase;
96 $this->type = $type;
97 $this->generator = new Generator;
98 }
99
100 /**
101 * Creates a mock object using a fluent interface.
102 *
103 * @return MockObject
104 */
105 public function getMock()
106 {
107 $object = $this->generator->getMock(
108 $this->type,
109 $this->methods,
110 $this->constructorArgs,
111 $this->mockClassName,
112 $this->originalConstructor,
113 $this->originalClone,
114 $this->autoload,
115 $this->cloneArguments,
116 $this->callOriginalMethods,
117 $this->proxyTarget,
118 $this->allowMockingUnknownTypes
119 );
120
121 $this->testCase->registerMockObject($object);
122
123 return $object;
124 }
125
126 /**
127 * Creates a mock object for an abstract class using a fluent interface.
128 *
129 * @return MockObject
130 */
131 public function getMockForAbstractClass()
132 {
133 $object = $this->generator->getMockForAbstractClass(
134 $this->type,
135 $this->constructorArgs,
136 $this->mockClassName,
137 $this->originalConstructor,
138 $this->originalClone,
139 $this->autoload,
140 $this->methods,
141 $this->cloneArguments
142 );
143
144 $this->testCase->registerMockObject($object);
145
146 return $object;
147 }
148
149 /**
150 * Creates a mock object for a trait using a fluent interface.
151 *
152 * @return MockObject
153 */
154 public function getMockForTrait()
155 {
156 $object = $this->generator->getMockForTrait(
157 $this->type,
158 $this->constructorArgs,
159 $this->mockClassName,
160 $this->originalConstructor,
161 $this->originalClone,
162 $this->autoload,
163 $this->methods,
164 $this->cloneArguments
165 );
166
167 $this->testCase->registerMockObject($object);
168
169 return $object;
170 }
171
172 /**
173 * Specifies the subset of methods to mock. Default is to mock none of them.
174 *
175 * @param array|null $methods
176 *
177 * @return MockBuilder
178 */
179 public function setMethods(array $methods = null)
180 {
181 $this->methods = $methods;
182
183 return $this;
184 }
185
186 /**
187 * Specifies the subset of methods to not mock. Default is to mock all of them.
188 *
189 * @param array $methods
190 *
191 * @return MockBuilder
192 */
193 public function setMethodsExcept(array $methods = [])
194 {
195 $this->methodsExcept = $methods;
196
197 $this->setMethods(
198 \array_diff(
199 $this->generator->getClassMethods($this->type),
200 $this->methodsExcept
201 )
202 );
203
204 return $this;
205 }
206
207 /**
208 * Specifies the arguments for the constructor.
209 *
210 * @param array $args
211 *
212 * @return MockBuilder
213 */
214 public function setConstructorArgs(array $args)
215 {
216 $this->constructorArgs = $args;
217
218 return $this;
219 }
220
221 /**
222 * Specifies the name for the mock class.
223 *
224 * @param string $name
225 *
226 * @return MockBuilder
227 */
228 public function setMockClassName($name)
229 {
230 $this->mockClassName = $name;
231
232 return $this;
233 }
234
235 /**
236 * Disables the invocation of the original constructor.
237 *
238 * @return MockBuilder
239 */
240 public function disableOriginalConstructor()
241 {
242 $this->originalConstructor = false;
243
244 return $this;
245 }
246
247 /**
248 * Enables the invocation of the original constructor.
249 *
250 * @return MockBuilder
251 */
252 public function enableOriginalConstructor()
253 {
254 $this->originalConstructor = true;
255
256 return $this;
257 }
258
259 /**
260 * Disables the invocation of the original clone constructor.
261 *
262 * @return MockBuilder
263 */
264 public function disableOriginalClone()
265 {
266 $this->originalClone = false;
267
268 return $this;
269 }
270
271 /**
272 * Enables the invocation of the original clone constructor.
273 *
274 * @return MockBuilder
275 */
276 public function enableOriginalClone()
277 {
278 $this->originalClone = true;
279
280 return $this;
281 }
282
283 /**
284 * Disables the use of class autoloading while creating the mock object.
285 *
286 * @return MockBuilder
287 */
288 public function disableAutoload()
289 {
290 $this->autoload = false;
291
292 return $this;
293 }
294
295 /**
296 * Enables the use of class autoloading while creating the mock object.
297 *
298 * @return MockBuilder
299 */
300 public function enableAutoload()
301 {
302 $this->autoload = true;
303
304 return $this;
305 }
306
307 /**
308 * Disables the cloning of arguments passed to mocked methods.
309 *
310 * @return MockBuilder
311 */
312 public function disableArgumentCloning()
313 {
314 $this->cloneArguments = false;
315
316 return $this;
317 }
318
319 /**
320 * Enables the cloning of arguments passed to mocked methods.
321 *
322 * @return MockBuilder
323 */
324 public function enableArgumentCloning()
325 {
326 $this->cloneArguments = true;
327
328 return $this;
329 }
330
331 /**
332 * Enables the invocation of the original methods.
333 *
334 * @return MockBuilder
335 */
336 public function enableProxyingToOriginalMethods()
337 {
338 $this->callOriginalMethods = true;
339
340 return $this;
341 }
342
343 /**
344 * Disables the invocation of the original methods.
345 *
346 * @return MockBuilder
347 */
348 public function disableProxyingToOriginalMethods()
349 {
350 $this->callOriginalMethods = false;
351 $this->proxyTarget = null;
352
353 return $this;
354 }
355
356 /**
357 * Sets the proxy target.
358 *
359 * @param object $object
360 *
361 * @return MockBuilder
362 */
363 public function setProxyTarget($object)
364 {
365 $this->proxyTarget = $object;
366
367 return $this;
368 }
369
370 /**
371 * @return MockBuilder
372 */
373 public function allowMockingUnknownTypes()
374 {
375 $this->allowMockingUnknownTypes = true;
376
377 return $this;
378 }
379
380 /**
381 * @return MockBuilder
382 */
383 public function disallowMockingUnknownTypes()
384 {
385 $this->allowMockingUnknownTypes = false;
386
387 return $this;
388 }
389 }