From ebe2210fe55f6afae60ce154a5b1e6495bca5069 Mon Sep 17 00:00:00 2001 From: Eric GELOEN Date: Sun, 7 Apr 2013 21:09:09 +0200 Subject: [PATCH] Add Places Autocomplete support --- README.md | 2 + UPGRADE.md | 5 + doc/usage/places/autocomplete.md | 69 ++++ doc/usage/places/index.md | 7 + .../GoogleMap/Exception/HelperException.php | 10 + .../GoogleMap/Exception/PlaceException.php | 108 +++++ src/Ivory/GoogleMap/Helper/ApiHelper.php | 91 +++++ src/Ivory/GoogleMap/Helper/MapHelper.php | 104 ++--- .../Helper/Places/AutocompleteHelper.php | 237 +++++++++++ src/Ivory/GoogleMap/Places/Autocomplete.php | 369 ++++++++++++++++++ .../GoogleMap/Places/AutocompleteType.php | 50 +++ .../Tests/GoogleMap/Helper/ApiHelperTest.php | 124 ++++++ .../Tests/GoogleMap/Helper/MapHelperTest.php | 72 ++-- .../Helper/Places/AutocompleteHelperTest.php | 274 +++++++++++++ .../GoogleMap/Places/AutocompleteTest.php | 217 ++++++++++ .../GoogleMap/Places/AutocompleteTypeTest.php | 35 ++ 16 files changed, 1691 insertions(+), 83 deletions(-) create mode 100644 doc/usage/places/autocomplete.md create mode 100644 doc/usage/places/index.md create mode 100644 src/Ivory/GoogleMap/Exception/PlaceException.php create mode 100644 src/Ivory/GoogleMap/Helper/ApiHelper.php create mode 100644 src/Ivory/GoogleMap/Helper/Places/AutocompleteHelper.php create mode 100644 src/Ivory/GoogleMap/Places/Autocomplete.php create mode 100644 src/Ivory/GoogleMap/Places/AutocompleteType.php create mode 100644 tests/Ivory/Tests/GoogleMap/Helper/ApiHelperTest.php create mode 100644 tests/Ivory/Tests/GoogleMap/Helper/Places/AutocompleteHelperTest.php create mode 100644 tests/Ivory/Tests/GoogleMap/Places/AutocompleteTest.php create mode 100644 tests/Ivory/Tests/GoogleMap/Places/AutocompleteTypeTest.php diff --git a/README.md b/README.md index c1ef4527..20c76f1c 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,8 @@ controls, overlays, events & services through the Google Map API v3. - [Layers](http://github.com/egeloen/ivory-google-map/blob/master/doc/usage/layers/index.md) - [KML Layer](http://github.com/egeloen/ivory-google-map/blob/master/doc/usage/layers/kml_layer.md) - [Events](http://github.com/egeloen/ivory-google-map/blob/master/doc/usage/events.md) + - [Places](http://github.com/egeloen/ivory-google-map/blob/master/doc/usage/places/index.md) + - [Autocomplete](http://github.com/egeloen/ivory-google-map/blob/master/doc/usage/places/autocomplete.md) - [Services](http://github.com/egeloen/ivory-google-map/blob/master/doc/usage/services/index.md) - [Geocoding API](http://github.com/egeloen/ivory-google-map/blob/master/doc/usage/services/geocoding/geocoder.md) - [Directions API](http://github.com/egeloen/ivory-google-map/blob/master/doc/usage/services/directions/directions.md) diff --git a/UPGRADE.md b/UPGRADE.md index a9220ca0..340e2c6d 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -1,5 +1,10 @@ # UPGRADE +### 0.9.1 to 1.0.0 + + * The `MapHelper` constructor now takes an `ApiHelper` as first argument (all the other parameters have been moved + forward). + ### 0.9.0 to 0.9.1 * All helpers classes have been refactored in order to generate a JS container by map. diff --git a/doc/usage/places/autocomplete.md b/doc/usage/places/autocomplete.md new file mode 100644 index 00000000..323006e3 --- /dev/null +++ b/doc/usage/places/autocomplete.md @@ -0,0 +1,69 @@ +# Autocomplete + +The Places Autocomplete feature attaches to a text field on your web page, and monitors that field for character +entries. As text is entered, Autocomplete returns Place predictions to the application in the form of a drop-down pick +list. You can use the Places Autocomplete feature to help users find a specific location, or assist them with filling +out address fields in online forms. + +## Build your autocomplete + +In order to use the Google Autocomplete feature, you need to build & configure it. + +``` php +use Ivory\GoogleMap\Places\Autocomplete; +use Ivory\GoogleMap\Places\AutocompleteType; + +$autocomplete = new Autocomplete(); + +$autocomplete->setPrefixJavascriptVariable('place_autocomplete_'); +$autocomplete->setInputId('place_input'); + +$autocomplete->setInputAttributes(array('class' => 'my-class')); +$autocomplete->setInputAttribute('class', 'my-class'); + +$autocomplete->setValue('foo'); + +$autocomplete->setTypes(array(AutocompleteType::ESTABLISHMENT)); +$autocomplete->setBound(-2.1, -3.9, 2.6, 1.4, true, true); + +$autocomplete->setAsync(false); +$autocomplete->setLanguage('en'); +``` + +## Render your autocomplete + +Now, you have builded & configured your autocomplete, you can render it. For this purpose, you will need the +`Ivory\GoogleMap\Helper\Places\AutocompleteHelper` which allows to render the autocomplete html container & some +javascript for being able to render it. + +``` php +use Ivory\GoogleMap\Helper\Places\AutocompleteHelper; + +$autocompleteHelper = new AutocompleteHelper(); +``` + +### Render the HTML container + +``` +echo $autocompleteHelper->renderHtmlContainer($autocomplete); +``` + +This function renders an html input with the ID, the value & the html attributes: + +``` html + +``` + +### Render the javascript + +``` +echo $autocompleteHelper->renderJavascripts($autocomplete); +``` + +This function renders an html javascript block with all code needed for displaying your autocomplete. + +``` html + +``` diff --git a/doc/usage/places/index.md b/doc/usage/places/index.md new file mode 100644 index 00000000..59f73411 --- /dev/null +++ b/doc/usage/places/index.md @@ -0,0 +1,7 @@ +# Places + +The Google Places JavaScript library's functions enable your application to search for Places (defined in this API as +establishments, geographic locations, or prominent points of interest) contained within a defined area, such as the +bounds of a map, or around a fixed point. + + 1. [Autocomplete](http://github.com/egeloen/ivory-google-map/blob/master/doc/usage/places/autocomplete.md) diff --git a/src/Ivory/GoogleMap/Exception/HelperException.php b/src/Ivory/GoogleMap/Exception/HelperException.php index 8bc5be99..1f4adb0a 100644 --- a/src/Ivory/GoogleMap/Exception/HelperException.php +++ b/src/Ivory/GoogleMap/Exception/HelperException.php @@ -81,6 +81,16 @@ public static function invalidMapTypeId() return new static(sprintf('The map type id can only be : %s.', implode(', ', MapTypeId::getMapTypeIds()))); } + /** + * Gets the "INVALID AUTOCOMPLETE BOUND" exception. + * + * @return \Ivory\GoogleMap\Exception\TemplatingException The "INVALID AUTOCOMPLETE BOUND" exception. + */ + public static function invalidAutocompleteBound() + { + return new static('The place autocomplete bound must have coordinates.'); + } + /** * Gets the "INVALID SCALE CONTROL STYLE" exception. * diff --git a/src/Ivory/GoogleMap/Exception/PlaceException.php b/src/Ivory/GoogleMap/Exception/PlaceException.php new file mode 100644 index 00000000..bc31e877 --- /dev/null +++ b/src/Ivory/GoogleMap/Exception/PlaceException.php @@ -0,0 +1,108 @@ + + * + * For the full copyright and license information, please read the LICENSE + * file that was distributed with this source code. + */ + +namespace Ivory\GoogleMap\Exception; + +use Ivory\GoogleMap\Places\AutocompleteType; + +/** + * Place exception. + * + * @author GeLo + */ +class PlaceException extends Exception +{ + /** + * Gets the "AUTOCOMPLETE TYPE ALREADY EXISTS" exception. + * + * @param string $type The type. + * + * @return \Ivory\GoogleMap\Exception\PlaceException The "AUTOCOMPLETE TYPE ALREADY EXISTS" exception. + */ + public static function autocompleteTypeAlreadyExists($type) + { + return new static(sprintf('The place autocomplete type "%s" already exists.', $type)); + } + + /** + * Gets the "AUTOCOMPLETE TYPE DOES NOT EXIST" exception. + * + * @param string $type The type. + * + * @return \Ivory\GoogleMap\Exception\PlaceException The "AUTOCOMPLETE TYPE DOES NOT EXIST" exception. + */ + public static function autocompleteTypeDoesNotExist($type) + { + return new static(sprintf('The place autocomplete type "%s" does not exist.', $type)); + } + + /** + * Gets the "INVALID AUTOCOMPLETE ASYNC" exception. + * + * @return \Ivory\GoogleMap\Exception\PlaceException The "INVALID AUTOCOMPLETE ASYNC" exception. + */ + public static function invalidAutocompleteAsync() + { + return new static('The asynchronous load of a place autocomplete must be a boolean value.'); + } + + /** + * Gets the "INVALID AUTOCOMPLETE BOUND" exception. + * + * @return \Ivory\GoogleMap\Exception\PlaceException The "INVALID AUTOCOMPLETE BOUND" exception. + */ + public static function invalidAutocompleteBound() + { + return new static(sprintf( + '%s'.PHP_EOL.'%s'.PHP_EOL.'%s'.PHP_EOL.'%s'.PHP_EOL.'%s', + 'The bound setter arguments is invalid.', + 'The available prototypes are :', + ' - function setBound(Ivory\GoogleMap\Base\Bound $bound)', + ' - function setBount('. + 'Ivory\GoogleMap\Base\Coordinate $southWest, '. + 'Ivory\GoogleMap\Base\Coordinate $northEast'. + ')', + ' - function setBound('. + 'double $southWestLatitude, '. + 'double $southWestLongitude, '. + 'double $northEastLatitude, '. + 'double $northEastLongitude, '. + 'boolean southWestNoWrap = true, '. + 'boolean $northEastNoWrap = true'. + ')' + )); + } + + /** + * Gets the "INVALID AUTOCOMPLETE INPUT ID" exception. + * + * @return \Ivory\GoogleMap\Exception\PlaceException The "INVALID AUTOCOMPLETE INPUT ID" exception. + */ + public static function invalidAutocompleteInputId() + { + return new static('The place autocomplete input ID must be a string value.'); + } + + /** + * Gets the "INVALID AUTOCOMPLETE TYPE" exception. + * + * @return \Ivory\GoogleMap\Exception\PlaceException The "INVALID AUTOCOMPLETE TYPE" exception. + */ + public static function invalidAutocompleteType() + { + return new static( + sprintf( + 'The place autocomplete type can only be: %s.', + implode(', ', AutocompleteType::getAvailableAutocompleteTypes()) + ) + ); + } +} diff --git a/src/Ivory/GoogleMap/Helper/ApiHelper.php b/src/Ivory/GoogleMap/Helper/ApiHelper.php new file mode 100644 index 00000000..12b70c82 --- /dev/null +++ b/src/Ivory/GoogleMap/Helper/ApiHelper.php @@ -0,0 +1,91 @@ + + * + * For the full copyright and license information, please read the LICENSE + * file that was distributed with this source code. + */ + +namespace Ivory\GoogleMap\Helper; + +/** + * Google Map API helper. + * + * @author GeLo + */ +class ApiHelper +{ + /** @var boolean */ + protected $loaded; + + /** + * Creates a Google Map API helper. + */ + public function __construct() + { + $this->loaded = false; + } + + /** + * Checks if the API is already loaded. + * + * @return boolean TRUE if the API is already loaded else FALSE. + */ + public function isLoaded() + { + return $this->loaded; + } + + /** + * Renders the API. + * + * @param string $language The language. + * @param array $libraries Additionnal libraries. + * @param string $callback A JS callback. + * @param boolean $sensor The sensor flag. + * + * @return string The HTML output. + */ + public function render( + $language = 'en', + array $libraries = array(), + $callback = null, + $sensor = false + ) + { + $this->loaded = true; + + $otherParameters = !empty($libraries) ? sprintf('libraries=%s&', implode(',', $libraries)) : null; + $otherParameters .= sprintf('sensor=%s', $sensor ? 'true' : 'false'); + + $options = array( + 'language' => $language, + 'other_params' => $otherParameters, + ); + + $jsonOptions = substr(json_encode($options), 0, -1); + + if ($callback !== null) { + $jsonOptions .= sprintf(', "callback": %s}', $callback); + } else { + $jsonOptions .= '}'; + } + + $loader = sprintf('google.load("maps", "3", %s);', $jsonOptions); + + $output = array(); + $output[] = ''.PHP_EOL; + + $output[] = sprintf( + ''.PHP_EOL, + '//www.google.com/jsapi?callback=load_ivory_google_map_api' + ); + + return implode('', $output); + } +} diff --git a/src/Ivory/GoogleMap/Helper/MapHelper.php b/src/Ivory/GoogleMap/Helper/MapHelper.php index 3174dd3f..957dd275 100644 --- a/src/Ivory/GoogleMap/Helper/MapHelper.php +++ b/src/Ivory/GoogleMap/Helper/MapHelper.php @@ -46,8 +46,8 @@ */ class MapHelper { - /** @var boolean */ - protected $loaded; + /** @var \Ivory\GoogleMap\Helper\ApiHelper */ + protected $apiHelper; /** @var \Ivory\GoogleMap\Helper\Base\CoordinateHelper */ protected $coordinateHelper; @@ -164,6 +164,7 @@ class MapHelper * helper. */ public function __construct( + ApiHelper $apiHelper = null, CoordinateHelper $coordinateHelper = null, BoundHelper $boundHelper = null, PointHelper $pointHelper = null, @@ -189,7 +190,9 @@ public function __construct( KMLLayerHelper $kmlLayerHelper = null, EventManagerHelper $eventManagerHelper = null ) { - $this->loaded = false; + if ($apiHelper === null) { + $apiHelper = new ApiHelper(); + } if ($coordinateHelper === null) { $coordinateHelper = new CoordinateHelper(); @@ -287,6 +290,8 @@ public function __construct( $eventManagerHelper = new EventManagerHelper(); } + $this->setApiHelper($apiHelper); + $this->setCoordinateHelper($coordinateHelper); $this->setBoundHelper($boundHelper); $this->setPointHelper($pointHelper); @@ -317,6 +322,26 @@ public function __construct( $this->setEventManagerHelper($eventManagerHelper); } + /** + * Gets the API helper. + * + * @return \Ivory\GoogleMap\Helper\ApiHelper The API helper. + */ + public function getApiHelper() + { + return $this->apiHelper; + } + + /** + * Sets the API helper. + * + * @param \Ivory\GoogleMap\Helper\ApiHelper $apiHelper The API helper. + */ + public function setApiHelper(ApiHelper $apiHelper) + { + $this->apiHelper = $apiHelper; + } + /** * Gets the coordinate helper. * @@ -870,8 +895,8 @@ public function renderJavascripts(Map $map) { $output = array(); - if (!$this->loaded && !$map->isAsync()) { - $output[] = $this->renderJsAPI($map); + if (!$this->apiHelper->isLoaded() && !$map->isAsync()) { + $output[] = $this->apiHelper->render($map->getLanguage(), $this->getLibraries($map)); } $output[] = ''.PHP_EOL; - if (!$this->loaded && $map->isAsync()) { - $output[] = $this->renderJsAPI($map); - } - - return implode('', $output); - } - - /** - * Renders the javascripts API. - * - * @param \Ivory\GoogleMap\Map $map The map - * - * @return string The HTML output. - */ - public function renderJsAPI(Map $map) - { - $this->loaded = true; - - $output = array(); - - $options = array( - 'language' => $map->getLanguage(), - 'other_params' => http_build_query(array( - 'libraries' => 'geometry', - 'sensor' => 'false', - )), - ); - - $jsonOptions = substr(json_encode($options), 0, -1); - - if ($map->isAsync()) { - $jsonOptions .= ', "callback": load_ivory_google_map}'; - } else { - $jsonOptions .= '}'; + if (!$this->apiHelper->isLoaded() && $map->isAsync()) { + $output[] = $this->apiHelper->render( + $map->getLanguage(), + $this->getLibraries($map), + 'load_ivory_google_map' + ); } - $output[] = sprintf( - ''.PHP_EOL, - sprintf('google.load("maps", "3", %s);', $jsonOptions) - ); - - $output[] = sprintf( - ''.PHP_EOL, - '//www.google.com/jsapi?callback=load_ivory_google_map_api' - ); - return implode('', $output); } @@ -1557,6 +1544,25 @@ public function renderMapBound(Map $map) ); } + /** + * Gets the libraries needed for the map. + * + * @param \Ivory\GoogleMap\Map $map The map. + * + * @return array The map libraries. + */ + protected function getLibraries(Map $map) + { + $libraries = array(); + + $encodedPolylines = $map->getEncodedPolylines(); + if (!empty($encodedPolylines)) { + $libraries[] = 'geometry'; + } + + return $libraries; + } + /** * Gets the javascript container name according to the map. * diff --git a/src/Ivory/GoogleMap/Helper/Places/AutocompleteHelper.php b/src/Ivory/GoogleMap/Helper/Places/AutocompleteHelper.php new file mode 100644 index 00000000..fb0cb120 --- /dev/null +++ b/src/Ivory/GoogleMap/Helper/Places/AutocompleteHelper.php @@ -0,0 +1,237 @@ + + * + * For the full copyright and license information, please read the LICENSE + * file that was distributed with this source code. + */ + +namespace Ivory\GoogleMap\Helper\Places; + +use Ivory\GoogleMap\Exception\HelperException; +use Ivory\GoogleMap\Helper\ApiHelper; +use Ivory\GoogleMap\Helper\Base\CoordinateHelper; +use Ivory\GoogleMap\Helper\Base\BoundHelper; +use Ivory\GoogleMap\Places\Autocomplete; + +/** + * Places autocomplete helper. + * + * @author GeLo + */ +class AutocompleteHelper +{ + /** @var \Ivory\GoogleMap\Helper\ApiHelper */ + protected $apiHelper; + + /** @var \Ivory\GoogleMap\Helper\Base\CoordinateHelper */ + protected $coordinateHelper; + + /** @var \Ivory\GoogleMap\Helper\Base\BoundHelper */ + protected $boundHelper; + + /** + * Creates an autocomplete helper. + * + * @param \Ivory\GoogleMap\Helper\ApiHelper $apiHelper The API helper. + * @param \Ivory\GoogleMap\Helper\Base\CoordinateHelper $coordinateHelper The coordinate helper. + * @param \Ivory\GoogleMap\Helper\Base\BoundHelper $boundHelper The bound helper. + */ + public function __construct( + ApiHelper $apiHelper = null, + CoordinateHelper $coordinateHelper = null, + BoundHelper $boundHelper = null + ) + { + if ($apiHelper === null) { + $apiHelper = new ApiHelper(); + } + + if ($coordinateHelper === null) { + $coordinateHelper = new CoordinateHelper(); + } + + if ($boundHelper === null) { + $boundHelper = new BoundHelper(); + } + + $this->setApiHelper($apiHelper); + $this->setCoordinateHelper($coordinateHelper); + $this->setBoundHelper($boundHelper); + } + + /** + * Gets the API helper. + * + * @return \Ivory\GoogleMap\Helper\ApiHelper The API helper. + */ + public function getApiHelper() + { + return $this->apiHelper; + } + + /** + * Sets the API helper. + * + * @param \Ivory\GoogleMap\Helper\ApiHelper $apiHelper The API helper. + */ + public function setApiHelper(ApiHelper $apiHelper) + { + $this->apiHelper = $apiHelper; + } + + /** + * Gets the coordinate helper. + * + * @return \Ivory\GoogleMap\Helper\Base\CoordinateHelper The coordinate helper. + */ + public function getCoordinateHelper() + { + return $this->coordinateHelper; + } + + /** + * Sets the coordinate helper. + * + * @param \Ivory\GoogleMap\Helper\Base\CoordinateHelper $coordinateHelper The coordinate helper. + */ + public function setCoordinateHelper(CoordinateHelper $coordinateHelper) + { + $this->coordinateHelper = $coordinateHelper; + } + + /** + * Gets the bound helper. + * + * @return \Ivory\GoogleMap\Helper\Base\BoundHelper The bound helper. + */ + public function getBoundHelper() + { + return $this->boundHelper; + } + + /** + * Sets the bound helper. + * + * @param \Ivory\GoogleMap\Helper\Base\BoundHelper $boundHelper The bound helper. + */ + public function setBoundHelper(BoundHelper $boundHelper) + { + $this->boundHelper = $boundHelper; + } + + /** + * Renders the autocomplete HTML container. + * + * @param \Ivory\GoogleMap\Places\Autocomplete $autocomplete The autocomplete. + * + * @return string The HTML output. + */ + public function renderHtmlContainer(Autocomplete $autocomplete) + { + $inputAttributes = $autocomplete->getInputAttributes(); + + $inputAttributes['id'] = $autocomplete->getInputId(); + + if ($autocomplete->hasValue()) { + $inputAttributes['value'] = $autocomplete->getValue(); + } + + $htmlAttributes = array(); + foreach ($inputAttributes as $attribute => $value) { + $htmlAttributes[] = sprintf('%s="%s"', $attribute, $value); + } + + return sprintf(''.PHP_EOL, implode(' ', $htmlAttributes)); + } + + /** + * Renders the autocomplete javascripts. + * + * @param \Ivory\GoogleMap\Places\Autocomplete $autocomplete The autocomplete. + * + * @throws \Ivory\GoogleMap\Exception\HelperException if the autocomplete bound does not have coordinates. + * + * @return string The HTML output. + */ + public function renderJavascripts(Autocomplete $autocomplete) + { + $output = array(); + + if (!$this->apiHelper->isLoaded() && !$autocomplete->isAsync()) { + $output[] = $this->apiHelper->render($autocomplete->getLanguage(), array('places')); + } + + $output[] = ''.PHP_EOL; + + if (!$this->apiHelper->isLoaded() && $autocomplete->isAsync()) { + $output[] = $this->apiHelper->render( + $autocomplete->getLanguage(), + array('places'), + 'load_ivory_google_place' + ); + } + + return implode('', $output); + } + + /** + * Renders the autocomplete. + * + * @param \Ivory\GoogleMap\Places\Autocomplete $autocomplete The autocomplete. + * + * @return string The JS output. + */ + public function renderAutocomplete(Autocomplete $autocomplete) + { + $types = $autocomplete->getTypes(); + + if (!empty($types)) { + $jsonOptions = substr(json_encode(array('types' => $types)), 0, -1); + } else { + $jsonOptions = '{'; + } + + if ($autocomplete->hasBound()) { + if (!empty($types)) { + $jsonOptions .= ', '; + } + + $jsonOptions .= sprintf('"bounds": %s}', $autocomplete->getBound()->getJavascriptVariable()); + } else { + $jsonOptions .= '}'; + } + + return sprintf( + '%s = new google.maps.places.Autocomplete(document.getElementById(\'%s\', %s));'.PHP_EOL, + $autocomplete->getJavascriptVariable(), + $autocomplete->getInputId(), + $jsonOptions + ); + } +} diff --git a/src/Ivory/GoogleMap/Places/Autocomplete.php b/src/Ivory/GoogleMap/Places/Autocomplete.php new file mode 100644 index 00000000..fd1202e3 --- /dev/null +++ b/src/Ivory/GoogleMap/Places/Autocomplete.php @@ -0,0 +1,369 @@ + + * + * For the full copyright and license information, please read the LICENSE + * file that was distributed with this source code. + */ + +namespace Ivory\GoogleMap\Places; + +use Ivory\GoogleMap\Assets\AbstractJavascriptVariableAsset; +use Ivory\GoogleMap\Base\Bound; +use Ivory\GoogleMap\Base\Coordinate; +use Ivory\GoogleMap\Exception\PlaceException; + +/** + * Places autocomplete. + * + * @author GeLo + */ +class Autocomplete extends AbstractJavascriptVariableAsset +{ + /** @var string */ + protected $inputId; + + /** @var \Ivory\GoogleMap\Base\Bound */ + protected $bound; + + /** @var array */ + protected $types; + + /** @var string */ + protected $value; + + /** @var array */ + protected $inputAttributes; + + /** @var boolean */ + protected $async; + + /** @var string */ + protected $language; + + /** + * Creates a place autocomplete. + */ + public function __construct() + { + $this->setPrefixJavascriptVariable('place_autocomplete_'); + + $this->inputId = 'place_input'; + $this->inputAttributes = array( + 'type' => 'text', + 'placeholder' => 'off', + ); + + $this->types = array(); + $this->async = false; + $this->language = 'en'; + } + + /** + * Gets the autocomplete input ID. + * + * @return string The autocomplete input ID. + */ + public function getInputId() + { + return $this->inputId; + } + + /** + * Sets the autocomplete input ID. + * + * @param string $inputId The autocomplete input ID. + * + * @throws \Ivory\GoogleMap\Exception\PlaceException If the input ID is not a valid string. + */ + public function setInputId($inputId) + { + if (!is_string($inputId) || (strlen($inputId) === 0)) { + throw PlaceException::invalidAutocompleteInputId(); + } + + $this->inputId = $inputId; + } + + /** + * Checks if the autocomplete has a bound. + * + * @return boolean TRUE if the autocomplete has a bound else FALSE. + */ + public function hasBound() + { + return $this->bound !== null; + } + + /** + * Gets the autocomplete bound. + * + * @return \Ivory\GoogleMap\Base\Bound The autocomplete bound. + */ + public function getBound() + { + return $this->bound; + } + + /** + * Sets the autocomplete bound. + * + * Available prototypes: + * - function setBound(Ivory\GoogleMap\Base\Bound $bound = null) + * - function setBount(Ivory\GoogleMap\Base\Coordinate $southWest, Ivory\GoogleMap\Base\Coordinate $northEast) + * - function setBound( + * double $southWestLatitude, + * double $southWestLongitude, + * double $northEastLatitude, + * double $northEastLongitude, + * boolean southWestNoWrap = true, + * boolean $northEastNoWrap = true + * ) + * + * @throws \Ivory\GoogleMap\Exception\PlaceException If the bound is not valid (prototypes). + */ + public function setBound() + { + $args = func_get_args(); + + if (isset($args[0]) && ($args[0] instanceof Bound)) { + $this->bound = $args[0]; + } elseif ((isset($args[0]) && ($args[0] instanceof Coordinate)) + && (isset($args[1]) && ($args[1] instanceof Coordinate)) + ) { + if ($this->bound === null) { + $this->bound = new Bound(); + } + + $this->bound->setSouthWest($args[0]); + $this->bound->setNorthEast($args[1]); + } elseif ((isset($args[0]) && is_numeric($args[0])) + && (isset($args[1]) && is_numeric($args[1])) + && (isset($args[2]) && is_numeric($args[2])) + && (isset($args[3]) && is_numeric($args[3])) + ) { + if ($this->bound === null) { + $this->bound = new Bound(); + } + + $this->bound->setSouthWest(new Coordinate($args[0], $args[1])); + $this->bound->setNorthEast(new Coordinate($args[2], $args[3])); + + if (isset($args[4]) && is_bool($args[4])) { + $this->bound->getSouthWest()->setNoWrap($args[4]); + } + + if (isset($args[5]) && is_bool($args[5])) { + $this->bound->getNorthEast()->setNoWrap($args[5]); + } + } elseif (!isset($args[0])) { + $this->bound = null; + } else { + throw PlaceException::invalidAutocompleteBound(); + } + } + + /** + * Checks if the autocomplete has types. + * + * @return boolean TRUE if the autocomplete has types else FALSE. + */ + public function hasTypes() + { + return !empty($this->types); + } + + /** + * Checks if the autocomplete has a specific type. + * + * @param string $type The type. + * + * @return boolean TRUE if the autocomplete has te specific type else FALSE. + */ + public function hasType($type) + { + return array_search($type, $this->types) !== false; + } + + /** + * Gets the autocomplete types. + * + * @return array The autocomplete types. + */ + public function getTypes() + { + return $this->types; + } + + /** + * Sets the autocomplete types. + * + * @param array $types The autocomplete types. + */ + public function setTypes(array $types) + { + $this->types = array(); + + foreach ($types as $type) { + $this->addType($type); + } + } + + /** + * Adds a type to the autocomplete. + * + * @param string $type The type to add. + * + * @throws \Ivory\GoogleMap\Exception\PlaceException If the type is not valid. + * @throws \Ivory\GoogleMap\Exception\PlaceException If the type already exists. + */ + public function addType($type) + { + if (!in_array($type, AutocompleteType::getAvailableAutocompleteTypes())) { + throw PlaceException::invalidAutocompleteType(); + } + + if ($this->hasType($type)) { + throw PlaceException::autocompleteTypeAlreadyExists($type); + } + + $this->types[] = $type; + } + + /** + * Removes a type from the autocomplete. + * + * @param string $type The type to remove. + * + * @throws \Ivory\GoogleMap\Exception\PlaceException If the type does not exist. + */ + public function removeType($type) + { + if (!$this->hasType($type)) { + throw PlaceException::autocompleteTypeDoesNotExist($type); + } + + $index = array_search($type, $this->types); + unset($this->types[$index]); + } + + /** + * Checks if the autocomplete has a value. + * + * @return boolean TRUE if the autocomplete has a value else FALSE. + */ + public function hasValue() + { + return $this->value !== null; + } + + /** + * Gets the autocomplete value. + * + * @return string The autocomplete value. + */ + public function getValue() + { + return $this->value; + } + + /** + * Sets the autocomplete value. + * + * @param string $value The autocomplete value. + */ + public function setValue($value) + { + $this->value = $value; + } + + /** + * Gets the autocomplete input attributes. + * + * @return array The autocomplete input attributes. + */ + public function getInputAttributes() + { + return $this->inputAttributes; + } + + /** + * Sets the autocomplete input attributes. + * + * @param array $inputAttributes The autocomplete input attributes. + */ + public function setInputAttributes(array $inputAttributes) + { + $this->inputAttributes = array(); + + foreach ($inputAttributes as $name => $value) { + $this->setInputAttribute($name, $value); + } + } + + /** + * Sets an autocomplete attribute. + * + * You can remove an attribute by setting it to `null`. + * + * @param string $name The attribute name. + * @param mixed $value The attribute value. + */ + public function setInputAttribute($name, $value) + { + if ($value === null) { + if (isset($this->inputAttributes[$name])) { + unset($this->inputAttributes[$name]); + } + } else { + $this->inputAttributes[$name] = $value; + } + } + + /** + * Checks if the autocomplete is loaded asynchronously. + * + * @return boolean TRUE if the autocomplete is loaded asynchronounsly else FALSE. + */ + public function isAsync() + { + return $this->async; + } + + /** + * Sets if the autocomplete is loaded asynchronously. + * + * @param boolean $async TRUE if the autocomplete is loaded asynchronously else FALSE. + */ + public function setAsync($async) + { + if (!is_bool($async)) { + throw PlaceException::invalidAutocompleteAsync(); + } + + $this->async = $async; + } + + /** + * Gets the autocomplete language + * + * @return string The autocomplete language + */ + public function getLanguage() + { + return $this->language; + } + + /** + * Sets the autocomplete language. + * + * @param string $language The autocomplete language + */ + public function setLanguage($language) + { + $this->language = $language; + } +} diff --git a/src/Ivory/GoogleMap/Places/AutocompleteType.php b/src/Ivory/GoogleMap/Places/AutocompleteType.php new file mode 100644 index 00000000..b2ce866f --- /dev/null +++ b/src/Ivory/GoogleMap/Places/AutocompleteType.php @@ -0,0 +1,50 @@ + + * + * For the full copyright and license information, please read the LICENSE + * file that was distributed with this source code. + */ + +namespace Ivory\GoogleMap\Places; + +/** + * Place autocomplete type. + * + * @author GeLo + */ +class AutocompleteType +{ + const ESTABLISHMENT = 'establishment'; + const GEOCODE = 'geocode'; + const REGIONS = '(regions)'; + const CITIES = '(cities)'; + + /** + * Disabled constructor. + * + * @codeCoverageIgnore + */ + final private function __construct() + { + + } + + /** + * Gets the available autocomplete types. + * + * @return array The available autocomplete types. + */ + public static function getAvailableAutocompleteTypes() + { + return array( + self::ESTABLISHMENT, + self::GEOCODE, + self::REGIONS, + self::CITIES, + ); + } +} diff --git a/tests/Ivory/Tests/GoogleMap/Helper/ApiHelperTest.php b/tests/Ivory/Tests/GoogleMap/Helper/ApiHelperTest.php new file mode 100644 index 00000000..344792bb --- /dev/null +++ b/tests/Ivory/Tests/GoogleMap/Helper/ApiHelperTest.php @@ -0,0 +1,124 @@ + + * + * For the full copyright and license information, please read the LICENSE + * file that was distributed with this source code. + */ + +namespace Ivory\Tests\GoogleMap\Helper; + +use Ivory\GoogleMap\Helper\ApiHelper; + +/** + * Api helper test. + * + * @author GeLo + */ +class ApiHelperTest extends \PHPUnit_Framework_TestCase +{ + /** @var \Ivory\GoogleMap\Helper\ApiHelper */ + protected $apiHelper; + + /** + * {@inheritdoc} + */ + protected function setUp() + { + $this->apiHelper = new ApiHelper(); + } + + /** + * {@inheritdoc} + */ + protected function tearDown() + { + unset($this->apiHelper); + } + + public function testDefaultState() + { + $this->assertFalse($this->apiHelper->isLoaded()); + } + + public function testRenderWithDefaultValues() + { + $expected = << +function load_ivory_google_map_api () { google.load("maps", "3", {"language":"en","other_params":"sensor=false"}); }; + + + +EOF; + + $this->assertSame($expected, $this->apiHelper->render()); + } + + public function testRenderWithLanguage() + { + $expected = << +function load_ivory_google_map_api () { google.load("maps", "3", {"language":"fr","other_params":"sensor=false"}); }; + + + +EOF; + + $this->assertSame($expected, $this->apiHelper->render('fr')); + } + + public function testRenderWithoutLibraries() + { + $expected = << +function load_ivory_google_map_api () { google.load("maps", "3", {"language":"en","other_params":"sensor=false"}); }; + + + +EOF; + + $this->assertSame($expected, $this->apiHelper->render('en', array())); + } + + public function testRenderWithLibraries() + { + $expected = << +function load_ivory_google_map_api () { google.load("maps", "3", {"language":"en","other_params":"libraries=geometry,places&sensor=false"}); }; + + + +EOF; + + $this->assertSame($expected, $this->apiHelper->render('en', array('geometry', 'places'))); + } + + public function testRenderWithCallback() + { + $expected = << +function load_ivory_google_map_api () { google.load("maps", "3", {"language":"en","other_params":"sensor=false", "callback": callback}); }; + + + +EOF; + + $this->assertSame($expected, $this->apiHelper->render('en', array(), 'callback')); + } + + public function testRenderWithSensor() + { + $expected = << +function load_ivory_google_map_api () { google.load("maps", "3", {"language":"en","other_params":"sensor=true"}); }; + + + +EOF; + + $this->assertSame($expected, $this->apiHelper->render('en', array(), null, true)); + } +} diff --git a/tests/Ivory/Tests/GoogleMap/Helper/MapHelperTest.php b/tests/Ivory/Tests/GoogleMap/Helper/MapHelperTest.php index a5a87213..3e4f9b2f 100644 --- a/tests/Ivory/Tests/GoogleMap/Helper/MapHelperTest.php +++ b/tests/Ivory/Tests/GoogleMap/Helper/MapHelperTest.php @@ -55,6 +55,11 @@ protected function tearDown() public function testDefaultState() { + $this->assertInstanceOf( + 'Ivory\GoogleMap\Helper\ApiHelper', + $this->mapHelper->getApiHelper() + ); + $this->assertInstanceOf( 'Ivory\GoogleMap\Helper\Base\CoordinateHelper', $this->mapHelper->getCoordinateHelper() @@ -178,6 +183,7 @@ public function testDefaultState() public function testInitialState() { + $apiHelper = $this->getMock('Ivory\GoogleMap\Helper\ApiHelper'); $coordinateHelper = $this->getMock('Ivory\GoogleMap\Helper\Base\CoordinateHelper'); $boundHelper = $this->getMock('Ivory\GoogleMap\Helper\Base\BoundHelper'); $pointHelper = $this->getMock('Ivory\GoogleMap\Helper\Base\PointHelper'); @@ -204,6 +210,7 @@ public function testInitialState() $eventManagerHelper = $this->getMock('Ivory\GoogleMap\Helper\Events\EventManagerHelper'); $this->mapHelper = new MapHelper( + $apiHelper, $coordinateHelper, $boundHelper, $pointHelper, @@ -230,6 +237,7 @@ public function testInitialState() $eventManagerHelper ); + $this->assertSame($apiHelper, $this->mapHelper->getApiHelper()); $this->assertSame($coordinateHelper, $this->mapHelper->getCoordinateHelper()); $this->assertSame($boundHelper, $this->mapHelper->getBoundHelper()); $this->assertSame($pointHelper, $this->mapHelper->getPointHelper()); @@ -984,62 +992,52 @@ public function testRenderJsContainerWithComplexMap() $this->assertSame($expected, $this->mapHelper->renderJsContainer($map)); } - public function testRenderJsAPI() + public function testRenderJavascripts() { $map = new Map(); + $map->setJavascriptVariable('map'); - $expected = <<function load_ivory_google_map_api () {google.load("maps", "3", {"language":"en","other_params":"libraries=geometry&sensor=false"});}; - - -EOF; - - $this->assertSame($expected, $this->mapHelper->renderJsAPI($map)); - } - - public function testRenderJsAPIWithEncodedPolylines() - { - $map = new Map(); - $map->addEncodedPolyline(new EncodedPolyline()); + $map->getCenter()->setJavascriptVariable('map_center'); $expected = <<function load_ivory_google_map_api () {google.load("maps", "3", {"language":"en","other_params":"libraries=geometry&sensor=false"});}; + + EOF; - $this->assertSame($expected, $this->mapHelper->renderJsAPI($map)); + $this->assertSame($expected, $this->mapHelper->renderJavascripts($map)); } - public function testRenderJsAPIWithAsync() + public function testRenderJavascriptsWithEncodedPolyline() { - $map = new Map(); - $map->setAsync(true); - - $expected = <<function load_ivory_google_map_api () {google.load("maps", "3", {"language":"en","other_params":"libraries=geometry&sensor=false", "callback": load_ivory_google_map});}; - - -EOF; - - $this->assertSame($expected, $this->mapHelper->renderJsAPI($map)); - } + $encodedPolyline = new EncodedPolyline('foo'); + $encodedPolyline->setJavascriptVariable('encoded_polyline'); - public function testRenderJavascripts() - { $map = new Map(); $map->setJavascriptVariable('map'); + $map->addEncodedPolyline($encodedPolyline); $map->getCenter()->setJavascriptVariable('map_center'); $expected = <<function load_ivory_google_map_api () {google.load("maps", "3", {"language":"en","other_params":"libraries=geometry&sensor=false"});}; + EOF; @@ -1064,7 +1062,9 @@ function load_ivory_google_map() { map.setCenter(map_center); } - + EOF; @@ -1079,7 +1079,9 @@ public function testRenderJavascriptsWithMultipleMaps() $map1->getCenter()->setJavascriptVariable('map1_center'); $expected1 = <<function load_ivory_google_map_api () {google.load("maps", "3", {"language":"en","other_params":"libraries=geometry&sensor=false"});}; + + + + + +EOF; + + $this->assertSame($expected, $this->autocompleteHelper->renderJavascripts($autocomplete)); + } + + public function testRenderJavascriptsWithMultipleAutocompletes() + { + $autocomplete1 = new Autocomplete(); + $autocomplete1->setJavascriptVariable('autocomplete1'); + + $autocomplete2 = new Autocomplete(); + $autocomplete2->setJavascriptVariable('autocomplete2'); + + $expected1 = << +function load_ivory_google_map_api () { google.load("maps", "3", {"language":"en","other_params":"libraries=places&sensor=false"}); }; + + + + +EOF; + + $expected2 = << +autocomplete2 = new google.maps.places.Autocomplete(document.getElementById('place_input', {})); + + +EOF; + + $this->assertSame($expected1, $this->autocompleteHelper->renderJavascripts($autocomplete1)); + $this->assertSame($expected2, $this->autocompleteHelper->renderJavascripts($autocomplete2)); + } + + public function testRenderJavascriptsWithBound() + { + $autocomplete = new Autocomplete(); + $autocomplete->setJavascriptVariable('autocomplete'); + + $autocomplete->setBound(1, 2, 3, 4, true, false); + $autocomplete->getBound()->setJavascriptVariable('bound'); + $autocomplete->getBound()->getSouthWest()->setJavascriptVariable('bound_south_west'); + $autocomplete->getBound()->getNorthEast()->setJavascriptVariable('bound_north_east'); + + $expected = << +function load_ivory_google_map_api () { google.load("maps", "3", {"language":"en","other_params":"libraries=places&sensor=false"}); }; + + + + +EOF; + + $this->assertSame($expected, $this->autocompleteHelper->renderJavascripts($autocomplete)); + } + + public function testRenderJavascriptsWithAsync() + { + $autocomplete = new Autocomplete(); + $autocomplete->setJavascriptVariable('autocomplete'); + $autocomplete->setAsync(true); + + $expected = << +function load_ivory_google_place () { +autocomplete = new google.maps.places.Autocomplete(document.getElementById('place_input', {})); +} + + + + +EOF; + + $this->assertSame($expected, $this->autocompleteHelper->renderJavascripts($autocomplete)); + } + + /** + * @expectedException Ivory\GoogleMap\Exception\HelperException + * @expectedExceptionMessage The place autocomplete bound must have coordinates. + */ + public function testRenderJavascriptsWithInvalidBound() + { + $autocomplete = new Autocomplete(); + + $autocomplete->setBound(1, 2, 3, 4); + $autocomplete->getBound()->setSouthWest(null); + $autocomplete->getBound()->setNorthEast(null); + + $this->autocompleteHelper->renderJavascripts($autocomplete); + } +} diff --git a/tests/Ivory/Tests/GoogleMap/Places/AutocompleteTest.php b/tests/Ivory/Tests/GoogleMap/Places/AutocompleteTest.php new file mode 100644 index 00000000..e3b7ee8d --- /dev/null +++ b/tests/Ivory/Tests/GoogleMap/Places/AutocompleteTest.php @@ -0,0 +1,217 @@ + + * + * For the full copyright and license information, please read the LICENSE + * file that was distributed with this source code. + */ + +namespace Ivory\Tests\GoogleMap\Places; + +use Ivory\GoogleMap\Places\Autocomplete; +use Ivory\GoogleMap\Places\AutocompleteType; + +/** + * Autocomplete test. + * + * @author GeLo + */ +class AutocompleteTest extends \PHPUnit_Framework_TestCase +{ + /** @var \Ivory\GoogleMap\Places\Autocomplete */ + protected $autocomplete; + + /** + * {@inheritdoc} + */ + protected function setUp() + { + $this->autocomplete = new Autocomplete(); + } + + /** + * {@inheritdoc} + */ + protected function tearDown() + { + unset($this->autocomplete); + } + + public function testDefaultState() + { + $this->assertSame('place_input', $this->autocomplete->getInputId()); + $this->assertFalse($this->autocomplete->hasBound()); + $this->assertFalse($this->autocomplete->hasTypes()); + $this->assertFalse($this->autocomplete->hasValue()); + $this->assertSame(array('type' => 'text', 'placeholder' => 'off'), $this->autocomplete->getInputAttributes()); + $this->assertFalse($this->autocomplete->isAsync()); + $this->assertSame('en', $this->autocomplete->getLanguage()); + } + + public function testInputIdWithValidValue() + { + $this->autocomplete->setInputId('input'); + + $this->assertSame('input', $this->autocomplete->getInputId()); + } + + /** + * @expectedException \Ivory\GoogleMap\Exception\PlaceException + * @expectedExceptionMessage The place autocomplete input ID must be a string value. + */ + public function testInputIdWithInvalidValue() + { + $this->autocomplete->setInputId(true); + } + + public function testBoundWithBound() + { + $bound = $this->getMock('Ivory\GoogleMap\Base\Bound'); + $this->autocomplete->setBound($bound); + + $this->assertSame($bound, $this->autocomplete->getBound()); + } + + public function testBoundWithCoordinates() + { + $southWestCoordinate = $this->getMock('Ivory\GoogleMap\Base\Coordinate'); + $northEastCoordinate = $this->getMock('Ivory\GoogleMap\Base\Coordinate'); + + $this->autocomplete->setBound($southWestCoordinate, $northEastCoordinate); + + $this->assertSame($southWestCoordinate, $this->autocomplete->getBound()->getSouthWest()); + $this->assertSame($northEastCoordinate, $this->autocomplete->getBound()->getNorthEast()); + } + + public function testBoundWithLatitudesAndLongitudes() + { + $this->autocomplete->setBound(1, 2, 3, 4, true, false); + + $this->assertSame(1, $this->autocomplete->getBound()->getSouthWest()->getLatitude()); + $this->assertSame(2, $this->autocomplete->getBound()->getSouthWest()->getLongitude()); + $this->assertTrue($this->autocomplete->getBound()->getSouthWest()->isNoWrap()); + + $this->assertEquals(3, $this->autocomplete->getBound()->getNorthEast()->getLatitude()); + $this->assertEquals(4, $this->autocomplete->getBound()->getNorthEast()->getLongitude()); + $this->assertFalse($this->autocomplete->getBound()->getNorthEast()->isNoWrap()); + } + + public function testBoundWithNullValue() + { + $this->autocomplete->setBound(1, 2, 3, 4); + $this->autocomplete->setBound(null); + + $this->assertNull($this->autocomplete->getBound()); + } + + /** + * @expectedException \Ivory\GoogleMap\Exception\PlaceException + * @expectedExceptionMessage The bound setter arguments is invalid. + * The available prototypes are : + * - function setBound(Ivory\GoogleMap\Base\Bound $bound) + * - function setBount(Ivory\GoogleMap\Base\Coordinate $southWest, Ivory\GoogleMap\Base\Coordinate $northEast) + * - function setBound( + * double $southWestLatitude, + * double $southWestLongitude, + * double $northEastLatitude, + * double $northEastLongitude, + * boolean southWestNoWrap = true, + * boolean $northEastNoWrap = true + * ) + */ + public function testBoundWithInvalidValue() + { + $this->autocomplete->setBound('foo'); + } + + public function testTypesWithValidTypes() + { + $types = array(AutocompleteType::ESTABLISHMENT, AutocompleteType::GEOCODE); + $this->autocomplete->setTypes($types); + + $this->assertSame($types, $this->autocomplete->getTypes()); + + $this->assertTrue($this->autocomplete->hasTypes()); + $this->assertTrue($this->autocomplete->hasType(AutocompleteType::ESTABLISHMENT)); + } + + /** + * @expectedException Ivory\GoogleMap\Exception\PlaceException + * @expectedExceptionMessage The place autocomplete type can only be: establishment, geocode, (regions), (cities). + */ + public function testAddTypeWithInvalidType() + { + $this->autocomplete->addType('foo'); + } + + /** + * @expectedException Ivory\GoogleMap\Exception\PlaceException + * @expectedExceptionMessage The place autocomplete type "establishment" already exists. + */ + public function testAddTypeWithExistingType() + { + $this->autocomplete->addType(AutocompleteType::ESTABLISHMENT); + $this->autocomplete->addType(AutocompleteType::ESTABLISHMENT); + } + + public function testRemoveTypeWithValidType() + { + $this->autocomplete->addType(AutocompleteType::ESTABLISHMENT); + $this->autocomplete->removeType(AutocompleteType::ESTABLISHMENT); + + $this->assertFalse($this->autocomplete->hasType(AutocompleteType::ESTABLISHMENT)); + } + + /** + * @expectedException Ivory\GoogleMap\Exception\PlaceException + * @expectedExceptionMessage The place autocomplete type "establishment" does not exist. + */ + public function testRemoveTypeWithNonExistingType() + { + $this->autocomplete->removeType(AutocompleteType::ESTABLISHMENT); + } + + public function testInputAttributesWithValidValue() + { + $this->autocomplete->setInputAttributes(array('foo' => 'bar')); + + $inputAttributes = $this->autocomplete->getInputAttributes(); + + $this->assertArrayHasKey('foo', $inputAttributes); + $this->assertSame('bar', $inputAttributes['foo']); + } + + public function testInputAttributesWithNullValue() + { + $this->autocomplete->setInputAttribute('foo', 'bar'); + $this->autocomplete->setInputAttribute('foo', null); + + $this->assertArrayNotHasKey('foo', $this->autocomplete->getInputAttributes()); + } + + public function testAsyncWithValidValue() + { + $this->autocomplete->setAsync(true); + + $this->assertTrue($this->autocomplete->isAsync()); + } + + /** + * @expectedException \Ivory\GoogleMap\Exception\PlaceException + * @expectedExceptionMessage The asynchronous load of a place autocomplete must be a boolean value. + */ + public function testAsyncWithInvalidValue() + { + $this->autocomplete->setAsync('foo'); + } + + public function testLanguage() + { + $this->autocomplete->setLanguage('fr'); + + $this->assertSame('fr', $this->autocomplete->getLanguage()); + } +} diff --git a/tests/Ivory/Tests/GoogleMap/Places/AutocompleteTypeTest.php b/tests/Ivory/Tests/GoogleMap/Places/AutocompleteTypeTest.php new file mode 100644 index 00000000..cd0f7710 --- /dev/null +++ b/tests/Ivory/Tests/GoogleMap/Places/AutocompleteTypeTest.php @@ -0,0 +1,35 @@ + + * + * For the full copyright and license information, please read the LICENSE + * file that was distributed with this source code. + */ + +namespace Ivory\Tests\GoogleMap\Places; + +use Ivory\GoogleMap\Places\AutocompleteType; + +/** + * Autocomplete type test. + * + * @author GeLo + */ +class AutocompleteTypeTest extends \PHPUnit_Framework_TestCase +{ + public function testAutocompleteTypes() + { + $this->assertSame( + array( + AutocompleteType::ESTABLISHMENT, + AutocompleteType::GEOCODE, + AutocompleteType::REGIONS, + AutocompleteType::CITIES, + ), + AutocompleteType::getAvailableAutocompleteTypes() + ); + } +}