diff --git a/composer.json b/composer.json index 946728868..436eefb8a 100644 --- a/composer.json +++ b/composer.json @@ -16,18 +16,12 @@ "igorw/get-in": "~1.0" }, "require-dev": { - "kriswallsmith/buzz": "@stable", - "guzzle/guzzle": "@stable", - "zendframework/zend-http": "~2.1", - "geoip/geoip": "~1.13", + "egeloen/http-adapter": "~0.1", "geoip2/geoip2": "~0.6" }, "suggest": { - "kriswallsmith/buzz": "Enabling Buzz allows you to use the BuzzHttpAdapter.", - "ext-curl": "Enabling the curl extension allows you to use the CurlHttpAdapter.", + "egeloen/http-adapter": "If you are going to use any http adapters", "ext-geoip": "Enabling the geoip extension allows you to use the MaxMindProvider.", - "guzzle/guzzle": "Enabling Guzzle allows you to use the GuzzleHttpAdapter.", - "zendframework/zend-http": "Enabling Zend Http allows you to use the ZendHttpAdapter.", "geoip/geoip": "If you are going to use the MaxMindBinaryProvider (conflict with geoip extension).", "geoip2/geoip2": "If you are going to use the GeoIP2DatabaseProvider." }, diff --git a/src/Geocoder/HttpAdapter/BuzzHttpAdapter.php b/src/Geocoder/HttpAdapter/BuzzHttpAdapter.php deleted file mode 100644 index eb574fa7b..000000000 --- a/src/Geocoder/HttpAdapter/BuzzHttpAdapter.php +++ /dev/null @@ -1,55 +0,0 @@ - - */ -class BuzzHttpAdapter implements HttpAdapterInterface -{ - /** - * @var Browser - */ - protected $browser; - - /** - * @param Browser $browser Browser object - */ - public function __construct(Browser $browser = null) - { - $this->browser = null === $browser ? new Browser() : $browser; - } - - /** - * {@inheritDoc} - */ - public function getContent($url) - { - try { - $response = $this->browser->get($url); - $content = $response->getContent(); - } catch (\Exception $e) { - $content = null; - } - - return $content; - } - - /** - * {@inheritDoc} - */ - public function getName() - { - return 'buzz'; - } -} diff --git a/src/Geocoder/HttpAdapter/CurlHttpAdapter.php b/src/Geocoder/HttpAdapter/CurlHttpAdapter.php deleted file mode 100644 index a0d052bde..000000000 --- a/src/Geocoder/HttpAdapter/CurlHttpAdapter.php +++ /dev/null @@ -1,87 +0,0 @@ - - */ -class CurlHttpAdapter implements HttpAdapterInterface -{ - private $timeout; - - private $connectTimeout; - - private $userAgent; - - /** - * Array for bulk setting of curl options - * @see http://php.net/manual/en/curl.constants.php - * @var array - */ - private $options; - - public function __construct($timeout = null, $connectTimeout = null, $userAgent = null, $options = array()) - { - $this->timeout = $timeout; - $this->connectTimeout = $connectTimeout; - $this->userAgent = $userAgent; - $this->options = $options; - } - - /** - * {@inheritDoc} - */ - public function getContent($url) - { - if (!function_exists('curl_init')) { - throw new ExtensionNotLoaded('cURL has to be enabled.'); - } - - $c = curl_init(); - curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); - curl_setopt($c, CURLOPT_URL, $url); - - if ($this->timeout) { - curl_setopt($c, CURLOPT_TIMEOUT, $this->timeout); - } - - if ($this->connectTimeout) { - curl_setopt($c, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout); - } - - if ($this->userAgent) { - curl_setopt($c, CURLOPT_USERAGENT, $this->userAgent); - } - - if ($this->options && is_array($this->options) && count($this->options)>0) { - curl_setopt_array($c, $this->options); - } - - $content = curl_exec($c); - curl_close($c); - - if (false === $content) { - $content = null; - } - - return $content; - } - - /** - * {@inheritDoc} - */ - public function getName() - { - return 'curl'; - } -} diff --git a/src/Geocoder/HttpAdapter/GeoIP2Adapter.php b/src/Geocoder/HttpAdapter/GeoIP2Adapter.php index 25bd1a541..6b7db5d79 100644 --- a/src/Geocoder/HttpAdapter/GeoIP2Adapter.php +++ b/src/Geocoder/HttpAdapter/GeoIP2Adapter.php @@ -17,7 +17,7 @@ /** * @author Jens Wiese */ -class GeoIP2Adapter implements HttpAdapterInterface +class GeoIP2Adapter { /** * GeoIP2 models (e.g. city or country) diff --git a/src/Geocoder/HttpAdapter/GuzzleHttpAdapter.php b/src/Geocoder/HttpAdapter/GuzzleHttpAdapter.php deleted file mode 100644 index f74063818..000000000 --- a/src/Geocoder/HttpAdapter/GuzzleHttpAdapter.php +++ /dev/null @@ -1,54 +0,0 @@ - - * @link http://www.guzzlephp.org - */ -class GuzzleHttpAdapter implements HttpAdapterInterface -{ - /** - * @var ClientInterface - */ - protected $client; - - /** - * @param ClientInterface $client Client object - */ - public function __construct(ClientInterface $client = null) - { - $this->client = null === $client ? new Client() : $client; - } - - /** - * {@inheritDoc} - */ - public function getContent($url) - { - $response = $this->client->get($url)->send(); - - return (string) $response->getBody(); - } - - /** - * {@inheritDoc} - */ - public function getName() - { - return 'guzzle'; - } -} diff --git a/src/Geocoder/HttpAdapter/HttpAdapterInterface.php b/src/Geocoder/HttpAdapter/HttpAdapterInterface.php deleted file mode 100644 index ce7ef3fc7..000000000 --- a/src/Geocoder/HttpAdapter/HttpAdapterInterface.php +++ /dev/null @@ -1,33 +0,0 @@ - - */ -interface HttpAdapterInterface -{ - /** - * Returns the content fetched from a given URL. - * - * @param string $url - * - * @return string - */ - public function getContent($url); - - /** - * Returns the name of the HTTP Adapter. - * - * @return string - */ - public function getName(); -} diff --git a/src/Geocoder/HttpAdapter/SocketHttpAdapter.php b/src/Geocoder/HttpAdapter/SocketHttpAdapter.php deleted file mode 100644 index 650a1bb0e..000000000 --- a/src/Geocoder/HttpAdapter/SocketHttpAdapter.php +++ /dev/null @@ -1,155 +0,0 @@ - - */ -class SocketHttpAdapter implements HttpAdapterInterface -{ - /** - * @var integer - */ - const MAX_REDIRECTS = 5; - - /** - * @var integer - */ - private $redirectsRemaining = self::MAX_REDIRECTS; - - /** - * {@inheritDoc} - */ - public function getContent($url) - { - $info = parse_url($url); - - $hostname = $info['host']; - $port = isset($info['port']) ? $info['port'] : 80; - $path = sprintf('%s%s', - isset($info['path']) ? $info['path'] : '/', - isset($info['query']) ? '?' . $info['query'] : '' - ); - - $socketHandle = $this->createSocket($hostname, $port, 30); - - if (!fwrite($socketHandle, $this->buildHttpRequest($path, $hostname))) { - throw new ExtensionNotLoaded('Could not send the request'); - } - - $httpResponse = $this->getParsedHttpResponse($socketHandle); - - if ($httpResponse['headers']['status'] === 301 && isset($httpResponse['headers']['location'])) { - if (--$this->redirectsRemaining) { - return $this->getContent($httpResponse['headers']['location']); - } else { - throw new HttpError('Too Many Redirects'); - } - } else { - $this->redirectsRemaining = self::MAX_REDIRECTS; - } - - if ($httpResponse['headers']['status'] !== 200) { - throw new HttpError(sprintf('The server return a %s status.', $httpResponse['headers']['status'])); - } - - return $httpResponse['content']; - } - - /** - * This method strictly doesn't need to exist but can act as a "seam" for substituting fake sockets in test. - * This would require a subclass that overloads the method and returns the fake socket. - * - * @param string $hostname The hostname. - * @param string $port The port number. - * @param integer $timeout The timeout. - * - * @return resource - * @throws HttpError - */ - protected function createSocket($hostname, $port, $timeout) - { - $socketHandle = fsockopen($hostname, $port, $errno, $errstr, $timeout) ?: null; - - //verify handle - if (null === $socketHandle) { - throw new HttpError(sprintf('Could not connect to socket. (%s)', $errstr)); - } - - return $socketHandle; - } - - /** - * Build the HTTP 1.1 request headers from the given inputs. - * - * @param string $path The path. - * @param string $hostname The hostname. - * - * @return string - */ - protected function buildHttpRequest($path, $hostname) - { - $r = array(); - $r[] = "GET {$path} HTTP/1.1"; - $r[] = "Host: {$hostname}"; - $r[] = "Connection: Close"; - $r[] = "User-Agent: Geocoder PHP-Library"; - $r[] = "\r\n"; - - return implode("\r\n", $r); - } - - /** - * Given a resource parse the contents into its component parts (headers/contents) - * - * @param resource $socketHandle - * - * @return array - */ - protected function getParsedHttpResponse($socketHandle) - { - $httpResponse = array(); - $httpResponse['headers'] = array(); - $httpResponse['content'] = ''; - - $reachedEndOfHeaders = false; - - while (!feof($socketHandle)) { - $line = trim(fgets($socketHandle)); - if (!$line) { - $reachedEndOfHeaders = true; - continue; - } - if (!$reachedEndOfHeaders) { - if (preg_match('@^HTTP/\d\.\d\s*(\d+)\s*.*$@', $line, $matches)) { - $httpResponse['headers']['status'] = (integer) $matches[1]; - } elseif (preg_match('@^([^:]+): (.+)$@', $line, $matches)) { - $httpResponse['headers'][strtolower($matches[1])] = trim($matches[2]); - } - } else { - $httpResponse['content'] .= $line; - } - } - - return $httpResponse; - } - - /** - * {@inheritDoc} - */ - public function getName() - { - return 'socket'; - } -} diff --git a/src/Geocoder/HttpAdapter/ZendHttpAdapter.php b/src/Geocoder/HttpAdapter/ZendHttpAdapter.php deleted file mode 100644 index 219ffc708..000000000 --- a/src/Geocoder/HttpAdapter/ZendHttpAdapter.php +++ /dev/null @@ -1,55 +0,0 @@ - - */ -class ZendHttpAdapter implements HttpAdapterInterface -{ - /** - * @var Client - */ - protected $client; - - /** - * @param Client $client Client object - */ - public function __construct(Client $client = null) - { - $this->client = null === $client ? new Client() : $client; - } - - /** - * {@inheritDoc} - */ - public function getContent($url) - { - try { - $response = $this->client->setUri($url)->send(); - $content = $response->isSuccess() ? $response->getBody() : null; - } catch (\Exception $e) { - $content = null; - } - - return $content; - } - - /** - * {@inheritDoc} - */ - public function getName() - { - return 'zend'; - } -} diff --git a/src/Geocoder/Provider/AbstractProvider.php b/src/Geocoder/Provider/AbstractProvider.php index 63265bfe2..39ac8d6dc 100644 --- a/src/Geocoder/Provider/AbstractProvider.php +++ b/src/Geocoder/Provider/AbstractProvider.php @@ -11,7 +11,7 @@ namespace Geocoder\Provider; use Geocoder\ProviderBasedGeocoder; -use Geocoder\HttpAdapter\HttpAdapterInterface; +use Ivory\HttpAdapter\HttpAdapterInterface; /** * @author William Durand diff --git a/src/Geocoder/Provider/ArcGISOnline.php b/src/Geocoder/Provider/ArcGISOnline.php index 8cb5e8904..8f9ca870e 100644 --- a/src/Geocoder/Provider/ArcGISOnline.php +++ b/src/Geocoder/Provider/ArcGISOnline.php @@ -12,7 +12,7 @@ use Geocoder\Exception\NoResult; use Geocoder\Exception\UnsupportedOperation; -use Geocoder\HttpAdapter\HttpAdapterInterface; +use Ivory\HttpAdapter\HttpAdapterInterface; /** * @author ALKOUM Dorian @@ -158,9 +158,7 @@ private function buildQuery($query) $query = sprintf('%s&sourceCountry=%s', $query, $this->sourceCountry); } - $query = sprintf('%s&maxLocations=%d&f=%s&outFields=*', $query, $this->getMaxResults(), 'json'); - - return $query; + return sprintf('%s&maxLocations=%d&f=%s&outFields=*', $query, $this->getMaxResults(), 'json'); } /** @@ -175,9 +173,9 @@ private function buildQuery($query) private function executeQuery($query) { $query = $this->buildQuery($query); + $content = (string) $this->getAdapter()->get($query)->getBody(); - $content = $this->getAdapter()->getContent($query); - if (null === $content) { + if (empty($content)) { throw new NoResult(sprintf('Could not execute query %s', $query)); } diff --git a/src/Geocoder/Provider/BingMaps.php b/src/Geocoder/Provider/BingMaps.php index 19f124c47..081e53e65 100644 --- a/src/Geocoder/Provider/BingMaps.php +++ b/src/Geocoder/Provider/BingMaps.php @@ -10,10 +10,10 @@ namespace Geocoder\Provider; -use Geocoder\HttpAdapter\HttpAdapterInterface; use Geocoder\Exception\InvalidCredentials; use Geocoder\Exception\NoResult; use Geocoder\Exception\UnsupportedOperation; +use Ivory\HttpAdapter\HttpAdapterInterface; /** * @author David Guyon @@ -99,9 +99,9 @@ private function executeQuery($query) $query = sprintf('%s&culture=%s', $query, str_replace('_', '-', $this->getLocale())); } - $content = $this->getAdapter()->getContent($query); + $content = (string) $this->getAdapter()->get($query)->getBody(); - if (null === $content) { + if (empty($content)) { throw new NoResult(sprintf('Could not execute query %s', $query)); } diff --git a/src/Geocoder/Provider/FreeGeoIp.php b/src/Geocoder/Provider/FreeGeoIp.php index b21619449..d2be04036 100644 --- a/src/Geocoder/Provider/FreeGeoIp.php +++ b/src/Geocoder/Provider/FreeGeoIp.php @@ -64,9 +64,9 @@ public function getName() */ private function executeQuery($query) { - $content = $this->getAdapter()->getContent($query); + $content = (string) $this->getAdapter()->get($query)->getBody(); - if (null === $content) { + if (empty($content)) { throw new NoResult(sprintf('Could not execute query %s', $query)); } diff --git a/src/Geocoder/Provider/GeoIP2.php b/src/Geocoder/Provider/GeoIP2.php index d6a7ab483..957ed3c3d 100644 --- a/src/Geocoder/Provider/GeoIP2.php +++ b/src/Geocoder/Provider/GeoIP2.php @@ -11,10 +11,8 @@ namespace Geocoder\Provider; use Geocoder\Exception\NoResult; -use Geocoder\Exception\InvalidArgument; use Geocoder\Exception\UnsupportedOperation; use Geocoder\HttpAdapter\GeoIP2Adapter; -use Geocoder\HttpAdapter\HttpAdapterInterface; use GeoIp2\Exception\AddressNotFoundException; use GeoIp2\Model\City; @@ -26,15 +24,10 @@ class GeoIP2 extends AbstractProvider implements Provider /** * {@inheritdoc} */ - public function __construct(HttpAdapterInterface $adapter, $locale = 'en') + public function __construct(GeoIP2Adapter $adapter, $locale = 'en') { - if (false === $adapter instanceof GeoIP2Adapter) { - throw new InvalidArgument( - 'GeoIP2Adapter is needed in order to access the GeoIP2 service.' - ); - } - - parent::__construct($adapter, $locale); + $this->setAdapter($adapter); + $this->setLocale($locale); } /** diff --git a/src/Geocoder/Provider/GeoIPs.php b/src/Geocoder/Provider/GeoIPs.php index 103811c27..fc400acf9 100644 --- a/src/Geocoder/Provider/GeoIPs.php +++ b/src/Geocoder/Provider/GeoIPs.php @@ -15,7 +15,7 @@ use Geocoder\Exception\UnsupportedOperation; use Geocoder\Exception\InvalidArgument; use Geocoder\Exception\QuotaExceeded; -use Geocoder\HttpAdapter\HttpAdapterInterface; +use Ivory\HttpAdapter\HttpAdapterInterface; /** * @author Andrea Cristaudo @@ -103,9 +103,9 @@ public function getName() */ private function executeQuery($query) { - $content = $this->getAdapter()->getContent($query); + $content = (string) $this->getAdapter()->get($query)->getBody(); - if (null === $content || '' === $content) { + if (empty($content)) { throw new NoResult(sprintf('Invalid response from GeoIPs server for query %s', $query)); } diff --git a/src/Geocoder/Provider/GeoPlugin.php b/src/Geocoder/Provider/GeoPlugin.php index 90ae19e77..2100296d1 100644 --- a/src/Geocoder/Provider/GeoPlugin.php +++ b/src/Geocoder/Provider/GeoPlugin.php @@ -64,9 +64,9 @@ public function getName() */ private function executeQuery($query) { - $content = $this->getAdapter()->getContent($query); + $content = (string) $this->getAdapter()->get($query)->getBody(); - if (null === $content || '' === $content) { + if (empty($content)) { throw new NoResult(sprintf('Could not execute query %s', $query)); } diff --git a/src/Geocoder/Provider/Geonames.php b/src/Geocoder/Provider/Geonames.php index 8401068c8..09b0731d5 100644 --- a/src/Geocoder/Provider/Geonames.php +++ b/src/Geocoder/Provider/Geonames.php @@ -13,7 +13,7 @@ use Geocoder\Exception\InvalidCredentials; use Geocoder\Exception\NoResult; use Geocoder\Exception\UnsupportedOperation; -use Geocoder\HttpAdapter\HttpAdapterInterface; +use Ivory\HttpAdapter\HttpAdapterInterface; /** * @author Giovanni Pirrotta @@ -100,9 +100,9 @@ private function executeQuery($query) $query = sprintf('%s&lang=%s', $query, substr($this->getLocale(), 0, 2)); } - $content = $this->getAdapter()->getContent($query); + $content = (string) $this->getAdapter()->get($query)->getBody(); - if (null === $content) { + if (empty($content)) { throw new NoResult(sprintf('Could not execute query %s', $query)); } diff --git a/src/Geocoder/Provider/GoogleMaps.php b/src/Geocoder/Provider/GoogleMaps.php index 9a227f7b3..406df361e 100644 --- a/src/Geocoder/Provider/GoogleMaps.php +++ b/src/Geocoder/Provider/GoogleMaps.php @@ -14,7 +14,7 @@ use Geocoder\Exception\QuotaExceeded; use Geocoder\Exception\UnsupportedOperation; use Geocoder\Exception\InvalidCredentials; -use Geocoder\HttpAdapter\HttpAdapterInterface; +use Ivory\HttpAdapter\HttpAdapterInterface; /** * @author William Durand @@ -127,15 +127,14 @@ protected function buildQuery($query) private function executeQuery($query) { $query = $this->buildQuery($query); - - $content = $this->getAdapter()->getContent($query); + $content = (string) $this->getAdapter()->get($query)->getBody(); // Throw exception if invalid clientID and/or privateKey used with GoogleMapsBusinessProvider if (strpos($content, "Provided 'signature' is not valid for the provided client ID") !== false) { throw new InvalidCredentials(sprintf('Invalid client ID / API Key %s', $query)); } - if (null === $content) { + if (empty($content)) { throw new NoResult(sprintf('Could not execute query %s', $query)); } diff --git a/src/Geocoder/Provider/GoogleMapsBusiness.php b/src/Geocoder/Provider/GoogleMapsBusiness.php index 0b00ed447..889c58cf2 100644 --- a/src/Geocoder/Provider/GoogleMapsBusiness.php +++ b/src/Geocoder/Provider/GoogleMapsBusiness.php @@ -10,7 +10,7 @@ namespace Geocoder\Provider; -use Geocoder\HttpAdapter\HttpAdapterInterface; +use Ivory\HttpAdapter\HttpAdapterInterface; /** * Google Maps for Business diff --git a/src/Geocoder/Provider/HostIp.php b/src/Geocoder/Provider/HostIp.php index 9a7e832d0..37c40522a 100644 --- a/src/Geocoder/Provider/HostIp.php +++ b/src/Geocoder/Provider/HostIp.php @@ -69,7 +69,7 @@ public function getName() */ private function executeQuery($query) { - $content = $this->getAdapter()->getContent($query); + $content = (string) $this->getAdapter()->get($query)->getBody(); $data = json_decode($content, true); diff --git a/src/Geocoder/Provider/IpInfoDb.php b/src/Geocoder/Provider/IpInfoDb.php index 202f3740a..4b0507c9a 100644 --- a/src/Geocoder/Provider/IpInfoDb.php +++ b/src/Geocoder/Provider/IpInfoDb.php @@ -13,7 +13,7 @@ use Geocoder\Exception\UnsupportedOperation; use Geocoder\Exception\NoResult; use Geocoder\Exception\InvalidCredentials; -use Geocoder\HttpAdapter\HttpAdapterInterface; +use Ivory\HttpAdapter\HttpAdapterInterface; /** * @author William Durand @@ -91,9 +91,9 @@ public function getName() */ private function executeQuery($query) { - $content = $this->getAdapter()->getContent($query); + $content = (string) $this->getAdapter()->get($query)->getBody(); - if (null === $content) { + if (empty($content)) { throw new NoResult(sprintf('Could not execute query %s', $query)); } diff --git a/src/Geocoder/Provider/MapQuest.php b/src/Geocoder/Provider/MapQuest.php index d3d621003..8c4f0b776 100644 --- a/src/Geocoder/Provider/MapQuest.php +++ b/src/Geocoder/Provider/MapQuest.php @@ -13,7 +13,7 @@ use Geocoder\Exception\InvalidCredentials; use Geocoder\Exception\NoResult; use Geocoder\Exception\UnsupportedOperation; -use Geocoder\HttpAdapter\HttpAdapterInterface; +use Ivory\HttpAdapter\HttpAdapterInterface; /** * @author William Durand @@ -123,9 +123,9 @@ public function getName() */ protected function executeQuery($query) { - $content = $this->getAdapter()->getContent($query); + $content = (string) $this->getAdapter()->get($query)->getBody(); - if (null === $content) { + if (empty($content)) { throw new NoResult(sprintf('Could not execute query: %s', $query)); } diff --git a/src/Geocoder/Provider/MaxMind.php b/src/Geocoder/Provider/MaxMind.php index 4b2a3c4f1..a91d7c9f9 100644 --- a/src/Geocoder/Provider/MaxMind.php +++ b/src/Geocoder/Provider/MaxMind.php @@ -13,7 +13,7 @@ use Geocoder\Exception\InvalidCredentials; use Geocoder\Exception\NoResult; use Geocoder\Exception\UnsupportedOperation; -use Geocoder\HttpAdapter\HttpAdapterInterface; +use Ivory\HttpAdapter\HttpAdapterInterface; /** * @author Andrea Cristaudo @@ -111,7 +111,7 @@ public function getReversedData(array $coordinates) */ private function executeQuery($query) { - $content = $this->getAdapter()->getContent($query); + $content = (string) $this->getAdapter()->get($query)->getBody(); $fields = $this->fieldsForService($this->service); if (null === $content || '' === $content) { diff --git a/src/Geocoder/Provider/Nominatim.php b/src/Geocoder/Provider/Nominatim.php index e14c6db37..944f5c888 100644 --- a/src/Geocoder/Provider/Nominatim.php +++ b/src/Geocoder/Provider/Nominatim.php @@ -12,7 +12,7 @@ use Geocoder\Exception\NoResult; use Geocoder\Exception\UnsupportedOperation; -use Geocoder\HttpAdapter\HttpAdapterInterface; +use Ivory\HttpAdapter\HttpAdapterInterface; /** * @author Niklas Närhinen @@ -50,7 +50,7 @@ public function getGeocodedData($address) $query = sprintf($this->getGeocodeEndpointUrl(), urlencode($address), $this->getMaxResults()); $content = $this->executeQuery($query); - if (null === $content) { + if (empty($content)) { throw new NoResult(sprintf('Could not resolve address "%s"', $address)); } @@ -101,7 +101,7 @@ public function getReversedData(array $coordinates) $query = sprintf($this->getReverseEndpointUrl(), $coordinates[0], $coordinates[1]); $content = $this->executeQuery($query); - if (null === $content) { + if (empty($content)) { throw new NoResult(sprintf('Unable to resolve the coordinates %s', implode(', ', $coordinates))); } @@ -148,7 +148,7 @@ private function executeQuery($query) $query = sprintf('%s&accept-language=%s', $query, $this->getLocale()); } - return $this->getAdapter()->getContent($query); + return (string) $this->getAdapter()->get($query)->getBody(); } /** diff --git a/src/Geocoder/Provider/OpenCage.php b/src/Geocoder/Provider/OpenCage.php index 8e9bd2d39..812d58db2 100644 --- a/src/Geocoder/Provider/OpenCage.php +++ b/src/Geocoder/Provider/OpenCage.php @@ -13,7 +13,7 @@ use Geocoder\Exception\InvalidCredentials; use Geocoder\Exception\NoResult; use Geocoder\Exception\UnsupportedOperation; -use Geocoder\HttpAdapter\HttpAdapterInterface; +use Ivory\HttpAdapter\HttpAdapterInterface; /** * @author mtm @@ -94,14 +94,13 @@ public function getName() */ private function executeQuery($query) { - if (null !== $this->getLocale()) { $query = sprintf('%s&language=%s', $query, $this->getLocale()); } - $content = $this->getAdapter()->getContent($query); + $content = (string) $this->getAdapter()->get($query)->getBody(); - if (null === $content) { + if (empty($content)) { throw new NoResult(sprintf('Could not execute query: %s', $query)); } diff --git a/src/Geocoder/Provider/OpenStreetMap.php b/src/Geocoder/Provider/OpenStreetMap.php index df0a22d72..152647905 100644 --- a/src/Geocoder/Provider/OpenStreetMap.php +++ b/src/Geocoder/Provider/OpenStreetMap.php @@ -10,7 +10,7 @@ namespace Geocoder\Provider; -use Geocoder\HttpAdapter\HttpAdapterInterface; +use Ivory\HttpAdapter\HttpAdapterInterface; /** * @author Niklas Närhinen diff --git a/src/Geocoder/Provider/TomTom.php b/src/Geocoder/Provider/TomTom.php index 8ac3a4d6d..0da7505aa 100644 --- a/src/Geocoder/Provider/TomTom.php +++ b/src/Geocoder/Provider/TomTom.php @@ -13,7 +13,7 @@ use Geocoder\Exception\InvalidCredentials; use Geocoder\Exception\NoResult; use Geocoder\Exception\UnsupportedOperation; -use Geocoder\HttpAdapter\HttpAdapterInterface; +use Ivory\HttpAdapter\HttpAdapterInterface; /** * @author Antoine Corcy @@ -101,7 +101,7 @@ private function executeQuery($query) $query = sprintf('%s&language=%s', $query, substr($this->getLocale(), 0, 2)); } - $content = $this->getAdapter()->getContent($query); + $content = (string) $this->getAdapter()->get($query)->getBody(); try { $xml = new \SimpleXmlElement($content); diff --git a/src/Geocoder/Provider/Yandex.php b/src/Geocoder/Provider/Yandex.php index 311f03aea..c03ec89fb 100644 --- a/src/Geocoder/Provider/Yandex.php +++ b/src/Geocoder/Provider/Yandex.php @@ -12,7 +12,7 @@ use Geocoder\Exception\UnsupportedOperation; use Geocoder\Exception\NoResult; -use Geocoder\HttpAdapter\HttpAdapterInterface; +use Ivory\HttpAdapter\HttpAdapterInterface; /** * @author Antoine Corcy @@ -96,7 +96,7 @@ private function executeQuery($query) $query = sprintf('%s&results=%d', $query, $this->getMaxResults()); - $content = $this->getAdapter()->getContent($query); + $content = (string) $this->getAdapter()->get($query)->getBody(); $json = (array) json_decode($content, true); if (empty($json) || '0' === $json['response']['GeoObjectCollection']['metaDataProperty']['GeocoderResponseMetaData']['found']) { diff --git a/tests/Geocoder/Tests/CachedResponseAdapter.php b/tests/Geocoder/Tests/CachedResponseAdapter.php index 5d6ec4ee6..1f1a674cf 100644 --- a/tests/Geocoder/Tests/CachedResponseAdapter.php +++ b/tests/Geocoder/Tests/CachedResponseAdapter.php @@ -2,9 +2,12 @@ namespace Geocoder\Tests; -use Geocoder\HttpAdapter\HttpAdapterInterface; +use Ivory\HttpAdapter\AbstractHttpAdapter; +use Ivory\HttpAdapter\HttpAdapterInterface; +use Ivory\HttpAdapter\Message\InternalRequestInterface; +use Ivory\HttpAdapter\Message\Stream\StringStream; -class CachedResponseAdapter implements HttpAdapterInterface +class CachedResponseAdapter extends AbstractHttpAdapter { private $adapter; @@ -14,6 +17,8 @@ class CachedResponseAdapter implements HttpAdapterInterface public function __construct(HttpAdapterInterface $adapter, $useCache = false, $cacheDir = '.cached_responses') { + parent::__construct(); + $this->adapter = $adapter; $this->useCache = $useCache; $this->cacheDir = $cacheDir; @@ -22,32 +27,36 @@ public function __construct(HttpAdapterInterface $adapter, $useCache = false, $c /** * {@inheritDoc} */ - public function getContent($url) + public function getName() + { + return 'cached_response'; + } + + /** + * {@inheritDoc} + */ + protected function doSend(InternalRequestInterface $internalRequest) { - $file = sprintf('%s/%s/%s', realpath(__DIR__ . '/../../'), $this->cacheDir, sha1($url)); + $file = sprintf('%s/%s/%s', realpath(__DIR__ . '/../../'), $this->cacheDir, sha1($internalRequest->getUrl())); if ($this->useCache && is_file($file) && is_readable($file)) { - $response = unserialize(file_get_contents($file)); + $content = unserialize(file_get_contents($file)); + $body = new StringStream($content); - if (!empty($response)) { + $response = $this->adapter->getConfiguration()->getMessageFactory()->createResponse(); + $response->setBody($body); + + if (!empty($content)) { return $response; } } - $response = $this->adapter->getContent($url); + $response = $this->adapter->get($internalRequest); if ($this->useCache) { - file_put_contents($file, serialize($response)); + file_put_contents($file, serialize((string) $response->getBody())); } return $response; } - - /** - * {@inheritDoc} - */ - public function getName() - { - return 'cached_response'; - } } diff --git a/tests/Geocoder/Tests/HttpAdapter/BuzzHttpAdapterTest.php b/tests/Geocoder/Tests/HttpAdapter/BuzzHttpAdapterTest.php deleted file mode 100644 index 796654c81..000000000 --- a/tests/Geocoder/Tests/HttpAdapter/BuzzHttpAdapterTest.php +++ /dev/null @@ -1,71 +0,0 @@ - - */ -class BuzzHttpAdapterTest extends TestCase -{ - protected $buzz; - - protected function setUp() - { - if (!class_exists('Buzz\Browser')) { - $this->markTestSkipped('Buzz library has to be installed'); - } - - $this->buzz = new BuzzHttpAdapter(); - } - - public function testGetNullContent() - { - $this->assertNull($this->buzz->getContent(null)); - } - - public function testGetFalseContent() - { - $this->assertNull($this->buzz->getContent(false)); - } - - public function testGetName() - { - $this->assertEquals('buzz', $this->buzz->getName()); - } - - public function testGetContentWithCustomBrowser() - { - $content = 'foobar content'; - $browser = $this->getBrowserMock($content); - - $buzz = new BuzzHttpAdapter($browser); - $this->assertEquals($content, $buzz->getContent('http://www.example.com')); - } - - protected function getBrowserMock($content) - { - $mock = $this->getMock('Buzz\Browser'); - $mock - ->expects($this->once()) - ->method('get') - ->will($this->returnValue($this->getResponseMock($content))) - ; - - return $mock; - } - - protected function getResponseMock($content) - { - $mock = $this->getMock('Buzz\Message\Response'); - $mock - ->expects($this->once()) - ->method('getContent') - ->will($this->returnValue($content)); - - return $mock; - } -} diff --git a/tests/Geocoder/Tests/HttpAdapter/CurlHttpAdapterTest.php b/tests/Geocoder/Tests/HttpAdapter/CurlHttpAdapterTest.php deleted file mode 100644 index 80023b949..000000000 --- a/tests/Geocoder/Tests/HttpAdapter/CurlHttpAdapterTest.php +++ /dev/null @@ -1,39 +0,0 @@ - - */ -class CurlHttpAdapterTest extends TestCase -{ - protected $curl; - - protected function setUp() - { - if (!function_exists('curl_init')) { - $this->markTestSkipped('cURL has to be enabled.'); - } - - $this->curl = new CurlHttpAdapter(); - } - - public function testGetNullContent() - { - $this->assertNull($this->curl->getContent(null)); - } - - public function testGetFalseContent() - { - $this->assertNull($this->curl->getContent(false)); - } - - public function testGetName() - { - $this->assertEquals('curl', $this->curl->getName()); - } -} diff --git a/tests/Geocoder/Tests/HttpAdapter/GuzzleHttpAdapterTest.php b/tests/Geocoder/Tests/HttpAdapter/GuzzleHttpAdapterTest.php deleted file mode 100644 index d5063e28f..000000000 --- a/tests/Geocoder/Tests/HttpAdapter/GuzzleHttpAdapterTest.php +++ /dev/null @@ -1,49 +0,0 @@ - - */ -class GuzzleHttpAdapterTest extends \Geocoder\Tests\TestCase -{ - protected function setUp() - { - if (!class_exists('Guzzle\Service\Client')) { - $this->markTestSkipped('Guzzle library has to be installed'); - } - } - - public function testGetName() - { - $adapter = new GuzzleHttpAdapter(); - $this->assertEquals('guzzle', $adapter->getName()); - } - - /** - * @covers Geocoder\HttpAdapter\GuzzleHttpAdapter::__construct - * @covers Geocoder\HttpAdapter\GuzzleHttpAdapter::getContent - */ - public function testRetrievesResponse() - { - $historyPlugin = new HistoryPlugin(); - $mockPlugin = new MockPlugin(array(new Response(200, null, 'body'))); - - $client = new Client(); - $client->getEventDispatcher()->addSubscriber($mockPlugin); - $client->getEventDispatcher()->addSubscriber($historyPlugin); - - $adapter = new GuzzleHttpAdapter($client); - $this->assertEquals('body', $adapter->getContent('http://test.com/')); - - $this->assertEquals('http://test.com/', - $historyPlugin->getLastRequest()->getUrl()); - } -} diff --git a/tests/Geocoder/Tests/HttpAdapter/SocketHttpAdapterTest.php b/tests/Geocoder/Tests/HttpAdapter/SocketHttpAdapterTest.php deleted file mode 100644 index 4e8883862..000000000 --- a/tests/Geocoder/Tests/HttpAdapter/SocketHttpAdapterTest.php +++ /dev/null @@ -1,165 +0,0 @@ - - */ -class SocketHttpAdapterTest extends TestCase -{ - protected function setUp() - { - $this->adapter = new SocketHttpAdapter(); - } - - public function testGetContent() - { - try { - $content = $this->adapter->getContent('http://www.google.de'); - } catch (\Exception $e) { - $this->fail('Exception catched: ' . $e->getMessage()); - } - - $this->assertNotNull($content); - $this->assertContains('google', $content); - } - - public function testGetContentHandlesQueryString() - { - $url = 'http://example.com/foobar?my=query&string=true'; - $adapter = new PartialSocketHttpAdapter(); - - try { - $adapter->getContent($url); - $this->fail('It should throw an exception'); - } catch (\Exception $e) { - // expected result - } - - $this->assertEquals('/foobar?my=query&string=true', $adapter->path); - $this->assertEquals('example.com', $adapter->hostname); - } - - /** - * NOTE ON REFLECTION: - * Not a great idea but the alternative would be to create a new class for - * HTTP parsing or set the method public. I don't like either of these much. - */ - public function testBuildRequest() - { - $method = new \ReflectionMethod( - $this->adapter, 'buildHttpRequest' - ); - - $method->setAccessible(true); - - $ex_host = 'www.google.com'; - $ex_path = '/'; - - $ex_body = array(); - $ex_body[] = "GET $ex_path HTTP/1.1"; - $ex_body[] = "Host: $ex_host"; - $ex_body[] = "Connection: Close"; - $ex_body[] = "User-Agent: Geocoder PHP-Library"; - $ex_body[] = "\r\n"; - - $this->assertEquals( - implode("\r\n", $ex_body), $method->invoke($this->adapter, $ex_path, $ex_host) - ); - } - - public function testParseHtmlResponse() - { - $method = new \ReflectionMethod( - $this->adapter, 'getParsedHttpResponse' - ); - $method->setAccessible(true); - - //create a file in memory - $tempFileHandle = fopen('php://memory', 'r+'); - - fwrite($tempFileHandle, 'HTTP/1.1 200 OK - Date: Mon, 01 Oct 2012 20:58:51 GMT - Expires: -1 - Cache-Control: private, max-age=0 - Content-Type: text/html; charset=ISO-8859-1 - X-Frame-Options: SAMEORIGIN - Connection: close - - - - - Foo - - -

Bar

- - - '); - - //get a parsed response - rewind($tempFileHandle); - $httpResponse = $method->invoke($this->adapter, $tempFileHandle); - - //does it look like what we went it? - $this->assertEquals('200', $httpResponse['headers']['status']); - $this->assertEquals('Mon, 01 Oct 2012 20:58:51 GMT', $httpResponse['headers']['date']); - $this->assertEquals('-1', $httpResponse['headers']['expires']); - $this->assertEquals('private, max-age=0', $httpResponse['headers']['cache-control']); - $this->assertEquals('text/html; charset=ISO-8859-1', $httpResponse['headers']['content-type']); - $this->assertEquals('SAMEORIGIN', $httpResponse['headers']['x-frame-options']); - $this->assertEquals('close', $httpResponse['headers']['connection']); - - $this->assertContains('

Bar

', $httpResponse['content']); - } - - /** - * @group isolate - */ - public function testParseJson() - { - $method = new \ReflectionMethod( - $this->adapter, 'getParsedHttpResponse' - ); - $method->setAccessible(true); - - $tempFileHandle = fopen('php://memory', 'r+'); - - fwrite($tempFileHandle, 'HTTP/1.1 200 OK - Foo: bar - Baz: cat - - {"foo":"bar","baz":"cat"} - '); - - //get a parsed response - rewind($tempFileHandle); - $httpResponse = $method->invoke($this->adapter, $tempFileHandle); - - //don't bother testing all this stuff again - $this->assertEquals('200', $httpResponse['headers']['status']); - $this->assertEquals('bar', $httpResponse['headers']['foo']); - $this->assertEquals('cat', $httpResponse['headers']['baz']); - - $this->assertContains('{"foo":"bar","baz":"cat"}', $httpResponse['content']); - } - -} - -class PartialSocketHttpAdapter extends SocketHttpAdapter -{ - public $path; - - public $hostname; - - public function buildHttpRequest($path, $hostname) - { - $this->path = $path; - $this->hostname = $hostname; - - throw new \Exception(); - } -} diff --git a/tests/Geocoder/Tests/HttpAdapter/ZendHttpAdapterTest.php b/tests/Geocoder/Tests/HttpAdapter/ZendHttpAdapterTest.php deleted file mode 100644 index 2cc60de70..000000000 --- a/tests/Geocoder/Tests/HttpAdapter/ZendHttpAdapterTest.php +++ /dev/null @@ -1,54 +0,0 @@ - - */ -class ZendHttpAdapterTest extends TestCase -{ - protected $zend; - - protected function setUp() - { - if (!class_exists('Zend\Http\Client')) { - $this->markTestSkipped('Zend library has to be installed'); - } - - $this->zend = new ZendHttpAdapter(); - } - - public function testGetNullContent() - { - $this->assertNull($this->zend->getContent(null)); - } - - public function testGetFalseContent() - { - $this->assertNull($this->zend->getContent(false)); - } - - public function testGetName() - { - $this->assertEquals('zend', $this->zend->getName()); - } - - public function testGetContentWithCustomAdapter() - { - $zend = new ZendHttpAdapter(); - - try { - $content = $zend->getContent('http://www.google.fr'); - } catch (\Exception $e) { - $this->fail('Exception catched: ' . $e->getMessage()); - } - - $this->assertNotNull($content); - $this->assertContains('google', $content); - } -} diff --git a/tests/Geocoder/Tests/Provider/AbstractProviderTest.php b/tests/Geocoder/Tests/Provider/AbstractProviderTest.php index b1a9d87ee..44b4307e9 100644 --- a/tests/Geocoder/Tests/Provider/AbstractProviderTest.php +++ b/tests/Geocoder/Tests/Provider/AbstractProviderTest.php @@ -4,8 +4,9 @@ use Geocoder\Tests\TestCase; -use Geocoder\HttpAdapter\HttpAdapterInterface; use Geocoder\Provider\AbstractProvider; +use Ivory\HttpAdapter\AbstractHttpAdapter; +use Ivory\HttpAdapter\Message\InternalRequestInterface; /** * @author William Durand @@ -68,14 +69,15 @@ public function getLocalhostDefaults() } } -class MockHttpAdapter implements HttpAdapterInterface +class MockHttpAdapter extends AbstractHttpAdapter { - public function getContent($url) + public function getName() { + return 'mock_http_adapter'; } - public function getName() + protected function doSend(InternalRequestInterface $internalRequest) { - return 'mock_http_adapter'; + } } diff --git a/tests/Geocoder/Tests/Provider/BingMapsTest.php b/tests/Geocoder/Tests/Provider/BingMapsTest.php index 50ef23558..f8faa704a 100644 --- a/tests/Geocoder/Tests/Provider/BingMapsTest.php +++ b/tests/Geocoder/Tests/Provider/BingMapsTest.php @@ -18,7 +18,7 @@ public function testGetName() */ public function testGetGeocodedDataWithNullApiKey() { - $provider = new BingMaps($this->getMock('Geocoder\HttpAdapter\HttpAdapterInterface'), null); + $provider = new BingMaps($this->getMockAdapter($this->never()), null); $provider->getGeocodedData('foo'); } diff --git a/tests/Geocoder/Tests/Provider/GeoIP2Test.php b/tests/Geocoder/Tests/Provider/GeoIP2Test.php index dc82500f8..476b4c280 100644 --- a/tests/Geocoder/Tests/Provider/GeoIP2Test.php +++ b/tests/Geocoder/Tests/Provider/GeoIP2Test.php @@ -11,7 +11,6 @@ namespace Geocoder\Tests\Provider; use Geocoder\Exception\NoResult; -use Geocoder\HttpAdapter\CurlHttpAdapter; use Geocoder\Provider\GeoIP2; use Geocoder\Tests\TestCase; @@ -30,15 +29,6 @@ public function setUp() $this->provider = new GeoIP2($this->getGeoIP2AdapterMock()); } - /** - * @expectedException \Geocoder\Exception\InvalidArgument - * @expectedExceptionMessage GeoIP2Adapter is needed in order to access the GeoIP2 service. - */ - public function testWrongAdapterLeadsToException() - { - new GeoIP2(new CurlHttpAdapter()); - } - public function testGetName() { $expectedName = 'maxmind_geoip2'; diff --git a/tests/Geocoder/Tests/Provider/GeoIPsTest.php b/tests/Geocoder/Tests/Provider/GeoIPsTest.php index 56ffb2db4..9b25bd615 100644 --- a/tests/Geocoder/Tests/Provider/GeoIPsTest.php +++ b/tests/Geocoder/Tests/Provider/GeoIPsTest.php @@ -18,7 +18,7 @@ public function testGetName() */ public function testGetGeocodedDataWithNullApiKey() { - $provider = new GeoIPs($this->getMock('Geocoder\HttpAdapter\HttpAdapterInterface'), null); + $provider = new GeoIPs($this->getMockAdapter($this->never()), null); $provider->getGeocodedData('foo'); } diff --git a/tests/Geocoder/Tests/Provider/GeonamesTest.php b/tests/Geocoder/Tests/Provider/GeonamesTest.php index 38e07486f..3b7a46d20 100644 --- a/tests/Geocoder/Tests/Provider/GeonamesTest.php +++ b/tests/Geocoder/Tests/Provider/GeonamesTest.php @@ -19,7 +19,7 @@ public function testGetName() */ public function testGetGeocodedDataWithNullUsername() { - $provider = new Geonames($this->getMock('\Geocoder\HttpAdapter\HttpAdapterInterface'), null); + $provider = new Geonames($this->getMock('\Ivory\HttpAdapter\HttpAdapterInterface'), null); $provider->getGeocodedData('foo'); } @@ -29,7 +29,7 @@ public function testGetGeocodedDataWithNullUsername() */ public function testGetReversedDataWithNullUsername() { - $provider = new Geonames($this->getMock('\Geocoder\HttpAdapter\HttpAdapterInterface'), null); + $provider = new Geonames($this->getMock('\Ivory\HttpAdapter\HttpAdapterInterface'), null); $provider->getReversedData(array(1,2)); } diff --git a/tests/Geocoder/Tests/Provider/IpInfoDbTest.php b/tests/Geocoder/Tests/Provider/IpInfoDbTest.php index d96516a45..9c5193341 100644 --- a/tests/Geocoder/Tests/Provider/IpInfoDbTest.php +++ b/tests/Geocoder/Tests/Provider/IpInfoDbTest.php @@ -18,7 +18,7 @@ public function testGetName() */ public function testGetDataWithNullApiKey() { - $provider = new IpInfoDb($this->getMock('\Geocoder\HttpAdapter\HttpAdapterInterface'), null); + $provider = new IpInfoDb($this->getMock('\Ivory\HttpAdapter\HttpAdapterInterface'), null); $provider->getGeocodedData('foo'); } @@ -157,7 +157,7 @@ public function testGetGeocodedDataWithRealIPv6() */ public function testReversedData() { - $provider = new IpInfoDb($this->getMock('\Geocoder\HttpAdapter\HttpAdapterInterface'), 'api_key'); + $provider = new IpInfoDb($this->getMock('\Ivory\HttpAdapter\HttpAdapterInterface'), 'api_key'); $provider->getReversedData(array()); } } diff --git a/tests/Geocoder/Tests/Provider/MapQuestTest.php b/tests/Geocoder/Tests/Provider/MapQuestTest.php index 9002f0336..a855616e5 100644 --- a/tests/Geocoder/Tests/Provider/MapQuestTest.php +++ b/tests/Geocoder/Tests/Provider/MapQuestTest.php @@ -22,7 +22,7 @@ public function testGetName() */ public function testGetGeocodedData() { - $provider = new MapQuest($this->getMockAdapter(), 'api_key'); + $provider = new MapQuest($this->getMockAdapterReturns('{}'), 'api_key'); $provider->getGeocodedData('foobar'); } diff --git a/tests/Geocoder/Tests/Provider/MaxMindTest.php b/tests/Geocoder/Tests/Provider/MaxMindTest.php index fb94a24b4..4dbb36edb 100644 --- a/tests/Geocoder/Tests/Provider/MaxMindTest.php +++ b/tests/Geocoder/Tests/Provider/MaxMindTest.php @@ -18,7 +18,7 @@ public function testGetName() */ public function testGetGeocodedDataWithNullApiKey() { - $provider = new MaxMind($this->getMock('Geocoder\HttpAdapter\HttpAdapterInterface'), null); + $provider = new MaxMind($this->getMockAdapter($this->never()), null); $provider->getGeocodedData('foo'); } @@ -100,7 +100,7 @@ public function testGetGeocodedDataWithLocalhostIPv6() */ public function testGetGeocodedDataWithRealIPv4AndNotSupportedService() { - $provider = new MaxMind($this->getMock('Geocoder\HttpAdapter\HttpAdapterInterface'), 'api_key', 'foo'); + $provider = new MaxMind($this->getMockAdapter(), 'api_key', 'foo'); $provider->getGeocodedData('74.200.247.59'); } @@ -110,7 +110,7 @@ public function testGetGeocodedDataWithRealIPv4AndNotSupportedService() */ public function testGetGeocodedDataWithRealIPv6AndNotSupportedService() { - $provider = new MaxMind($this->getMock('Geocoder\HttpAdapter\HttpAdapterInterface'), 'api_key', 12345); + $provider = new MaxMind($this->getMockAdapter(), 'api_key', 12345); $provider->getGeocodedData('::ffff:74.200.247.59'); } diff --git a/tests/Geocoder/Tests/Provider/OpenCageTest.php b/tests/Geocoder/Tests/Provider/OpenCageTest.php index a2b0aafed..928cc6ec0 100644 --- a/tests/Geocoder/Tests/Provider/OpenCageTest.php +++ b/tests/Geocoder/Tests/Provider/OpenCageTest.php @@ -22,7 +22,7 @@ public function testGetName() */ public function testGetGeocodedData() { - $provider = new OpenCage($this->getMockAdapter(), 'api_key'); + $provider = new OpenCage($this->getMockAdapterReturns('{}'), 'api_key'); $provider->getGeocodedData('foobar'); } @@ -32,7 +32,7 @@ public function testGetGeocodedData() */ public function testSslSchema() { - $provider = new OpenCage($this->getMockAdapter(), 'api_key', true); + $provider = new OpenCage($this->getMockAdapterReturns('{}'), 'api_key', true); $provider->getGeocodedData('foobar'); } diff --git a/tests/Geocoder/Tests/Provider/OpenStreetMapTest.php b/tests/Geocoder/Tests/Provider/OpenStreetMapTest.php index bb8c30e1e..a183fcb07 100644 --- a/tests/Geocoder/Tests/Provider/OpenStreetMapTest.php +++ b/tests/Geocoder/Tests/Provider/OpenStreetMapTest.php @@ -444,7 +444,7 @@ public function testGetGeocodedDataWithAddressGetsNullContent() */ public function testGetGeocodedDataWithAddressGetsEmptyContent() { - $provider = new OpenStreetMap($this->getMockAdapterReturns('')); + $provider = new OpenStreetMap($this->getMockAdapterReturns('')); $provider->getGeocodedData('Läntinen Pitkäkatu 35, Turku'); } @@ -477,7 +477,7 @@ public function testGetReversedDataWithCoordinatesGetsNullContent() */ public function testGetReversedDataWithCoordinatesGetsEmptyContent() { - $provider = new OpenStreetMap($this->getMockAdapterReturns('')); + $provider = new OpenStreetMap($this->getMockAdapterReturns('')); $provider->getReversedData(array('60.4539471728726', '22.2567841926781')); } diff --git a/tests/Geocoder/Tests/TestCase.php b/tests/Geocoder/Tests/TestCase.php index 85d93eab0..08be66a7b 100644 --- a/tests/Geocoder/Tests/TestCase.php +++ b/tests/Geocoder/Tests/TestCase.php @@ -2,9 +2,9 @@ namespace Geocoder\Tests; -use Geocoder\HttpAdapter\CurlHttpAdapter; -use Geocoder\HttpAdapter\HttpAdapterInterface; use Geocoder\Model\AddressFactory; +use Ivory\HttpAdapter\HttpAdapterInterface; +use Ivory\HttpAdapter\SocketHttpAdapter; /** * @author William Durand @@ -21,13 +21,15 @@ protected function getMockAdapter($expects = null) $expects = $this->once(); } - $mock = $this->getMock('Geocoder\HttpAdapter\HttpAdapterInterface'); - $mock + $response = $this->getMock('Psr\Http\Message\ResponseInterface'); + + $adapter = $this->getMock('Ivory\HttpAdapter\HttpAdapterInterface'); + $adapter ->expects($expects) - ->method('getContent') - ->will($this->returnArgument(0)); + ->method('get') + ->will($this->returnValue($response)); - return $mock; + return $adapter; } /** @@ -36,13 +38,25 @@ protected function getMockAdapter($expects = null) */ protected function getMockAdapterReturns($returnValue) { - $mock = $this->getMock('Geocoder\HttpAdapter\HttpAdapterInterface'); - $mock + $body = $this->getMock('Psr\Http\Message\StreamableInterface'); + $body + ->expects($this->once()) + ->method('__toString') + ->will($this->returnValue((string) $returnValue)); + + $response = $this->getMock('Psr\Http\Message\ResponseInterface'); + $response + ->expects($this->once()) + ->method('getBody') + ->will($this->returnValue($body)); + + $adapter = $this->getMock('Ivory\HttpAdapter\HttpAdapterInterface'); + $adapter ->expects($this->once()) - ->method('getContent') - ->will($this->returnValue($returnValue)); + ->method('get') + ->will($this->returnValue($response)); - return $mock; + return $adapter; } /** @@ -53,7 +67,7 @@ protected function getMockAdapterReturns($returnValue) */ protected function getAdapter() { - return new CachedResponseAdapter(new CurlHttpAdapter(), $this->useCache()); + return new CachedResponseAdapter(new SocketHttpAdapter(), $this->useCache()); } /**