Browse Source

Catch invalid results

euromark 11 years ago
parent
commit
6539cd34aa
2 changed files with 20 additions and 0 deletions
  1. 9 0
      Lib/GeocodeLib.php
  2. 11 0
      Test/Case/Lib/GeocodeLibTest.php

+ 9 - 0
Lib/GeocodeLib.php

@@ -415,10 +415,19 @@ class GeocodeLib {
 			}
 			}
 			$this->result = $res;
 			$this->result = $res;
 		}
 		}
+		// Only necessary for allow_inconclusive and no valid results found
+		if (!$this->result && !empty($result['results'])) {
+			$this->result = $result['results'][0];
+		}
+
 		$this->result['valid_results'] = $validResults;
 		$this->result['valid_results'] = $validResults;
 		$this->result['all'] = $result['results'];
 		$this->result['all'] = $result['results'];
 
 
 		// validate
 		// validate
+		if (!$this->options['allow_inconclusive'] && $validResults === 0) {
+			$this->setError(__d('tools', 'No results found'));
+			return false;
+		}
 		if (!$this->options['allow_inconclusive'] && $validResults > 1) {
 		if (!$this->options['allow_inconclusive'] && $validResults > 1) {
 			$this->setError(__d('tools', 'Inconclusive result (total of %s)', $validResults));
 			$this->setError(__d('tools', 'Inconclusive result (total of %s)', $validResults));
 			return false;
 			return false;

+ 11 - 0
Test/Case/Lib/GeocodeLibTest.php

@@ -134,6 +134,17 @@ class GeocodeLibTest extends MyCakeTestCase {
 	}
 	}
 
 
 	/**
 	/**
+	 * GeocodeLibTest::testInvalid()
+	 *
+	 * @return void
+	 */
+	public function testInvalid() {
+		$this->Geocode->setOptions(array('allow_inconclusive' => false));
+		$result = $this->Geocode->geocode('204 HWY 287 SOUTH, CACTUS, TX, 79013');
+		$this->assertFalse($result);
+	}
+
+	/**
 	 * With lower min accuracy
 	 * With lower min accuracy
 	 *
 	 *
 	 * @return void
 	 * @return void