Passed
Push — master ( 9013bb...125cf1 )
by Kirill
03:56
created

HttpTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Tests\Http;
13
14
use PHPUnit\Framework\TestCase;
15
use Psr\Http\Message\ResponseInterface;
16
use Psr\Http\Message\ServerRequestInterface;
17
use Psr\Http\Server\MiddlewareInterface;
18
use Psr\Http\Server\RequestHandlerInterface;
19
use Spiral\Core\Container;
20
use Spiral\Http\CallableHandler;
21
use Spiral\Http\Config\HttpConfig;
22
use Spiral\Http\Exception\HttpException;
23
use Spiral\Http\Http;
24
use Spiral\Http\Pipeline;
25
use Spiral\Tests\Http\Diactoros\ResponseFactory;
26
use Laminas\Diactoros\ServerRequest;
27
28
class HttpTest extends TestCase
29
{
30
    private $container;
31
32
    public function setUp(): void
33
    {
34
        $this->container = new Container();
35
    }
36
37
    public function testGetPipeline(): void
38
    {
39
        $core = $this->getCore();
40
        $this->assertInstanceOf(Pipeline::class, $core->getPipeline());
41
    }
42
43
    public function testRunHandler(): void
44
    {
45
        $core = $this->getCore();
46
47
        $core->setHandler(function () {
48
            return 'hello world';
49
        });
50
51
        $response = $core->handle(new ServerRequest());
52
        $this->assertSame('hello world', (string)$response->getBody());
53
    }
54
55
    public function testNoHandler(): void
56
    {
57
        $this->expectException(HttpException::class);
58
59
        $core = $this->getCore();
60
61
        $response = $core->handle(new ServerRequest());
62
        $this->assertSame('hello world', (string)$response->getBody());
63
    }
64
65
    public function testBadHandler(): void
66
    {
67
        $this->expectException(HttpException::class);
68
69
        $core = $this->getCore();
70
        $core->setHandler('hi');
71
    }
72
73
    public function testHandlerInterface(): void
74
    {
75
        $core = $this->getCore();
76
        $core->setHandler(new CallableHandler(function () {
77
            return 'hello world';
78
        }, new ResponseFactory(new HttpConfig(['headers' => []]))));
79
80
        $response = $core->handle(new ServerRequest());
81
        $this->assertSame('hello world', (string)$response->getBody());
82
    }
83
84
    public function testDefaultHeaders(): void
85
    {
86
        $core = $this->getCore();
87
88
        $core->setHandler(function ($req, $resp) {
89
            return $resp->withAddedHeader('hello', 'value');
90
        });
91
92
        $response = $core->handle(new ServerRequest());
93
        $this->assertSame(['text/html; charset=UTF-8'], $response->getHeader('Content-Type'));
94
        $this->assertSame(['value'], $response->getHeader('hello'));
95
    }
96
97
    public function testOutput(): void
98
    {
99
        $core = $this->getCore();
100
101
        $core->setHandler(function ($req, $resp) {
102
            echo 'hello!';
103
104
            return $resp->withAddedHeader('hello', 'value');
105
        });
106
107
        $response = $core->handle(new ServerRequest());
108
        $this->assertSame(['text/html; charset=UTF-8'], $response->getHeader('Content-Type'));
109
        $this->assertSame(['value'], $response->getHeader('hello'));
110
        $this->assertSame('hello!', (string)$response->getBody());
111
    }
112
113
    public function testOutputAndWrite(): void
114
    {
115
        $core = $this->getCore();
116
117
        $core->setHandler(function ($req, $resp) {
118
            echo 'hello!';
119
            $resp->getBody()->write('world ');
120
121
            return $resp->withAddedHeader('hello', 'value');
122
        });
123
124
        $response = $core->handle(new ServerRequest());
125
        $this->assertSame(['text/html; charset=UTF-8'], $response->getHeader('Content-Type'));
126
        $this->assertSame(['value'], $response->getHeader('hello'));
127
        $this->assertSame('world hello!', (string)$response->getBody());
128
    }
129
130
    public function testNestedOutput(): void
131
    {
132
        $core = $this->getCore();
133
134
        $core->setHandler(function () {
135
            ob_start();
136
            ob_start();
137
            echo 'hello!';
138
            ob_start();
139
            ob_start();
140
141
            return 'world ';
142
        });
143
144
        $this->assertSame(1, ob_get_level());
145
        $response = $core->handle(new ServerRequest());
146
        $this->assertSame(['text/html; charset=UTF-8'], $response->getHeader('Content-Type'));
147
        $this->assertSame('world hello!', (string)$response->getBody());
148
        $this->assertSame(1, ob_get_level());
149
    }
150
151
    public function testJson(): void
152
    {
153
        $core = $this->getCore();
154
155
        $core->setHandler(function () {
156
            return [
157
                'status'  => 404,
158
                'message' => 'not found',
159
            ];
160
        });
161
162
        $response = $core->handle(new ServerRequest());
163
        $this->assertSame(404, $response->getStatusCode());
164
        $this->assertSame(['application/json'], $response->getHeader('Content-Type'));
165
    }
166
167
    public function testJsonSerializable(): void
168
    {
169
        $core = $this->getCore();
170
171
        $core->setHandler(function () {
172
            return new Json([
173
                'status'  => 404,
174
                'message' => 'not found',
175
            ]);
176
        });
177
178
        $response = $core->handle(new ServerRequest());
179
        $this->assertSame(404, $response->getStatusCode());
180
        $this->assertSame(['application/json'], $response->getHeader('Content-Type'));
181
    }
182
183
    public function testMiddleware(): void
184
    {
185
        $core = $this->getCore([HeaderMiddleware::class]);
186
187
        $core->setHandler(function () {
188
            return 'hello?';
189
        });
190
191
        $response = $core->handle(new ServerRequest());
192
        $this->assertSame(['text/html; charset=UTF-8'], $response->getHeader('Content-Type'));
193
        $this->assertSame(['Value*'], $response->getHeader('header'));
194
        $this->assertSame('hello?', (string)$response->getBody());
195
    }
196
197
    public function testMiddlewareTrait(): void
198
    {
199
        $core = $this->getCore();
200
201
        $core->getPipeline()->pushMiddleware(new Header2Middleware());
202
        $core->getPipeline()->riseMiddleware(new HeaderMiddleware());
203
204
        $core->setHandler(function () {
205
            return 'hello?';
206
        });
207
208
        $response = $core->handle(new ServerRequest());
209
        $this->assertSame(['text/html; charset=UTF-8'], $response->getHeader('Content-Type'));
210
        $this->assertSame(['Value+', 'Value*'], $response->getHeader('header'));
211
        $this->assertSame('hello?', (string)$response->getBody());
212
    }
213
214
    public function testMiddlewareTraitReversed(): void
215
    {
216
        $core = $this->getCore();
217
218
        $core->getPipeline()->pushMiddleware(new HeaderMiddleware());
219
        $core->getPipeline()->riseMiddleware(new Header2Middleware());
220
221
        $core->setHandler(function () {
222
            return 'hello?';
223
        });
224
225
        $response = $core->handle(new ServerRequest());
226
        $this->assertSame(['text/html; charset=UTF-8'], $response->getHeader('Content-Type'));
227
        $this->assertSame(['Value*', 'Value+'], $response->getHeader('header'));
228
        $this->assertSame('hello?', (string)$response->getBody());
229
    }
230
231
    public function testScope(): void
232
    {
233
        $core = $this->getCore();
234
235
        $core->setHandler(function () {
236
            $this->assertTrue($this->container->has(ServerRequestInterface::class));
237
238
            return 'OK';
239
        });
240
241
        $response = $core->handle(new ServerRequest());
242
        $this->assertSame('OK', (string)$response->getBody());
243
    }
244
245
    public function testPassException(): void
246
    {
247
        $this->expectException(\RuntimeException::class);
248
249
        $core = $this->getCore();
250
251
        $core->setHandler(function ($req, $resp): void {
0 ignored issues
show
Unused Code introduced by
The parameter $resp is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

251
        $core->setHandler(function ($req, /** @scrutinizer ignore-unused */ $resp): void {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $req is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

251
        $core->setHandler(function (/** @scrutinizer ignore-unused */ $req, $resp): void {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
252
            throw new \RuntimeException('error');
253
        });
254
255
        $response = $core->handle(new ServerRequest());
256
        $this->assertSame(['text/html;charset=UTF-8'], $response->getHeader('Content-Type'));
257
        $this->assertSame(['value'], $response->getHeader('hello'));
258
    }
259
260
    protected function getCore(array $middleware = []): Http
261
    {
262
        $config = new HttpConfig([
263
            'basePath'   => '/',
264
            'headers'    => [
265
                'Content-Type' => 'text/html; charset=UTF-8',
266
            ],
267
            'middleware' => $middleware,
268
        ]);
269
270
        return new Http(
271
            $config,
272
            new Pipeline($this->container),
273
            new ResponseFactory($config),
274
            $this->container
275
        );
276
    }
277
}
278