Completed
Push — master ( 15f476...52f8b5 )
by Alex
07:26
created

DynamicRoutesInvalidCasesUnitTest::setUp()   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
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
        $exception = '';
62
        $router = new \Mezon\Router\Router();
63
        $router->addRoute('/catalog/[il:cat_id]/', [
64
            $this,
65
            'helloWorldOutput'
66
        ]);
67
68
        try {
69
            $router->callRoute('/catalog/12345./');
70
        } catch (\Exception $e) {
71
            $exception = $e->getMessage();
72
        }
73
74
        $msg = "The processor was not found for the route catalog/12345.";
75
76
        $this->assertNotFalse(strpos($exception, $msg));
77
    }
78
79
    /**
80
     * Testing dynamic routes for DELETE requests.
81
     */
82
    public function testDeleteRequestForUnExistingDynamicRoute(): void
83
    {
84
        $_SERVER['REQUEST_METHOD'] = 'DELETE';
85
86
        $exception = '';
87
        $router = new \Mezon\Router\Router();
88
        $router->addRoute('/catalog/[i:cat_id]', [
89
            $this,
90
            'helloWorldOutput'
91
        ]);
92
93
        try {
94
            $router->callRoute('/catalog/1025/');
95
        } catch (\Exception $e) {
96
            $exception = $e->getMessage();
97
        }
98
99
        $msg = "The processor was not found for the route catalog/1025";
100
101
        $this->assertNotFalse(strpos($exception, $msg));
102
    }
103
104
    /**
105
     * Testing dynamic routes for PUT requests.
106
     */
107
    public function testPutRequestForUnExistingDynamicRoute(): void
108
    {
109
        $_SERVER['REQUEST_METHOD'] = 'PUT';
110
111
        $exception = '';
112
        $router = new \Mezon\Router\Router();
113
        $router->addRoute('/catalog/[i:cat_id]', [
114
            $this,
115
            'helloWorldOutput'
116
        ]);
117
118
        try {
119
            $router->callRoute('/catalog/1024/');
120
        } catch (\Exception $e) {
121
            $exception = $e->getMessage();
122
        }
123
124
        $msg = "The processor was not found for the route catalog/1024";
125
126
        $this->assertNotFalse(strpos($exception, $msg));
127
    }
128
129
    /**
130
     * Testing dynamic routes for POST requests.
131
     */
132
    public function testPostRequestForUnExistingDynamicRoute(): void
133
    {
134
        $_SERVER['REQUEST_METHOD'] = 'POST';
135
136
        $exception = '';
137
        $router = new \Mezon\Router\Router();
138
        $router->addRoute('/catalog/[i:item_id]', [
139
            $this,
140
            'helloWorldOutput'
141
        ]);
142
143
        try {
144
            $router->callRoute('/catalog/1024/');
145
        } catch (\Exception $e) {
146
            $exception = $e->getMessage();
147
        }
148
149
        $msg = "The processor was not found for the route catalog/1024";
150
151
        $this->assertNotFalse(strpos($exception, $msg));
152
    }
153
154
    /**
155
     * Testing that
156
     */
157
    public function testNotMatchingRoutes(): void
158
    {
159
        // setup
160
        $_SERVER['REQUEST_METHOD'] = 'GET';
161
        $router = new \Mezon\Router\Router();
162
        $router->addRoute('/catalog/[i:some_id]', [
163
            $this,
164
            'helloWorldOutput'
165
        ]);
166
167
        // assertions
168
        $this->expectException(\Exception::class);
169
170
        // test body
171
        $router->callRoute('/catalog/1/a/');
172
    }
173
}
174