Completed
Push — master ( 6db92e...c5cf91 )
by Alex
02:09
created

testValidInvalidTypes()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 3
nop 0
dl 0
loc 13
rs 9.9666
c 0
b 0
f 0
1
<?php
2
namespace Mezon\Router\Tests;
3
4
class DynamicRoutesInvalidCasesUnitTest extends \PHPUnit\Framework\TestCase
5
{
6
7
    /**
8
     * Default setup
9
     *
10
     * {@inheritdoc}
11
     * @see \PHPUnit\Framework\TestCase::setUp()
12
     */
13
    public function setUp(): void
14
    {
15
        $_SERVER['REQUEST_METHOD'] = 'GET';
16
    }
17
18
    /**
19
     * Testing getParam for unexisting param
20
     */
21
    public function testGettingUnexistingParameter(): void
22
    {
23
        // setup
24
        $router = new \Mezon\Router\Router();
25
        $router->addRoute('/catalog/[i:foo]/', function () {
26
            // do nothing
27
        });
28
29
        $router->callRoute('/catalog/1/');
30
31
        $this->expectException(\Exception::class);
32
33
        // test body and assertions
34
        $router->getParam('unexisting');
35
    }
36
37
    /**
38
     * Testing exception throwing for unexisting request method
39
     */
40
    public function testExceptionForUnexistingRequestMethod(): void
41
    {
42
        // setup
43
        $_SERVER['REQUEST_METHOD'] = 'HEAD';
44
        $router = new \Mezon\Router\Router();
45
        $router->addRoute('/catalog/[i:foo]/', function () {
46
            // do nothing
47
        });
48
49
        // assertions
50
        $this->expectException(\Exception::class);
51
52
        // test body
53
        $router->callRoute('/catalog/1/');
54
    }
55
56
    /**
57
     * Testing invalid id list data types behaviour.
58
     */
59
    public function testInValidIdListParams(): void
60
    {
61
        // setup
62
        $router = new \Mezon\Router\Router();
63
        $router->addRoute('/catalog/[il:cat_id]/', [
64
            $this,
65
            function () {return 1;}
66
        ]);
67
68
        // assertion
69
        $this->expectException(\Exception::class);
70
        $this->expectExceptionMessage('The processor was not found for the route catalog/12345.');
71
72
        // test body
73
        $router->callRoute('/catalog/12345./');
74
    }
75
76
    /**
77
     * Testing dynamic routes for DELETE requests.
78
     */
79
    public function testDeleteRequestForUnExistingDynamicRoute(): void
80
    {
81
        $_SERVER['REQUEST_METHOD'] = 'DELETE';
82
83
        $exception = '';
84
        $router = new \Mezon\Router\Router();
85
        $router->addRoute('/catalog/[i:cat_id]', [
86
            $this,
87
            function () {return 1;}
88
        ]);
89
90
        try {
91
            $router->callRoute('/catalog/1025/');
92
        } catch (\Exception $e) {
93
            $exception = $e->getMessage();
94
        }
95
96
        $msg = "The processor was not found for the route catalog/1025";
97
98
        $this->assertNotFalse(strpos($exception, $msg));
99
    }
100
101
    /**
102
     * Testing dynamic routes for PUT requests.
103
     */
104
    public function testPutRequestForUnExistingDynamicRoute(): void
105
    {
106
        $_SERVER['REQUEST_METHOD'] = 'PUT';
107
108
        $exception = '';
109
        $router = new \Mezon\Router\Router();
110
        $router->addRoute('/catalog/[i:cat_id]', [
111
            $this,
112
            function () {return 1;}
113
        ]);
114
115
        try {
116
            $router->callRoute('/catalog/1024/');
117
        } catch (\Exception $e) {
118
            $exception = $e->getMessage();
119
        }
120
121
        $msg = "The processor was not found for the route catalog/1024";
122
123
        $this->assertNotFalse(strpos($exception, $msg));
124
    }
125
126
    /**
127
     * Testing dynamic routes for POST requests.
128
     */
129
    public function testPostRequestForUnExistingDynamicRoute(): void
130
    {
131
        $_SERVER['REQUEST_METHOD'] = 'POST';
132
133
        $exception = '';
134
        $router = new \Mezon\Router\Router();
135
        $router->addRoute('/catalog/[i:item_id]', [
136
            $this,
137
            function () {return 1;}
138
        ]);
139
140
        try {
141
            $router->callRoute('/catalog/1024/');
142
        } catch (\Exception $e) {
143
            $exception = $e->getMessage();
144
        }
145
146
        $msg = "The processor was not found for the route catalog/1024";
147
148
        $this->assertNotFalse(strpos($exception, $msg));
149
    }
150
151
    /**
152
     * Data provider for the testNotMatchingRoutes
153
     *
154
     * @return array testing data
155
     */
156
    public function invalidCasesDataProvider(): array
157
    {
158
        return [
159
            [
160
                '/catalog/[i:some_id]',
161
                '/catalog/1/a/'
162
            ],
163
            [
164
                '/existing/[i:bar]/',
165
                '/unexisting/1/'
166
            ]
167
        ];
168
    }
169
170
    /**
171
     * Testing that all invalid cases will be covered
172
     *
173
     * @dataProvider invalidCasesDataProvider
174
     */
175
    public function testNotMatchingRoutes(string $route, string $calling): void
176
    {
177
        // setup
178
        $router = new \Mezon\Router\Router();
179
        $router->addRoute($route, function () {
180
            // do nothing
181
        });
182
183
        // assertions
184
        $this->expectException(\Exception::class);
185
186
        // test body
187
        $router->callRoute($calling);
188
    }
189
190
    /**
191
     * Testing invalid data types behaviour.
192
     */
193
    public function testInvalidType(): void
194
    {
195
        $router = new \Mezon\Router\Router();
196
        $router->addRoute('/catalog/[unexisting-type:i]/item/', [
197
            $this,
198
            function () {return 1;}
199
        ]);
200
201
        try {
202
            $router->callRoute('/catalog/1024/item/');
203
            $this->assertFalse(true, 'Exception expected');
204
        } catch (\Exception $e) {
205
            $this->assertFalse(false, '');
206
        }
207
    }
208
209
    /**
210
     * Testing invalid data types behaviour.
211
     */
212
    public function testValidInvalidTypes(): void
213
    {
214
        $router = new \Mezon\Router\Router();
215
        $router->addRoute('/catalog/[i:cat_id]/item/[unexisting-type-trace:item_id]/', [
216
            $this,
217
            function () {return 1;}
218
        ]);
219
220
        try {
221
            $router->callRoute('/catalog/1024/item/2048/');
222
            $this->assertFalse(true, 'Exception expected');
223
        } catch (\Exception $e) {
224
            $this->assertFalse(false, '');
225
        }
226
    }
227
}
228