RouterUnitTestClass::setRequestMethod()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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