RouterTest::testGETRoutesWithNamedArgs()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 0
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
}