Passed
Push — master ( 17d331...42ece1 )
by Alex
02:01
created

RouterUnitTest::actionA2()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
namespace Mezon\Router\Tests;
3
4
use Mezon\Router\Router;
5
6
class RouterUnitTest extends \PHPUnit\Framework\TestCase
7
{
8
9
    const DELETE_REQUEST_METHOD = 'DELETE';
10
11
    /**
12
     * Function sets $_SERVER['REQUEST_METHOD']
13
     *
14
     * @param string $requestMethod
15
     *            request method
16
     */
17
    public static function setRequestMethod(string $requestMethod): void
18
    {
19
        $_SERVER['REQUEST_METHOD'] = $requestMethod;
20
    }
21
22
    /**
23
     * Default setup
24
     *
25
     * {@inheritdoc}
26
     * @see \PHPUnit\Framework\TestCase::setUp()
27
     */
28
    public function setUp(): void
29
    {
30
        RouterUnitTest::setRequestMethod('GET');
31
    }
32
33
    /**
34
     * Function simply returns string.
35
     */
36
    public function helloWorldOutput(): string
37
    {
38
        return 'Hello world!';
39
    }
40
41
    /**
42
     * Data provider for the test
43
     *
44
     * @return array
45
     */
46
    public function differentHandlersDataProvider(): array
47
    {
48
        return [
49
            # 0, class method
50
            [
51
                '/one-component-class-method/',
52
                [
53
                    $this,
54
                    'helloWorldOutput'
55
                ],
56
                'Hello world!'
57
            ],
58
            # 1, lambda
59
            [
60
                '/one-component-lambda/',
61
                function () {
62
                    return 'Hello lambda!';
63
                },
64
                'Hello lambda!'
65
            ],
66
        ];
67
    }
68
69
    /**
70
     * Testing router with different handlers
71
     *
72
     * @dataProvider differentHandlersDataProvider
73
     */
74
    public function testDifferentHandlers(string $url, $handler, string $expectedResult): void
75
    {
76
        // setup
77
        $router = new Router();
78
        $router->addRoute($url, $handler);
79
80
        // test body
81
        $content = $router->callRoute($url);
82
83
        // assertions
84
        $this->assertEquals($expectedResult, $content);
85
    }
86
87
    /**
88
     * Testing case when all processors exist
89
     */
90
    public function testRequestMethodsConcurrency(): void
91
    {
92
        $route = '/catalog/';
93
        $router = new \Mezon\Router\Router();
94
        $router->addRoute($route, function () {
95
            return 'POST';
96
        }, 'POST');
97
        $router->addRoute($route, function () {
98
            return 'GET';
99
        }, 'GET');
100
        $router->addRoute($route, function () {
101
            return 'PUT';
102
        }, 'PUT');
103
        $router->addRoute($route, function () {
104
            return RouterUnitTest::DELETE_REQUEST_METHOD;
105
        }, RouterUnitTest::DELETE_REQUEST_METHOD);
106
107
        RouterUnitTest::setRequestMethod('POST');
108
        $result = $router->callRoute($route);
109
        $this->assertEquals($result, 'POST');
110
111
        RouterUnitTest::setRequestMethod('GET');
112
        $result = $router->callRoute($route);
113
        $this->assertEquals($result, 'GET');
114
115
        RouterUnitTest::setRequestMethod('PUT');
116
        $result = $router->callRoute($route);
117
        $this->assertEquals($result, 'PUT');
118
119
        RouterUnitTest::setRequestMethod(RouterUnitTest::DELETE_REQUEST_METHOD);
120
        $result = $router->callRoute($route);
121
        $this->assertEquals($result, RouterUnitTest::DELETE_REQUEST_METHOD);
122
    }
123
124
    /**
125
     * Method increments assertion count
126
     */
127
    protected function errorHandler(): void
128
    {
129
        $this->addToAssertionCount(1);
130
    }
131
132
    /**
133
     * Test validate custom error handlers.
134
     */
135
    public function testSetErrorHandler(): void
136
    {
137
        // setup
138
        $router = new \Mezon\Router\Router();
139
        $router->setNoProcessorFoundErrorHandler(function () {
140
            $this->errorHandler();
141
        });
142
143
        // test body and assertions
144
        RouterUnitTest::setRequestMethod('POST');
145
        $router->callRoute('/unexisting/');
146
    }
147
}
148