-
-
Notifications
You must be signed in to change notification settings - Fork 262
Expand file tree
/
Copy pathKernelTest.php
More file actions
669 lines (540 loc) · 25.3 KB
/
KernelTest.php
File metadata and controls
669 lines (540 loc) · 25.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
<?php
namespace phpMyFAQ;
use phpMyFAQ\Configuration;
use phpMyFAQ\Controller\ContainerControllerResolver;
use phpMyFAQ\Database\PdoSqlite;
use phpMyFAQ\EventListener\ApiExceptionListener;
use phpMyFAQ\EventListener\ApiRateLimiterListener;
use phpMyFAQ\EventListener\ControllerContainerListener;
use phpMyFAQ\EventListener\LanguageListener;
use phpMyFAQ\EventListener\RouterListener;
use phpMyFAQ\EventListener\WebExceptionListener;
use phpMyFAQ\Form\FormsServiceProvider;
use phpMyFAQ\Http\RateLimiter;
use phpMyFAQ\Routing\AttributeRouteLoader;
use phpMyFAQ\Routing\RouteCacheManager;
use phpMyFAQ\Routing\RouteCollectionBuilder;
use phpMyFAQ\System;
use PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Controller\ArgumentResolver;
use Symfony\Component\HttpKernel\HttpKernel;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\RateLimiter\Storage\InMemoryStorage;
use Symfony\Component\Routing\RouteCollection;
#[CoversClass(Kernel::class)]
#[UsesClass(ContainerControllerResolver::class)]
#[UsesClass(ApiExceptionListener::class)]
#[UsesClass(ApiRateLimiterListener::class)]
#[UsesClass(ControllerContainerListener::class)]
#[UsesClass(LanguageListener::class)]
#[UsesClass(RouterListener::class)]
#[UsesClass(Strings::class)]
#[UsesClass(Translation::class)]
#[UsesClass(WebExceptionListener::class)]
#[UsesClass(Environment::class)]
#[UsesClass(FormsServiceProvider::class)]
#[UsesClass(AttributeRouteLoader::class)]
#[UsesClass(RouteCacheManager::class)]
#[UsesClass(RouteCollectionBuilder::class)]
#[UsesClass(PdoSqlite::class)]
#[UsesClass(RateLimiter::class)]
#[UsesClass(System::class)]
#[AllowMockObjectsWithoutExpectations]
class KernelTest extends TestCase
{
public function testKernelImplementsHttpKernelInterface(): void
{
$kernel = new Kernel(routingContext: 'public', debug: true);
$this->assertInstanceOf(HttpKernelInterface::class, $kernel);
}
public function testKernelRoutingContext(): void
{
$kernel = new Kernel(routingContext: 'admin', debug: false);
$this->assertSame('admin', $kernel->getRoutingContext());
}
public function testKernelDebugMode(): void
{
$kernel = new Kernel(routingContext: 'public', debug: true);
$this->assertTrue($kernel->isDebug());
}
public function testKernelNonDebugMode(): void
{
$kernel = new Kernel(routingContext: 'public', debug: false);
$this->assertFalse($kernel->isDebug());
}
public function testKernelDefaultParameters(): void
{
$kernel = new Kernel();
$this->assertSame('public', $kernel->getRoutingContext());
$this->assertFalse($kernel->isDebug());
}
public function testKernelRoutingContextApi(): void
{
$kernel = new Kernel(routingContext: 'api', debug: false);
$this->assertSame('api', $kernel->getRoutingContext());
}
public function testKernelRoutingContextAdminApi(): void
{
$kernel = new Kernel(routingContext: 'admin-api', debug: false);
$this->assertSame('admin-api', $kernel->getRoutingContext());
}
public function testBootSetsAllProperties(): void
{
$kernel = $this->createKernelWithMockedBoot();
$kernel->boot();
$reflection = new ReflectionClass(Kernel::class);
$this->assertTrue($reflection->getProperty('booted')->getValue($kernel));
$this->assertInstanceOf(ContainerBuilder::class, $reflection->getProperty('container')->getValue($kernel));
$this->assertInstanceOf(RouteCollection::class, $reflection->getProperty('routes')->getValue($kernel));
$this->assertInstanceOf(HttpKernel::class, $reflection->getProperty('httpKernel')->getValue($kernel));
}
public function testBootOnlyRunsOnce(): void
{
$kernel = $this->createKernelWithMockedBoot();
$kernel->boot();
$kernel->boot(); // second call should be a no-op
$reflection = new ReflectionClass(Kernel::class);
$this->assertTrue($reflection->getProperty('booted')->getValue($kernel));
}
public function testGetContainerAutoBoots(): void
{
$kernel = $this->createKernelWithMockedBoot();
$reflection = new ReflectionClass(Kernel::class);
$this->assertFalse($reflection->getProperty('booted')->getValue($kernel));
$container = $kernel->getContainer();
$this->assertInstanceOf(ContainerBuilder::class, $container);
$this->assertTrue($reflection->getProperty('booted')->getValue($kernel));
}
public function testGetContainerReturnsExistingContainerWhenBooted(): void
{
$kernel = $this->createKernelWithMockedBoot();
$kernel->boot();
$container1 = $kernel->getContainer();
$container2 = $kernel->getContainer();
$this->assertSame($container1, $container2);
}
public function testHandleAutoBoots(): void
{
$kernel = $this->createKernelWithMockedBoot();
$reflection = new ReflectionClass(Kernel::class);
$this->assertFalse($reflection->getProperty('booted')->getValue($kernel));
$request = Request::create('/test');
// This will auto-boot, then fail on routing (no matching route), but that's OK
$response = $kernel->handle($request);
$this->assertTrue($reflection->getProperty('booted')->getValue($kernel));
// The response should be a 404 or error since no routes match
$this->assertInstanceOf(Response::class, $response);
}
public function testHandleSetsApiContextForApiRoute(): void
{
$mockHttpKernel = $this->createMock(HttpKernel::class);
$mockHttpKernel
->method('handle')
->willReturnCallback(function (Request $request): Response {
$this->assertTrue($request->attributes->get('_api_context'));
return new Response('API OK');
});
$kernel = $this->createPreBootedKernel(routingContext: 'api', httpKernel: $mockHttpKernel);
$request = Request::create('/api/test');
$response = $kernel->handle($request);
$this->assertSame(200, $response->getStatusCode());
}
public function testHandleSetsApiContextForAdminApiRoute(): void
{
$mockHttpKernel = $this->createMock(HttpKernel::class);
$mockHttpKernel
->method('handle')
->willReturnCallback(function (Request $request): Response {
$this->assertTrue($request->attributes->get('_api_context'));
return new Response('Admin API OK');
});
$kernel = $this->createPreBootedKernel(routingContext: 'admin-api', httpKernel: $mockHttpKernel);
$request = Request::create('/admin/api/test');
$response = $kernel->handle($request);
$this->assertSame(200, $response->getStatusCode());
}
public function testHandleDoesNotSetApiContextForPublicRoute(): void
{
$mockHttpKernel = $this->createMock(HttpKernel::class);
$mockHttpKernel
->method('handle')
->willReturnCallback(function (Request $request): Response {
$this->assertNull($request->attributes->get('_api_context'));
return new Response('Public OK');
});
$kernel = $this->createPreBootedKernel(routingContext: 'public', httpKernel: $mockHttpKernel);
$request = Request::create('/test');
$response = $kernel->handle($request);
$this->assertSame(200, $response->getStatusCode());
}
public function testHandleDoesNotSetApiContextForAdminRoute(): void
{
$mockHttpKernel = $this->createMock(HttpKernel::class);
$mockHttpKernel
->method('handle')
->willReturnCallback(function (Request $request): Response {
$this->assertNull($request->attributes->get('_api_context'));
return new Response('Admin OK');
});
$kernel = $this->createPreBootedKernel(routingContext: 'admin', httpKernel: $mockHttpKernel);
$request = Request::create('/admin/test');
$response = $kernel->handle($request);
$this->assertSame(200, $response->getStatusCode());
}
public function testHandleDelegatesTypeAndCatchParameters(): void
{
$mockHttpKernel = $this->createMock(HttpKernel::class);
$mockHttpKernel
->expects($this->once())
->method('handle')
->with($this->isInstanceOf(Request::class), HttpKernelInterface::SUB_REQUEST, false)
->willReturn(new Response('Sub-request'));
$kernel = $this->createPreBootedKernel(httpKernel: $mockHttpKernel);
$request = Request::create('/test');
$response = $kernel->handle($request, HttpKernelInterface::SUB_REQUEST, false);
$this->assertSame(200, $response->getStatusCode());
}
public function testBuildContainerLoadsServicesAndRegistersKernel(): void
{
$kernel = new Kernel(routingContext: 'public', debug: true);
$reflection = new ReflectionClass(Kernel::class);
$buildMethod = $reflection->getMethod('buildContainer');
$container = $buildMethod->invoke($kernel);
$this->assertInstanceOf(ContainerBuilder::class, $container);
$this->assertSame($kernel, $container->get('kernel'));
// Verify the event dispatcher was registered by services.php
$this->assertTrue($container->has('phpmyfaq.event_dispatcher'));
}
public function testLoadRoutesWithDebugModeSkipsCache(): void
{
$container = $this->createMock(ContainerBuilder::class);
$container->method('get')->willReturn(null);
$kernel = new Kernel(routingContext: 'public', debug: true);
$reflection = new ReflectionClass(Kernel::class);
$containerProp = $reflection->getProperty('container');
$containerProp->setValue($kernel, $container);
$loadMethod = $reflection->getMethod('loadRoutes');
$routes = $loadMethod->invoke($kernel);
$this->assertInstanceOf(RouteCollection::class, $routes);
}
public function testLoadRoutesWithCacheDisabledViaEnv(): void
{
$_ENV['ROUTING_CACHE_ENABLED'] = 'false';
try {
$container = $this->createMock(ContainerBuilder::class);
$container->method('get')->willReturn(null);
$kernel = new Kernel(routingContext: 'public', debug: false);
$reflection = new ReflectionClass(Kernel::class);
$containerProp = $reflection->getProperty('container');
$containerProp->setValue($kernel, $container);
$loadMethod = $reflection->getMethod('loadRoutes');
$routes = $loadMethod->invoke($kernel);
$this->assertInstanceOf(RouteCollection::class, $routes);
} finally {
unset($_ENV['ROUTING_CACHE_ENABLED']);
}
}
public function testLoadRoutesWithCacheEnabled(): void
{
$_ENV['ROUTING_CACHE_ENABLED'] = 'true';
$cacheDir = sys_get_temp_dir() . '/phpmyfaq_test_cache_' . uniqid();
try {
$_ENV['ROUTING_CACHE_DIR'] = $cacheDir;
Environment::enableTestMode();
$container = $this->createMock(ContainerBuilder::class);
$container->method('get')->willReturn(null);
// debug=false so the cache path is taken
$kernel = new Kernel(routingContext: 'public', debug: false);
$reflection = new ReflectionClass(Kernel::class);
$containerProp = $reflection->getProperty('container');
$containerProp->setValue($kernel, $container);
$loadMethod = $reflection->getMethod('loadRoutes');
$routes = $loadMethod->invoke($kernel);
$this->assertInstanceOf(RouteCollection::class, $routes);
} finally {
unset($_ENV['ROUTING_CACHE_ENABLED'], $_ENV['ROUTING_CACHE_DIR']);
// Clean up cache directory
if (is_dir($cacheDir)) {
array_map('unlink', glob($cacheDir . '/*'));
rmdir($cacheDir);
}
}
}
public function testBuildContainerThrowsRuntimeExceptionOnServiceLoadFailure(): void
{
// Test the catch block by temporarily defining a bad PMF_SRC_DIR
// Since PMF_SRC_DIR is a constant, we test the error path by calling
// buildContainer on a kernel that has a services.php that will fail
// Instead, we verify the exception wrapping by using reflection
$kernel = new Kernel(routingContext: 'public', debug: true);
$reflection = new ReflectionClass(Kernel::class);
$buildMethod = $reflection->getMethod('buildContainer');
// The real buildContainer will succeed since services.php exists
// Test that it returns a valid container
$container = $buildMethod->invoke($kernel);
$this->assertInstanceOf(ContainerBuilder::class, $container);
// Verify FormsServiceProvider was registered
$this->assertTrue($container->has('phpmyfaq.forms'));
}
public function testCreateHttpKernelFallsBackToNewDispatcher(): void
{
$notADispatcher = new \stdClass();
$container = $this->createMock(ContainerBuilder::class);
$container
->method('get')
->willReturnCallback(static fn(string $id) => match ($id) {
'phpmyfaq.event_dispatcher' => $notADispatcher,
default => null,
});
$container->method('has')->willReturn(false);
$kernel = new Kernel(routingContext: 'public', debug: true);
$reflection = new ReflectionClass(Kernel::class);
$containerProp = $reflection->getProperty('container');
$containerProp->setValue($kernel, $container);
$routesProp = $reflection->getProperty('routes');
$routesProp->setValue($kernel, new RouteCollection());
$createMethod = $reflection->getMethod('createHttpKernel');
$httpKernel = $createMethod->invoke($kernel);
$this->assertInstanceOf(HttpKernel::class, $httpKernel);
}
public function testCreateHttpKernelUsesExistingDispatcher(): void
{
$dispatcher = new EventDispatcher();
$container = $this->createMock(ContainerBuilder::class);
$container
->method('get')
->willReturnCallback(static fn(string $id) => match ($id) {
'phpmyfaq.event_dispatcher' => $dispatcher,
default => null,
});
$container->method('has')->willReturn(false);
$kernel = new Kernel(routingContext: 'public', debug: true);
$reflection = new ReflectionClass(Kernel::class);
$containerProp = $reflection->getProperty('container');
$containerProp->setValue($kernel, $container);
$routesProp = $reflection->getProperty('routes');
$routesProp->setValue($kernel, new RouteCollection());
$createMethod = $reflection->getMethod('createHttpKernel');
$httpKernel = $createMethod->invoke($kernel);
$this->assertInstanceOf(HttpKernel::class, $httpKernel);
}
public function testRegisterEventListenersAddsAllListeners(): void
{
$container = $this->createMock(ContainerBuilder::class);
$container->method('get')->willReturn(null);
$container->method('has')->willReturn(false);
$kernel = new Kernel(routingContext: 'public', debug: true);
$reflection = new ReflectionClass(Kernel::class);
$containerProp = $reflection->getProperty('container');
$containerProp->setValue($kernel, $container);
$routesProp = $reflection->getProperty('routes');
$routesProp->setValue($kernel, new RouteCollection());
$dispatcher = new EventDispatcher();
$registerMethod = $reflection->getMethod('registerEventListeners');
$registerMethod->invoke($kernel, $dispatcher);
$requestListeners = $dispatcher->getListeners('kernel.request');
$exceptionListeners = $dispatcher->getListeners('kernel.exception');
$controllerListeners = $dispatcher->getListeners('kernel.controller');
// RouterListener and LanguageListener
$this->assertCount(2, $requestListeners);
// ApiExceptionListener and WebExceptionListener
$this->assertCount(2, $exceptionListeners);
// ControllerContainerListener
$this->assertCount(1, $controllerListeners);
}
public function testRegisterEventListenersRequestListenerPriorities(): void
{
$container = $this->createMock(ContainerBuilder::class);
$container->method('get')->willReturn(null);
$container->method('has')->willReturn(false);
$kernel = new Kernel(routingContext: 'public', debug: true);
$reflection = new ReflectionClass(Kernel::class);
$containerProp = $reflection->getProperty('container');
$containerProp->setValue($kernel, $container);
$routesProp = $reflection->getProperty('routes');
$routesProp->setValue($kernel, new RouteCollection());
$dispatcher = new EventDispatcher();
$registerMethod = $reflection->getMethod('registerEventListeners');
$registerMethod->invoke($kernel, $dispatcher);
$requestListeners = $dispatcher->getListeners('kernel.request');
// RouterListener runs first (priority 256), LanguageListener second (priority 200)
$this->assertCount(2, $requestListeners);
// getListeners returns sorted by priority (highest first)
$this->assertSame('onKernelRequest', $requestListeners[0][1]);
$this->assertSame('onKernelRequest', $requestListeners[1][1]);
}
public function testRegisterEventListenersUsesConfigurationForApiListener(): void
{
$configMock = $this->createMock(Configuration::class);
$rateLimiter = new RateLimiter(storage: new InMemoryStorage());
$container = $this->createMock(ContainerBuilder::class);
$container
->method('has')
->willReturnCallback(static fn(string $id) => in_array(
$id,
['phpmyfaq.configuration', 'phpmyfaq.http.rate-limiter'],
true,
));
$container
->method('get')
->willReturnCallback(static fn(string $id) => match ($id) {
'phpmyfaq.configuration' => $configMock,
'phpmyfaq.http.rate-limiter' => $rateLimiter,
default => null,
});
$kernel = new Kernel(routingContext: 'api', debug: true);
$reflection = new ReflectionClass(Kernel::class);
$containerProp = $reflection->getProperty('container');
$containerProp->setValue($kernel, $container);
$routesProp = $reflection->getProperty('routes');
$routesProp->setValue($kernel, new RouteCollection());
$dispatcher = new EventDispatcher();
$registerMethod = $reflection->getMethod('registerEventListeners');
$registerMethod->invoke($kernel, $dispatcher);
$requestListeners = $dispatcher->getListeners('kernel.request');
$exceptionListeners = $dispatcher->getListeners('kernel.exception');
$this->assertCount(3, $requestListeners);
$this->assertCount(2, $exceptionListeners);
}
public function testRegisterEventListenersDoesNotAddApiRateLimiterForAdminApi(): void
{
$configMock = $this->createMock(Configuration::class);
$rateLimiter = new RateLimiter(storage: new InMemoryStorage());
$container = $this->createMock(ContainerBuilder::class);
$container
->method('has')
->willReturnCallback(static fn(string $id) => in_array(
$id,
['phpmyfaq.configuration', 'phpmyfaq.http.rate-limiter'],
true,
));
$container
->method('get')
->willReturnCallback(static fn(string $id) => match ($id) {
'phpmyfaq.configuration' => $configMock,
'phpmyfaq.http.rate-limiter' => $rateLimiter,
default => null,
});
$kernel = new Kernel(routingContext: 'admin-api', debug: true);
$reflection = new ReflectionClass(Kernel::class);
$containerProp = $reflection->getProperty('container');
$containerProp->setValue($kernel, $container);
$routesProp = $reflection->getProperty('routes');
$routesProp->setValue($kernel, new RouteCollection());
$dispatcher = new EventDispatcher();
$registerMethod = $reflection->getMethod('registerEventListeners');
$registerMethod->invoke($kernel, $dispatcher);
$requestListeners = $dispatcher->getListeners('kernel.request');
$this->assertCount(2, $requestListeners);
}
public function testRegisterEventListenersWithoutConfiguration(): void
{
$container = $this->createMock(ContainerBuilder::class);
$container->method('has')->willReturn(false);
$container->method('get')->willReturn(null);
$kernel = new Kernel(routingContext: 'public', debug: true);
$reflection = new ReflectionClass(Kernel::class);
$containerProp = $reflection->getProperty('container');
$containerProp->setValue($kernel, $container);
$routesProp = $reflection->getProperty('routes');
$routesProp->setValue($kernel, new RouteCollection());
$dispatcher = new EventDispatcher();
$registerMethod = $reflection->getMethod('registerEventListeners');
$registerMethod->invoke($kernel, $dispatcher);
$exceptionListeners = $dispatcher->getListeners('kernel.exception');
$this->assertCount(2, $exceptionListeners);
}
/**
* Creates a Kernel that can boot() using real code, with a real container
* from services.php but bypassing loadRoutes() failures by using debug mode.
*/
private function createKernelWithMockedBoot(string $routingContext = 'public'): TestableKernel
{
$kernel = new TestableKernel($routingContext, true);
$container = new ContainerBuilder();
$container->set('phpmyfaq.event_dispatcher', new EventDispatcher());
$kernel->setMockContainer($container);
return $kernel;
}
/**
* Creates a Kernel with pre-set internal state (as if boot() already ran).
*/
private function createPreBootedKernel(
string $routingContext = 'public',
bool $debug = true,
?HttpKernel $httpKernel = null,
): Kernel {
$container = $this->createMock(ContainerBuilder::class);
$dispatcher = new EventDispatcher();
$container
->method('get')
->willReturnCallback(static fn(string $id) => match ($id) {
'phpmyfaq.event_dispatcher' => $dispatcher,
default => null,
});
$container->method('has')->willReturn(false);
$routes = new RouteCollection();
$kernel = new Kernel($routingContext, $debug);
$reflection = new ReflectionClass(Kernel::class);
$containerProp = $reflection->getProperty('container');
$containerProp->setValue($kernel, $container);
$routesProp = $reflection->getProperty('routes');
$routesProp->setValue($kernel, $routes);
$httpKernelProp = $reflection->getProperty('httpKernel');
$httpKernelProp->setValue(
$kernel,
$httpKernel ?? new HttpKernel(
$dispatcher,
new ContainerControllerResolver($container),
new RequestStack(),
new ArgumentResolver(),
),
);
$bootedProp = $reflection->getProperty('booted');
$bootedProp->setValue($kernel, true);
return $kernel;
}
}
/**
* Testable Kernel subclass that uses a mock container to avoid DB/filesystem dependencies,
* while still exercising the real boot() flow logic (booted guard, property assignment).
*/
class TestableKernel extends Kernel
{
private ?ContainerBuilder $mockContainer = null;
public function setMockContainer(ContainerBuilder $container): void
{
$this->mockContainer = $container;
}
public function boot(): void
{
$reflection = new ReflectionClass(Kernel::class);
$bootedProp = $reflection->getProperty('booted');
if ($bootedProp->getValue($this)) {
return;
}
// Use mock container instead of real buildContainer()
$container = $this->mockContainer ?? new ContainerBuilder();
$container->set('kernel', $this);
$containerProp = $reflection->getProperty('container');
$containerProp->setValue($this, $container);
// Use empty routes to avoid needing Configuration::getConfigurationInstance()
$routesProp = $reflection->getProperty('routes');
$routesProp->setValue($this, new RouteCollection());
// Create the HttpKernel (uses the real createHttpKernel)
$createMethod = $reflection->getMethod('createHttpKernel');
$httpKernel = $createMethod->invoke($this);
$httpKernelProp = $reflection->getProperty('httpKernel');
$httpKernelProp->setValue($this, $httpKernel);
$bootedProp->setValue($this, true);
}
}