Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Adding Guzzle HTTP adapter #28

Closed
wants to merge 1 commit into from

2 participants

@mtdowling

No description provided.

@Baachi
Collaborator

What is the state of this PR?

@Baachi Baachi closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 12, 2011
  1. @mtdowling

    Adding Guzzle HTTP adapter

    mtdowling authored
This page is out of date. Refresh to see the latest.
View
3  .gitmodules 100755 → 100644
@@ -4,3 +4,6 @@
[submodule "lib/vendor/doctrine-common"]
path = lib/vendor/doctrine-common
url = https://github.com/doctrine/common.git
+[submodule "lib/vendor/Guzzle"]
+ path = lib/vendor/Guzzle
+ url = https://github.com/guzzle/guzzle.git
View
50 lib/Doctrine/Search/Http/Client/GuzzleClient.php
@@ -0,0 +1,50 @@
+<?php
+
+namespace Doctrine\Search\Http\Client;
+
+use Doctrine\Search\Http\RequestInterface;
+use Doctrine\Search\Http\Response\GuzzleResponse as Response;
+use Guzzle\Service\ClientInterface;
+use Guzzle\Service\Client;
+use Guzzle\Http\Url;
+
+class GuzzleClient extends AbstractClient
+{
+ protected $client;
+
+ public function __construct($host = 'localhost', $port = 80, $username = '', $password = '')
+ {
+ $scheme = ($this->getOption('port') == 443) ? 'https' : 'http';
+ $url = new Url($scheme, $host, $username ?: null, $password ?: null, $port);
+ $this->setClient(new Client((string) $url));
+ parent::__construct($host, $port, $username, $password);
+ }
+
+ /**
+ * You can overwrite the default client if needed
+ *
+ * @param ClientInterface $client
+ */
+ public function setClient(ClientInterface $client)
+ {
+ $this->client = $client;
+ }
+
+ /**
+ * Send a request
+ *
+ * @param string $method The request method
+ * @param array $headers Some http headers
+ * @param string $body POST variables
+ * @return ResponseInterface
+ */
+ public function sendRequest($method = RequestInterface::METHOD_GET, $path = '/', $data = '')
+ {
+ $headers = array();
+ $headers['Connection'] = (true === $this->getOption('keep-alive') ? 'Keep-Alive' : 'Close');
+
+ $response = $this->client->createRequest($method, (string) $path, $headers, $data)->send();
+
+ return new Response($response);
+ }
+}
View
25 lib/Doctrine/Search/Http/Request/GuzzleRequest.php
@@ -0,0 +1,25 @@
+<?php
+namespace Doctrine\Search\Http\Request;
+
+use Guzzle\Http\Message\RequestInterface as GuzzleRequestInterface;
+use Doctrine\Search\Http\RequestInterface;
+
+class GuzzleRequest implements RequestInterface
+{
+ private $request;
+
+ public function __construct(GuzzleRequestInterface $request)
+ {
+ $this->request = $request;
+ }
+
+ public function __toString()
+ {
+ return (string) $this->request;
+ }
+
+ public function getUrl()
+ {
+ return (string) $this->request->getUrl();
+ }
+}
View
50 lib/Doctrine/Search/Http/Response/GuzzleResponse.php
@@ -0,0 +1,50 @@
+<?php
+namespace Doctrine\Search\Http\Response;
+
+use Guzzle\Http\Message\Response;
+use Doctrine\Search\Http\ResponseInterface;
+
+class GuzzleResponse implements ResponseInterface
+{
+ private $guzzleResponse;
+
+ /**
+ * @param Guzzle\Http\Message\Response $response
+ */
+ public function __construct(Response $response)
+ {
+ $this->guzzleResponse = $response;
+ }
+
+ /**
+ * @return int
+ */
+ public function getStatusCode()
+ {
+ return $this->guzzleResponse->getStatusCode();
+ }
+
+ /**
+ * @return string
+ */
+ public function getContent()
+ {
+ return (string) $this->guzzleResponse->getBody();
+ }
+
+ /**
+ * @return bool
+ */
+ public function isSuccessfull()
+ {
+ return $this->guzzleResponse->isSuccessful();
+ }
+
+ /**
+ * @return string
+ */
+ public function __toString()
+ {
+ return (string) $this->guzzleResponse;
+ }
+}
1  lib/vendor/Guzzle
@@ -0,0 +1 @@
+Subproject commit 3acd76070a11099ef93d2aefe156c4c76bec34d6
View
48 tests/Doctrine/Search/Http/Client/GuzzleTest.php
@@ -0,0 +1,48 @@
+<?php
+
+namespace Doctrine\Search\Http\Adapter\Client;
+
+use Guzzle\Http\Message\Response;
+use Guzzle\Service\Client;
+use Guzzle\Service\Plugin\MockPlugin;
+
+/**
+ * @author mtdowling <michael@guzzlephp.org>
+ */
+class GuzzleTest extends \PHPUnit_Framework_TestCase
+{
+ public function testCallExistingHost()
+ {
+ $guzzle = new Client('http://test.com/');
+ // Queue up a mock response
+ $mock = new MockPlugin();
+ $mock->addResponse(new Response(200, null, 'body'));
+ $guzzle->getEventManager()->attach($mock);
+
+ $client = new \Doctrine\Search\Http\Client\GuzzleClient('test.com', 80);
+ $client->setClient($guzzle);
+ $response = $client->sendRequest('get');
+
+ $this->assertInstanceOf('Doctrine\\Search\\Http\\ResponseInterface', $response);
+ $this->assertEquals('body', $response->getContent());
+ $this->assertTrue($response->isSuccessfull());
+ }
+
+ /**
+ * @expectedException Exception
+ */
+ public function testCallNotExistingHost()
+ {
+ $guzzle = new Client('http://www.test.com/');
+
+ $client = new \Doctrine\Search\Http\Client\GuzzleClient('www.not-existing-host.de', 80);
+ $client->setClient($guzzle);
+
+ // Queue up a mock response
+ $mock = new MockPlugin();
+ $mock->addResponse(new Response(404));
+ $guzzle->getEventManager()->attach($mock);
+
+ $client->sendRequest('get');
+ }
+}
View
26 tests/Doctrine/Search/Http/Request/GuzzleRequestTest.php
@@ -0,0 +1,26 @@
+<?php
+
+namespace Doctrine\Search\Http\Request;
+
+use Doctrine\Search\Http\Request\GuzzleRequest;
+use Guzzle\Http\Message\RequestFactory;
+use Guzzle\Service\Client;
+use Guzzle\Service\Plugin\MockPlugin;
+
+/**
+ * @author mtdowling <michael@guzzlephp.org>
+ */
+class GuzzleRequestTest extends \PHPUnit_Framework_TestCase
+{
+ public function testCastsToString()
+ {
+ $request = new GuzzleRequest(RequestFactory::get('http://test.com/'));
+ $this->assertContains("GET / HTTP/1.1\r\n", (string) $request);
+ }
+
+ public function testRetreivesUrl()
+ {
+ $request = new GuzzleRequest(RequestFactory::get('http://test.com/'));
+ $this->assertEquals('http://test.com/', $request->getUrl());
+ }
+}
View
44 tests/Doctrine/Search/Http/Response/GuzzleResponseTest.php
@@ -0,0 +1,44 @@
+<?php
+
+namespace Doctrine\Search\Http\Response;
+
+use Doctrine\Search\Http\Response\GuzzleResponse;
+use Guzzle\Http\Message\Response;
+
+/**
+ * @author mtdowling <michael@guzzlephp.org>
+ */
+class GuzzleRequestTest extends \PHPUnit_Framework_TestCase
+{
+ public function testCastsToString()
+ {
+ $guzzle = new Response(200, null, 'test');
+ $response = new GuzzleResponse($guzzle);
+ $this->assertEquals("HTTP/1.1 200 OK\r\n\r\ntest", (string) $response);
+ }
+
+ public function testIsSuccessful()
+ {
+ $guzzle = new Response(200, null, 'test');
+ $response = new GuzzleResponse($guzzle);
+ $this->assertTrue($response->isSuccessfull());
+
+ $guzzle = new Response(401);
+ $response = new GuzzleResponse($guzzle);
+ $this->assertFalse($response->isSuccessfull());
+ }
+
+ public function testGetStatusCode()
+ {
+ $guzzle = new Response(401);
+ $response = new GuzzleResponse($guzzle);
+ $this->assertEquals(401, $response->getStatusCode());
+ }
+
+ public function testGetContent()
+ {
+ $guzzle = new Response(200, null, 'test');
+ $response = new GuzzleResponse($guzzle);
+ $this->assertEquals('test', $response->getContent());
+ }
+}
View
5 tests/bootstrap.php
@@ -14,4 +14,7 @@
$loader->register();
AnnotationRegistry::registerFile(__DIR__ . '/../lib/Doctrine/Search/Mapping/Annotations/DoctrineAnnotations.php');
-BuzzAutoloader::register();
+BuzzAutoloader::register();
+
+$loader = new ClassLoader('Guzzle', __DIR__ . '/../lib/vendor/Guzzle/src');
+$loader->register();
Something went wrong with that request. Please try again.