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 CoiSA\Http\Middleware\RequestHandlerMiddleware; |
13
|
|
|
use PHPUnit\Framework\TestCase; |
14
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
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
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class RequestHandlerMiddlewareTest |
22
|
|
|
*/ |
23
|
|
|
final class RequestHandlerMiddlewareTest extends TestCase |
24
|
|
|
{ |
25
|
|
|
/** @var EchoBodyMiddleware */ |
26
|
|
|
private $middleware; |
27
|
|
|
|
28
|
|
|
/** @var ObjectProphecy|RequestHandlerInterface */ |
29
|
|
|
private $requestHandler; |
30
|
|
|
|
31
|
|
|
/** @var ObjectProphecy|RequestHandlerInterface */ |
32
|
|
|
private $next; |
33
|
|
|
|
34
|
|
|
/** @var ObjectProphecy|ServerRequestInterface */ |
35
|
|
|
private $serverRequest; |
36
|
|
|
|
37
|
|
|
/** @var ObjectProphecy|ResponseInterface */ |
38
|
|
|
private $response; |
39
|
|
|
|
40
|
|
|
/** @var array */ |
41
|
|
|
private $execution = []; |
42
|
|
|
|
43
|
|
|
public function setUp(): void |
44
|
|
|
{ |
45
|
|
|
$this->requestHandler = $this->prophesize(RequestHandlerInterface::class); |
46
|
|
|
$this->next = $this->prophesize(RequestHandlerInterface::class); |
47
|
|
|
$this->serverRequest = $this->prophesize(ServerRequestInterface::class); |
48
|
|
|
$this->response = $this->prophesize(ResponseInterface::class); |
49
|
|
|
|
50
|
|
|
$this->middleware = new RequestHandlerMiddleware($this->requestHandler->reveal()); |
|
|
|
|
51
|
|
|
|
52
|
|
|
$testClass = $this; |
53
|
|
|
$callback = function () use ($testClass) { |
54
|
|
|
$testClass->execution[] = spl_object_hash($this); |
55
|
|
|
return $testClass->response->reveal(); |
56
|
|
|
}; |
57
|
|
|
|
58
|
|
|
$this->requestHandler->handle($this->serverRequest->reveal())->will($callback); |
59
|
|
|
$this->next->handle($this->serverRequest->reveal())->will($callback); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function testImplementPsrInterfaces(): void |
63
|
|
|
{ |
64
|
|
|
$this->assertInstanceOf(RequestHandlerInterface::class, $this->middleware); |
65
|
|
|
$this->assertInstanceOf(MiddlewareInterface::class, $this->middleware); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function testHandleReturnHandlerResponse() |
69
|
|
|
{ |
70
|
|
|
$serverRequest = $this->serverRequest->reveal(); |
71
|
|
|
$response = $this->middleware->handle($serverRequest); |
|
|
|
|
72
|
|
|
$requestHandler = $this->requestHandler->reveal(); |
73
|
|
|
$this->assertEquals($requestHandler->handle($serverRequest), $response); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function testProcessExecuteBothHandlersInOrder() |
77
|
|
|
{ |
78
|
|
|
$expected = [ |
79
|
|
|
spl_object_hash($this->requestHandler), |
80
|
|
|
spl_object_hash($this->next), |
81
|
|
|
]; |
82
|
|
|
$this->middleware->process($this->serverRequest->reveal(), $this->next->reveal()); |
83
|
|
|
$this->assertEquals($expected, $this->execution); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function testProcessReturnResponseFromDependencyHandler() |
87
|
|
|
{ |
88
|
|
|
$serverRequest = $this->serverRequest->reveal(); |
89
|
|
|
$response = $this->middleware->process($serverRequest, $this->next->reveal()); |
90
|
|
|
$requestHandler = $this->requestHandler->reveal(); |
91
|
|
|
$this->assertEquals($requestHandler->handle($serverRequest), $response); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
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..