RouterTest   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 1
dl 0
loc 133
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A setup() 0 3 1
A testGetRequest() 0 4 1
A testPostRequest() 0 4 1
A testAnyRequest() 0 7 1
A testPutRequest() 0 4 1
A testDeleteRequest() 0 4 1
A testRouteBasedController() 0 9 1
A testGETRoutesWithNamedArgs() 0 13 1
A testPOSToutesWithNamedArgs() 0 13 1
A testPUToutesWithNamedArgs() 0 13 1
A testNamedArgsPassedViaFunctionParamsMatch() 0 19 1
A testCalableRouteEvaluatesCallableArguments() 0 13 1
1
<?php
2
3
namespace Torpedo\Test;
4
use \Torpedo\Routing;
5
6
class RouterTest extends \PHPUnit_Framework_TestCase {
7
	protected $server = [
8
		'SERVER_PROTOCOL' => '1.0',
9
		'REQUEST_METHOD' => 'GET',
10
		'REQUEST_URI' => '/blog',
11
		'REQUEST_URI_PATH' => '/index.php',
12
		'HTTPS' => FALSE,
13
		'HTTP_MY_HEADER' => 'my value'
14
	];
15
16
	protected $form = [
17
		
18
	];
19
20
	public function setup(){
21
		$this->router = new \Torpedo\Routing\Router; 
0 ignored issues
show
Bug introduced by
The property router does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
22
	}
23
24
	public function testGetRequest(){
25
		$this->router->get('/', 'bar'); 
26
		$this->assertContains('bar', $this->router->routes['GET'][0]);	
27
	}
28
29
	public function testPostRequest(){
30
		$this->router->post('/', 'tar'); 
31
		$this->assertContains('tar', $this->router->routes['POST'][0]);	
32
	}
33
34
	public function testAnyRequest(){
35
		$this->router->any('/', 'samayo'); 
36
		$this->assertContains('samayo', $this->router->routes['GET'][0]);
37
		$this->assertContains('samayo', $this->router->routes['POST'][0]);
38
		$this->assertContains('samayo', $this->router->routes['PUT'][0]);	
39
		$this->assertContains('samayo', $this->router->routes['DELETE'][0]);
40
	}
41
42
	public function testPutRequest(){
43
		$this->router->put('/', 'atl'); 
44
		$this->assertContains('atl', $this->router->routes['PUT'][0]);	
45
	}
46
47
	public function testDeleteRequest(){
48
		$this->router->delete('/', 'ben'); 
49
		$this->assertContains('ben', $this->router->routes['DELETE'][0]);	
50
	}
51
52
53
	public function testRouteBasedController(){
54
		$server = $this->server; 
55
		$post = $this->form; 
56
		$this->router->get('/blog', 'fooController@indexAction');
57
		$result = $this->router->match($server, $post); 	
0 ignored issues
show
Coding Style introduced by
There is some trailing whitespace on this line which should be avoided as per coding-style.
Loading history...
58
		$this->assertInternalType('array', $result);
59
		$this->assertContains('fooController', $result);
60
		$this->assertContains('indexAction', $result);
61
	}
62
63
	public function testGETRoutesWithNamedArgs(){
64
		$server = [
65
			'REQUEST_URI' => '/user/simon',
66
			'REQUEST_METHOD' => 'GET'
67
		]; 
68
		$post = $this->form; 
69
		$this->router->get('/user/{:name}', 'userController@profiles');
70
		$result = $this->router->match($server, $post); 	
0 ignored issues
show
Coding Style introduced by
There is some trailing whitespace on this line which should be avoided as per coding-style.
Loading history...
71
		$this->assertInternalType('array', $result);
72
		$this->assertContains('userController', $result);
73
		$this->assertContains('profiles', $result);
74
		$this->assertContains([0 => 'simon'], $result);
75
	}
76
77
	public function testPOSToutesWithNamedArgs(){
78
		$server = [
79
			'REQUEST_URI' => '/article/ronpaul',
80
			'REQUEST_METHOD' => 'POST'
81
		]; 
82
		$post = $this->form; 
83
		$this->router->post('/article/{:slug}', 'barController@barAction');
84
		$result = $this->router->match($server, $post); 	
0 ignored issues
show
Coding Style introduced by
There is some trailing whitespace on this line which should be avoided as per coding-style.
Loading history...
85
		$this->assertInternalType('array', $result);
86
		$this->assertContains('barController', $result);
87
		$this->assertContains('barAction', $result);
88
		$this->assertContains([0 => 'ronpaul'], $result);
89
	}
90
91
	public function testPUToutesWithNamedArgs(){
92
		$server = [
93
			'REQUEST_URI' => '/article/ronpaul/1',
94
			'REQUEST_METHOD' => 'GET'
95
		]; 
96
		$post = ['_method' => 'PUT']; 
97
		$this->router->put('/article/ronpaul/{:id}', 'ArticleController@article');
98
		$result = $this->router->match($server, $post); 	
0 ignored issues
show
Coding Style introduced by
There is some trailing whitespace on this line which should be avoided as per coding-style.
Loading history...
99
		$this->assertInternalType('array', $result);
100
		$this->assertContains('ArticleController', $result);
101
		$this->assertContains('article', $result);
102
		$this->assertContains([0 => 1], $result);
103
	}
104
	public function testNamedArgsPassedViaFunctionParamsMatch(){
105
		$server = [
106
			'REQUEST_URI' => '/user/simon/age/11/url/i-am-slug',
107
			'REQUEST_METHOD' => 'GET'
108
		]; 
109
		$post = $this->form; 
110
111
		$this->router->get('/user/{:name}/age/{:id}/url/{:slug}', 
112
			function($name, $id, $slug){
113
				return [$name, $id, $slug];
114
			});
115
116
			$result = $this->router->match($server, $post); 
117
			$this->assertInternalType('array', $result);
118
			$this->assertContains('simon', $result);
119
			$this->assertContains('11', $result);
120
			$this->assertContains('i-am-slug', $result);
121
			$this->assertEquals(['simon', 11, 'i-am-slug'], $result);
122
		}
123
	
124
125
	public function testCalableRouteEvaluatesCallableArguments(){
126
		$server = [
127
			'REQUEST_URI' => '/article/ronpaul/1',
128
			'REQUEST_METHOD' => 'GET'
129
		]; 
130
		$post = ['_method' => 'POST']; 
131
		$this->router->any('/article/ronpaul/{:id}', function(){
132
			
133
		});
134
135
		$result = $this->router->match($server, $post); 
136
		$this->assertEquals(null, $result);
137
	}
138
}