Completed
Push — master ( ee49f4...f8fced )
by Alejandro
02:23
created

RouterTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 155
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 12
c 3
b 0
f 1
lcom 1
cbo 2
dl 0
loc 155
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
B setUp() 0 34 1
A tearDown() 0 3 1
A testRouterDispatch() 0 6 1
A testRouterNotFound() 0 5 1
A testRouterGet() 0 5 1
A testRouterPost() 0 5 1
A testRouterMap() 0 5 1
A testRouteUpdate() 0 4 1
A testRouteDelete() 0 4 1
A testRouteMiddleware() 0 4 1
A testRouterGetRequestUrn() 0 3 1
A testRouterMvc() 0 4 1
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
    private $a = "a";
0 ignored issues
show
Unused Code introduced by
The property $a is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
Comprehensibility introduced by
Avoid variables with short names like $a. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
39
40
    /**
41
     * @covers Tight\Router::__construct
42
     */
43
    public function setUp() {
44
        $this->router = new \Tight\Router("/");
45
        $this->router->get("/hello/", function() {
46
            return "Hello";
47
        });
48
        $this->router->get("/hello/:id", function($id) {
49
            return "Hello " . $id;
50
        });
51
        $this->router->post("/world/", function() {
52
            return "world";
53
        });
54
        $this->router->post("/world/:id", function($id) {
55
            return $id . " world";
56
        });
57
        $this->router->map(["get", "post"], "/map/", function() {
58
            return "map";
59
        });
60
        $this->router->update("/upd/", function() {
61
            return "upd";
62
        });
63
        $this->router->delete("/del/", function() {
64
            return "del";
65
        });
66
        $this->router->get("/middle/", function() {
67
            return "mid1 ";
68
        }, function() {
69
            return "mid2 ";
70
        }, function() {
71
            return "end";
72
        });
73
        $this->router->notFound(function() {
74
            return "Error 404: Page not found";
75
        });
76
    }
77
78
    public function tearDown() {
79
        
80
    }
81
82
    /**
83
     * @test
84
     * @covers \Tight\Router::dispatch
85
     * @covers \Tight\Router::getRoutes
86
     */
87
    public function testRouterDispatch() {
88
        $firstRoute = $this->router->getRoutes()[0];
89
        $actual = $this->router->dispatch("/hello/", "get");
90
        $expected = $firstRoute->dispatch();
91
        $this->assertEquals($actual, $expected);
92
    }
93
94
    /**
95
     * @test
96
     * @covers Tight\Router::notFound
97
     * @covers \Tight\Router::dispatchNotFound
98
     * @depends testRouterDispatch
99
     */
100
    public function testRouterNotFound() {
101
        $expected = $this->router->dispatch("/invalid/url/", "post");
102
        $this->assertEquals($expected, "Error 404: Page not found");
103
        return $expected;
104
    }
105
106
    /**
107
     * @test
108
     * @covers Tight\Router::get
109
     * @covers Tight\Router::__construct
110
     * @depends testRouterNotFound
111
     */
112
    public function testRouterGet($notFound) {
113
        $this->assertEquals($this->router->dispatch("/hello/", "get"), "Hello");
114
        $this->assertEquals($this->router->dispatch("/hello/", "post"), $notFound);
115
        $this->assertEquals($this->router->dispatch("/hello/world", "get"), "Hello world");
116
    }
117
118
    /**
119
     * @test
120
     * @covers Tight\Router::post
121
     * @depends testRouterNotFound
122
     */
123
    public function testRouterPost($notFound) {
124
        $this->assertEquals($this->router->dispatch("/world/", "post"), "world");
125
        $this->assertEquals($this->router->dispatch("/world/", "get"), $notFound);
126
        $this->assertEquals($this->router->dispatch("/world/hello", "post"), "hello world");
127
    }
128
129
    /**
130
     * @test
131
     * @covers Tight\Router::map
132
     * @covers Tight\Router::url
133
     * @depends testRouterNotFound
134
     */
135
    public function testRouterMap($notFound) {
136
        $this->assertEquals($this->router->dispatch("/map/", "post"), "map");
137
        $this->assertEquals($this->router->dispatch("/map/", "get"), "map");
138
        $this->assertEquals($this->router->dispatch("/map/", "options"), $notFound);
139
    }
140
141
    /**
142
     * @test
143
     * @covers Tight\Router::update
144
     * @depends testRouterNotFound
145
     */
146
    public function testRouteUpdate($notFound) {
147
        $this->assertEquals($this->router->dispatch("/upd/", "update"), "upd");
148
        $this->assertEquals($this->router->dispatch("/upd/", "get"), $notFound);
149
    }
150
151
    /**
152
     * @test
153
     * @covers Tight\Router::delete
154
     * @depends testRouterNotFound
155
     */
156
    public function testRouteDelete($notFound) {
157
        $this->assertEquals($this->router->dispatch("/del/", "delete"), "del");
158
        $this->assertEquals($this->router->dispatch("/del/", "post"), $notFound);
159
    }
160
161
    /**
162
     * @test
163
     * @covers Tight\Route::setMiddleware
164
     * @depends testRouterNotFound
165
     */
166
    public function testRouteMiddleware($notFound) {
167
        $this->assertEquals($this->router->dispatch("/middle/", "get"), "mid1 mid2 end");
168
        $this->assertEquals($this->router->dispatch("/middle/", "post"), $notFound);
169
    }
170
171
    /**
172
     * @test
173
     */
174
    public function testRouterGetRequestUrn() {
1 ignored issue
show
Coding Style introduced by
testRouterGetRequestUrn uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
175
        $this->assertEquals($_SERVER['REQUEST_URI'], $this->router->getRequestUrn());
176
    }
177
178
    /**
179
     * @test
180
     * @covers \Tight\Router::runMvc
181
     * @covers \Tight\Router::registerClasses
182
     */
183
    public function testRouterMvc() {
184
        /* $this->router->runMvc();
0 ignored issues
show
Unused Code Comprehensibility introduced by
71% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
185
          $this->expectOutputString("Page not found"); */
186
    }
187
188
}
189