1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
<?php /** * Klein (klein.php) - A fast & flexible router for PHP * * @author Chris O'Hara <cohara87@gmail.com> * @author Trevor Suarez (Rican7) (contributor and v2 refactorer) * @copyright (c) Chris O'Hara * @link https://github.com/klein/klein.php * @license MIT */
namespace Klein\Tests;
use InvalidArgumentException; use Klein\Klein; use Klein\Route;
/** * RouteTest */ class RouteTest extends AbstractKleinTest {
protected function getTestCallable() { return function () { echo 'dog'; }; }
public function testCallbackGetSet() { // Test functions $test_callable = $this->getTestCallable(); $test_class_callable = __NAMESPACE__ . '\Mocks\TestClass::GET';
// Callback set in constructor $route = new Route($test_callable);
$this->assertSame($test_callable, $route->getCallback()); $this->assertInternalType('callable', $route->getCallback());
// Callback set in method $route = new Route($test_callable); $route->setCallback($test_class_callable);
$this->assertSame($test_class_callable, $route->getCallback()); $this->assertInternalType('callable', $route->getCallback()); }
public function testPathGetSet() { // Test data $test_callable = $this->getTestCallable(); $test_path = '/this-is-a-path';
// Empty constructor $route = new Route($test_callable);
$this->assertNotNull($route->getPath()); $this->assertInternalType('string', $route->getPath());
// Set in constructor $route = new Route($test_callable, $test_path);
$this->assertSame($test_path, $route->getPath());
// Set in method $route = new Route($test_callable); $route->setPath($test_path);
$this->assertSame($test_path, $route->getPath()); }
public function testMethodGetSet() { // Test data $test_callable = $this->getTestCallable(); $test_method_string = 'POST'; $test_method_array = array('POST', 'PATCH');
// Empty constructor $route = new Route($test_callable);
$this->assertNull($route->getMethod());
// Set in constructor $route = new Route($test_callable, null, $test_method_string);
$this->assertSame($test_method_string, $route->getMethod());
// Set in method $route = new Route($test_callable); $route->setMethod($test_method_array);
$this->assertSame($test_method_array, $route->getMethod()); }
public function testCountMatchGetSet() { // Test data $test_callable = $this->getTestCallable(); $test_count_match = false;
// Empty constructor $route = new Route($test_callable);
$this->assertTrue($route->getCountMatch());
// Set in constructor $route = new Route($test_callable, null, null, $test_count_match);
$this->assertSame($test_count_match, $route->getCountMatch());
// Set in count_match $route = new Route($test_callable); $route->setCountMatch($test_count_match);
$this->assertSame($test_count_match, $route->getCountMatch()); }
public function testNameGetSet() { // Test data $test_callable = $this->getTestCallable(); $test_name = 'trevor';
// Empty constructor $route = new Route($test_callable);
$this->assertNull($route->getName());
// Set in constructor $route = new Route($test_callable, null, null, null, $test_name);
$this->assertSame($test_name, $route->getName());
// Set in method $route = new Route($test_callable); $route->setName($test_name);
$this->assertSame($test_name, $route->getName()); }
public function testInvokeMethod() { // Test data $test_callable = function ($id, $name) { return array($id, $name); }; $test_arguments = array(7, 'Trevor');
$route = new Route($test_callable);
$this->assertSame( call_user_func_array($test_callable, $test_arguments), call_user_func_array($route, $test_arguments) ); }
/** * Exception tests */
/** * @expectedException InvalidArgumentException */ public function testCallbackSetWithIncorrectType() { $route = new Route($this->getTestCallable());
// Test setting with the WRONG type $route->setCallback(100); }
/** * @expectedException InvalidArgumentException */ public function testMethodSetWithIncorrectType() { $route = new Route($this->getTestCallable());
// Test setting with the WRONG type $route->setMethod(100); } }
|