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 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
|
|
|
// configure a logger, if insight into router behavior is desired |
72
|
|
|
$router->setLogger(new \Psr\Log\NullLogger()); |
73
|
|
|
|
74
|
|
|
// an example MVC request |
75
|
|
|
$_SERVER['REQUEST_URI'] = '/Test/test'; |
76
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET'; |
77
|
|
|
$_GET['param'] = 'value'; |
78
|
|
|
$response = $router->handleRoute('apache2handler'); |
79
|
|
|
|
80
|
|
|
$expectedResponse = 'This is a test service.'; |
81
|
|
|
$this->assertEquals($expectedResponse, $response); |
82
|
|
|
|
83
|
|
|
unset($_SERVER['REQUEST_URI']); |
84
|
|
|
$_GET = array(); |
85
|
|
|
$_POST = array(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Tests that the router handles a generic exception. |
90
|
|
|
*/ |
91
|
|
|
public function testGenericException() |
92
|
|
|
{ |
93
|
|
|
$config = $this->getStandardConfig(); |
94
|
|
|
$router = new SnappyRouter(new Config($config)); |
95
|
|
|
|
96
|
|
|
// an example MVC request |
97
|
|
|
$path = '/Test/genericException'; |
98
|
|
|
$query = array('jsoncall' => 'testMethod'); |
99
|
|
|
$response = $router->handleHttpRoute($path, $query, array(), 'get'); |
100
|
|
|
|
101
|
|
|
$expectedResponse = 'A generic exception.'; |
102
|
|
|
$this->assertEquals($expectedResponse, $response); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Tests that an empty config array results in no handler being found. |
107
|
|
|
*/ |
108
|
|
|
public function testNoHandlerFoundException() |
109
|
|
|
{ |
110
|
|
|
// turn on debug mode so we get a verbose description of the exception |
111
|
|
|
$router = new SnappyRouter(new Config(array( |
112
|
|
|
'debug' => true |
113
|
|
|
))); |
114
|
|
|
|
115
|
|
|
// an example MVC request |
116
|
|
|
$path = '/Test/test'; |
117
|
|
|
$query = array('jsoncall' => 'testMethod'); |
118
|
|
|
$response = $router->handleHttpRoute($path, $query, array(), 'get'); |
119
|
|
|
$this->assertEquals('No handler responded to the request.', $response); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Tests that an exception is thrown if a handler class does not exist. |
124
|
|
|
* @expectedException Exception |
125
|
|
|
* @expectedExceptionMessage Cannot instantiate instance of Vectorface\SnappyRouter\Handler\NonexistantHandler |
126
|
|
|
*/ |
127
|
|
|
public function testInvalidHandlerClass() |
128
|
|
|
{ |
129
|
|
|
$config = $this->getStandardConfig(); |
130
|
|
|
$config[Config::KEY_HANDLERS]['InvalidHandler'] = array( |
131
|
|
|
'class' => 'Vectorface\SnappyRouter\Handler\NonexistantHandler' |
132
|
|
|
); |
133
|
|
|
$router = new SnappyRouter(new Config($config)); |
134
|
|
|
|
135
|
|
|
// an example MVC request |
136
|
|
|
$path = '/Test/test'; |
137
|
|
|
$query = array('jsoncall' => 'testMethod'); |
138
|
|
|
$response = $router->handleHttpRoute($path, $query, array(), 'get'); |
139
|
|
|
|
140
|
|
|
$expectedResponse = 'No handler responded to request.'; |
141
|
|
|
$this->assertEquals($expectedResponse, $response); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Tests that the CLI routing functionality works. |
146
|
|
|
*/ |
147
|
|
View Code Duplication |
public function testStandardCliRoute() |
|
|
|
|
148
|
|
|
{ |
149
|
|
|
$config = $this->getStandardConfig(); |
150
|
|
|
$router = new SnappyRouter(new Config($config)); |
151
|
|
|
|
152
|
|
|
$_SERVER['argv'] = array( |
153
|
|
|
'dummyScript.php', |
154
|
|
|
'--task', |
155
|
|
|
'TestTask', |
156
|
|
|
'--action', |
157
|
|
|
'testMethod' |
158
|
|
|
); |
159
|
|
|
$_SERVER['argc'] = count($_SERVER['argv']); |
160
|
|
|
$response = $router->handleRoute(); |
161
|
|
|
|
162
|
|
|
$expected = 'Hello World'.PHP_EOL; |
163
|
|
|
$this->assertEquals($expected, $response); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Tests a CLI route that throws an exception. |
168
|
|
|
*/ |
169
|
|
View Code Duplication |
public function testCliRouteWithException() |
|
|
|
|
170
|
|
|
{ |
171
|
|
|
$config = $this->getStandardConfig(); |
172
|
|
|
$router = new SnappyRouter(new Config($config)); |
173
|
|
|
|
174
|
|
|
$_SERVER['argv'] = array( |
175
|
|
|
'dummyScript.php', |
176
|
|
|
'--task', |
177
|
|
|
'TestTask', |
178
|
|
|
'--action', |
179
|
|
|
'throwsException' |
180
|
|
|
); |
181
|
|
|
$_SERVER['argc'] = count($_SERVER['argv']); |
182
|
|
|
$response = $router->handleRoute(); |
183
|
|
|
|
184
|
|
|
$expected = 'An exception was thrown.'.PHP_EOL; |
185
|
|
|
$this->assertEquals($expected, $response); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Tests that a CLI route with no appropriate handlers throws an |
190
|
|
|
* exception. |
191
|
|
|
*/ |
192
|
|
View Code Duplication |
public function testCliRouteWithNoHandler() |
|
|
|
|
193
|
|
|
{ |
194
|
|
|
$config = $this->getStandardConfig(); |
195
|
|
|
$router = new SnappyRouter(new Config($config)); |
196
|
|
|
|
197
|
|
|
$_SERVER['argv'] = array( |
198
|
|
|
'dummyScript.php', |
199
|
|
|
'--task', |
200
|
|
|
'NotDefinedTask', |
201
|
|
|
'--action', |
202
|
|
|
'anyAction' |
203
|
|
|
); |
204
|
|
|
$_SERVER['argc'] = count($_SERVER['argv']); |
205
|
|
|
$response = $router->handleRoute(); |
206
|
|
|
|
207
|
|
|
$expected = 'No CLI handler registered.'.PHP_EOL; |
208
|
|
|
$this->assertEquals($expected, $response); |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.