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\Handler; |
12
|
|
|
|
13
|
|
|
use CoiSA\Http\Handler\MiddlewareHandler; |
14
|
|
|
use PHPUnit\Framework\TestCase; |
15
|
|
|
use Prophecy\Argument; |
16
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
17
|
|
|
use Psr\Http\Message\ResponseInterface; |
18
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
19
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
20
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class MiddlewareHandlerTest |
24
|
|
|
* |
25
|
|
|
* @package CoiSA\Http\Test\Handler |
26
|
|
|
*/ |
27
|
|
|
final class MiddlewareHandlerTest extends TestCase |
28
|
|
|
{ |
29
|
|
|
/** @var MiddlewareInterface|ObjectProphecy */ |
30
|
|
|
private $middleware; |
31
|
|
|
|
32
|
|
|
/** @var ObjectProphecy|RequestHandlerInterface */ |
33
|
|
|
private $handler; |
34
|
|
|
|
35
|
|
|
/** @var MiddlewareHandler */ |
36
|
|
|
private $middlewareHandler; |
37
|
|
|
|
38
|
|
|
/** @var ObjectProphecy|ServerRequestInterface */ |
39
|
|
|
private $serverRequest; |
40
|
|
|
|
41
|
|
|
/** @var ObjectProphecy|ResponseInterface */ |
42
|
|
|
private $response; |
43
|
|
|
|
44
|
|
|
public function setUp(): void |
45
|
|
|
{ |
46
|
|
|
$this->middleware = $this->prophesize(MiddlewareInterface::class); |
47
|
|
|
$this->handler = $this->prophesize(RequestHandlerInterface::class); |
48
|
|
|
$this->serverRequest = $this->prophesize(ServerRequestInterface::class); |
49
|
|
|
$this->response = $this->prophesize(ResponseInterface::class); |
50
|
|
|
$this->middlewareHandler = new MiddlewareHandler($this->middleware->reveal(), $this->handler->reveal()); |
51
|
|
|
|
52
|
|
|
$this->middleware->process($this->serverRequest->reveal(), Argument::type(RequestHandlerInterface::class))->will([$this->response, 'reveal']); |
53
|
|
|
$this->handler->handle($this->serverRequest->reveal())->will([$this->response, 'reveal']); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function testImplementPsrInterfaces(): void |
57
|
|
|
{ |
58
|
|
|
$this->assertInstanceOf(RequestHandlerInterface::class, $this->middlewareHandler); |
59
|
|
|
$this->assertInstanceOf(MiddlewareInterface::class, $this->middlewareHandler); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function testHandleReturnHandlerResponse(): void |
63
|
|
|
{ |
64
|
|
|
$response = $this->middlewareHandler->handle($this->serverRequest->reveal()); |
65
|
|
|
$this->assertSame($response, $this->response->reveal()); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function testHandleExecuteMiddlewareBeforeHandler(): void |
69
|
|
|
{ |
70
|
|
|
$this->markTestIncomplete(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testProcessReturnMiddlewareHandlerResponse(): void |
74
|
|
|
{ |
75
|
|
|
$next = $this->prophesize(RequestHandlerInterface::class); |
76
|
|
|
$nextResponse = $this->prophesize(ResponseInterface::class); |
77
|
|
|
|
78
|
|
|
$next->handle($this->serverRequest->reveal())->will([$nextResponse, 'reveal']); |
79
|
|
|
|
80
|
|
|
$response = $this->middlewareHandler->process($this->serverRequest->reveal(), $next->reveal()); |
81
|
|
|
$this->assertSame($this->response->reveal(), $response); |
82
|
|
|
$this->assertNotSame($nextResponse, $response); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|