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; |
12
|
|
|
|
13
|
|
|
use CoiSA\Http\Middleware\EchoBodyMiddleware; |
14
|
|
|
use CoiSA\Http\Middleware\RequestHandlerMiddleware; |
15
|
|
|
use PHPUnit\Framework\TestCase; |
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 RequestHandlerMiddlewareTest |
24
|
|
|
* |
25
|
|
|
* @package CoiSA\Http\Test |
26
|
|
|
*/ |
27
|
|
|
final class RequestHandlerMiddlewareTest extends TestCase |
28
|
|
|
{ |
29
|
|
|
/** @var EchoBodyMiddleware */ |
30
|
|
|
private $middleware; |
31
|
|
|
|
32
|
|
|
/** @var ObjectProphecy|RequestHandlerInterface */ |
33
|
|
|
private $requestHandler; |
34
|
|
|
|
35
|
|
|
/** @var ObjectProphecy|RequestHandlerInterface */ |
36
|
|
|
private $next; |
37
|
|
|
|
38
|
|
|
/** @var ObjectProphecy|ServerRequestInterface */ |
39
|
|
|
private $serverRequest; |
40
|
|
|
|
41
|
|
|
/** @var ObjectProphecy|ResponseInterface */ |
42
|
|
|
private $response; |
43
|
|
|
|
44
|
|
|
/** @var array */ |
45
|
|
|
private $execution = []; |
46
|
|
|
|
47
|
|
|
public function setUp(): void |
48
|
|
|
{ |
49
|
|
|
$this->requestHandler = $this->prophesize(RequestHandlerInterface::class); |
50
|
|
|
$this->next = $this->prophesize(RequestHandlerInterface::class); |
51
|
|
|
$this->serverRequest = $this->prophesize(ServerRequestInterface::class); |
52
|
|
|
$this->response = $this->prophesize(ResponseInterface::class); |
53
|
|
|
|
54
|
|
|
$this->middleware = new RequestHandlerMiddleware($this->requestHandler->reveal()); |
|
|
|
|
55
|
|
|
|
56
|
|
|
$testClass = $this; |
57
|
|
|
$callback = function () use ($testClass) { |
58
|
|
|
$testClass->execution[] = \spl_object_hash($this); |
59
|
|
|
|
60
|
|
|
return $testClass->response->reveal(); |
61
|
|
|
}; |
62
|
|
|
|
63
|
|
|
$this->requestHandler->handle($this->serverRequest->reveal())->will($callback); |
64
|
|
|
$this->next->handle($this->serverRequest->reveal())->will($callback); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function testImplementPsrInterfaces(): void |
68
|
|
|
{ |
69
|
|
|
$this->assertInstanceOf(RequestHandlerInterface::class, $this->middleware); |
70
|
|
|
$this->assertInstanceOf(MiddlewareInterface::class, $this->middleware); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testHandleReturnHandlerResponse(): void |
74
|
|
|
{ |
75
|
|
|
$serverRequest = $this->serverRequest->reveal(); |
76
|
|
|
$response = $this->middleware->handle($serverRequest); |
|
|
|
|
77
|
|
|
$requestHandler = $this->requestHandler->reveal(); |
78
|
|
|
$this->assertEquals($requestHandler->handle($serverRequest), $response); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function testProcessExecuteBothHandlersInOrder(): void |
82
|
|
|
{ |
83
|
|
|
$expected = [ |
84
|
|
|
\spl_object_hash($this->requestHandler), |
85
|
|
|
\spl_object_hash($this->next), |
86
|
|
|
]; |
87
|
|
|
$this->middleware->process($this->serverRequest->reveal(), $this->next->reveal()); |
88
|
|
|
$this->assertEquals($expected, $this->execution); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testProcessReturnResponseFromDependencyHandler(): void |
92
|
|
|
{ |
93
|
|
|
$serverRequest = $this->serverRequest->reveal(); |
94
|
|
|
$response = $this->middleware->process($serverRequest, $this->next->reveal()); |
95
|
|
|
$requestHandler = $this->requestHandler->reveal(); |
96
|
|
|
$this->assertEquals($requestHandler->handle($serverRequest), $response); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..