This repository was archived by the owner on Jul 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathStackedHttpKernelTest.php
156 lines (127 loc) · 4.74 KB
/
StackedHttpKernelTest.php
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
<?php
namespace Stack;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\TerminableInterface;
class StackedHttpKernelTest extends TestCase
{
/** @test */
public function handleShouldDelegateToApp()
{
$app = $this->getHttpKernelMock(new Response('ok'));
$kernel = new StackedHttpKernel($app, array($app));
$request = Request::create('/');
$response = $kernel->handle($request);
$this->assertSame('ok', $response->getContent());
}
/** @test */
public function handleShouldStillDelegateToAppWithMiddlewares()
{
$app = $this->getHttpKernelMock(new Response('ok'));
$bar = $this->getHttpKernelMock(new Response('bar'));
$foo = $this->getHttpKernelMock(new Response('foo'));
$kernel = new StackedHttpKernel($app, array($foo, $bar, $app));
$request = Request::create('/');
$response = $kernel->handle($request);
$this->assertSame('ok', $response->getContent());
}
/** @test */
public function terminateShouldDelegateToMiddlewares()
{
$first = new TerminableKernelSpy();
$second = new TerminableKernelSpy($first);
$third = new KernelSpy($second);
$fourth = new TerminableKernelSpy($third);
$fifth = new TerminableKernelSpy($fourth);
$kernel = new StackedHttpKernel($fifth, $middlewares = array($fifth, $fourth, $third, $second, $first));
$request = Request::create('/');
$response = $kernel->handle($request);
$kernel->terminate($request, $response);
$this->assertTerminablesCalledOnce($middlewares);
}
private function assertTerminablesCalledOnce(array $middlewares)
{
foreach ($middlewares as $kernel) {
if ($kernel instanceof TerminableInterface) {
$this->assertEquals(1, $kernel->terminateCallCount(), "Terminate was called {$kernel->terminateCallCount()} times");
}
}
}
private function getHttpKernelMock(Response $response)
{
$app = $this->createMock(HttpKernelInterface::class);
$app->expects($this->any())
->method('handle')
->with($this->isInstanceOf('Symfony\Component\HttpFoundation\Request'))
->will($this->returnValue($response));
return $app;
}
private function getTerminableMock(Response $response = null)
{
$app = $this->getMock('Stack\TerminableHttpKernel');
if ($response) {
$app->expects($this->any())
->method('handle')
->with($this->isInstanceOf('Symfony\Component\HttpFoundation\Request'))
->will($this->returnValue($response));
}
$app->expects($this->once())
->method('terminate')
->with(
$this->isInstanceOf('Symfony\Component\HttpFoundation\Request'),
$this->isInstanceOf('Symfony\Component\HttpFoundation\Response')
);
return $app;
}
private function getDelegatingTerminableMock(TerminableInterface $next)
{
$app = $this->getMock('Stack\TerminableHttpKernel');
$app->expects($this->once())
->method('terminate')
->with(
$this->isInstanceOf('Symfony\Component\HttpFoundation\Request'),
$this->isInstanceOf('Symfony\Component\HttpFoundation\Response')
)
->will($this->returnCallback(function ($request, $response) use ($next) {
$next->terminate($request, $response);
}));
return $app;
}
}
class KernelSpy implements HttpKernelInterface
{
private $handleCallCount = 0;
public function __construct(HttpKernelInterface $kernel = null)
{
$this->kernel = $kernel;
}
public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true): Response
{
$this->handleCallCount++;
if ($this->kernel) {
return $this->kernel->handle($request, $type, $catch);
}
return new Response('OK');
}
public function handleCallCount()
{
return $this->handleCallCount;
}
}
class TerminableKernelSpy extends KernelSpy implements TerminableInterface
{
private $terminateCallCount = 0;
public function terminate(Request $request, Response $response)
{
$this->terminateCallCount++;
if ($this->kernel && $this->kernel instanceof TerminableInterface) {
return $this->kernel->terminate($request, $response);
}
}
public function terminateCallCount()
{
return $this->terminateCallCount;
}
}