diff --git a/composer.json b/composer.json index be1ed5c53..5370890ca 100644 --- a/composer.json +++ b/composer.json @@ -17,6 +17,7 @@ "require-dev": { "kriswallsmith/buzz": "@stable", "guzzle/guzzle": "@stable", + "guzzlehttp/guzzle": "@stable", "zendframework/zend-http": "~2.1", "geoip/geoip": "~1.13", "geoip2/geoip2": "~0.6" @@ -25,7 +26,8 @@ "kriswallsmith/buzz": "Enabling Buzz allows you to use the BuzzHttpAdapter.", "ext-curl": "Enabling the curl extension allows you to use the CurlHttpAdapter.", "ext-geoip": "Enabling the geoip extension allows you to use the MaxMindProvider.", - "guzzle/guzzle": "Enabling Guzzle allows you to use the GuzzleHttpAdapter.", + "guzzle/guzzle": "Enabling Guzzle 3 allows you to use the GuzzleHttpAdapter.", + "guzzlehttp/guzzle": "Enabling Guzzle 4 allows you to use the Guzzle4HttpAdapter.", "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/Guzzle4HttpAdapter.php b/src/Geocoder/HttpAdapter/Guzzle4HttpAdapter.php new file mode 100644 index 000000000..2c66befec --- /dev/null +++ b/src/Geocoder/HttpAdapter/Guzzle4HttpAdapter.php @@ -0,0 +1,54 @@ + + * @link http://www.guzzlephp.org + */ +class Guzzle4HttpAdapter 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); + + return (string) $response->getBody(); + } + + /** + * {@inheritDoc} + */ + public function getName() + { + return 'guzzle4'; + } +} diff --git a/tests/Geocoder/Tests/HttpAdapter/Guzzle4HttpAdapterTest.php b/tests/Geocoder/Tests/HttpAdapter/Guzzle4HttpAdapterTest.php new file mode 100644 index 000000000..58343be10 --- /dev/null +++ b/tests/Geocoder/Tests/HttpAdapter/Guzzle4HttpAdapterTest.php @@ -0,0 +1,50 @@ + + */ +class Guzzle4HttpAdapterTest extends \Geocoder\Tests\TestCase +{ + protected function setUp() + { + if (!class_exists('GuzzleHttp\Client')) { + $this->markTestSkipped('Guzzle library has to be installed'); + } + } + + public function testGetName() + { + $adapter = new Guzzle4HttpAdapter(); + $this->assertEquals('guzzle4', $adapter->getName()); + } + + /** + * @covers Geocoder\HttpAdapter\GuzzleHttpAdapter::__construct + * @covers Geocoder\HttpAdapter\GuzzleHttpAdapter::getContent + */ + public function testRetrievesResponse() + { + $historyPlugin = new History(); + $mockPlugin = new Mock(array(new Response(200, array(), Stream::factory('body')))); + + $client = new Client(); + $client->getEmitter()->attach($mockPlugin); + $client->getEmitter()->attach($historyPlugin); + + $adapter = new Guzzle4HttpAdapter($client); + $this->assertEquals('body', $adapter->getContent('http://test.com/')); + + $this->assertEquals('http://test.com/', + $historyPlugin->getLastRequest()->getUrl()); + } +}