Completed
Push — master ( f46bec...dd2aaa )
by Yasir
01:21
created

GeoIPLocationTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 77
Duplicated Lines 66.23 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 51
loc 77
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetGeoLocation() 0 19 1
A testGetGeoLocationFail() 10 10 1
A testGetGeoLocationIpInvalid() 10 10 1
A testRequestInvalid() 16 16 1
A testResponseInvalid() 15 15 1

How to fix   Duplicated Code   

Duplicated Code

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
2
3
namespace ipGeolocation\tests;
4
5
use ipGeolocation\GeoIPLocation;
6
use PHPUnit\Framework\TestCase;
7
8
/**
9
 * Tests for GeoIPLocation class
10
 *
11
 * PHP version 7
12
 *
13
 * @author yasir khurshid <[email protected]>
14
 */
15
class GeoIPLocationTest extends TestCase
16
{
17
    public function testGetGeoLocation()
18
    {
19
        $_SERVER['REMOTE_ADDR'] = '188.110.9.8';
20
        $location = (new GeoIPLocation())->getGeoLocation();
21
22
        $this->assertInstanceOf('ipGeolocation\Location', $location);
23
24
        $this->assertTrue($location->getStatus());
25
        $this->assertSame('Wesseling', $location->getCity());
26
        $this->assertSame('Germany', $location->getCountry());
27
        $this->assertSame('DE', $location->getCountryCode());
28
        $this->assertSame(50.8271, $location->getLatitude());
29
        $this->assertSame(6.9747, $location->getLongitude());
30
        $this->assertSame('NW', $location->getRegionCode());
31
        $this->assertSame('North Rhine-Westphalia', $location->getRegionName());
32
        $this->assertSame('Europe/Berlin', $location->getTimezone());
33
        $this->assertSame(50389, $location->getPostalCode());
34
        $this->assertSame('EUR', $location->getCurrencyIso());
35
    }
36
37 View Code Duplication
    public function testGetGeoLocationFail()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
38
    {
39
        $_SERVER['REMOTE_ADDR'] = '';
40
        $location = (new GeoIPLocation())->getGeoLocation();
41
42
        $this->assertInstanceOf('ipGeolocation\Location', $location);
43
44
        $this->assertFalse($location->getStatus());
45
        $this->assertSame('reserved range', $location->getMessage());
46
    }
47
48 View Code Duplication
    public function testGetGeoLocationIpInvalid()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
49
    {
50
        $_SERVER['REMOTE_ADDR'] = '188.11098';
51
        $geoIp = new GeoIPLocation();
52
        $location = $geoIp->getGeoLocation();
53
54
        $this->assertFalse($location->getStatus());
55
        $this->assertSame('reserved range', $location->getMessage());
56
        $this->assertSame('127.0.0.1', $geoIp->getIpAddress());
57
    }
58
59 View Code Duplication
    public function testRequestInvalid()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
60
    {
61
        $stub = $this->getMockBuilder('ipGeolocation\GeoIPLocation')
62
            ->setMethods(array('getRequestUrl'))
63
            ->getMock();
64
65
        $stub->method('getRequestUrl')
66
            ->with()
67
            ->willReturn('http://httpbin.org/status/undefined');
68
69
70
        $location = $stub->getGeoLocation();
71
72
        $this->assertFalse($location->getStatus());
73
        $this->assertSame('Client error: 400', $location->getMessage());
74
    }
75
76 View Code Duplication
    public function testResponseInvalid()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
77
    {
78
        $stub = $this->getMockBuilder('ipGeolocation\GeoIPLocation')
79
            ->setMethods(array('getRequestUrl'))
80
            ->getMock();
81
82
        $stub->method('getRequestUrl')
83
            ->with()
84
            ->willReturn('http://httpbin.org/status/300');
85
86
        $location = $stub->getGeoLocation();
87
88
        $this->assertFalse($location->getStatus());
89
        $this->assertSame('Request failed with response code: 300 and response: Multiple Choices', $location->getMessage());
90
    }
91
}
92