From 8733cc6802b79b84f4f1c30df58daeab579c8bfc Mon Sep 17 00:00:00 2001 From: Matthias Jouan Date: Tue, 31 Jan 2017 13:23:12 +0000 Subject: [PATCH 1/2] Add a Guzzle client Add guzzlehttp/guzzle as composer suggestion and provide a client based on it. --- composer.json | 3 ++- src/Client/Guzzle.php | 44 +++++++++++++++++++++++++++++++++++++ tests/Client/GuzzleTest.php | 34 ++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 src/Client/Guzzle.php create mode 100644 tests/Client/GuzzleTest.php diff --git a/composer.json b/composer.json index 2021d22..124d291 100644 --- a/composer.json +++ b/composer.json @@ -21,6 +21,7 @@ } }, "suggest": { - "ext-curl": "*" + "ext-curl": "*", + "guzzlehttp/guzzle": "~6.2" } } diff --git a/src/Client/Guzzle.php b/src/Client/Guzzle.php new file mode 100644 index 0000000..2114c6a --- /dev/null +++ b/src/Client/Guzzle.php @@ -0,0 +1,44 @@ +client = new \GuzzleHttp\Client([ + 'base_uri' => $base_uri, + 'timeout' => $timeout, + ]); + + $this->options = []; + } + + public function setUserAgent($agent) { + $this->options = [ + 'User-Agent' => $agent + ]; + } + + public function get($url) { + try { + $response = $this->client->request('GET', $url, $this->options); + + } catch (\GuzzleHttp\Exception\RequestException $e) { + throw new Exception($url, $e->getResponse()->getStatusCode()); + } + + return (string)$response->getBody(); + } +} diff --git a/tests/Client/GuzzleTest.php b/tests/Client/GuzzleTest.php new file mode 100644 index 0000000..2efe4b0 --- /dev/null +++ b/tests/Client/GuzzleTest.php @@ -0,0 +1,34 @@ + + * @license MIT + */ + +namespace Essence\Http\Client; + +use PHPUnit_Framework_TestCase as TestCase; + + + +/** + * Test case for Guzzle. + */ +class GuzzleTest extends TestCase { + + public $Guzzle = null; + + public function setUp() { + $this->Guzzle = new Guzzle(); + } + + public function testGet() { + $content = $this->Guzzle->get('http://example.com/'); + $this->assertRegExp('/This domain is established to be used/', $content); + } + + public function testGetUnreachable() { + $this->setExpectedException('\\Essence\\Http\\Exception'); + $this->Guzzle->get('http://example.com/dfzgdz/czcdcd'); + } +} From 8be78ddab2aacdc3de2bfc541c511e9f4ee53342 Mon Sep 17 00:00:00 2001 From: Matthias Jouan Date: Tue, 31 Jan 2017 15:16:23 +0000 Subject: [PATCH 2/2] fix: add guzzle as dev dependency new unit tests break the build because guzzle was only a suggestion --- composer.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/composer.json b/composer.json index 124d291..d3e2cf4 100644 --- a/composer.json +++ b/composer.json @@ -20,6 +20,9 @@ "Essence\\Http\\": "src" } }, + "require-dev": { + "guzzlehttp/guzzle": "~6.2" + }, "suggest": { "ext-curl": "*", "guzzlehttp/guzzle": "~6.2"