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
|
|
|
namespace CoiSA\Http\Test\Middleware; |
12
|
|
|
|
13
|
|
|
use CoiSA\Http\Middleware\EchoBodyMiddleware; |
14
|
|
|
use PHPUnit\Framework\TestCase; |
15
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
16
|
|
|
use Psr\Http\Message\ResponseInterface; |
17
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
18
|
|
|
use Psr\Http\Message\StreamInterface; |
19
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
20
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class EchoBodyMiddlewareTest |
24
|
|
|
* |
25
|
|
|
* @package CoiSA\Http\Test |
26
|
|
|
*/ |
27
|
|
|
final class EchoBodyMiddlewareTest extends TestCase |
28
|
|
|
{ |
29
|
|
|
/** @var EchoBodyMiddleware */ |
30
|
|
|
private $middleware; |
31
|
|
|
|
32
|
|
|
/** @var ObjectProphecy|RequestHandlerInterface */ |
33
|
|
|
private $requestHandler; |
34
|
|
|
|
35
|
|
|
/** @var ObjectProphecy|ServerRequestInterface */ |
36
|
|
|
private $serverRequest; |
37
|
|
|
|
38
|
|
|
/** @var ObjectProphecy|ResponseInterface */ |
39
|
|
|
private $response; |
40
|
|
|
|
41
|
|
|
/** @var string */ |
42
|
|
|
private $content; |
43
|
|
|
|
44
|
|
|
/** @var ObjectProphecy|StreamInterface */ |
45
|
|
|
private $body; |
46
|
|
|
|
47
|
|
|
public function setUp(): void |
48
|
|
|
{ |
49
|
|
|
$this->middleware = new EchoBodyMiddleware(); |
50
|
|
|
$this->requestHandler = $this->prophesize(RequestHandlerInterface::class); |
51
|
|
|
$this->serverRequest = $this->prophesize(ServerRequestInterface::class); |
52
|
|
|
$this->response = $this->prophesize(ResponseInterface::class); |
53
|
|
|
$this->body = $this->prophesize(StreamInterface::class); |
54
|
|
|
|
55
|
|
|
$this->requestHandler->handle($this->serverRequest->reveal())->will([$this->response, 'reveal']); |
56
|
|
|
|
57
|
|
|
$this->content = \uniqid('content', true); |
58
|
|
|
$this->response->getBody()->will([$this->body, 'reveal']); |
59
|
|
|
$this->body->getContents()->willReturn($this->content); |
60
|
|
|
|
61
|
|
|
\ob_start(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function tearDown(): void |
65
|
|
|
{ |
66
|
|
|
while (\ob_get_level() > 1) { |
67
|
|
|
\ob_end_clean(); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testImplementsPsrServerMiddleware(): void |
72
|
|
|
{ |
73
|
|
|
$this->assertInstanceOf(MiddlewareInterface::class, $this->middleware); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function testProcessReturnResponse(): void |
77
|
|
|
{ |
78
|
|
|
$response = $this->middleware->process($this->serverRequest->reveal(), $this->requestHandler->reveal()); |
79
|
|
|
$this->assertInstanceOf(ResponseInterface::class, $response); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function testProcessEchoResponseBodyContent(): void |
83
|
|
|
{ |
84
|
|
|
$this->middleware->process($this->serverRequest->reveal(), $this->requestHandler->reveal()); |
85
|
|
|
$content = \ob_get_clean(); |
86
|
|
|
$this->assertEquals($this->content, $content); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|