1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/* |
3
|
|
|
* This file is part of coisa/http. |
4
|
|
|
* |
5
|
|
|
* (c) Felipe Sayão Lobato Abreu <[email protected]> |
6
|
|
|
* |
7
|
|
|
* This source file is subject to the license that is bundled |
8
|
|
|
* with this source code in the file LICENSE. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
use CoiSA\Http\Middleware\EchoBodyMiddleware; |
12
|
|
|
use PHPUnit\Framework\TestCase; |
13
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
14
|
|
|
use Psr\Http\Message\ResponseInterface; |
15
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
16
|
|
|
use Psr\Http\Message\StreamInterface; |
17
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
18
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class EchoBodyMiddlewareTest |
22
|
|
|
*/ |
23
|
|
|
final class EchoBodyMiddlewareTest extends TestCase |
24
|
|
|
{ |
25
|
|
|
/** @var EchoBodyMiddleware */ |
26
|
|
|
private $middleware; |
27
|
|
|
|
28
|
|
|
/** @var RequestHandlerInterface|ObjectProphecy */ |
29
|
|
|
private $requestHandler; |
30
|
|
|
|
31
|
|
|
/** @var ServerRequestInterface|ObjectProphecy */ |
32
|
|
|
private $serverRequest; |
33
|
|
|
|
34
|
|
|
/** @var ResponseInterface|ObjectProphecy */ |
35
|
|
|
private $response; |
36
|
|
|
|
37
|
|
|
/** @var string */ |
38
|
|
|
private $content; |
39
|
|
|
|
40
|
|
|
/** @var StreamInterface|ObjectProphecy */ |
41
|
|
|
private $body; |
42
|
|
|
|
43
|
|
|
public function setUp(): void |
44
|
|
|
{ |
45
|
|
|
$this->middleware = new EchoBodyMiddleware(); |
46
|
|
|
$this->requestHandler = $this->prophesize(RequestHandlerInterface::class); |
47
|
|
|
$this->serverRequest = $this->prophesize(ServerRequestInterface::class); |
48
|
|
|
$this->response = $this->prophesize(ResponseInterface::class); |
49
|
|
|
$this->body = $this->prophesize(StreamInterface::class); |
50
|
|
|
|
51
|
|
|
$this->requestHandler->handle($this->serverRequest->reveal())->will([$this->response, 'reveal']); |
52
|
|
|
|
53
|
|
|
$this->content = uniqid('content', true); |
54
|
|
|
$this->response->getBody()->will([$this->body, 'reveal']); |
55
|
|
|
$this->body->getContents()->willReturn($this->content); |
56
|
|
|
|
57
|
|
|
ob_start(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function tearDown(): void |
61
|
|
|
{ |
62
|
|
|
while (ob_get_level() > 1) { |
63
|
|
|
ob_end_clean(); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function testImplementsPsrServerMiddleware() |
68
|
|
|
{ |
69
|
|
|
$this->assertInstanceOf(MiddlewareInterface::class, $this->middleware); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testProcessReturnResponse() |
73
|
|
|
{ |
74
|
|
|
$response = $this->middleware->process($this->serverRequest->reveal(), $this->requestHandler->reveal()); |
75
|
|
|
$this->assertInstanceOf(ResponseInterface::class, $response); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function testProcessEchoResponseBodyContent() |
79
|
|
|
{ |
80
|
|
|
$this->middleware->process($this->serverRequest->reveal(), $this->requestHandler->reveal()); |
81
|
|
|
$content = ob_get_clean(); |
82
|
|
|
$this->assertEquals($this->content, $content); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|