Skip to content

Commit

Permalink
Fix #412
Browse files Browse the repository at this point in the history
  • Loading branch information
willdurand committed Apr 20, 2015
1 parent 7c6928a commit 6b570b5
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/Geocoder/Exception/CollectionIsEmpty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Geocoder\Exception;

class CollectionIsEmpty extends \RuntimeException implements Exception
{
}
6 changes: 4 additions & 2 deletions src/Geocoder/Model/AddressCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Geocoder\Model;

use Geocoder\Exception\CollectionIsEmpty;

final class AddressCollection implements \IteratorAggregate, \Countable
{
/**
Expand Down Expand Up @@ -34,12 +36,12 @@ public function count()
}

/**
* @return Address|null
* @return Address
*/
public function first()
{
if (empty($this->addresses)) {
return null;
throw new CollectionIsEmpty('The AddressCollection instance is empty.');
}

return reset($this->addresses);
Expand Down
65 changes: 65 additions & 0 deletions tests/.cached_responses/c7b4c0eac1b73b7eb7be01c558f330415f7ea0d6
Original file line number Diff line number Diff line change
@@ -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"
}
";
13 changes: 13 additions & 0 deletions tests/Geocoder/Tests/Model/AddressFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit 6b570b5

Please sign in to comment.