Passed
Push — master ( a4ddd3...82bd79 )
by Alex
06:44
created

testMultipleMiddlewaresInOrderAndOmitMalformed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 74
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 34
c 1
b 1
f 0
dl 0
loc 74
rs 9.376
cc 1
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
declare(strict_types = 1);
3
namespace Mezon\Router\Tests\Base;
4
5
use PHPUnit\Framework\TestCase;
6
7
/**
8
 *
9
 * @psalm-suppress PropertyNotSetInConstructor
10
 */
11
abstract class MiddlewareTestClass extends BaseRouterUnitTestClass
12
{
13
14
    /**
15
     * Default setup
16
     *
17
     * {@inheritdoc}
18
     * @see TestCase::setUp()
19
     */
20
    public function setUp(): void
21
    {
22
        $_SERVER['REQUEST_METHOD'] = 'GET';
23
    }
24
25
    /**
26
     * Testing middleware
27
     */
28
    public function testMiddleware(): void
29
    {
30
        // setup
31
        $route = '/route-with-middleware/';
32
        $router = $this->getRouter();
33
        $router->registerMiddleware('*', function () {
34
            return [
35
                1,
36
                2
37
            ];
38
        });
39
        $router->addRoute($route, function (int $i, int $j) {
40
            return [
41
                $i,
42
                $j
43
            ];
44
        });
45
        $router->registerMiddleware($route, function (int $i, int $j) {
46
            return [
47
                $i,
48
                $j
49
            ];
50
        });
51
52
        // test body
53
        /** @var array{0: int, 1: int} $result */
54
        $result = $router->callRoute($route);
55
56
        // assertions
57
        $this->assertEquals(1, $result[0]);
58
        $this->assertEquals(2, $result[1]);
59
    }
60
61
    /**
62
     * Testing multiple middlewares
63
     */
64
    public function testMultipleMiddlewaresInOrderAndOmitMalformed(): void
65
    {
66
        // setup
67
        $route = '/route/[i:id]';
68
        $router = $this->getRouter();
69
70
        $router->addRoute($route, function (string $route, array $parameters): array {
71
            return $parameters;
72
        });
73
74
        $router->registerMiddleware($route, /**
75
         *
76
         * @psalm-param string $route route
77
         * @psalm-param array{id: int} $parameters parameters
78
         */
79
        function (string $route, array $parameters) {
80
            $parameters['_second'] = $parameters['id'];
81
            $parameters['id'] += 9;
82
83
            return [
84
                $route,
85
                $parameters
86
            ];
87
        });
88
89
        // This middleware is broken, don't parse the result
90
        $router->registerMiddleware($route, function (string $route, array $parameters) {
91
            $parameters['_dont_set_this'] = true;
92
93
            return null;
94
        });
95
96
        $router->registerMiddleware($route, /**
97
         *
98
         * @psalm-param string $route
99
         *            route
100
         * @psalm-param array{id: int} $parameters parameters
101
         */
102
        function (string $route, array $parameters) {
103
            $parameters['_third'] = $parameters['id'];
104
            $parameters['id'] *= 2;
105
106
            return [
107
                $route,
108
                $parameters
109
            ];
110
        });
111
112
        $router->registerMiddleware('*', /**
113
         *
114
         * @psalm-param string $route
115
         *            route
116
         * @psalm-param array{id: int} $parameters parameters
117
         */
118
        function (string $route, array $parameters) {
119
            $parameters['_first'] = $parameters['id'];
120
            $parameters['id'] *= 2;
121
122
            return [
123
                $route,
124
                $parameters
125
            ];
126
        });
127
128
        // test body
129
        /** @var array<string, mixed> $result */
130
        $result = $router->callRoute('/route/1');
131
132
        // assertions
133
        $this->assertEquals(22, $result['id']);
134
        $this->assertEquals(1, $result['_first']);
135
        $this->assertEquals(2, $result['_second']);
136
        $this->assertEquals(11, $result['_third']);
137
        $this->assertTrue(empty($result['_dont_set_this']));
138
    }
139
}
140