1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ChadicusTest\Psr\Http\ServerMiddleware; |
4
|
|
|
|
5
|
|
|
use Chadicus\Psr\Http\ServerMiddleware\ClientExtractorInterface; |
6
|
|
|
use Chadicus\Psr\Http\ServerMiddleware\LimitedClientInterface; |
7
|
|
|
use Chadicus\Psr\Http\ServerMiddleware\LimitedResponseFactoryInterface; |
8
|
|
|
use Chadicus\Psr\Http\ServerMiddleware\RateLimitMiddleware; |
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
use Zend\Diactoros\ServerRequest; |
11
|
|
|
use Zend\Diactoros\Response; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @coversDefaultClass \Chadicus\Psr\Http\ServerMiddleware\RateLimitMiddleware |
15
|
|
|
* @covers ::__construct |
16
|
|
|
*/ |
17
|
|
|
final class RateLimitMiddlewareTest extends TestCase |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @test |
21
|
|
|
* @covers ::__invoke |
22
|
|
|
* |
23
|
|
|
* @return void |
24
|
|
|
*/ |
25
|
|
View Code Duplication |
public function invoke() |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
$middleware = $this->getMiddleware( |
28
|
|
|
$this->getClientExtractor($this->getLimitedClient(true)), |
29
|
|
|
$this->getResponseFactory(new Response('php://memory', 429)) |
30
|
|
|
); |
31
|
|
|
|
32
|
|
|
$nextMiddleware = function ($request, $response) { |
33
|
|
|
return $response; |
34
|
|
|
}; |
35
|
|
|
|
36
|
|
|
$response = new Response(); |
37
|
|
|
|
38
|
|
|
$this->assertSame( |
39
|
|
|
$response, |
40
|
|
|
$middleware(new ServerRequest(), $response, $nextMiddleware) |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @test |
46
|
|
|
* @covers ::__invoke |
47
|
|
|
* |
48
|
|
|
* @return void |
49
|
|
|
*/ |
50
|
|
View Code Duplication |
public function invokeLimitExceeded() |
|
|
|
|
51
|
|
|
{ |
52
|
|
|
$limitResponse = new Response('php://memory', 429); |
53
|
|
|
|
54
|
|
|
$middleware = $this->getMiddleware( |
55
|
|
|
$this->getClientExtractor($this->getLimitedClient(false)), |
56
|
|
|
$this->getResponseFactory($limitResponse) |
57
|
|
|
); |
58
|
|
|
|
59
|
|
|
$nextMiddleware = function ($request, $response) { |
|
|
|
|
60
|
|
|
throw new \Exception('$next was call but should not have been.'); |
61
|
|
|
}; |
62
|
|
|
|
63
|
|
|
$this->assertSame( |
64
|
|
|
$limitResponse, |
65
|
|
|
$middleware(new ServerRequest(), new Response(), $nextMiddleware) |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
private function getLimitedClient(bool $canMakeRequest) : LimitedClientInterface |
70
|
|
|
{ |
71
|
|
|
$mock = $this->getMockBuilder('\\Chadicus\\Psr\\Http\\ServerMiddleware\\LimitedClientInterface')->getMock(); |
72
|
|
|
$mock->method('canMakeRequest')->willReturn($canMakeRequest); |
73
|
|
|
return $mock; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
private function getClientExtractor(LimitedClientInterface $client) : ClientExtractorInterface |
77
|
|
|
{ |
78
|
|
|
$mock = $this->getMockBuilder('\\Chadicus\\Psr\\Http\\ServerMiddleware\\ClientExtractorInterface')->getMock(); |
79
|
|
|
$mock->method('extract')->willReturn($client); |
80
|
|
|
return $mock; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
private function getResponseFactory(Response $response) : LimitedResponseFactoryInterface |
84
|
|
|
{ |
85
|
|
|
$mock = $this->getMockBuilder( |
86
|
|
|
'\\Chadicus\\Psr\\Http\\ServerMiddleware\\LimitedResponseFactoryInterface' |
87
|
|
|
)->getMock(); |
88
|
|
|
$mock->method('createResponse')->willReturn($response); |
89
|
|
|
return $mock; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
private function getMiddleware( |
93
|
|
|
ClientExtractorInterface $clientExtractor, |
94
|
|
|
LimitedResponseFactoryInterface $responseFactory |
95
|
|
|
) : RateLimitMiddleware { |
96
|
|
|
return new RateLimitMiddleware($clientExtractor, $responseFactory); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.