Skip to content

Commit 93bfade

Browse files
Fix compatibility of Kernel as HttpCache instance (#57)
* Fix compatibility of Kernel as HttpCache instance * Add missing tests * Update typehint * Update Runner.php * Update src/roadrunner-symfony-nyholm/src/Runner.php Co-authored-by: Tobias Nyholm <[email protected]>
1 parent 8694594 commit 93bfade

File tree

5 files changed

+119
-2
lines changed

5 files changed

+119
-2
lines changed

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@
1010
}
1111
],
1212
"require": {
13+
"php": ">=7.2.5",
1314
"nyholm/psr7": "^1.4",
1415
"spiral/roadrunner": "^2.0",
16+
"symfony/dependency-injection": "^5.3 || ^6.0",
17+
"symfony/http-kernel": "^5.3 || ^6.0",
1518
"symfony/psr-http-message-bridge": "^2.1",
1619
"symfony/runtime": "^5.3 || ^6.0"
1720
},

src/Runner.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use Symfony\Component\HttpFoundation\Cookie;
1212
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
1313
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
14+
use Symfony\Component\HttpKernel\HttpCache\HttpCache;
15+
use Symfony\Component\HttpKernel\HttpKernelInterface;
1416
use Symfony\Component\HttpKernel\KernelInterface;
1517
use Symfony\Component\HttpKernel\TerminableInterface;
1618
use Symfony\Component\Runtime\RunnerInterface;
@@ -30,13 +32,24 @@ class Runner implements RunnerInterface
3032
*/
3133
private $sessionOptions;
3234

33-
public function __construct(KernelInterface $kernel, ?HttpFoundationFactoryInterface $httpFoundationFactory = null, ?HttpMessageFactoryInterface $httpMessageFactory = null)
35+
/**
36+
* @param HttpKernelInterface|KernelInterface $kernel
37+
*/
38+
public function __construct($kernel, ?HttpFoundationFactoryInterface $httpFoundationFactory = null, ?HttpMessageFactoryInterface $httpMessageFactory = null)
3439
{
3540
$this->kernel = $kernel;
3641
$this->psrFactory = new Psr7\Factory\Psr17Factory();
3742
$this->httpFoundationFactory = $httpFoundationFactory ?? new HttpFoundationFactory();
3843
$this->httpMessageFactory = $httpMessageFactory ?? new PsrHttpFactory($this->psrFactory, $this->psrFactory, $this->psrFactory, $this->psrFactory);
3944

45+
if ($kernel instanceof HttpCache) {
46+
$kernel = $kernel->getKernel();
47+
}
48+
49+
if (!$kernel instanceof KernelInterface) {
50+
throw new \InvalidArgumentException(sprintf('Expected argument of type "%s" or "%s", "%s" given.', KernelInterface::class, HttpCache::class, get_class($kernel)));
51+
}
52+
4053
$kernel->boot();
4154
$container = $kernel->getContainer();
4255
$this->sessionOptions = $container->getParameter('session.storage.options');

src/Runtime.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Runtime\RoadRunnerSymfonyNyholm;
44

5+
use Symfony\Component\HttpKernel\HttpCache\HttpCache;
56
use Symfony\Component\HttpKernel\KernelInterface;
67
use Symfony\Component\Runtime\RunnerInterface;
78
use Symfony\Component\Runtime\SymfonyRuntime;
@@ -15,7 +16,9 @@ class Runtime extends SymfonyRuntime
1516
{
1617
public function getRunner(?object $application): RunnerInterface
1718
{
18-
if ($application instanceof KernelInterface) {
19+
if ($application instanceof KernelInterface
20+
|| ($application instanceof HttpCache && $application->getKernel() instanceof KernelInterface)
21+
) {
1922
return new Runner($application);
2023
}
2124

tests/.gitignore

Whitespace-only changes.

tests/RuntimeTest.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
namespace Runtime\RoadRunnerSymfonyNyholm;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Symfony\Component\DependencyInjection\ContainerInterface;
7+
use Symfony\Component\HttpKernel\HttpCache\HttpCache;
8+
use Symfony\Component\HttpKernel\HttpKernelInterface;
9+
use Symfony\Component\HttpKernel\KernelInterface;
10+
use Symfony\Component\Runtime\Runner\Symfony\HttpKernelRunner;
11+
12+
/**
13+
* @author Alexander Schranz <[email protected]>
14+
*/
15+
class RuntimeTest extends TestCase
16+
{
17+
public function testGetRuntimeHttpKernel(): void
18+
{
19+
$kernel = $this->getMockBuilder(KernelInterface::class)
20+
->disableOriginalConstructor()
21+
->getMock();
22+
23+
$kernel->expects($this->once())
24+
->method('boot');
25+
26+
$container = $this->getMockBuilder(ContainerInterface::class)
27+
->disableOriginalConstructor()
28+
->getMock();
29+
30+
$container->expects($this->once())
31+
->method('getParameter')
32+
->with('session.storage.options')
33+
->will($this->returnValue([]));
34+
35+
$kernel->expects($this->once())
36+
->method('getContainer')
37+
->will($this->returnValue($container));
38+
39+
$kernel->expects($this->once())
40+
->method('shutdown');
41+
42+
$runtime = new Runtime();
43+
$runner = $runtime->getRunner($kernel);
44+
45+
$this->assertInstanceOf(Runner::class, $runner);
46+
}
47+
48+
public function testGetRuntimeHttpCache(): void
49+
{
50+
$httpCache = $this->getMockBuilder(HttpCache::class)
51+
->disableOriginalConstructor()
52+
->getMock();
53+
54+
$kernel = $this->getMockBuilder(KernelInterface::class)
55+
->disableOriginalConstructor()
56+
->getMock();
57+
58+
$httpCache->expects($this->atLeastOnce())
59+
->method('getKernel')
60+
->will($this->returnValue($kernel));
61+
62+
$kernel->expects($this->once())
63+
->method('boot');
64+
65+
$container = $this->getMockBuilder(ContainerInterface::class)
66+
->disableOriginalConstructor()
67+
->getMock();
68+
69+
$container->expects($this->once())
70+
->method('getParameter')
71+
->with('session.storage.options')
72+
->will($this->returnValue([]));
73+
74+
$kernel->expects($this->once())
75+
->method('getContainer')
76+
->will($this->returnValue($container));
77+
78+
$kernel->expects($this->once())
79+
->method('shutdown');
80+
81+
$runtime = new Runtime();
82+
$runner = $runtime->getRunner($httpCache);
83+
84+
$this->assertInstanceOf(Runner::class, $runner);
85+
}
86+
87+
public function testGetRuntimeHttpKernelInterface(): void
88+
{
89+
$kernel = $this->getMockBuilder(HttpKernelInterface::class)
90+
->disableOriginalConstructor()
91+
->getMock();
92+
93+
$runtime = new Runtime();
94+
$runner = $runtime->getRunner($kernel);
95+
96+
$this->assertInstanceOf(HttpKernelRunner::class, $runner);
97+
}
98+
}

0 commit comments

Comments
 (0)