1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace KamranAhmed\Geocode; |
4
|
|
|
|
5
|
|
|
class GeocodeTest extends \PHPUnit_Framework_TestCase |
6
|
|
|
{ |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @dataProvider providerTestGeocodeProvider |
10
|
|
|
*/ |
11
|
|
|
public function testGeocode($address, $expected) |
12
|
|
|
{ |
13
|
|
|
$actual = new Geocode(); |
14
|
|
|
$location = $actual->get($address); |
15
|
|
|
$methods = array_keys($expected); |
16
|
|
|
$this->assertTrue($location->isValid()); |
17
|
|
|
foreach ($methods as $method) { |
18
|
|
|
$this->assertEquals($expected[$method], $location->$method()); |
19
|
|
|
} |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function testInvalidLocation() |
23
|
|
|
{ |
24
|
|
|
$geo = new Geocode(); |
25
|
|
|
$location = $geo->get("House of the rights for the poor people"); |
26
|
|
|
$this->assertFalse($location->isValid()); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @test |
31
|
|
|
* @expectedException \Exception |
32
|
|
|
* @expectedExceptionMessage Address is required in order to process |
33
|
|
|
*/ |
34
|
|
|
public function testEmptyOrNullAdress() |
35
|
|
|
{ |
36
|
|
|
$actual = new Geocode(); |
37
|
|
|
$actual->get(""); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
View Code Duplication |
public function testGeocodeProtocol() |
41
|
|
|
{ |
42
|
|
|
$actual = new Geocode(); |
43
|
|
|
$this->assertEquals( |
44
|
|
|
'http://maps.googleapis.com/maps/api/geocode/json?', |
45
|
|
|
$actual->getServiceUrl() |
46
|
|
|
); |
47
|
|
|
|
48
|
|
|
$actual = new Geocode(); |
49
|
|
|
$this->assertEquals( |
50
|
|
|
'http://maps.googleapis.com/maps/api/geocode/json?', |
51
|
|
|
$actual->getServiceUrl() |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
View Code Duplication |
public function testGeocodeKey() |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
$actual = new Geocode('DUMMYKEY'); |
58
|
|
|
$this->assertEquals( |
59
|
|
|
'https://maps.googleapis.com/maps/api/geocode/json?key=DUMMYKEY', |
60
|
|
|
$actual->getServiceUrl() |
61
|
|
|
); |
62
|
|
|
|
63
|
|
|
$actual = new Geocode('DUMMYKEY'); |
64
|
|
|
$this->assertEquals( |
65
|
|
|
'https://maps.googleapis.com/maps/api/geocode/json?key=DUMMYKEY', |
66
|
|
|
$actual->getServiceUrl() |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
public function providerTestGeocodeProvider() |
72
|
|
|
{ |
73
|
|
|
$providers = []; |
74
|
|
|
|
75
|
|
|
$providers[] = [ |
76
|
|
|
"1600 Amphitheatre Parkway, Mountain View, CA", |
77
|
|
|
[ |
78
|
|
|
'getAddress' => '1600 Amphitheatre Parkway, Mountain View, CA', |
79
|
|
|
'getCountry' => 'United States', |
80
|
|
|
'getShortCountry' => 'US', |
81
|
|
|
'getLocality' => 'Mountain View', |
82
|
|
|
'getShortLocality' => 'Mountain View', |
83
|
|
|
'getDistrict' => 'California', |
84
|
|
|
'getShortDistrict' => 'CA', |
85
|
|
|
'getPostcode' => '94043', |
86
|
|
|
'getStreetAddress' => 'Amphitheatre Parkway', |
87
|
|
|
'getShortStreetAddress' => 'Amphitheatre Pkwy', |
88
|
|
|
'getStreetNumber' => '1600', |
89
|
|
|
], |
90
|
|
|
]; |
91
|
|
|
|
92
|
|
|
$providers[] = [ |
93
|
|
|
"9 Little St, Beachburg, Ontario, Canada", |
94
|
|
|
[ |
95
|
|
|
'getAddress' => '9 Little St, Beachburg, Ontario, Canada', |
96
|
|
|
'getCountry' => 'Canada', |
97
|
|
|
'getShortCountry' => 'CA', |
98
|
|
|
'getLocality' => 'Beachburg', |
99
|
|
|
'getShortLocality'=> 'Beachburg', |
100
|
|
|
'getDistrict' => 'Ontario', |
101
|
|
|
'getShortDistrict'=> 'ON', |
102
|
|
|
'getPostcode' => 'K0J 1C0', |
103
|
|
|
'getTown' => '', |
104
|
|
|
'getStreetNumber' => '9', |
105
|
|
|
], |
106
|
|
|
]; |
107
|
|
|
|
108
|
|
|
return $providers; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
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.