Completed
Push — master ( 2ae44a...643987 )
by Daryl
01:45
created

TestGeocoder::testMakeLocation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

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