Skip to content

Commit c15d7d2

Browse files
author
euromark
committed
Mock geocoding away.
1 parent 6539cd3 commit c15d7d2

File tree

13 files changed

+1266
-4
lines changed

13 files changed

+1266
-4
lines changed

Lib/GeocodeLib.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,9 @@ protected function _transformData($record) {
695695
* @return mixed
696696
**/
697697
protected function _fetch($url, $query) {
698-
$this->HttpSocket = new HttpSocket();
698+
if (!isset($this->HttpSocket)) {
699+
$this->HttpSocket = new HttpSocket();
700+
}
699701
foreach ($query as $k => $v) {
700702
if ($v === '') {
701703
unset($query[$k]);

Test/Case/Lib/GeocodeLibTest.php

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
App::uses('GeocodeLib', 'Tools.Lib');
44
App::uses('MyCakeTestCase', 'Tools.TestSuite');
5+
App::uses('HttpSocketResponse', 'Network/Http');
56

67
# google maps
78
Configure::write('Google', array(
@@ -64,6 +65,8 @@ public function setUp() {
6465
parent::setUp();
6566

6667
$this->Geocode = new GeocodeLib();
68+
69+
$this->mockFilePath = CakePlugin::path('Tools') . 'Test' . DS . 'test_files' . DS . 'google' . DS;
6770
}
6871

6972
public function tearDown() {
@@ -87,7 +90,11 @@ public function testReverseGeocode() {
8790
array(48.8934, 8.70492, 'B294, 75175 Pforzheim, Deutschland')
8891
);
8992

90-
foreach ($coords as $coord) {
93+
foreach ($coords as $k => $coord) {
94+
if (!$this->isDebug()) {
95+
$this->_getMock('reverse' . $k);
96+
}
97+
9198
$is = $this->Geocode->reverseGeocode($coord[0], $coord[1]);
9299
$this->assertTrue($is);
93100

@@ -111,6 +118,10 @@ public function testReverseGeocode() {
111118
public function testGeocodeInconclusive() {
112119
$address = 'Bibersfeld';
113120

121+
if (!$this->isDebug()) {
122+
$this->_getMock('inconclusive', 2);
123+
}
124+
114125
$this->Geocode->setOptions(array('allow_inconclusive' => true, 'min_accuracy' => GeocodeLib::ACC_POSTAL));
115126
$is = $this->Geocode->geocode($address);
116127
$this->assertTrue($is);
@@ -139,6 +150,11 @@ public function testGeocodeInconclusive() {
139150
* @return void
140151
*/
141152
public function testInvalid() {
153+
// Dont mock in debug mode (live query), otherwise mock it out
154+
if (!$this->isDebug()) {
155+
$this->_getMock('invalid');
156+
}
157+
142158
$this->Geocode->setOptions(array('allow_inconclusive' => false));
143159
$result = $this->Geocode->geocode('204 HWY 287 SOUTH, CACTUS, TX, 79013');
144160
$this->assertFalse($result);
@@ -151,6 +167,9 @@ public function testInvalid() {
151167
*/
152168
public function testGeocodeInconclusiveMinAccuracy() {
153169
$address = 'Bibersfeld';
170+
if (!$this->isDebug()) {
171+
$this->_getMock('inconclusive');
172+
}
154173

155174
$this->Geocode->setOptions(array('allow_inconclusive' => true, 'min_accuracy' => GeocodeLib::ACC_STREET));
156175
$is = $this->Geocode->geocode($address);
@@ -168,6 +187,9 @@ public function testGeocodeInconclusiveMinAccuracy() {
168187
*/
169188
public function testGeocodeExpect() {
170189
$address = 'Bibersfeld';
190+
if (!$this->isDebug()) {
191+
$this->_getMock('inconclusive');
192+
}
171193

172194
$this->Geocode->setOptions(array(
173195
'allow_inconclusive' => true,
@@ -287,6 +309,10 @@ public function testSetOptions() {
287309
* @return void
288310
*/
289311
public function testGeocode() {
312+
if (!$this->isDebug()) {
313+
$this->_getMock('geocode0');
314+
}
315+
290316
$address = '74523 Deutschland';
291317
//echo '<h2>'.$address.'</h2>';
292318
$is = $this->Geocode->geocode($address);
@@ -301,6 +327,10 @@ public function testGeocode() {
301327
//debug($is);
302328
$this->assertTrue(empty($is));
303329

330+
if (!$this->isDebug()) {
331+
$this->_getMock('geocode1');
332+
}
333+
304334
$address = 'Leopoldstraße 100, München';
305335
//echo '<h2>'.$address.'</h2>';
306336
$is = $this->Geocode->geocode($address);
@@ -317,6 +347,10 @@ public function testGeocode() {
317347
//debug($is);
318348
$this->assertTrue(empty($is));
319349

350+
if (!$this->isDebug()) {
351+
$this->_getMock('geocode2');
352+
}
353+
320354
$address = 'Oranienburger Straße 87, 10178 Berlin, Deutschland';
321355
//echo '<h2>'.$address.'</h2>';
322356
$is = $this->Geocode->geocode($address);
@@ -355,6 +389,10 @@ public function testGeocodeReachedQueryLimit() {
355389
* @return void
356390
*/
357391
public function testGeocodeBadApiKey() {
392+
if (!$this->isDebug()) {
393+
$this->_getMock('apikey');
394+
}
395+
358396
$address = 'Oranienburger Straße 87, 10178 Berlin, Deutschland';
359397
$result = $this->Geocode->geocode($address, array('sensor' => false, 'key' => 'testingBadApiKey'));
360398
$this->assertFalse($result);
@@ -369,6 +407,10 @@ public function testGeocodeBadApiKey() {
369407
* @return void
370408
*/
371409
public function testGeocodeInvalid() {
410+
if (!$this->isDebug()) {
411+
$this->_getMock('zero');
412+
}
413+
372414
$address = 'Hjfjosdfhosj, 78878 Mdfkufsdfk';
373415
$result = $this->Geocode->geocode($address);
374416
$this->assertFalse($result);
@@ -427,8 +469,13 @@ public function testGetMaxAddress() {
427469
* @return void
428470
*/
429471
public function testGeocodeMinAcc() {
472+
if (!$this->isDebug()) {
473+
$this->_getMock('minacc');
474+
}
475+
430476
// address = postal_code, minimum = street level
431477
$address = 'Deutschland';
478+
432479
$this->Geocode->setOptions(array('min_accuracy' => GeocodeLib::ACC_STREET));
433480
$is = $this->Geocode->geocode($address);
434481
$this->assertFalse($is);
@@ -543,4 +590,18 @@ public function testTransformData() {
543590
$this->assertEquals($expected, $result);
544591
}
545592

593+
protected function _getMock($type, $count = 1) {
594+
$this->Geocode->HttpSocket = $this->getMock('HttpSocket', array('get'));
595+
$responseContent = file_get_contents($this->mockFilePath . $type . '.json');
596+
$response = new HttpSocketResponse();
597+
$response->body = $responseContent;
598+
$response->code = 200;
599+
$this->Geocode->HttpSocket->response = $response;
600+
for ($i = 0; $i < $count; $i++) {
601+
$this->Geocode->HttpSocket->expects($this->at($i))
602+
->method('get')
603+
->will($this->returnValue($response));
604+
}
605+
}
606+
546607
}

Test/test_files/google/apikey.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"error_message" : "The provided API key is invalid.",
3+
"results" : [],
4+
"status" : "REQUEST_DENIED"
5+
}

Test/test_files/google/geocode0.json

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
{
2+
"results" : [
3+
{
4+
"address_components" : [
5+
{
6+
"long_name" : "74523",
7+
"short_name" : "74523",
8+
"types" : [ "postal_code" ]
9+
},
10+
{
11+
"long_name" : "Schwäbisch Hall",
12+
"short_name" : "Schwäbisch Hall",
13+
"types" : [ "locality", "political" ]
14+
},
15+
{
16+
"long_name" : "Schwäbisch Hall",
17+
"short_name" : "SHA",
18+
"types" : [ "administrative_area_level_3", "political" ]
19+
},
20+
{
21+
"long_name" : "Stuttgart",
22+
"short_name" : "Süd",
23+
"types" : [ "administrative_area_level_2", "political" ]
24+
},
25+
{
26+
"long_name" : "Baden-Württemberg",
27+
"short_name" : "BW",
28+
"types" : [ "administrative_area_level_1", "political" ]
29+
},
30+
{
31+
"long_name" : "Deutschland",
32+
"short_name" : "DE",
33+
"types" : [ "country", "political" ]
34+
}
35+
],
36+
"formatted_address" : "74523 Schwäbisch Hall, Deutschland",
37+
"geometry" : {
38+
"bounds" : {
39+
"northeast" : {
40+
"lat" : 49.1670039,
41+
"lng" : 9.875680899999999
42+
},
43+
"southwest" : {
44+
"lat" : 49.04597,
45+
"lng" : 9.5970288
46+
}
47+
},
48+
"location" : {
49+
"lat" : 49.1257616,
50+
"lng" : 9.7544127
51+
},
52+
"location_type" : "APPROXIMATE",
53+
"viewport" : {
54+
"northeast" : {
55+
"lat" : 49.1670039,
56+
"lng" : 9.875680899999999
57+
},
58+
"southwest" : {
59+
"lat" : 49.04597,
60+
"lng" : 9.5970288
61+
}
62+
}
63+
},
64+
"postcode_localities" : [ "Braunsbach", "Michelbach an der Bilz", "Schwäbisch Hall" ],
65+
"types" : [ "postal_code" ]
66+
}
67+
],
68+
"status" : "OK"
69+
}

Test/test_files/google/geocode1.json

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
{
2+
"results" : [
3+
{
4+
"address_components" : [
5+
{
6+
"long_name" : "100",
7+
"short_name" : "100",
8+
"types" : [ "street_number" ]
9+
},
10+
{
11+
"long_name" : "Leopoldstraße",
12+
"short_name" : "Leopoldstraße",
13+
"types" : [ "route" ]
14+
},
15+
{
16+
"long_name" : "Schwabing-Freimann",
17+
"short_name" : "Schwabing-Freimann",
18+
"types" : [ "sublocality_level_1", "sublocality", "political" ]
19+
},
20+
{
21+
"long_name" : "München",
22+
"short_name" : "M",
23+
"types" : [ "locality", "political" ]
24+
},
25+
{
26+
"long_name" : "Oberbayern",
27+
"short_name" : "Oberbayern",
28+
"types" : [ "administrative_area_level_2", "political" ]
29+
},
30+
{
31+
"long_name" : "Bayern",
32+
"short_name" : "BY",
33+
"types" : [ "administrative_area_level_1", "political" ]
34+
},
35+
{
36+
"long_name" : "Deutschland",
37+
"short_name" : "DE",
38+
"types" : [ "country", "political" ]
39+
},
40+
{
41+
"long_name" : "80802",
42+
"short_name" : "80802",
43+
"types" : [ "postal_code" ]
44+
}
45+
],
46+
"formatted_address" : "Leopoldstraße 100, 80802 München, Deutschland",
47+
"geometry" : {
48+
"location" : {
49+
"lat" : 48.16485000000001,
50+
"lng" : 11.58681
51+
},
52+
"location_type" : "ROOFTOP",
53+
"viewport" : {
54+
"northeast" : {
55+
"lat" : 48.16619898029151,
56+
"lng" : 11.5881589802915
57+
},
58+
"southwest" : {
59+
"lat" : 48.16350101970851,
60+
"lng" : 11.5854610197085
61+
}
62+
}
63+
},
64+
"types" : [ "street_address" ]
65+
}
66+
],
67+
"status" : "OK"
68+
}

Test/test_files/google/geocode2.json

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
{
2+
"results" : [
3+
{
4+
"address_components" : [
5+
{
6+
"long_name" : "87",
7+
"short_name" : "87",
8+
"types" : [ "street_number" ]
9+
},
10+
{
11+
"long_name" : "Oranienburger Straße",
12+
"short_name" : "Oranienburger Str.",
13+
"types" : [ "route" ]
14+
},
15+
{
16+
"long_name" : "Mitte",
17+
"short_name" : "Mitte",
18+
"types" : [ "sublocality_level_2", "sublocality", "political" ]
19+
},
20+
{
21+
"long_name" : "Bezirk Mitte",
22+
"short_name" : "Bezirk Mitte",
23+
"types" : [ "sublocality_level_1", "sublocality", "political" ]
24+
},
25+
{
26+
"long_name" : "Berlin",
27+
"short_name" : "Berlin",
28+
"types" : [ "locality", "political" ]
29+
},
30+
{
31+
"long_name" : "Berlin",
32+
"short_name" : "Berlin",
33+
"types" : [ "administrative_area_level_1", "political" ]
34+
},
35+
{
36+
"long_name" : "Deutschland",
37+
"short_name" : "DE",
38+
"types" : [ "country", "political" ]
39+
},
40+
{
41+
"long_name" : "10178",
42+
"short_name" : "10178",
43+
"types" : [ "postal_code" ]
44+
}
45+
],
46+
"formatted_address" : "Oranienburger Straße 87, 10178 Berlin, Deutschland",
47+
"geometry" : {
48+
"location" : {
49+
"lat" : 52.52332999999999,
50+
"lng" : 13.40021
51+
},
52+
"location_type" : "ROOFTOP",
53+
"viewport" : {
54+
"northeast" : {
55+
"lat" : 52.5246789802915,
56+
"lng" : 13.4015589802915
57+
},
58+
"southwest" : {
59+
"lat" : 52.5219810197085,
60+
"lng" : 13.3988610197085
61+
}
62+
}
63+
},
64+
"types" : [ "street_address" ]
65+
}
66+
],
67+
"status" : "OK"
68+
}

0 commit comments

Comments
 (0)