Completed
Push — master ( fdd4bc...8c65ff )
by Daryl
02:15
created

TestGeocoder::testMakeLocation()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 24
rs 8.9713
cc 1
eloc 22
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
 * @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
}