ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
LightMockBuilderTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use PHPUnit\Framework\TestCase;
24use LogicException;
29
30// ---------------------------------------------------------------------------
31// Test fixtures – prefixed "Lmb" to avoid collisions with other test files
32// in the same namespace.
33// ---------------------------------------------------------------------------
34
36{
37}
38
40{
41 public function returnsVoid(): void;
42 public function returnsBool(): bool;
43 public function returnsInt(): int;
44 public function returnsFloat(): float;
45 public function returnsString(): string;
46 public function returnsArray(): array;
47 public function returnsNullableString(): ?string;
48 public function returnsObject(): object;
49 public function returnsSelf(): static;
50 public function returnsCallable(): callable;
52 public function takesArgs(string $a, int $b): string;
53}
54
56{
57 public function neverReturns(): never;
58}
59
61{
62 public static function staticMethod(): array;
63 public function instanceMethod(): string;
64}
65
67{
68 abstract public static function staticMethod(): array;
69}
70
71abstract class LmbAbstractBase
72{
73 abstract public function abstractMethod(): string;
74}
75
77{
78 public function getValue(): int
79 {
80 return 1;
81 }
82}
83
84final class LmbFinalClass
85{
86}
87
89{
90 case A;
92
93final class LightMockBuilderTest extends TestCase
94{
96
97 protected function setUp(): void
98 {
99 $this->builder = new EvalLightMockBuilder();
100 }
101
102 // --- create(): type resolution ---
103
105 {
106 $mock = $this->builder->create(LmbEmptyInterface::class);
107 $this->assertInstanceOf(LmbEmptyInterface::class, $mock);
108 }
109
111 {
112 $mock = $this->builder->create(LmbAbstractBase::class);
113 $this->assertInstanceOf(LmbAbstractBase::class, $mock);
114 }
115
117 {
118 $mock = $this->builder->create(LmbConcreteClass::class);
119 $this->assertInstanceOf(LmbConcreteClass::class, $mock);
120 }
121
123 {
124 $mock = $this->builder->create(LmbStaticInterface::class);
125 $this->assertInstanceOf(LmbStaticInterface::class, $mock);
126 $this->assertSame('', $mock->instanceMethod());
127 }
128
130 {
131 $mock = $this->builder->create(LmbAbstractStaticBase::class);
132 $this->assertInstanceOf(LmbAbstractStaticBase::class, $mock);
133 }
134
136 {
137 $mock = $this->builder->create(LmbStaticInterface::class);
138 $this->expectException(LogicException::class);
139 $mock::staticMethod();
140 }
141
143 {
144 $this->expectException(LogicException::class);
145 $this->builder->create(LmbFinalClass::class);
146 }
147
149 {
150 $this->expectException(LogicException::class);
151 $this->builder->create(LmbTestEnum::class);
152 }
153
155 {
156 $this->expectException(LogicException::class);
157 $this->builder->create('\\Non\\Existent\\TypeXyz');
158 }
159
160 // --- Default return values ---
161
162 public function testDefaultVoidMethodReturnsNull(): void
163 {
164 $mock = $this->builder->create(LmbReturnTypesInterface::class);
165 $this->assertNull($mock->returnsVoid());
166 }
167
168 public function testDefaultBoolReturnsFalse(): void
169 {
170 $mock = $this->builder->create(LmbReturnTypesInterface::class);
171 $this->assertFalse($mock->returnsBool());
172 }
173
174 public function testDefaultIntReturnsZero(): void
175 {
176 $mock = $this->builder->create(LmbReturnTypesInterface::class);
177 $this->assertSame(0, $mock->returnsInt());
178 }
179
181 {
182 $mock = $this->builder->create(LmbReturnTypesInterface::class);
183 $this->assertEqualsWithDelta(0.0, $mock->returnsFloat(), PHP_FLOAT_EPSILON);
184 }
185
187 {
188 $mock = $this->builder->create(LmbReturnTypesInterface::class);
189 $this->assertSame('', $mock->returnsString());
190 }
191
192 public function testDefaultArrayReturnsEmptyArray(): void
193 {
194 $mock = $this->builder->create(LmbReturnTypesInterface::class);
195 $this->assertSame([], $mock->returnsArray());
196 }
197
199 {
200 $mock = $this->builder->create(LmbReturnTypesInterface::class);
201 $this->assertNull($mock->returnsNullableString());
202 }
203
205 {
206 $mock = $this->builder->create(LmbReturnTypesInterface::class);
207 $this->assertInstanceOf(\stdClass::class, $mock->returnsObject());
208 }
209
210 public function testDefaultSelfReturnsSameObject(): void
211 {
212 $mock = $this->builder->create(LmbReturnTypesInterface::class);
213 $this->assertSame($mock, $mock->returnsSelf());
214 }
215
217 {
218 $mock = $this->builder->create(LmbReturnTypesInterface::class);
219 $this->assertIsCallable($mock->returnsCallable());
220 }
221
223 {
224 $mock = $this->builder->create(LmbReturnTypesInterface::class);
225 $this->assertInstanceOf(LmbReturnTypesInterface::class, $mock->returnsInterface());
226 }
227
229 {
230 $mock = $this->builder->create(LmbNeverInterface::class);
231 $this->expectException(LogicException::class);
232 $mock->neverReturns();
233 }
234
235 // --- Stub configuration ---
236
238 {
239 $mock = $this->builder->create(LmbReturnTypesInterface::class);
240 $mock->__mockSetReturnValue('returnsInt', 42);
241 $this->assertSame(42, $mock->returnsInt());
242 }
243
245 {
246 $mock = $this->builder->create(LmbReturnTypesInterface::class);
247 $this->assertSame($mock, $mock->__mockSetReturnValue('returnsInt', 1));
248 }
249
250 public function testSetCallbackOverridesDefault(): void
251 {
252 $mock = $this->builder->create(LmbReturnTypesInterface::class);
253 $mock->__mockSetCallback('returnsString', fn(): string => 'hello');
254 $this->assertSame('hello', $mock->returnsString());
255 }
256
257 public function testCallbackReceivesArguments(): void
258 {
259 $mock = $this->builder->create(LmbReturnTypesInterface::class);
260 $mock->__mockSetCallback('takesArgs', fn(string $a, int $b): string => $a . $b);
261 $this->assertSame('test7', $mock->takesArgs('test', 7));
262 }
263
265 {
266 $mock = $this->builder->create(LmbReturnTypesInterface::class);
267 $mock->__mockSetReturnValue('returnsString', 'from-value');
268 $mock->__mockSetCallback('returnsString', fn(): string => 'from-callback');
269 $this->assertSame('from-callback', $mock->returnsString());
270 }
271
272 // --- Generated-class caching ---
273
275 {
276 $first = $this->builder->create(LmbEmptyInterface::class);
277 $second = $this->builder->create(LmbEmptyInterface::class);
278 $this->assertInstanceOf($first::class, $second);
279 }
280
282 {
283 $first = $this->builder->create(LmbEmptyInterface::class);
284 $second = $this->builder->create(LmbReturnTypesInterface::class);
285 $this->assertNotInstanceOf($first::class, $second);
286 }
287
288 // --- PHPUnitMockBuilder: keeps the alternative implementation compiling ---
289
291 {
292 $mock = (new PHPUnitMockBuilder())->create(LmbReturnTypesInterface::class);
293
294 $this->assertInstanceOf(LmbReturnTypesInterface::class, $mock);
295 $this->assertSame(0, $mock->returnsInt());
296 }
297
298 // --- defaultValueFor boundary behaviour ---
299
301 {
302 $result = GeneratedMockRegistry::defaultValueFor(new \stdClass(), 'nonExistentMethod');
303 $this->assertNull($result);
304 }
305
307 {
308 $mock = $this->builder->create(LmbReturnTypesInterface::class);
309 $this->assertSame(
310 $mock->returnsInt(),
311 GeneratedMockRegistry::defaultValueFor($mock, 'returnsInt')
312 );
313 }
314}
Bookkeeping about the mock classes that have been generated into the running process.
static defaultValueFor(object $mock, string $method)
Entry point for the generated mocks themselves, see {.
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples