Completed
Push — master ( 9e0ca5...478748 )
by Ehsan
10:57 queued 02:20
created

GeocoderTest::testGetLatLng()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 12
rs 9.4285
c 3
b 1
f 0
1
<?php
2
3
namespace Geocoder;
4
5
use PHPUnit\Framework\TestCase;
6
7
class GeocoderTest extends TestCase
8
{
9
    const VALID_ADDRESS = 'Melbourne, Australia';
10
    const VALID_ADDRESS_MULTIPLE_RESULT = 'springfield';
11
    const UNKNOWN_ADDRESS = 'Dummy Address';
12
13
    public function testGeocode()
14
    {
15
        $this->assertEquals(
16
            $this->getExpectedData(),
17
            (new Geocoder())->geocode(self::VALID_ADDRESS)
18
        );
19
    }
20
21
    public function testGeocodeMultipleResult()
22
    {
23
        $result = (new Geocoder())->geocode(self::VALID_ADDRESS_MULTIPLE_RESULT);
24
        $result = json_decode($result, true);
25
26
        $this->assertEquals(
27
            6,
28
            count($result['results'])
29
        );
30
    }
31
32
    public function testGeocodeUnknownAddress()
33
    {
34
        $this->assertEquals(
35
            $this->getExpectedNullData(),
36
            (new Geocoder())->geocode(self::UNKNOWN_ADDRESS)
37
        );
38
    }
39
40
    public function testGetLatLngUnknownAddress()
41
    {
42
        $this->assertEquals(
43
            null,
44
            (new Geocoder())->getLatLng(self::UNKNOWN_ADDRESS)
45
        );
46
    }
47
48
    public function testGetLatLng()
49
    {
50
        $this->assertEquals(
51
            [
52
                [
53
                    'lat' => '-37.8136276',
54
                    'lng' => '144.9630576',
55
                ],
56
            ],
57
            (new Geocoder())->getLatLng(self::VALID_ADDRESS)
58
        );
59
    }
60
61
    public function testGetLatLngMultipleResult()
62
    {
63
        $this->assertEquals(
64
            6,
65
            count((new Geocoder())->getLatLng(self::VALID_ADDRESS_MULTIPLE_RESULT))
66
        );
67
    }
68
69
    private function getExpectedNullData()
70
    {
71
        return '{
72
   "results" : [],
73
   "status" : "ZERO_RESULTS"
74
}
75
';
76
    }
77
78
    private function getExpectedData()
79
    {
80
        return '{
81
   "results" : [
82
      {
83
         "address_components" : [
84
            {
85
               "long_name" : "Melbourne",
86
               "short_name" : "Melbourne",
87
               "types" : [ "colloquial_area", "locality", "political" ]
88
            },
89
            {
90
               "long_name" : "Victoria",
91
               "short_name" : "VIC",
92
               "types" : [ "administrative_area_level_1", "political" ]
93
            },
94
            {
95
               "long_name" : "Australia",
96
               "short_name" : "AU",
97
               "types" : [ "country", "political" ]
98
            }
99
         ],
100
         "formatted_address" : "Melbourne VIC, Australia",
101
         "geometry" : {
102
            "bounds" : {
103
               "northeast" : {
104
                  "lat" : -37.5112737,
105
                  "lng" : 145.5125288
106
               },
107
               "southwest" : {
108
                  "lat" : -38.4338593,
109
                  "lng" : 144.5937418
110
               }
111
            },
112
            "location" : {
113
               "lat" : -37.8136276,
114
               "lng" : 144.9630576
115
            },
116
            "location_type" : "APPROXIMATE",
117
            "viewport" : {
118
               "northeast" : {
119
                  "lat" : -37.5112737,
120
                  "lng" : 145.5125288
121
               },
122
               "southwest" : {
123
                  "lat" : -38.4338593,
124
                  "lng" : 144.5937418
125
               }
126
            }
127
         },
128
         "place_id" : "ChIJ90260rVG1moRkM2MIXVWBAQ",
129
         "types" : [ "colloquial_area", "locality", "political" ]
130
      }
131
   ],
132
   "status" : "OK"
133
}
134
';
135
    }
136
}
137