Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

[WIP] Refactor ElasticaClient #76

Merged
merged 3 commits into from

2 participants

@Baachi
Collaborator

I remove the Http component and use the ruflin/Elastica instead. I don't want to reinvent the wheel.

see #69

@Baachi
Collaborator

@Danizord do you use elastica? I have some problems with the index type.
I don't see a setter for the index type, only a getter.

@danizord

@Baachi I never used it =/ but i'll search about it

@Baachi
Collaborator

Great! Let me know if you find a solution :)

@Baachi
Collaborator

@Danizord I merge this PR, if you find any solution for the type problem, feel free to submit a PR.

@Baachi Baachi merged commit 45a233e into doctrine:master
@danizord

@Baachi I'm not having a lot of time this month, but I will contribute soon.

@Baachi
Collaborator

No problem :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 20, 2013
  1. @Baachi
  2. @Baachi

    Update testcase

    Baachi authored
  3. @Baachi
This page is out of date. Refresh to see the latest.
View
2  composer.json
@@ -11,7 +11,7 @@
"doctrine/common": "2.3.*"
},
"require-dev": {
- "kriswallsmith/buzz": "0.7.*",
+ "ruflin/Elastica": "0.19.*",
"doctrine/orm": "2.3.*",
"doctrine/mongodb": "1.0.*",
"doctrine/mongodb-odm": "1.0.*"
View
75 lib/Doctrine/Search/ElasticSearch/Client.php
@@ -20,99 +20,62 @@
namespace Doctrine\Search\ElasticSearch;
use Doctrine\Search\SearchClientInterface;
-use Doctrine\Common\Persistence\ObjectManager;
-use Doctrine\Search\Http\ClientInterface as HttpClientInterface;
-
-use Doctrine\Search\Exception\Json\JsonEncodeException;
-use Doctrine\Search\Exception\Json\JsonDecodeException;
-
/**
* SearchManager for ElasticSearch-Backend
*
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @author Mike Lohmann <mike.h.lohmann@googlemail.com>
+ * @author Markus Bachmann <markus.bachmann@bachi.biz>
*/
class Client implements SearchClientInterface
{
-
+ /**
+ * @var \Elastica_Client
+ */
private $client;
- private $host;
-
- private $port;
-
- /*
- * @param Connection $conn
- * @param Configuration $config
+ /**
+ * @param \Elastica_Client $client
*/
- public function __construct(HttpClientInterface $client = null, $host = 'localhost', $port = '9200')
+ public function __construct(\Elastica_Client $client)
{
- $this->host = (string)$host;
- $this->port = (string)$port;
- $this->client = $client ? : new \Doctrine\Search\Http\Client\BuzzClient();
+ $this->client = $client;
}
/**
- *
- * @see Doctrine\Search.SearchClientInterface::find()
+ * {@inheritDoc}
*/
public function find($index, $type, $query)
{
- $searchUrl = $this->host . ':' . $this->port . '/' . (string)$index . '/' . (string)$type . '/_search?' . (string)$query;
-
- $response = $this->client->sendRequest('GET', $searchUrl);
-
- $content = $response->getContent();
- $decodedJson = json_decode($content);
-
- if ($decodedJson == NULL) {
- throw new JsonDecodeException();
- }
-
- return $decodedJson;
-
+ $index = $this->client->getIndex($index);
+ return iterator_to_array($index->search($query));
}
/**
- * (non-PHPdoc)
- * @see Doctrine\Search.SearchClientInterface::createIndex()
- *
- * @return Doctrine\Search\Http\ResponseInterface
+ * {@inheritDoc}
*/
public function createIndex($index, $type, array $data)
{
- $encodedJson = json_encode($data);
-
- if ($encodedJson == NULL) {
- throw new JsonEncodeException($data);
- }
- $indexUrl = $this->host . ':' . $this->port . '/' . (string)$index . '/' . (string)$type . '/';
-
- return $this->client->sendRequest('PUT', $indexUrl, $encodedJson);
-
- }
-
- public function updateIndex(array $data)
- {
+ $index = $this->client->getIndex($index);
+ $index->create();
+ $index->addDocuments($data);
}
/**
- *
- * @param array $data
+ * {@inheritDoc}
*/
public function deleteIndex($index)
{
- // TODO: Implement deleteIndex() method.
+ $index = $this->client->getIndex($index);
+ $index->delete();
}
/**
- * @param array $data
+ * {@inheritDoc}
*/
public function bulkSearch(array $data)
{
- // TODO: Implement bulkAction() method.
}
-
}
View
36 lib/Doctrine/Search/ElasticSearch/Configuration.php
@@ -1,36 +0,0 @@
-<?php
-/*
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- * This software consists of voluntary contributions made by many individuals
- * and is licensed under the LGPL. For more information, see
- * <http://www.doctrine-project.org>.
- */
-
-namespace Doctrine\Search\ElasticSearch;
-
-/**
- * Configuration handler for ElasticSearch-Backend
- *
- * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
- * @author Mike Lohmann <mike.h.lohmann@googlemail.com>
- */
-class Configuration
-{
-
-
- public function __construct()
- {
-
- }
-}
View
52 lib/Doctrine/Search/ElasticSearch/Connection.php
@@ -1,52 +0,0 @@
-<?php
-/*
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- * This software consists of voluntary contributions made by many individuals
- * and is licensed under the LGPL. For more information, see
- * <http://www.doctrine-project.org>.
- */
-
-namespace Doctrine\Search\ElasticSearch;
-
-use Doctrine\Search\Http\ClientInterface as HttpClientInterface;
-
-/**
- * Connections handler for ElasticSearch-Backend
- *
- * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
- * @author Mike Lohmann <mike.h.lohmann@googlemail.com>
- */
-class Connection
-{
- private $host;
-
- private $port;
-
- private $path;
-
- private $httpClient;
-
- public function __construct($host = null, $port = null, $path = null, HttpClientInterface $httpClient)
- {
- $this->host = $host;
- $this->port = $port;
- $this->path = $path;
- $this->httpClient = $httpClient;
- }
-
- public function initialize()
- {
-
- }
-}
View
27 lib/Doctrine/Search/Exception/Json/JsonDecodeException.php
@@ -1,27 +0,0 @@
-<?php
-namespace Doctrine\Search\Exception\Json;
-
-use \Doctrine\Search\Exception\DoctrineSearchException;
-
-class JsonDecodeException extends DoctrineSearchException
-{
-
- static private $errors = array(
- JSON_ERROR_NONE => 'unknown error',
- JSON_ERROR_DEPTH => 'maximum stack depth has been exceeded',
- JSON_ERROR_STATE_MISMATCH => 'invalid or malformed JSON',
- JSON_ERROR_SYNTAX => 'Syntax error',
- JSON_ERROR_CTRL_CHAR => 'Control character error',
- JSON_ERROR_UTF8 => 'Malformed UTF-8 characters',
- );
-
- public function __construct()
- {
- $message = sprintf(
- 'Failure by decode from json [%s]',
- static::$errors[json_last_error()]
- );
-
- parent::__construct($message);
- }
-}
View
28 lib/Doctrine/Search/Exception/Json/JsonEncodeException.php
@@ -1,28 +0,0 @@
-<?php
-namespace Doctrine\Search\Exception\Json;
-
-use \Doctrine\Search\Exception\DoctrineSearchException;
-
-class JsonEncodeException extends DoctrineSearchException
-{
-
- static private $errors = array(
- JSON_ERROR_NONE => 'unknown error',
- JSON_ERROR_DEPTH => 'maximum stack depth has been exceeded',
- JSON_ERROR_STATE_MISMATCH => 'invalid or malformed JSON',
- JSON_ERROR_SYNTAX => 'Syntax error',
- JSON_ERROR_CTRL_CHAR => 'Control character error',
- JSON_ERROR_UTF8 => 'Malformed UTF-8 characters',
- );
-
- public function __construct($value)
- {
- $message = sprintf(
- 'Failure by encode %s to json [%s]',
- (is_object($value) ? get_class($value) : gettype($value)),
- static::$errors[json_last_error()]
- );
-
- parent::__construct($message);
- }
-}
View
44 lib/Doctrine/Search/Http/Client/AbstractClient.php
@@ -1,44 +0,0 @@
-<?php
-namespace Doctrine\Search\Http\Client;
-
-use \Doctrine\Search\Http\ClientInterface;
-
-abstract class AbstractClient implements ClientInterface
-{
-
- protected $options = array(
- 'host' => 'localhost',
- 'port' => '80',
- 'timeout' => 10,
- 'keep-alive' => true,
- 'username' => '',
- 'password' => '',
- );
-
- public function __construct($host = 'localhost', $port = 80, $username = '', $password = '')
- {
- $this->setOption('host', rtrim($host, '/'));
- $this->setOption('port', $port);
- $this->setOption('username', $username);
- $this->setOption('password', $password);
- }
-
- public function getOption($key)
- {
- if (!isset($this->options[$key])) {
- throw new Exception(sprintf('The option %s is not available', $key));
- }
-
- return $this->options[$key];
- }
-
- public function setOption($key, $value)
- {
- if (!isset($this->options[$key])) {
- throw new Exception(sprintf('The option %s does not exist', $key));
- }
-
- $this->options[$key] = $value;
- }
-
-}
View
50 lib/Doctrine/Search/Http/Client/BuzzClient.php
@@ -1,50 +0,0 @@
-<?php
-namespace Doctrine\Search\Http\Client;
-
-use Doctrine\Search\Http\Response\BuzzResponse as Response;
-use Buzz\Browser;
-
-class BuzzClient extends AbstractClient
-{
- protected $browser;
-
- public function __construct(Browser $browser = null, $host = 'localhost', $port = 80, $username = '', $password = '')
- {
- $this->browser = $browser ? : new Browser();
-
- parent::__construct($host, $port, $username, $password);
- }
-
- /**
- * 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 = 'get', $path = '/', $data = '')
- {
- $url = $this->getOption('host') . ':' . $this->getOption('port');
- $headers = array();
-
- $username = $this->getOption('username');
- $password = $this->getOption('password');
-
- if (null !== $username && null !== $password) {
- $headers['Authorization'] = sprintf('Basic: %s', base64_encode($username . ':' . $password));
- }
-
- $headers['Connection'] = (true === $this->getOption('keep-alive') ? 'Keep-Alive' : 'Close');
-
- if ($method === 'post') {
- $response = $this->browser->call($url, $method, $headers, $data);
- } else {
- $response = $this->browser->call($url, $method, $headers);
- }
-
- return new Response($response);
- }
-
-
-}
View
114 lib/Doctrine/Search/Http/Client/CurlClient.php
@@ -1,114 +0,0 @@
-<?php
-
-namespace Doctrine\Search\Http\Client;
-
-use Doctrine\Search\Http\Response\Response;
-
-/**
- * @author Markus Bachmann <markus.bachmann@bachi.biz>
- */
-class CurlClient extends AbstractClient
-{
- /**
- * Send a request
- *
- * @todo: replace exceptions with specialized ones.
- * @todo: if host is unreachable a standard \Exception is thrown
- *
- * @param string $method The request method
- * @param array $headers Some http headers
- * @param string $body POST variables
- * @return ResponseInterface
- */
- public function sendRequest($method = 'get', $path = '/', $data = '')
- {
- $url = sprintf('%s:%s/%s', $this->getOption('host'), $this->getOption('port'), ltrim($path, '/'));
- $options = array();
- list($curlHttpMethod, $curlHttpMethodValue) = $this->getCurlMethod($method);
-
- $options[CURL_HTTP_VERSION_1_1] = true;
- $options[CURLOPT_HEADER] = true;
- $options[CURLOPT_RETURNTRANSFER] = true;
- $options[$curlHttpMethod] = $curlHttpMethodValue;
-
- if ('post' === $method || 'put' === $method) {
- $options[CURLOPT_POSTFIELDS] = $data;
- }
-
- $ch = curl_init($url);
- curl_setopt_array($ch, $options);
- $content = curl_exec($ch);
- $rawHeadersString = curl_getinfo($ch, CURLINFO_HEADER_OUT);
- $rawHeaders = explode(PHP_EOL, $rawHeadersString);
- $headers = array();
-
- foreach ($rawHeaders as $line) {
- $line = trim($line);
- if (preg_match('@^(\w+)\s*(.*)\sHTTP\s*/\s*\d{1}\.\d{1}$@', $line, $matches)) {
- $headers['method'] = $matches[1];
- $headers['path'] = $matches[2];
- } else if (preg_match('@^(.*)\s*:\s*(.*)\s*$@', $line, $matches)) {
- $headers[$matches[1]] = $matches[2];
- }
- }
-
- $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
-
- if (false === $content) {
- //@todo: replace Exception with specialized one
- throw new \Exception(sprintf(
- 'Request to %s failed (%s)',
- $url,
- curl_error($ch)
- ));
- }
-
- curl_close($ch);
-
- return new Response($status, $headers, $content);
- }
-
- private function getCurlMethod($method)
- {
- $curlMethodValue = true;
- switch (strtoupper($method)) {
- case 'GET':
- $curlMethod = CURLOPT_HTTPGET;
- break;
-
- case 'POST':
- $curlMethod = CURLOPT_POST;
- break;
-
- case 'PUT':
- $curlMethod = CURLOPT_CUSTOMREQUEST;
- $curlMethodValue = "PUT";
- break;
-
- case 'DELETE' :
- $curlMethod = CURLOPT_CUSTOMREQUEST;
- $curlMethodValue = "DELETE";
- break;
-
- case 'OPTIONS' :
- $curlMethod = CURLOPT_CUSTOMREQUEST;
- $curlMethodValue = "OPTIONS";
- break;
-
- case 'TRACE' :
- $curlMethod = CURLOPT_CUSTOMREQUEST;
- $curlMethodValue = "TRACE";
- break;
-
- case 'HEAD' :
- $curlMethod = CURLOPT_CUSTOMREQUEST;
- $curlMethodValue = "HEAD";
- break;
- default:
- //@todo: replace Exception with specialized one
- throw new \RuntimeException('Method ' . strtoupper($method) . ' is not supported');
- }
-
- return array($curlMethod, $curlMethodValue);
- }
-}
View
17 lib/Doctrine/Search/Http/ClientInterface.php
@@ -1,17 +0,0 @@
-<?php
-namespace Doctrine\Search\Http;
-
-
-interface ClientInterface
-{
- /**
- * Send a request
- *
- * @param string $method The request method
- * @param array $headers Some http headers
- * @param string $body POST variables
- * @return ResponseInterface
- */
- function sendRequest($method, $path, $data);
-
-}
View
70 lib/Doctrine/Search/Http/Response/BuzzResponse.php
@@ -1,70 +0,0 @@
-<?php
-namespace Doctrine\Search\Http\Response;
-
-use Buzz\Message\Response;
-use Doctrine\Search\Http\ResponseInterface;
-
-class BuzzResponse implements ResponseInterface
-{
- private $buzzResponse;
-
- /**
- * @param \Buzz\Message\Response $response
- */
- public function __construct(Response $response)
- {
- $this->buzzResponse = $response;
- }
-
- /**
- * @return int
- */
- public function getStatusCode()
- {
- return $this->buzzResponse->getStatusCode();
- }
-
- /**
- * @return string
- */
- public function getContent()
- {
- return $this->buzzResponse->getContent();
- }
-
- /**
- * @return bool
- */
- public function isSuccessfull()
- {
- return 200 === $this->buzzResponse->getStatusCode();
- }
-
- /**
- * Get a header
- *
- * @return string
- */
- public function getHeader($key)
- {
- return $this->buzzResponse->getHeader($key);
- }
-
- /**
- * Get all headers
- *
- * @return array
- */
- public function getHeaders()
- {
- return $this->buzzResponse->getHeaders();
- }
-
- /**
- * @return string
- */
- public function __toString()
- {
- return $this->buzzResponse->__toString();
- }
-}
View
87 lib/Doctrine/Search/Http/Response/Response.php
@@ -1,87 +0,0 @@
-<?php
-namespace Doctrine\Search\Http\Response;
-
-use \Doctrine\Search\Http\ResponseInterface;
-
-class Response implements ResponseInterface
-{
-
- /**
- * @var integer
- */
- private $statusCode;
-
- /**
- * @var string
- */
- private $body;
-
- /**
- * @var array
- */
- private $headers;
-
- /**
- * @param integer $statusCode
- * @param string $body
- */
- public function __construct($statusCode, array $headers, $body)
- {
- $this->statusCode = $statusCode;
- $this->body = $body;
- $this->headers = $headers;
- }
-
- public function getHeaders()
- {
- return $this->headers;
- }
-
- /**
- * Get a header
- *
- * @return string
- */
- public function getHeader($key)
- {
- return isset($this->headers[$key]) ? $this->headers[$key] : null;
- }
-
-
- /**
- * Get the statuscode
- *
- * @return int
- */
- public function getStatusCode()
- {
- return $this->statusCode;
- }
-
- /**
- * @return bool
- */
- public function isSuccessfull()
- {
- return 200 === $this->getStatusCode();
- }
-
- /**
- * Get content
- *
- * @return string
- */
- public function getContent()
- {
- return $this->body;
- }
-
- /**
- * @return string
- */
- public function __toString()
- {
- return $this->body;
- }
-
-}
View
41 lib/Doctrine/Search/Http/ResponseInterface.php
@@ -1,41 +0,0 @@
-<?php
-namespace Doctrine\Search\Http;
-
-interface ResponseInterface
-{
- /**
- * Get the statuscode
- *
- * @return int
- */
- function getStatusCode();
-
- /**
- * Get a header
- *
- * @return string
- */
- function getHeader($key);
-
- /**
- * Get all headers
- *
- * @return array
- */
- function getHeaders();
-
- /**
- * @return bool
- */
- function isSuccessfull();
-
- /**
- * @return string
- */
- function __toString();
-
- /**
- * @return string
- */
- function getContent();
-}
View
77 tests/Doctrine/Tests/Search/ElasticSearch/ClientTest.php
@@ -0,0 +1,77 @@
+<?php
+
+namespace Doctrine\Tests\Search\ElasticSearch;
+
+use Doctrine\Search\ElasticSearch\Client;
+
+/**
+ * @author Markus Bachmann <markus.bachmann@bachi.biz>
+ */
+class ClientTest extends \PHPUnit_Framework_TestCase
+{
+ protected function setUp()
+ {
+ $this->elasticaClient = $this->getMock('Elastica_Client');
+ $this->client = new Client($this->elasticaClient);
+ }
+
+ public function testFind()
+ {
+ $index = $this->getMockBuilder('Elastica_Index')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $result = $this->getMockBuilder('Elastica_ResultSet')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $this->elasticaClient->expects($this->once())
+ ->method('getIndex')
+ ->with('foo')
+ ->will($this->returnValue($index));
+
+ $index->expects($this->once())
+ ->method('search')
+ ->with('foobar')
+ ->will($this->returnValue($result));
+
+ $this->assertEquals(array(), $this->client->find('foo', '', 'foobar'));
+ }
+
+ public function testCreateIndex()
+ {
+ $index = $this->getMockBuilder('Elastica_Index')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $index->expects($this->once())
+ ->method('create');
+
+ $index->expects($this->once())
+ ->method('addDocuments');
+
+ $this->elasticaClient->expects($this->once())
+ ->method('getIndex')
+ ->with('foo')
+ ->will($this->returnValue($index));
+
+ $this->client->createIndex('foo', '', array('foo' => 'bar'));
+ }
+
+ public function testDeleteIndex()
+ {
+ $index = $this->getMockBuilder('Elastica_Index')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $index->expects($this->once())
+ ->method('delete');
+
+ $this->elasticaClient->expects($this->once())
+ ->method('getIndex')
+ ->with('foo')
+ ->will($this->returnValue($index));
+
+ $this->client->deleteIndex('foo');
+ }
+}
View
135 tests/Doctrine/Tests/Search/ElasticSearch/Tests/ClientTest.php
@@ -1,135 +0,0 @@
-<?php
-namespace Doctrine\Tests\Search\ElasticSearch\Tests;
-use Doctrine\Search\ElasticSearch\Client;
-use Doctrine\Search\ElasticSearch\Connection;
-use Doctrine\Search\Http\Response\BuzzResponse;
-
-class ClientTest extends \PHPUnit_Framework_TestCase
-{
- private $client;
-
- private $httpClient;
-
- private $json;
-
- private $connection;
-
- protected function setUp()
- {
- $this->httpClient = $this->getMock('Doctrine\\Search\\Http\\Client\\BuzzClient', array(), array(), '', false);
- $this->json = '{
- "user": "kimchy",
- "postDate": "2009-11-15T14:12:12",
- "message": "Another tweet, will it be indexed?"
- }';
-
- $this->client = new Client($this->httpClient);
- }
-
- /**
- *
- * @expectedException PHPUnit_Framework_Error
- */
- public function testFindWrongParameterIndex()
- {
- $buzzResponse = $this->getMock('Buzz\Message\Response');
- $buzzResponse->expects($this->never())
- ->method('getContent')
- ->will($this->returnValue($this->json));
-
- $mockedResponse = new BuzzResponse($buzzResponse);
-
- $this->httpClient->expects($this->never())
- ->method('sendRequest')
- ->will($this->returnValue($mockedResponse));
-
- $this->client->find(new \StdClass(), 'tester', 's=user:kimchy');
- }
-
- /**
- *
- * @expectedException PHPUnit_Framework_Error
- */
- public function testFindWrongParameterType()
- {
- $buzzResponse = $this->getMock('Buzz\Message\Response');
- $buzzResponse->expects($this->never())
- ->method('getContent')
- ->will($this->returnValue($this->json));
-
- $mockedResponse = new BuzzResponse($buzzResponse);
-
- $this->httpClient->expects($this->never())
- ->method('sendRequest')
- ->will($this->returnValue($mockedResponse));
-
- $this->client->find('test', new \StdClass(), 's=user:kimchy');
- }
-
- /**
- *
- * @expectedException PHPUnit_Framework_Error
- */
- public function testFindWrongParameterQuery()
- {
- $buzzResponse = $this->getMock('Buzz\Message\Response');
- $buzzResponse->expects($this->never())
- ->method('getContent')
- ->will($this->returnValue($this->json));
-
- $mockedResponse = new BuzzResponse($buzzResponse);
-
- $this->httpClient->expects($this->never())
- ->method('sendRequest')
- ->will($this->returnValue($mockedResponse));
-
- $this->client->find('test', 'tester', new \StdClass());
- }
-
- /**
- *
- * @expectedException \Doctrine\Search\Exception\Json\JsonDecodeException
- */
- public function testThrowDecodeException()
- {
- $buzzResponse = $this->getMock('Buzz\Message\Response');
- $buzzResponse->expects($this->once())
- ->method('getContent')
- ->will($this->returnValue(',,'));
-
- $mockedResponse = new BuzzResponse($buzzResponse);
-
- $this->httpClient->expects($this->once())
- ->method('sendRequest')
- ->will($this->returnValue($mockedResponse));
-
- $this->client->find('test', 'tester', 'index');
- }
-
- /**
- * @dataProvider getQuery
- */
- public function testFind($index, $type, $query)
- {
- $buzzResponse = $this->getMock('Buzz\Message\Response');
- $buzzResponse->expects($this->once())
- ->method('getContent')
- ->will($this->returnValue($this->json));
-
- $mockedResponse = new BuzzResponse($buzzResponse);
-
- $this->httpClient->expects($this->once())
- ->method('sendRequest')
- ->will($this->returnValue($mockedResponse));
-
- $result = $this->client->find($index, $type, $query);
- $this->assertEquals(json_decode($this->json), $result);
- }
-
- static public function getQuery()
- {
- return array(
- array('http://localhost:9200','test','tester'),
- );
- }
-}
View
41 tests/Doctrine/Tests/Search/Http/Client/BuzzTest.php
@@ -1,41 +0,0 @@
-<?php
-namespace Doctrine\Tests\Search\Http\Adapter\Client;
-
-use Doctrine\Tests\Search\Http\Client\Mocks\FileGetContentsMock;
-
-use Doctrine\Search\Http\Client\BuzzClient;
-use Buzz\Browser;
-
-/**
- * @author Bachi
- */
-class BuzzTest extends \PHPUnit_Framework_TestCase
-{
- private $client;
-
- private $browser;
-
- protected function setUp()
- {
- $this->browser = $this->getMock('Buzz\\Browser', array(), array(), '', false);
- $this->client = new BuzzClient($this->browser, 'www.google.de', 80);
- }
-
- public function testCallExistingHost()
- {
- $buzzResponse = $this->getMockBuilder('Buzz\\Message\\Response')
- ->disableOriginalConstructor()
- ->getMock();
-
- $this->browser->expects($this->once())
- ->method('call')
- ->will($this->returnValue($buzzResponse));
-
- $response = $this->client->sendRequest();
- $this->assertInstanceOf('Doctrine\\Search\\Http\\ResponseInterface', $response);
-
- //@todo this should be tested in the Response-Test
- /*$this->assertContains('<html>', $response->getContent());
- $this->assertTrue($response->isSuccessfull());*/
- }
-}
View
30 tests/Doctrine/Tests/Search/Http/Client/CurlClientTest.php
@@ -1,30 +0,0 @@
-<?php
-namespace Doctrine\Tests\Search\Http\Client;
-
-use Doctrine\Search\Http\Client\CurlClient;
-
-/**
- * @author Markus Bachmann <markus.bachmann@bachi.biz>
- */
-class CurlClientTest extends \PHPUnit_Framework_TestCase
-{
-
- public function testCallExistHostWithMethodGet()
- {
- $client = new CurlClient('http://www.google.de/');
- $response = $client->sendRequest();
- $this->assertTrue($response->isSuccessfull());
- $this->assertEquals('GET', $response->getHeader('method'));
- $this->assertEquals(200, $response->getStatusCode());
- }
-
- public function testCallNonExististingHostWithMethodGet()
- {
- try {
- $client = new CurlClient('http://www/');
- $response = $client->sendRequest();
- } catch (\Exception $e) {
- $this->assertRegExp('#Request to .+ failed (.+)#', $e->getMessage());
- }
- }
-}
Something went wrong with that request. Please try again.