Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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."
Expand Down
54 changes: 54 additions & 0 deletions src/Geocoder/HttpAdapter/Guzzle4HttpAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

/**
* This file is part of the Geocoder package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/

namespace Geocoder\HttpAdapter;

use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;

/**
* Http adapter for the Guzzle framework
*
* @author Michael Dowling <michael@guzzlephp.org>
* @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';
}
}
50 changes: 50 additions & 0 deletions tests/Geocoder/Tests/HttpAdapter/Guzzle4HttpAdapterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Geocoder\Tests\HttpAdapter;

use Geocoder\HttpAdapter\Guzzle4HttpAdapter;

use GuzzleHttp\Message\Response;
use GuzzleHttp\Stream\Stream;
use GuzzleHttp\Subscriber\History;
use GuzzleHttp\Subscriber\Mock;
use GuzzleHttp\Client;

/**
* @author Michael Dowling <michael@guzzlephp.org>
*/
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());
}
}