Completed
Push — master ( 41a706...46045d )
by Alex
20s queued 12s
created

testMultipleMiddlewaresInOrderAndOmitMalformed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 38
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 18
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 38
rs 9.6666
1
<?php
2
namespace Mezon\Router\Tests;
3
4
use PHPUnit\Framework\TestCase;
5
use Mezon\Router\Router;
6
7
class MiddlewareUnitTest extends TestCase
8
{
9
10
    /**
11
     * Default setup
12
     *
13
     * {@inheritdoc}
14
     * @see \PHPUnit\Framework\TestCase::setUp()
15
     */
16
    public function setUp(): void
17
    {
18
        $_SERVER['REQUEST_METHOD'] = 'GET';
19
    }
20
21
    /**
22
     * Testing middleware
23
     */
24
    public function testMiddleware(): void
25
    {
26
        // setup
27
        $route = '/route-with-middleware/';
28
        $router = new Router();
29
        $router->addRoute($route, function (int $i, int $j) {
30
            return [
31
                $i,
32
                $j
33
            ];
34
        });
35
        $router->registerMiddleware($route, function () {
36
            return [
37
                1,
38
                2
39
            ];
40
        });
41
42
        // test body
43
        $result = $router->callRoute($route);
44
45
        // assertions
46
        $this->assertEquals(1, $result[0]);
47
        $this->assertEquals(2, $result[1]);
48
    }
49
50
    public function testMultipleMiddlewaresInOrderAndOmitMalformed(): void
51
    {
52
        // setup
53
        $route = '/route/[i:id]';
54
        $router = new Router();
55
56
        $router->addRoute($route, function (string $route, $parameters) {
57
            return $parameters;
58
        });
59
60
        $router->registerMiddleware($route, function (string $route, array $parameters) {
61
            $parameters['id'] += 9;
62
63
            return [
64
                $route,
65
                $parameters
66
            ];
67
        });
68
69
        // This middleware is broken, don't parse the result
70
        $router->registerMiddleware($route, function (string $route, array $parameters) {
0 ignored issues
show
Unused Code introduced by
The parameter $route is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

70
        $router->registerMiddleware($route, function (/** @scrutinizer ignore-unused */ string $route, array $parameters) {

This check looks for 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 $parameters is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

70
        $router->registerMiddleware($route, function (string $route, /** @scrutinizer ignore-unused */ array $parameters) {

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

Loading history...
71
            return null;
72
        });
73
74
        $router->registerMiddleware($route, function (string $route, array $parameters) {
75
            $parameters['id'] *= 2;
76
77
            return [
78
                $route,
79
                $parameters
80
            ];
81
        });
82
83
        // test body
84
        $result = $router->callRoute('/route/1');
85
86
        // assertions
87
        $this->assertEquals(20, $result['id']);
88
    }
89
}
90