diff --git a/src/Geocoder/Exception/CollectionIsEmpty.php b/src/Geocoder/Exception/CollectionIsEmpty.php new file mode 100644 index 000000000..ed0aadb9a --- /dev/null +++ b/src/Geocoder/Exception/CollectionIsEmpty.php @@ -0,0 +1,7 @@ +addresses)) { - return null; + throw new CollectionIsEmpty('The AddressCollection instance is empty.'); } return reset($this->addresses); diff --git a/tests/.cached_responses/c7b4c0eac1b73b7eb7be01c558f330415f7ea0d6 b/tests/.cached_responses/c7b4c0eac1b73b7eb7be01c558f330415f7ea0d6 new file mode 100644 index 000000000..a33132c90 --- /dev/null +++ b/tests/.cached_responses/c7b4c0eac1b73b7eb7be01c558f330415f7ea0d6 @@ -0,0 +1,65 @@ +s:1907:"{ + "results" : [ + { + "address_components" : [ + { + "long_name" : "10", + "short_name" : "10", + "types" : [ "street_number" ] + }, + { + "long_name" : "Avenue Gambetta", + "short_name" : "Avenue Gambetta", + "types" : [ "route" ] + }, + { + "long_name" : "Paris", + "short_name" : "Paris", + "types" : [ "locality", "political" ] + }, + { + "long_name" : "Paris", + "short_name" : "75", + "types" : [ "administrative_area_level_2", "political" ] + }, + { + "long_name" : "Île-de-France", + "short_name" : "IDF", + "types" : [ "administrative_area_level_1", "political" ] + }, + { + "long_name" : "France", + "short_name" : "FR", + "types" : [ "country", "political" ] + }, + { + "long_name" : "75020", + "short_name" : "75020", + "types" : [ "postal_code" ] + } + ], + "formatted_address" : "10 Avenue Gambetta, 75020 Paris, France", + "geometry" : { + "location" : { + "lat" : 48.8631013, + "lng" : 2.3888086 + }, + "location_type" : "ROOFTOP", + "viewport" : { + "northeast" : { + "lat" : 48.8644502802915, + "lng" : 2.390157580291502 + }, + "southwest" : { + "lat" : 48.8617523197085, + "lng" : 2.387459619708498 + } + } + }, + "place_id" : "ChIJ4b303vJt5kcRF9AQdh4ZjWc", + "types" : [ "street_address" ] + } + ], + "status" : "OK" +} +"; \ No newline at end of file diff --git a/tests/Geocoder/Tests/Model/AddressFactoryTest.php b/tests/Geocoder/Tests/Model/AddressFactoryTest.php index db5ce10db..015b80c13 100644 --- a/tests/Geocoder/Tests/Model/AddressFactoryTest.php +++ b/tests/Geocoder/Tests/Model/AddressFactoryTest.php @@ -59,4 +59,17 @@ public function testFormatStringWithLeadingNumeral() $this->assertEquals('1st ave 1A', $addresses->first()->getStreetName()); } + + /** + * @expectedException \Geocoder\Exception\CollectionIsEmpty + */ + public function testCreateFromEmptyArray() + { + $addresses = $this->factory->createFromArray([]); + + $this->assertInstanceOf('Geocoder\Model\AddressCollection', $addresses); + $this->assertCount(0, $addresses); + + $addresses->first(); // expecting exception here + } }