RateLimitMiddlewareTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 82
Duplicated Lines 43.9 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 36
loc 82
c 0
b 0
f 0
wmc 6
lcom 1
cbo 5
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A invoke() 18 18 1
A invokeLimitExceeded() 18 18 1
A getLimitedClient() 0 6 1
A getClientExtractor() 0 6 1
A getResponseFactory() 0 8 1
A getMiddleware() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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) {
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $response is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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