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 |
||
37 | class WhipTest extends PHPUnit_Framework_TestCase |
||
38 | { |
||
39 | /** |
||
40 | * Tests that an invalid source format is rejected. |
||
41 | * @expectedException \InvalidArgumentException |
||
42 | */ |
||
43 | public function testInvalidSource() |
||
47 | /** |
||
48 | * Tests that we get back the right IP when there using superglobals. |
||
49 | */ |
||
50 | public function testSuperglobal() |
||
56 | |||
57 | /** |
||
58 | * Tests that we get back 127.0.0.1 when there is no superglobal information |
||
59 | * at all. |
||
60 | */ |
||
61 | public function testEmptySuperglobal() |
||
67 | |||
68 | /** |
||
69 | * Helper to get a mocked PSR-7 instance. |
||
70 | * |
||
71 | * @param string $remoteAddr The remote address to mock. |
||
72 | * @param string[][] $headers The headers, in the format expected by Psr-7. |
||
73 | */ |
||
74 | private function getHttpMessageMock($remoteAddr, array $headers = array()) |
||
86 | /** |
||
87 | * Tests that we can use a PSR-7 ServerRequestInterface compatible class. |
||
88 | */ |
||
89 | public function testPsr7Request() |
||
105 | |||
106 | /** |
||
107 | * Tests that we get false when no valid IP address could be found. |
||
108 | */ |
||
109 | public function testNoAddresFoundDueToBitmask() |
||
115 | |||
116 | /** |
||
117 | * Tests the standard REMOTE_ADDR method. |
||
118 | */ |
||
119 | public function testRemoteAddrMethod() |
||
125 | |||
126 | /** |
||
127 | * Tests that an invalid IPv4 address returns false. |
||
128 | */ |
||
129 | public function testInvalidIPv4Address() |
||
135 | |||
136 | /** |
||
137 | * Tests a valid IPv6 address. |
||
138 | */ |
||
139 | public function testValidIPv6Address() |
||
145 | |||
146 | /** |
||
147 | * Tests that we accept whitelisted proxy methods when the IP matches, even |
||
148 | * if the IP listed is a comma separated list. |
||
149 | * |
||
150 | * @dataProvider proxyMethodWhitelistProvider |
||
151 | */ |
||
152 | View Code Duplication | public function testValidWhitelistedProxyMethod($remoteAddr) |
|
169 | |||
170 | /** |
||
171 | * Repeats the above test twice for ipv4 and ipv6 |
||
172 | */ |
||
173 | public function proxyMethodWhitelistProvider() |
||
180 | |||
181 | /** |
||
182 | * Tests that we accept proxy method based on a whitelisted IP using the |
||
183 | * dashed range notation. |
||
184 | */ |
||
185 | View Code Duplication | public function testValidWhitelistedProxyMethodWithDashNotation() |
|
206 | |||
207 | /** |
||
208 | * Tests that we accept proxy method based on a whitelisted IP using the |
||
209 | * wildcard asterix notation. |
||
210 | */ |
||
211 | View Code Duplication | public function testValidWhitelistedProxyMethodWithWildcardNotation() |
|
232 | |||
233 | /** |
||
234 | * Tests that we accept proxy method based on a whitelisted IP using the |
||
235 | * CIDR address notation. |
||
236 | */ |
||
237 | View Code Duplication | public function testValidWhitelistedProxyMethodWithCIDRdNotation() |
|
258 | |||
259 | /** |
||
260 | * Tests that we get false if there is a valid IP in a proxy header but |
||
261 | * we reject it due to REMOTE_ADDR not being in the whitelist. |
||
262 | */ |
||
263 | View Code Duplication | public function testValidIpRejectedDueToWhitelist() |
|
284 | |||
285 | /** |
||
286 | * Tests that we reject a proxy listed IPv6 address that does not fall within |
||
287 | * the allowed subnet. |
||
288 | */ |
||
289 | public function testIPv6AddressRejectedDueToWhitelist() |
||
307 | |||
308 | /** |
||
309 | * Tests that we reject a proxy listed IPv6 address that does not fall within |
||
310 | * the allowed subnet. |
||
311 | */ |
||
312 | public function testIPv6AddressFoundInWhitelist() |
||
330 | |||
331 | /** |
||
332 | * Test that an IPv4 address is rejected because the whitelist is empty for |
||
333 | * IPv4. |
||
334 | */ |
||
335 | public function testIPv4AddressRejectedDueToEmptyWhitelist() |
||
353 | |||
354 | /** |
||
355 | * Test that an IPv6 address is rejected because the whitelist is empty for |
||
356 | * IPv6. |
||
357 | */ |
||
358 | public function testIPv6AddressRejectedDueToEmptyWhitelist() |
||
376 | |||
377 | /** |
||
378 | * Test a custom header with a whitelisted IP. |
||
379 | */ |
||
380 | View Code Duplication | public function testCustomHeader() |
|
402 | |||
403 | /** |
||
404 | * Test HTTP_X_REAL_IP header. |
||
405 | */ |
||
406 | public function testHttpXRealIpHeader() |
||
418 | |||
419 | /** |
||
420 | * Tests that if we specify the source array, it overrides any values found |
||
421 | * in the $_SERVER array. |
||
422 | */ |
||
423 | View Code Duplication | public function testSourceArrayOverridesServerSuperglobal() |
|
432 | |||
433 | /** |
||
434 | * Tests that if we specify the source array through Whip::setSource, the |
||
435 | * class will override any values found in $_SERVER. |
||
436 | */ |
||
437 | View Code Duplication | public function testSetSourceArrayOverridesServerSuperglobal() |
|
447 | |||
448 | /** |
||
449 | * Tests that we fallback to REMOTE_ADDR if the custom header was not found |
||
450 | */ |
||
451 | public function testFallbackToRemoteAddr() |
||
459 | } |
||
460 |
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: