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\Middleware\RequestMethodMiddleware; |
14
|
|
|
use Fig\Http\Message\RequestMethodInterface; |
15
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class RequestMethodMiddlewareTest |
19
|
|
|
* |
20
|
|
|
* @package CoiSA\Http\Test\Handler |
21
|
|
|
*/ |
22
|
|
|
final class RequestMethodMiddlewareTest extends AbstractMiddlewareTest |
23
|
|
|
{ |
24
|
|
|
public function setUp(): void |
25
|
|
|
{ |
26
|
|
|
parent::setUp(); |
27
|
|
|
|
28
|
|
|
$this->middleware = new RequestMethodMiddleware( |
29
|
|
|
RequestMethodInterface::METHOD_GET, |
30
|
|
|
$this->handler->reveal() |
31
|
|
|
); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function testInvalidMethodThrowException() |
35
|
|
|
{ |
36
|
|
|
$this->expectException(\UnexpectedValueException::class); |
37
|
|
|
|
38
|
|
|
new RequestMethodMiddleware( |
39
|
|
|
uniqid('test', false), |
40
|
|
|
$this->handler->reveal() |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function provideMethods() |
45
|
|
|
{ |
46
|
|
|
$reflection = new \ReflectionClass(RequestMethodInterface::class); |
47
|
|
|
$allowedMethods = $reflection->getConstants(); |
48
|
|
|
|
49
|
|
|
return \array_chunk($allowedMethods, 1); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @dataProvider provideMethods |
54
|
|
|
* |
55
|
|
|
* @param string $method |
56
|
|
|
*/ |
57
|
|
|
public function testGivenMethodReturnHandlerResponse(string $method) |
58
|
|
|
{ |
59
|
|
|
$this->serverRequest->getMethod()->willReturn($method); |
60
|
|
|
|
61
|
|
|
$middleware = new RequestMethodMiddleware( |
62
|
|
|
$method, |
63
|
|
|
$this->handler->reveal() |
64
|
|
|
); |
65
|
|
|
|
66
|
|
|
$response = $middleware->process($this->serverRequest->reveal(), $this->nextHandler->reveal()); |
67
|
|
|
|
68
|
|
|
$this->assertSame($this->response->reveal(), $response); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @dataProvider provideMethods |
73
|
|
|
* |
74
|
|
|
* @param string $method |
75
|
|
|
*/ |
76
|
|
|
public function testDiffMethodReturnNextResponse(string $method) |
77
|
|
|
{ |
78
|
|
|
$this->serverRequest->getMethod()->willReturn( |
79
|
|
|
$method === RequestMethodInterface::METHOD_GET ? |
80
|
|
|
RequestMethodInterface::METHOD_POST : |
81
|
|
|
RequestMethodInterface::METHOD_GET |
82
|
|
|
); |
83
|
|
|
|
84
|
|
|
$middleware = new RequestMethodMiddleware( |
85
|
|
|
$method, |
86
|
|
|
$this->handler->reveal() |
87
|
|
|
); |
88
|
|
|
|
89
|
|
|
$response = $middleware->process($this->serverRequest->reveal(), $this->nextHandler->reveal()); |
90
|
|
|
|
91
|
|
|
$this->assertSame($this->nextResponse->reveal(), $response); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|