|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Vectorface\SnappyRouterTests; |
|
4
|
|
|
|
|
5
|
|
|
use Vectorface\SnappyRouter\SnappyRouter; |
|
6
|
|
|
use Vectorface\SnappyRouter\Config\Config; |
|
7
|
|
|
use Vectorface\SnappyRouter\Di\Di; |
|
8
|
|
|
use Vectorface\SnappyRouter\Plugin\PluginInterface; |
|
9
|
|
|
use Vectorface\SnappyRouter\Handler\AbstractHandler; |
|
10
|
|
|
use Vectorface\SnappyRouter\Handler\ControllerHandler; |
|
11
|
|
|
|
|
12
|
|
|
use \PHPUnit_Framework_TestCase; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Tests the main SnappyRouter class. |
|
16
|
|
|
* @copyright Copyright (c) 2014, VectorFace, Inc. |
|
17
|
|
|
* @author Dan Bruce <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
class SnappyRouterTest extends PHPUnit_Framework_TestCase |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Returns a standard router config array. |
|
23
|
|
|
* @return array A standard router config. |
|
24
|
|
|
*/ |
|
25
|
|
|
private function getStandardConfig() |
|
26
|
|
|
{ |
|
27
|
|
|
return array( |
|
28
|
|
|
Config::KEY_DI => 'Vectorface\SnappyRouter\Di\Di', |
|
29
|
|
|
Config::KEY_HANDLERS => array( |
|
30
|
|
|
'BogusCliHandler' => array( |
|
31
|
|
|
Config::KEY_CLASS => 'Vectorface\SnappyRouter\Handler\CliTaskHandler' |
|
32
|
|
|
), |
|
33
|
|
|
'ControllerHandler' => array( |
|
34
|
|
|
Config::KEY_CLASS => 'Vectorface\SnappyRouter\Handler\ControllerHandler', |
|
35
|
|
|
Config::KEY_OPTIONS => array( |
|
36
|
|
|
ControllerHandler::KEY_BASE_PATH => '/', |
|
37
|
|
|
Config::KEY_CONTROLLERS => array( |
|
38
|
|
|
'TestController' => 'Vectorface\SnappyRouterTests\Controller\TestDummyController' |
|
39
|
|
|
), |
|
40
|
|
|
Config::KEY_PLUGINS => array( |
|
41
|
|
|
'TestPlugin' => array( |
|
42
|
|
|
Config::KEY_CLASS => 'Vectorface\SnappyRouterTests\Plugin\TestPlugin', |
|
43
|
|
|
Config::KEY_OPTIONS => array() |
|
44
|
|
|
), |
|
45
|
|
|
'AnotherPlugin' => 'Vectorface\SnappyRouterTests\Plugin\TestPlugin' |
|
46
|
|
|
) |
|
47
|
|
|
) |
|
48
|
|
|
), |
|
49
|
|
|
'CliHandler' => array( |
|
50
|
|
|
Config::KEY_CLASS => 'Vectorface\SnappyRouter\Handler\CliTaskHandler', |
|
51
|
|
|
Config::KEY_OPTIONS => array( |
|
52
|
|
|
'tasks' => array( |
|
53
|
|
|
'TestTask' => 'Vectorface\SnappyRouterTests\Task\DummyTestTask' |
|
54
|
|
|
) |
|
55
|
|
|
) |
|
56
|
|
|
) |
|
57
|
|
|
) |
|
58
|
|
|
); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* An overview of how to use the SnappyRouter class. |
|
63
|
|
|
* @test |
|
64
|
|
|
*/ |
|
65
|
|
|
public function synopsis() |
|
|
|
|
|
|
66
|
|
|
{ |
|
67
|
|
|
// an example configuration of the router |
|
68
|
|
|
$config = $this->getStandardConfig(); |
|
69
|
|
|
// instantiate the router |
|
70
|
|
|
$router = new SnappyRouter(new Config($config)); |
|
71
|
|
|
|
|
72
|
|
|
// an example MVC request |
|
73
|
|
|
$_SERVER['REQUEST_URI'] = '/Test/test'; |
|
74
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET'; |
|
75
|
|
|
$_GET['param'] = 'value'; |
|
76
|
|
|
$response = $router->handleRoute('apache2handler'); |
|
77
|
|
|
|
|
78
|
|
|
$expectedResponse = 'This is a test service.'; |
|
79
|
|
|
$this->assertEquals($expectedResponse, $response); |
|
80
|
|
|
|
|
81
|
|
|
unset($_SERVER['REQUEST_URI']); |
|
82
|
|
|
$_GET = array(); |
|
83
|
|
|
$_POST = array(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Tests that the router handles a generic exception. |
|
88
|
|
|
*/ |
|
89
|
|
|
public function testGenericException() |
|
90
|
|
|
{ |
|
91
|
|
|
$config = $this->getStandardConfig(); |
|
92
|
|
|
$router = new SnappyRouter(new Config($config)); |
|
93
|
|
|
|
|
94
|
|
|
// an example MVC request |
|
95
|
|
|
$path = '/Test/genericException'; |
|
96
|
|
|
$query = array('jsoncall' => 'testMethod'); |
|
97
|
|
|
$response = $router->handleHttpRoute($path, $query, array(), 'get'); |
|
98
|
|
|
|
|
99
|
|
|
$expectedResponse = 'A generic exception.'; |
|
100
|
|
|
$this->assertEquals($expectedResponse, $response); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Tests that an empty config array results in no handler being found. |
|
105
|
|
|
*/ |
|
106
|
|
|
public function testNoHandlerFoundException() |
|
107
|
|
|
{ |
|
108
|
|
|
// turn on debug mode so we get a verbose description of the exception |
|
109
|
|
|
$router = new SnappyRouter(new Config(array( |
|
110
|
|
|
'debug' => true |
|
111
|
|
|
))); |
|
112
|
|
|
|
|
113
|
|
|
// an example MVC request |
|
114
|
|
|
$path = '/Test/test'; |
|
115
|
|
|
$query = array('jsoncall' => 'testMethod'); |
|
116
|
|
|
$response = $router->handleHttpRoute($path, $query, array(), 'get'); |
|
117
|
|
|
$this->assertEquals('No handler responded to the request.', $response); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Tests that an exception is thrown if a handler class does not exist. |
|
122
|
|
|
* @expectedException Exception |
|
123
|
|
|
* @expectedExceptionMessage Cannot instantiate instance of Vectorface\SnappyRouter\Handler\NonexistantHandler |
|
124
|
|
|
*/ |
|
125
|
|
|
public function testInvalidHandlerClass() |
|
126
|
|
|
{ |
|
127
|
|
|
$config = $this->getStandardConfig(); |
|
128
|
|
|
$config[Config::KEY_HANDLERS]['InvalidHandler'] = array( |
|
129
|
|
|
'class' => 'Vectorface\SnappyRouter\Handler\NonexistantHandler' |
|
130
|
|
|
); |
|
131
|
|
|
$router = new SnappyRouter(new Config($config)); |
|
132
|
|
|
|
|
133
|
|
|
// an example MVC request |
|
134
|
|
|
$path = '/Test/test'; |
|
135
|
|
|
$query = array('jsoncall' => 'testMethod'); |
|
136
|
|
|
$response = $router->handleHttpRoute($path, $query, array(), 'get'); |
|
137
|
|
|
|
|
138
|
|
|
$expectedResponse = 'No handler responded to request.'; |
|
139
|
|
|
$this->assertEquals($expectedResponse, $response); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Tests that the CLI routing functionality works. |
|
144
|
|
|
*/ |
|
145
|
|
View Code Duplication |
public function testStandardCliRoute() |
|
|
|
|
|
|
146
|
|
|
{ |
|
147
|
|
|
$config = $this->getStandardConfig(); |
|
148
|
|
|
$router = new SnappyRouter(new Config($config)); |
|
149
|
|
|
|
|
150
|
|
|
$_SERVER['argv'] = array( |
|
151
|
|
|
'dummyScript.php', |
|
152
|
|
|
'--task', |
|
153
|
|
|
'TestTask', |
|
154
|
|
|
'--action', |
|
155
|
|
|
'testMethod' |
|
156
|
|
|
); |
|
157
|
|
|
$_SERVER['argc'] = count($_SERVER['argv']); |
|
158
|
|
|
$response = $router->handleRoute(); |
|
159
|
|
|
|
|
160
|
|
|
$expected = 'Hello World'.PHP_EOL; |
|
161
|
|
|
$this->assertEquals($expected, $response); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Tests a CLI route that throws an exception. |
|
166
|
|
|
*/ |
|
167
|
|
View Code Duplication |
public function testCliRouteWithException() |
|
|
|
|
|
|
168
|
|
|
{ |
|
169
|
|
|
$config = $this->getStandardConfig(); |
|
170
|
|
|
$router = new SnappyRouter(new Config($config)); |
|
171
|
|
|
|
|
172
|
|
|
$_SERVER['argv'] = array( |
|
173
|
|
|
'dummyScript.php', |
|
174
|
|
|
'--task', |
|
175
|
|
|
'TestTask', |
|
176
|
|
|
'--action', |
|
177
|
|
|
'throwsException' |
|
178
|
|
|
); |
|
179
|
|
|
$_SERVER['argc'] = count($_SERVER['argv']); |
|
180
|
|
|
$response = $router->handleRoute(); |
|
181
|
|
|
|
|
182
|
|
|
$expected = 'An exception was thrown.'.PHP_EOL; |
|
183
|
|
|
$this->assertEquals($expected, $response); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Tests that a CLI route with no appropriate handlers throws an |
|
188
|
|
|
* exception. |
|
189
|
|
|
*/ |
|
190
|
|
View Code Duplication |
public function testCliRouteWithNoHandler() |
|
|
|
|
|
|
191
|
|
|
{ |
|
192
|
|
|
$config = $this->getStandardConfig(); |
|
193
|
|
|
$router = new SnappyRouter(new Config($config)); |
|
194
|
|
|
|
|
195
|
|
|
$_SERVER['argv'] = array( |
|
196
|
|
|
'dummyScript.php', |
|
197
|
|
|
'--task', |
|
198
|
|
|
'NotDefinedTask', |
|
199
|
|
|
'--action', |
|
200
|
|
|
'anyAction' |
|
201
|
|
|
); |
|
202
|
|
|
$_SERVER['argc'] = count($_SERVER['argv']); |
|
203
|
|
|
$response = $router->handleRoute(); |
|
204
|
|
|
|
|
205
|
|
|
$expected = 'No CLI handler registered.'.PHP_EOL; |
|
206
|
|
|
$this->assertEquals($expected, $response); |
|
207
|
|
|
} |
|
208
|
|
|
} |
|
209
|
|
|
|
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: