1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Clubdeuce\WPLib\Components\GoogleMaps\Tests\UnitTests; |
4
|
|
|
|
5
|
|
|
use Clubdeuce\WPLib\Components\GoogleMaps\Geocoder; |
6
|
|
|
use Clubdeuce\WPLib\Components\GoogleMaps\Tests\TestCase; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class TestGeocoder |
10
|
|
|
* @package Clubdeuce\WPLib\Components\GoogleMaps\Tests\UnitTests |
11
|
|
|
* @coversDefaultClass Clubdeuce\WPLib\Components\GoogleMaps\Geocoder |
12
|
|
|
* @group geocoder |
13
|
|
|
*/ |
14
|
|
|
class TestGeocoder extends TestCase { |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var Geocoder |
18
|
|
|
*/ |
19
|
|
|
private $_geocoder; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private $_api_key = '12345'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* |
28
|
|
|
*/ |
29
|
|
|
public function setUp() { |
30
|
|
|
$this->_geocoder = new Geocoder(['api_key' => $this->_api_key]); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @covers ::__construct |
35
|
|
|
* @covers ::api_key |
36
|
|
|
*/ |
37
|
|
|
public function testConstructorWithAPIKey() { |
38
|
|
|
$this->assertEquals($this->_api_key, $this->_geocoder->api_key()); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @covers ::_make_url |
43
|
|
|
*/ |
44
|
|
|
public function testMakeUrl() { |
45
|
|
|
$response = $this->reflectionMethodInvokeArgs($this->_geocoder, '_make_url', '123 Anywhere Street New York 10001'); |
46
|
|
|
|
47
|
|
|
$this->assertInternalType('string', $response); |
48
|
|
|
$this->assertRegExp('/address\=123\+Anywhere\+Street\+New\+York\+10001/', $response); |
49
|
|
|
$this->assertRegExp("/key={$this->_api_key}/", $response); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @covers ::_get_data |
54
|
|
|
*/ |
55
|
|
|
public function testGetDataCache() { |
56
|
|
|
wp_cache_add( md5(serialize('foo.bar')), 'foobar'); |
57
|
|
|
|
58
|
|
|
$this->assertEquals('foobar', $this->reflectionMethodInvokeArgs($this->_geocoder, '_get_data', 'foo.bar')); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @covers ::_make_location |
63
|
|
|
*/ |
64
|
|
|
public function testMakeLocation() { |
65
|
|
|
$response = json_decode(file_get_contents(TEST_INCLUDES_DIR . '/geocoder-response.json'), true); |
66
|
|
|
$location = $this->reflectionMethodInvokeArgs($this->_geocoder, '_make_location', $response['results'][0] ); |
67
|
|
|
|
68
|
|
|
$this->assertInstanceOf('\Clubdeuce\WPLib\Components\GoogleMaps\Location', $location); |
69
|
|
|
$this->assertEquals('1600 Amphitheatre Parkway, Mountain View, CA 94043, USA', $location->address()); |
70
|
|
|
$this->assertEquals('1600 Amphitheatre Parkway, Mountain View, CA 94043, USA', $location->formatted_address()); |
71
|
|
|
$this->assertEquals(37.4224764, $location->latitude()); |
72
|
|
|
$this->assertEquals(-122.0842499, $location->longitude()); |
73
|
|
|
$this->assertEquals('ChIJ2eUgeAK6j4ARbn5u_wAGqWA', $location->place_id()); |
74
|
|
|
$this->assertInternalType('array', $location->viewport()); |
75
|
|
|
$this->assertArrayHasKey('northeast', $location->viewport()); |
76
|
|
|
$this->assertArrayHasKey('southwest', $location->viewport()); |
77
|
|
|
$this->assertInternalType('array', $location->viewport()['northeast']); |
78
|
|
|
$this->assertArrayHasKey('lat', $location->viewport()['northeast']); |
79
|
|
|
$this->assertArrayHasKey('lng', $location->viewport()['northeast']); |
80
|
|
|
$this->assertEquals(37.4238253802915, $location->viewport()['northeast']['lat']); |
81
|
|
|
$this->assertEquals(-122.0829009197085, $location->viewport()['northeast']['lng']); |
82
|
|
|
$this->assertInternalType('array', $location->viewport()['southwest']); |
83
|
|
|
$this->assertArrayHasKey('lat', $location->viewport()['southwest']); |
84
|
|
|
$this->assertArrayHasKey('lng', $location->viewport()['southwest']); |
85
|
|
|
$this->assertEquals(37.4211274197085, $location->viewport()['southwest']['lat']); |
86
|
|
|
$this->assertEquals(-122.0855988802915, $location->viewport()['southwest']['lng']); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @covers ::_make_request |
91
|
|
|
*/ |
92
|
|
|
public function testMakeRequestInvalidURL() { |
93
|
|
|
$response = $this->reflectionMethodInvokeArgs($this->_geocoder, '_make_request', 'foo.bar'); |
94
|
|
|
$this->assertInstanceOf('WP_Error', $response); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
} |