Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
9 | class myRouterClass extends \PHPUnit_Framework_TestCase |
||
10 | { |
||
11 | private $config = [ |
||
12 | 'folder' => '', |
||
13 | 'default_return_type' =>'html' |
||
14 | ]; |
||
15 | |||
16 | private $routes = [ |
||
17 | 'home' => ['get', '/', 'app/main'], |
||
18 | ['get', '/json', 'app/json', 'json'], |
||
19 | ['post', '/json', 'app/redirect', 'redirect'], |
||
20 | 'alias' => ['get', '/alias', 'app/alias'], |
||
21 | ]; |
||
22 | private $request = null; |
||
23 | |||
24 | public function setUp() |
||
25 | { |
||
26 | $basedir = dirname(__DIR__) . '/app'; |
||
27 | $this->config['base_dir'] = $basedir; |
||
28 | $this->config['app_dir'] = $basedir; |
||
29 | $_SERVER = []; |
||
30 | $_FILES = []; |
||
31 | $_GET = []; |
||
32 | $_POST = []; |
||
33 | $_SERVER = []; |
||
34 | $_COOKIE = []; |
||
35 | $_SERVER['DOCUMENT_ROOT'] = $basedir."/htdocs"; |
||
36 | $_SERVER['SCRIPT_NAME'] = '/index.php'; |
||
37 | $_SERVER['REQUEST_URI'] = '/alias'; |
||
38 | $_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1'; |
||
39 | $_SERVER['SERVER_NAME'] = 'Selami'; |
||
40 | $_SERVER['SERVER_PORT'] = '8080'; |
||
41 | $_SERVER['REQUEST_METHOD'] = 'GET'; |
||
42 | $_SERVER['QUERY_STRING'] = 'p1=1&p2=2'; |
||
43 | $_SERVER['HTTPS'] = ''; |
||
44 | $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; |
||
45 | $_SERVER['REQUEST_TIME'] = time(); |
||
46 | $this->request = ServerRequestFactory::fromGlobals($_SERVER, $_GET, $_POST, $_COOKIE, $_FILES); |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * @test |
||
51 | * @dataProvider extractFolderDataProvider |
||
52 | * @param $requestedPath string |
||
53 | * @param $folder string |
||
54 | * @param $expected string |
||
55 | */ |
||
56 | public function shouldExtractRouteFromURLSuccessfully($requestedPath, $folder, $expected) |
||
57 | { |
||
58 | $router = new Selami\Router( |
||
59 | $this->routes, |
||
60 | $this->config['default_return_type'], |
||
61 | $this->request->getMethod(), |
||
62 | $this->request->getUri()->getPath(), |
||
63 | $this->config['folder'] |
||
64 | ); |
||
65 | $reflector = new ReflectionObject($router); |
||
66 | $method = $reflector->getMethod('extractFolder'); |
||
67 | $method->setAccessible(true); |
||
68 | $result = $method->invoke($router, $requestedPath, $folder ); |
||
69 | $this->assertEquals( |
||
70 | $expected, |
||
71 | $result, |
||
72 | "extractFolder did not correctly extract the requested path for sub folder" |
||
73 | ); |
||
74 | } |
||
75 | |||
76 | public function extractFolderDataProvider() |
||
77 | { |
||
78 | return [ |
||
79 | ['/', '', '/'], |
||
80 | ['', '', '/'], |
||
81 | ['/admin/dashboard', 'admin', '/dashboard'] |
||
82 | ]; |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * @test |
||
87 | */ |
||
88 | public function shouldCorrectlyInstantiateRouter() |
||
89 | { |
||
90 | $router = new Selami\Router( |
||
91 | $this->routes, |
||
92 | $this->config['default_return_type'], |
||
93 | $this->request->getMethod(), |
||
94 | $this->request->getUri()->getPath(), |
||
95 | $this->config['folder'] |
||
96 | ); |
||
97 | $this->assertInstanceOf('Selami\Router', $router); |
||
98 | $this->assertAttributeContains('GET', 'method', $router, "Router didn't correctly return method as GET."); |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * @test |
||
103 | */ |
||
104 | public function shouldCorrectlyReturnAliases() |
||
105 | { |
||
106 | $router = new Selami\Router( |
||
107 | $this->routes, |
||
108 | $this->config['default_return_type'], |
||
109 | $this->request->getMethod(), |
||
110 | $this->request->getUri()->getPath(), |
||
111 | $this->config['folder'] |
||
112 | ); |
||
113 | $reflector = new ReflectionObject($router); |
||
114 | $method = $reflector->getMethod('getAliases'); |
||
115 | $method->setAccessible(true); |
||
116 | $aliases = $method->invoke($router); |
||
117 | $this->assertArrayHasKey('home', $aliases, "Router didn't correctly return aliases"); |
||
118 | $this->assertArrayHasKey('alias', $aliases, "Router didn't correctly return aliases"); |
||
119 | $this->assertEquals('/', $aliases['home'], "Router didn't correctly return aliases"); |
||
120 | $this->assertEquals('/alias', $aliases['alias'], "Router didn't correctly return aliases"); |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * @test |
||
125 | */ |
||
126 | public function shouldCorrectlyReturnRouteAndRouteAliases() |
||
127 | { |
||
128 | $router = new Selami\Router( |
||
129 | $this->routes, |
||
130 | $this->config['default_return_type'], |
||
131 | $this->request->getMethod(), |
||
132 | $this->request->getUri()->getPath(), |
||
133 | $this->config['folder'] |
||
134 | ); |
||
135 | $routeInfo = $router->getRoute(); |
||
136 | $this->assertArrayHasKey('aliases', $routeInfo, "Router didn't correctly return route data"); |
||
137 | $this->assertArrayHasKey('controller', $routeInfo['route'], "Router didn't correctly return route data"); |
||
138 | $this->assertArrayHasKey('action', $routeInfo['route'], "Router didn't correctly return router data"); |
||
139 | $this->assertEquals('app', $routeInfo['route']['controller'], "Router didn't correctly return router data"); |
||
140 | $this->assertEquals('alias', $routeInfo['route']['action'], "Router didn't correctly return router data"); |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * @test |
||
145 | */ |
||
146 | public function shouldCorrectlyReturnMethodNotAllowed() |
||
147 | { |
||
148 | $_SERVER['REQUEST_METHOD'] = 'POST'; |
||
149 | $this->request = ServerRequestFactory::fromGlobals($_SERVER, $_GET, $_POST, $_COOKIE, $_FILES); |
||
150 | $router = new Selami\Router( |
||
151 | $this->routes, |
||
152 | $this->config['default_return_type'], |
||
153 | $this->request->getMethod(), |
||
154 | $this->request->getUri()->getPath(), |
||
155 | $this->config['folder'] |
||
156 | ); |
||
157 | $routeInfo = $router->getRoute(); |
||
158 | $this->assertEquals('405', $routeInfo['route']['status'], "Router didn't correctly return Method Not Allowed"); |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * @test |
||
163 | */ |
||
164 | public function shouldCorrectlyReturnNotFound() |
||
165 | { |
||
166 | $_SERVER['REQUEST_URI'] = '/notexists'; |
||
167 | $_SERVER['REQUEST_METHOD'] = 'POST'; |
||
168 | $this->request = ServerRequestFactory::fromGlobals($_SERVER, $_GET, $_POST, $_COOKIE, $_FILES); |
||
169 | $router = new Selami\Router( |
||
170 | $this->routes, |
||
171 | $this->config['default_return_type'], |
||
172 | $this->request->getMethod(), |
||
173 | $this->request->getUri()->getPath(), |
||
174 | $this->config['folder'] |
||
175 | ); |
||
176 | $routeInfo = $router->getRoute(); |
||
177 | $this->assertEquals('404', $routeInfo['route']['status'], "Router didn't correctly returnNot FOund"); |
||
178 | } |
||
179 | } |
||
180 |