RouterTest::testRouterDispatch()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
/*
4
 * The MIT License
5
 *
6
 * Copyright 2016 Alejandro Peña Florentín ([email protected]).
7
 *
8
 * Permission is hereby granted, free of charge, to any person obtaining a copy
9
 * of this software and associated documentation files (the "Software"), to deal
10
 * in the Software without restriction, including without limitation the rights
11
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
 * copies of the Software, and to permit persons to whom the Software is
13
 * furnished to do so, subject to the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be included in
16
 * all copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
 * THE SOFTWARE.
25
 */
26
27
namespace Tight\Tests;
28
29
/**
30
 * Description of RouterTest
31
 *
32
 * @author Alejandro Peña Florentín ([email protected])
33
 */
34
class RouterTest extends \PHPUnit_Framework_TestCase
35
{
36
37
    private $router;
38
39
    /**
40
     * @covers Tight\Router::__construct
41
     */
42
    public function setUp() {
43
        $dirs = ["models","views","controllers"];
44
        $size = count($dirs);
45
        for ($index = 0; $index < $size; $index++) {
46
            if(!is_dir($dirs[$index])){
47
                mkdir($dirs[$index]);
48
            }
49
        }
50
        $this->router = new \Tight\Router("/");
51
        $this->router->get("/hello/", function() {
52
            return "Hello";
53
        });
54
        $this->router->get("/hello/:id", function($id) {
55
            return "Hello " . $id;
56
        });
57
        $this->router->post("/world/", function() {
58
            return "world";
59
        });
60
        $this->router->post("/world/:id", function($id) {
61
            return $id . " world";
62
        });
63
        $this->router->map(["get", "post"], "/map/", function() {
64
            return "map";
65
        });
66
        $this->router->update("/upd/", function() {
67
            return "upd";
68
        });
69
        $this->router->delete("/del/", function() {
70
            return "del";
71
        });
72
        $this->router->get("/middle/", function() {
73
            return "mid1 ";
74
        }, function() {
75
            return "mid2 ";
76
        }, function() {
77
            return "end";
78
        });
79
        $this->router->notFound(function() {
80
            return "Error 404: Page not found";
81
        });
82
    }
83
84
    public function tearDown() {
85
        
86
    }
87
88
    /**
89
     * @test
90
     * @covers \Tight\Router::dispatch
91
     * @covers \Tight\Router::getRoutes
92
     */
93
    public function testRouterDispatch() {
94
        $firstRoute = $this->router->getRoutes()[0];
95
        $actual = $this->router->dispatch("/hello/", "get");
96
        $expected = $firstRoute->dispatch();
97
        $this->assertEquals($actual, $expected);
98
    }
99
100
    /**
101
     * @test
102
     * @covers Tight\Router::notFound
103
     * @covers \Tight\Router::dispatchNotFound
104
     * @depends testRouterDispatch
105
     */
106
    public function testRouterNotFound() {
107
        $expected = $this->router->dispatch("/invalid/url/", "post");
108
        $this->assertEquals($expected, "Error 404: Page not found");
109
        return $expected;
110
    }
111
112
    /**
113
     * @test
114
     * @covers Tight\Router::get
115
     * @covers Tight\Router::__construct
116
     * @depends testRouterNotFound
117
     */
118
    public function testRouterGet($notFound) {
119
        $this->assertEquals($this->router->dispatch("/hello/", "get"), "Hello");
120
        $this->assertEquals($this->router->dispatch("/hello/", "post"), $notFound);
121
        $this->assertEquals($this->router->dispatch("/hello/world", "get"), "Hello world");
122
    }
123
124
    /**
125
     * @test
126
     * @covers Tight\Router::post
127
     * @depends testRouterNotFound
128
     */
129
    public function testRouterPost($notFound) {
130
        $this->assertEquals($this->router->dispatch("/world/", "post"), "world");
131
        $this->assertEquals($this->router->dispatch("/world/", "get"), $notFound);
132
        $this->assertEquals($this->router->dispatch("/world/hello", "post"), "hello world");
133
    }
134
135
    /**
136
     * @test
137
     * @covers Tight\Router::map
138
     * @covers Tight\Router::url
139
     * @depends testRouterNotFound
140
     */
141
    public function testRouterMap($notFound) {
142
        $this->assertEquals($this->router->dispatch("/map/", "post"), "map");
143
        $this->assertEquals($this->router->dispatch("/map/", "get"), "map");
144
        $this->assertEquals($this->router->dispatch("/map/", "options"), $notFound);
145
    }
146
147
    /**
148
     * @test
149
     * @covers Tight\Router::update
150
     * @depends testRouterNotFound
151
     */
152
    public function testRouteUpdate($notFound) {
153
        $this->assertEquals($this->router->dispatch("/upd/", "update"), "upd");
154
        $this->assertEquals($this->router->dispatch("/upd/", "get"), $notFound);
155
    }
156
157
    /**
158
     * @test
159
     * @covers Tight\Router::delete
160
     * @depends testRouterNotFound
161
     */
162
    public function testRouteDelete($notFound) {
163
        $this->assertEquals($this->router->dispatch("/del/", "delete"), "del");
164
        $this->assertEquals($this->router->dispatch("/del/", "post"), $notFound);
165
    }
166
167
    /**
168
     * @test
169
     * @covers Tight\Route::setMiddleware
170
     * @depends testRouterNotFound
171
     */
172
    public function testRouteMiddleware($notFound) {
173
        $this->assertEquals($this->router->dispatch("/middle/", "get"), "mid1 mid2 end");
174
        $this->assertEquals($this->router->dispatch("/middle/", "post"), $notFound);
175
    }
176
177
    /**
178
     * @test
179
     */
180
    public function testRouterGetRequestUrn() {
181
        $this->assertEquals($_SERVER['REQUEST_URI'], $this->router->getRequestUrn());
182
    }
183
184
}
185