Passed
Push — master ( f3dde3...f14a68 )
by Alex
01:32
created

RouterUnitTest::testDifferentHandlers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 3
dl 0
loc 11
rs 10
c 0
b 0
f 0
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
     * Testing action #1.
43
     */
44
    public function actionA1(): string
45
    {
46
        return 'action #1';
47
    }
48
49
    /**
50
     * Testing action #2.
51
     */
52
    public function actionA2(): string
53
    {
54
        return 'action #2';
55
    }
56
57
    public function actionDoubleWord(): string
58
    {
59
        return 'action double word';
60
    }
61
62
    /**
63
     * Data provider for the test
64
     *
65
     * @return array
66
     */
67
    public function differentHandlersDataProvider(): array
68
    {
69
        return [
70
            # 0, class method
71
            [
72
                '/one-component-class-method/',
73
                [
74
                    $this,
75
                    'helloWorldOutput'
76
                ],
77
                'Hello world!'
78
            ],
79
            # 1, lambda
80
            [
81
                '/one-component-lambda/',
82
                function () {
83
                    return 'Hello lambda!';
84
                },
85
                'Hello lambda!'
86
            ],
87
        ];
88
    }
89
90
    /**
91
     * Testing router with different handlers
92
     *
93
     * @dataProvider differentHandlersDataProvider
94
     */
95
    public function testDifferentHandlers(string $url, $handler, string $expectedResult): void
96
    {
97
        // setup
98
        $router = new Router();
99
        $router->addRoute($url, $handler);
100
101
        // test body
102
        $content = $router->callRoute($url);
103
104
        // assertions
105
        $this->assertEquals($expectedResult, $content);
106
    }
107
108
    /**
109
     * Data provider for the test testClassAction
110
     *
111
     * @return array test data
112
     */
113
    public function classActionDataProvider(): array
114
    {
115
        $testData = [];
116
117
        foreach ([
118
            'GET',
119
            'POST'
120
        ] as $requestMethod) {
121
            $testData[] = [
122
                $requestMethod,
123
                '/a1/',
124
                'action #1'
125
            ];
126
127
            $testData[] = [
128
                $requestMethod,
129
                '/a2/',
130
                'action #2'
131
            ];
132
133
            $testData[] = [
134
                $requestMethod,
135
                '/double-word/',
136
                'action double word'
137
            ];
138
        }
139
140
        return $testData;
141
    }
142
143
    /**
144
     * Method tests class actions
145
     *
146
     * @param string $requestMethod
147
     *            request method
148
     * @param string $route
149
     *            requesting route
150
     * @param string $expectedResult
151
     *            expected result of route processing
152
     * @dataProvider classActionDataProvider
153
     */
154
    public function testClassAction(string $requestMethod, string $route, string $expectedResult): void
155
    {
156
        RouterUnitTest::setRequestMethod($requestMethod);
157
158
        $router = new \Mezon\Router\Router();
159
        $router->fetchActions($this);
160
        $result = $router->callRoute($route);
161
        $this->assertEquals($expectedResult, $result);
162
    }
163
164
    /**
165
     * Testing case when all processors exist
166
     */
167
    public function testRequestMethodsConcurrency(): void
168
    {
169
        $route = '/catalog/';
170
        $router = new \Mezon\Router\Router();
171
        $router->addRoute($route, function () {
172
            return 'POST';
173
        }, 'POST');
174
        $router->addRoute($route, function () {
175
            return 'GET';
176
        }, 'GET');
177
        $router->addRoute($route, function () {
178
            return 'PUT';
179
        }, 'PUT');
180
        $router->addRoute($route, function () {
181
            return RouterUnitTest::DELETE_REQUEST_METHOD;
182
        }, RouterUnitTest::DELETE_REQUEST_METHOD);
183
184
        RouterUnitTest::setRequestMethod('POST');
185
        $result = $router->callRoute($route);
186
        $this->assertEquals($result, 'POST');
187
188
        RouterUnitTest::setRequestMethod('GET');
189
        $result = $router->callRoute($route);
190
        $this->assertEquals($result, 'GET');
191
192
        RouterUnitTest::setRequestMethod('PUT');
193
        $result = $router->callRoute($route);
194
        $this->assertEquals($result, 'PUT');
195
196
        RouterUnitTest::setRequestMethod(RouterUnitTest::DELETE_REQUEST_METHOD);
197
        $result = $router->callRoute($route);
198
        $this->assertEquals($result, RouterUnitTest::DELETE_REQUEST_METHOD);
199
    }
200
201
    /**
202
     * Data provider
203
     *
204
     * @return array
205
     */
206
    public function clearMethodTestDataProvider(): array
207
    {
208
        $result = [];
209
210
        foreach (\Mezon\Router\Router::getListOfSupportedRequestMethods() as $method) {
211
            $result[] = [
212
                $method
213
            ];
214
        }
215
216
        return $result;
217
    }
218
219
    /**
220
     * Testing 'clear' method
221
     *
222
     * @param string $method
223
     *            request method
224
     * @dataProvider clearMethodTestDataProvider
225
     */
226
    public function testClearMethod(string $method): void
227
    {
228
        $router = new \Mezon\Router\Router();
229
        $router->addRoute('/route-to-clear/', function () use ($method) {
230
            return $method;
231
        }, $method);
232
        $router->clear();
233
234
        try {
235
            RouterUnitTest::setRequestMethod($method);
236
            $router->callRoute('/route-to-clear/');
237
            $flag = 'not cleared';
238
        } catch (\Exception $e) {
239
            $flag = 'cleared';
240
        }
241
        $this->assertEquals($flag, 'cleared', 'Data was not cleared');
242
    }
243
244
    /**
245
     * Method increments assertion count
246
     */
247
    protected function errorHandler(): void
248
    {
249
        $this->addToAssertionCount(1);
250
    }
251
252
    /**
253
     * Test validate custom error handlers.
254
     */
255
    public function testSetErrorHandler(): void
256
    {
257
        // setup
258
        $router = new \Mezon\Router\Router();
259
        $router->setNoProcessorFoundErrorHandler(function () {
260
            $this->errorHandler();
261
        });
262
263
        // test body and assertions
264
        RouterUnitTest::setRequestMethod('POST');
265
        $router->callRoute('/unexisting/');
266
    }
267
}
268