Passed
Branch master (41ef34)
by Roberto
04:08
created

GoogleMapsGeocodingTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 198
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 102
dl 0
loc 198
c 0
b 0
f 0
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testCheckGeocodingConfigWithSensor() 0 3 1
A testCamelToSnake() 0 3 1
B setUp() 0 117 1
A testSnakeToCamel() 0 3 1
A testCheckGeocodingResponseOk() 0 23 1
A testCheckGeocodingConfig() 0 5 1
A testResponseKO() 0 4 1
1
<?php
2
/**
3
 * Copyright (c) 2018 - present
4
 * Google Maps PHP - GoogleMapsGeocodingTest.php
5
 * author: Roberto Belotti - [email protected]
6
 * web : robertobelotti.com, github.com/biscolab
7
 * Initial version created on: 5/9/2018
8
 * MIT license: https://github.com/biscolab/google-maps-php/blob/master/LICENSE
9
 */
10
11
namespace Biscolab\geocode\Tests;
12
13
use Biscolab\GoogleMaps\Api\Geocoding;
14
use Biscolab\GoogleMaps\Enum\GoogleMapsApiConfigFields;
15
use Biscolab\GoogleMaps\Exception\RequestException;
16
use Biscolab\GoogleMaps\Http\GoogleMapsResponse;
17
use Biscolab\GoogleMaps\Http\Result\GeocodingResultsCollection;
18
use function Biscolab\GoogleMaps\snake2Camel;
19
use Biscolab\GoogleMaps\Values\GoogleMapsResponseStatusValues;
20
use GuzzleHttp\Psr7\Response;
21
use PHPUnit\Framework\TestCase;
22
use function Biscolab\GoogleMaps\camel2Snake;
23
24
class GoogleMapsGeocodingTest extends TestCase {
25
26
	/**
27
	 * @var Geocoding
28
	 */
29
	protected $geocoding_no_key;
30
31
	/**
32
	 * @var Geocoding
33
	 */
34
	protected $geocoding_with_key;
35
36
	/**
37
	 * @var Geocoding
38
	 */
39
	protected $geocoding_with_sensor;
40
41
	/**
42
	 * @var Response
43
	 */
44
	protected $mock_response_ok;
45
46
	/**
47
	 * @var Response
48
	 */
49
	protected $mock_response_ko;
50
51
	public function setUp() {
52
53
		// This is the sample value from Google Maps official documentation
54
		$default_response_OK = [
55
			'results' => [
56
				0 => [
57
					'address_components' => [
58
						0 => [
59
							'long_name'  => '277',
60
							'short_name' => '277',
61
							'types'      => [
62
								0 => 'street_number',
63
							],
64
						],
65
						1 => [
66
							'long_name'  => 'Bedford Avenue',
67
							'short_name' => 'Bedford Ave',
68
							'types'      => [
69
								0 => 'route',
70
							],
71
						],
72
						2 => [
73
							'long_name'  => 'Williamsburg',
74
							'short_name' => 'Williamsburg',
75
							'types'      => [
76
								0 => 'neighborhood',
77
								1 => 'political',
78
							],
79
						],
80
						3 => [
81
							'long_name'  => 'Brooklyn',
82
							'short_name' => 'Brooklyn',
83
							'types'      => [
84
								0 => 'political',
85
								1 => 'sublocality',
86
								2 => 'sublocality_level_1',
87
							],
88
						],
89
						4 => [
90
							'long_name'  => 'Kings County',
91
							'short_name' => 'Kings County',
92
							'types'      => [
93
								0 => 'administrative_area_level_2',
94
								1 => 'political',
95
							],
96
						],
97
						5 => [
98
							'long_name'  => 'New York',
99
							'short_name' => 'NY',
100
							'types'      => [
101
								0 => 'administrative_area_level_1',
102
								1 => 'political',
103
							],
104
						],
105
						6 => [
106
							'long_name'  => 'United States',
107
							'short_name' => 'US',
108
							'types'      => [
109
								0 => 'country',
110
								1 => 'political',
111
							],
112
						],
113
						7 => [
114
							'long_name'  => '11211',
115
							'short_name' => '11211',
116
							'types'      => [
117
								0 => 'postal_code',
118
							],
119
						],
120
					],
121
					'formatted_address'  => '277 Bedford Ave, Brooklyn, NY 11211, USA',
122
					'geometry'           => [
123
						'location'      => [
124
							'lat' => 40.71422050000000325553628499619662761688232421875,
125
							'lng' => -73.961290300000001707303454168140888214111328125,
126
						],
127
						'location_type' => 'ROOFTOP',
128
						'viewport'      => [
129
							'northeast' => [
130
								'lat' => 40.7155694802914922547643072903156280517578125,
131
								'lng' => -73.9599413197084913917933590710163116455078125,
132
							],
133
							'southwest' => [
134
								'lat' => 40.712871519708500045453547500073909759521484375,
135
								'lng' => -73.9626392802914978119588340632617473602294921875,
136
							],
137
						],
138
					],
139
					'place_id'           => 'ChIJd8BlQ2BZwokRAFUEcm_qrcA',
140
					'types'              => [
141
						0 => 'street_address',
142
					],
143
				],
144
			],
145
			'status'  => 'OK',
146
		];
147
148
		$default_response_KO = array_merge($default_response_OK, [
149
			'status' => GoogleMapsResponseStatusValues::REQUEST_DENIED
150
		]);
151
152
		// geocoding with API key
153
		// Remember to associate a valid payment method to your project
154
		$this->geocoding_with_key = new Geocoding([
155
			GoogleMapsApiConfigFields::KEY => 'MyKey'
156
		]);
157
158
		// geocoding with sensor
159
		$this->geocoding_with_sensor = new Geocoding([
160
			GoogleMapsApiConfigFields::SENSOR => 'true'
161
		]);
162
163
		// geocoding with NO API key
164
		$this->geocoding_no_key = new Geocoding();
165
166
		$this->mock_response_ok = new Response(200, [], \GuzzleHttp\json_encode($default_response_OK));
167
		$this->mock_response_ko = new Response(200, [], \GuzzleHttp\json_encode($default_response_KO));
168
	}
169
170
	public function testCheckGeocodingConfig() {
171
172
		$this->assertEquals(Geocoding::SERVICE_ENDPOINT, $this->geocoding_with_key->getGoogleMapsApi()->getServiceEndpoint());
173
		$this->assertEquals('MyKey', $this->geocoding_with_key->getGoogleMapsApi()->getKey());
174
		$this->assertEquals('', $this->geocoding_no_key->getGoogleMapsApi()->getKey());
175
	}
176
177
	public function testCheckGeocodingConfigWithSensor() {
178
179
		$this->assertEquals('true', $this->geocoding_with_sensor->getGoogleMapsApi()->getSensor());
180
	}
181
182
	public function testCheckGeocodingResponseOk() {
183
184
		$response = new GoogleMapsResponse($this->mock_response_ok);
185
186
		/** @var GeocodingResultsCollection $result */
187
		$result = new GeocodingResultsCollection($response->getResults());
188
189
		$this->assertNotNull($result);
190
191
		$array_result = $result->first()->toArray();
192
		// Response array keys
193
		$this->assertArrayHasKey('address_components', $array_result);
194
		$this->assertArrayHasKey('geometry', $array_result);
195
		$this->assertArrayHasKey('place_id', $array_result);
196
		$this->assertArrayHasKey('formatted_address', $array_result);
197
		$this->assertArrayHasKey('types', $array_result);
198
		$this->assertArrayHasKey('location', $array_result['geometry']);
199
200
		$address = $result->first()->getAddress();
201
202
		$this->assertEquals(8, $address->count());
203
204
		$this->assertEquals(200, $response->getHttpStatusCode());
205
206
	}
207
208
	public function testResponseKO() {
209
210
		$this->expectException(RequestException::class);
211
		new GoogleMapsResponse($this->mock_response_ko);
212
	}
213
214
	public function testCamelToSnake() {
215
216
		$this->assertEquals('test_field_name', camel2Snake('testFieldName'));
217
	}
218
219
	public function testSnakeToCamel() {
220
221
		$this->assertEquals('testFieldName', snake2Camel('test_field_name'));
222
	}
223
224
}
225