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 |
||
| 38 | class WhipTest extends PHPUnit_Framework_TestCase |
||
| 39 | { |
||
| 40 | /** |
||
| 41 | * Tests that an invalid source format is rejected. |
||
| 42 | * @expectedException \InvalidArgumentException |
||
| 43 | */ |
||
| 44 | public function testInvalidSource() |
||
| 48 | /** |
||
| 49 | * Tests that we get back the right IP when there using superglobals. |
||
| 50 | */ |
||
| 51 | public function testSuperglobal() |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Tests that we get back 127.0.0.1 when there is no superglobal information |
||
| 60 | * at all. |
||
| 61 | */ |
||
| 62 | public function testEmptySuperglobal() |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Helper to get a mocked PSR-7 instance. |
||
| 71 | * |
||
| 72 | * @param string $remoteAddr The remote address to mock. |
||
| 73 | * @param string[][] $headers The headers, in the format expected by Psr-7. |
||
| 74 | */ |
||
| 75 | private function getHttpMessageMock($remoteAddr, array $headers = array()) |
||
| 87 | /** |
||
| 88 | * Tests that we can use a PSR-7 ServerRequestInterface compatible class. |
||
| 89 | */ |
||
| 90 | public function testPsr7Request() |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Tests that we get false when no valid IP address could be found. |
||
| 109 | */ |
||
| 110 | public function testNoAddresFoundDueToBitmask() |
||
| 111 | { |
||
| 112 | $lookup = new Whip(Whip::PROXY_HEADERS); |
||
| 113 | $lookup->setSource(array('REMOTE_ADDR' => '127.0.0.1')); |
||
| 114 | $this->assertFalse($lookup->getIpAddress()); |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Tests the standard REMOTE_ADDR method. |
||
| 119 | */ |
||
| 120 | public function testRemoteAddrMethod() |
||
| 121 | { |
||
| 122 | $lookup = new Whip(Whip::REMOTE_ADDR); |
||
| 123 | $lookup->setSource(array('REMOTE_ADDR' => '24.24.24.24')); |
||
| 124 | $this->assertEquals('24.24.24.24', $lookup->getValidIpAddress()); |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Tests that an invalid IPv4 address returns false. |
||
| 129 | */ |
||
| 130 | public function testInvalidIPv4Address() |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Tests a valid IPv6 address. |
||
| 139 | */ |
||
| 140 | public function testValidIPv6Address() |
||
| 141 | { |
||
| 142 | $lookup = new Whip(Whip::REMOTE_ADDR); |
||
| 143 | $lookup->setSource(array('REMOTE_ADDR' => '::1')); |
||
| 144 | $this->assertEquals('::1', $lookup->getValidIpAddress()); |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Tests that we accept whitelisted proxy methods when the IP matches, even |
||
| 149 | * if the IP listed is a comma separated list. |
||
| 150 | * |
||
| 151 | * @dataProvider proxyMethodWhitelistProvider |
||
| 152 | */ |
||
| 153 | public function testValidWhitelistedProxyMethod($remoteAddr) |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Repeats the above test twice for ipv4 and ipv6 |
||
| 173 | */ |
||
| 174 | public function proxyMethodWhitelistProvider() |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Tests that we accept proxy method based on a whitelisted IP using the |
||
| 184 | * dashed range notation. |
||
| 185 | */ |
||
| 186 | View Code Duplication | public function testValidWhitelistedProxyMethodWithDashNotation() |
|
| 207 | |||
| 208 | /** |
||
| 209 | * Tests that we accept proxy method based on a whitelisted IP using the |
||
| 210 | * wildcard asterix notation. |
||
| 211 | */ |
||
| 212 | View Code Duplication | public function testValidWhitelistedProxyMethodWithWildcardNotation() |
|
| 233 | |||
| 234 | /** |
||
| 235 | * Tests that we accept proxy method based on a whitelisted IP using the |
||
| 236 | * CIDR address notation. |
||
| 237 | */ |
||
| 238 | View Code Duplication | public function testValidWhitelistedProxyMethodWithCIDRdNotation() |
|
| 259 | |||
| 260 | /** |
||
| 261 | * Tests that we get false if there is a valid IP in a proxy header but |
||
| 262 | * we reject it due to REMOTE_ADDR not being in the whitelist. |
||
| 263 | */ |
||
| 264 | View Code Duplication | public function testValidIpRejectedDueToWhitelist() |
|
| 285 | |||
| 286 | /** |
||
| 287 | * Tests that we reject a proxy listed IPv6 address that does not fall within |
||
| 288 | * the allowed subnet. |
||
| 289 | */ |
||
| 290 | public function testIPv6AddressRejectedDueToWhitelist() |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Tests that we reject a proxy listed IPv6 address that does not fall within |
||
| 311 | * the allowed subnet. |
||
| 312 | */ |
||
| 313 | public function testIPv6AddressFoundInWhitelist() |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Test that an IPv4 address is rejected because the whitelist is empty for |
||
| 334 | * IPv4. |
||
| 335 | */ |
||
| 336 | public function testIPv4AddressRejectedDueToEmptyWhitelist() |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Test that an IPv6 address is rejected because the whitelist is empty for |
||
| 357 | * IPv6. |
||
| 358 | */ |
||
| 359 | public function testIPv6AddressRejectedDueToEmptyWhitelist() |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Test a custom header with a whitelisted IP. |
||
| 380 | */ |
||
| 381 | View Code Duplication | public function testCustomHeader() |
|
| 403 | |||
| 404 | /** |
||
| 405 | * Test HTTP_X_REAL_IP header. |
||
| 406 | */ |
||
| 407 | public function testHttpXRealIpHeader() |
||
| 408 | { |
||
| 409 | $lookup = new Whip( |
||
| 410 | Whip::PROXY_HEADERS | Whip::REMOTE_ADDR, |
||
| 411 | array(), |
||
| 412 | array( |
||
| 413 | 'REMOTE_ADDR' => '127.0.0.1', |
||
| 414 | 'HTTP_X_REAL_IP' => '24.24.24.24' |
||
| 415 | ) |
||
| 416 | ); |
||
| 417 | $this->assertEquals('24.24.24.24', $lookup->getIpAddress()); |
||
| 418 | } |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Tests that if we specify the source array, it overrides any values found |
||
| 422 | * in the $_SERVER array. |
||
| 423 | */ |
||
| 424 | View Code Duplication | public function testSourceArrayOverridesServerSuperglobal() |
|
| 433 | |||
| 434 | /** |
||
| 435 | * Tests that if we specify the source array through Whip::setSource, the |
||
| 436 | * class will override any values found in $_SERVER. |
||
| 437 | */ |
||
| 438 | View Code Duplication | public function testSetSourceArrayOverridesServerSuperglobal() |
|
| 448 | } |
||
| 449 |
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: