Passed
Push — master ( 4cfdc5...37c7d0 )
by Alex
07:00
created

testRequestMethodsConcurrency()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 26
c 1
b 0
f 1
dl 0
loc 32
rs 9.504
cc 1
nc 1
nop 0
1
<?php
2
namespace Mezon\Router\Tests\Base;
3
4
use Mezon\Router\RouterInterface;
5
6
/**
7
 *
8
 * @psalm-suppress PropertyNotSetInConstructor
9
 */
10
abstract class RouterUnitTestClass extends BaseRouterUnitTestClass
11
{
12
13
    /**
14
     * Method creates router object
15
     *
16
     * @return RouterInterface
17
     */
18
    protected abstract function getRouter(): RouterInterface;
19
20
    /**
21
     * Function sets $_SERVER['REQUEST_METHOD']
22
     *
23
     * @param string $requestMethod
24
     *            request method
25
     */
26
    public static function setRequestMethod(string $requestMethod): void
27
    {
28
        $_SERVER['REQUEST_METHOD'] = $requestMethod;
29
    }
30
31
    /**
32
     * Default setup
33
     *
34
     * {@inheritdoc}
35
     * @see \PHPUnit\Framework\TestCase::setUp()
36
     */
37
    public function setUp(): void
38
    {
39
        RouterUnitTestUtils::setRequestMethod('GET');
40
    }
41
42
    /**
43
     * Function simply returns string.
44
     */
45
    public function helloWorldOutput(): string
46
    {
47
        return 'Hello world!';
48
    }
49
50
    /**
51
     * Data provider for the test
52
     *
53
     * @return array
54
     */
55
    public function differentHandlersDataProvider(): array
56
    {
57
        return [
58
            # 0, class method
59
            [
60
                '/one-component-class-method/',
61
                [
62
                    $this,
63
                    'helloWorldOutput'
64
                ],
65
                'Hello world!'
66
            ],
67
            # 1, lambda
68
            [
69
                '/one-component-lambda/',
70
                function () {
71
                    return 'Hello lambda!';
72
                },
73
                'Hello lambda!'
74
            ]
75
        ];
76
    }
77
78
    /**
79
     * Testing router with different handlers
80
     *
81
     * @param string $url
82
     *            url
83
     * @param callable $handler
84
     *            handler
85
     * @param string $expectedResult
86
     *            expected result
87
     * @dataProvider differentHandlersDataProvider
88
     */
89
    public function testDifferentHandlers(string $url, callable $handler, string $expectedResult): void
90
    {
91
        // setup
92
        $router = $this->getRouter();
93
        $router->addRoute($url, $handler);
94
95
        // test body
96
        $content = $router->callRoute($url);
97
98
        // assertions
99
        $this->assertEquals($expectedResult, $content);
100
    }
101
102
    /**
103
     * Testing case when all processors exist
104
     */
105
    public function testRequestMethodsConcurrency(): void
106
    {
107
        $route = '/catalog/';
108
        $router = $this->getRouter();
109
        $router->addRoute($route, function () {
110
            return 'POST';
111
        }, 'POST');
112
        $router->addRoute($route, function () {
113
            return 'GET';
114
        }, 'GET');
115
        $router->addRoute($route, function () {
116
            return 'PUT';
117
        }, 'PUT');
118
        $router->addRoute($route, function () {
119
            return RouterUnitTestUtils::DELETE_REQUEST_METHOD;
120
        }, RouterUnitTestUtils::DELETE_REQUEST_METHOD);
121
122
        RouterUnitTestUtils::setRequestMethod('POST');
123
        $result = $router->callRoute($route);
124
        $this->assertEquals($result, 'POST');
125
126
        RouterUnitTestUtils::setRequestMethod('GET');
127
        $result = $router->callRoute($route);
128
        $this->assertEquals($result, 'GET');
129
130
        RouterUnitTestUtils::setRequestMethod('PUT');
131
        $result = $router->callRoute($route);
132
        $this->assertEquals($result, 'PUT');
133
134
        RouterUnitTestUtils::setRequestMethod(RouterUnitTestUtils::DELETE_REQUEST_METHOD);
135
        $result = $router->callRoute($route);
136
        $this->assertEquals($result, RouterUnitTestUtils::DELETE_REQUEST_METHOD);
137
    }
138
139
    /**
140
     * Method increments assertion count
141
     */
142
    protected function errorHandler(): void
143
    {
144
        $this->assertTrue(true);
145
    }
146
147
    /**
148
     * Test validate custom error handlers.
149
     */
150
    public function testSetErrorHandler(): void
151
    {
152
        // setup
153
        $router = $this->getRouter();
154
        $router->setNoProcessorFoundErrorHandler(function () {
155
            $this->errorHandler();
156
        });
157
158
        // test body and assertions
159
        RouterUnitTestUtils::setRequestMethod('POST');
160
        $router->callRoute('/unexisting/');
161
    }
162
163
    /**
164
     * Testing addGetMethod method
165
     */
166
    public function testAddGetRoute(): void
167
    {
168
        // setup
169
        $router = $this->getRouter();
170
        $router->addGetRoute('/route/', $this, 'helloWorldOutput');
171
172
        // test body
173
        $result = $router->callRoute('/route/');
174
175
        // assertions
176
        $this->assertEquals('Hello world!', $result);
177
    }
178
179
    /**
180
     * Testing addPostMethod method
181
     */
182
    public function testAddPostRoute(): void
183
    {
184
        // setup
185
        $router = $this->getRouter();
186
        $router->addPostRoute('/route/', $this, 'helloWorldOutput');
187
        $_SERVER['REQUEST_METHOD'] = 'POST';
188
189
        // test body
190
        $result = $router->callRoute('/route/');
191
192
        // assertions
193
        $this->assertEquals('Hello world!', $result);
194
    }
195
}
196