Commit 2c7bd3d9 authored by Bas de Nooijer's avatar Bas de Nooijer

Merge branch 'develop' of github.com:basdenooijer/solarium into develop

parents 9b01f91c 508e73e1
......@@ -16,7 +16,7 @@
you could also write the switches here)
-->
<target name="phpunit">
<exec executable="phpunit" failonerror="true" />
<exec executable="phpunit" />
</target>
<!-- Run pdepend, phpmd, phpcpd, and phpcs in parallel -->
......@@ -33,7 +33,10 @@ you could also write the switches here)
<!-- Generate jdepend.xml and software metrics charts -->
<target name="pdepend">
<exec executable="pdepend">
<arg line="--jdepend-xml=${basedir}/build/logs/jdepend.xml library" />
<arg line="--overview-pyramid=${basedir}/build/pdepend/overview-pyramid.svg" />
<arg line="--jdepend-chart=${basedir}/build/pdepend/dependencies.svg" />
<arg line="--jdepend-xml=${basedir}/build/logs/jdepend.xml" />
<arg line="library" />
</exec>
</target>
......
......@@ -55,7 +55,7 @@ class Solarium_Client_Adapter_PeclHttp extends Solarium_Client_Adapter
protected function _init()
{
// @codeCoverageIgnoreStart
if (!function_exists('http_get')) {
if (!class_exists('HttpRequest', false)) {
throw new Solarium_Exception('Pecl_http is not available, install it to use the PeclHttp adapter');
}
......@@ -71,93 +71,100 @@ class Solarium_Client_Adapter_PeclHttp extends Solarium_Client_Adapter
*/
public function execute($request)
{
list($data, $headers) = $this->_getData($request);
$this->check($data, $headers);
return new Solarium_Client_Response($data, $headers);
$httpRequest = $this->toHttpRequest($request);
try {
$httpMessage = $httpRequest->send();
} catch (Exception $e) {
throw new Solarium_Client_HttpException($e->getMessage());
}
if ($error = $httpRequest->getResponseInfo('error')) {
throw new Solarium_Client_HttpException($error);
}
return new Solarium_Client_Response(
$httpMessage->getBody(),
$this->_toRawHeaders($httpMessage)
);
}
/**
* Execute request
* Convert key/value pair header to raw header.
*
* @param Solarium_Client_Request $request
* <code>
* //before
* $headers['Content-Type'] = 'text/plain';
*
* ...
*
* //after
* $headers[0] = 'Content-Type: text/plain';
* </code>
*
* @param $message HttpMessage
* @return array
*/
protected function _getData($request)
protected function _toRawHeaders($message)
{
// @codeCoverageIgnoreStart
$uri = $this->getBaseUri() . $request->getUri();
$method = $request->getMethod();
$options = $this->_createOptions($request);
if ($method == Solarium_Client_Request::METHOD_POST) {
if (!isset($options['headers']['Content-Type'])) {
$options['headers']['Content-Type'] = 'text/xml; charset=utf-8';
}
$httpResponse = http_post_data(
$uri, $request->getRawData(), $options
);
} else if ($method == Solarium_Client_Request::METHOD_GET) {
$httpResponse = http_get($uri, $options);
} else if ($method == Solarium_Client_Request::METHOD_HEAD) {
$httpResponse = http_head($uri, $options);
} else {
throw new Solarium_Exception("unsupported method: $method");
}
$headers[] = 'HTTP/' . $message->getHttpVersion()
. ' ' . $message->getResponseCode()
. ' ' . $message->getResponseStatus();
$headers = array();
$data = '';
if ($message = http_parse_message($httpResponse)) {
$data = $message->body;
if ($firstPositionOfCRLF = strpos($httpResponse, "\r\n\r\n")) {
$headersAsString = substr(
$httpResponse, 0, $firstPositionOfCRLF
);
$headers = explode("\n", $headersAsString);
}
foreach ($message->getHeaders() as $header => $value) {
$headers[] = "$header: $value";
}
return array($data, $headers);
// @codeCoverageIgnoreEnd
return $headers;
}
/**
* Create http request options from request.
*
* @link http://php.net/manual/en/http.request.options.php
* adapt Solarium_Client_Request to HttpRequest
*
* {@link http://us.php.net/manual/en/http.constants.php
* HTTP Predefined Constant}
*
* @param Solarium_Client_Request $request
* @return array
* @param HttpRequest
*/
protected function _createOptions($request)
public function toHttpRequest($request)
{
// @codeCoverageIgnoreStart
$options = array(
'timeout' => $this->getTimeout()
);
$url = $this->getBaseUri() . $request->getUri();
$httpRequest = new HttpRequest($url);
$headers = array();
foreach ($request->getHeaders() as $headerLine) {
list($header, $value) = explode(':', $headerLine);
if ($header = trim($header)) {
$options['headers'][$header] = trim($value);
$headers[$header] = trim($value);
}
}
return $options;
// @codeCoverageIgnoreEnd
}
/**
* Check result of a request
*
* @throws Solarium_Client_HttpException
* @param string $data
* @param array $headers
* @return void
*/
public function check($data, $headers)
{
// if there is no data and there are no headers it's a total failure,
// a connection to the host was impossible.
if (empty($data) && count($headers) == 0) {
throw new Solarium_Client_HttpException("HTTP request failed");
switch($request->getMethod()) {
case Solarium_Client_Request::METHOD_GET:
$method = HTTP_METH_GET;
break;
case Solarium_Client_Request::METHOD_POST:
$method = HTTP_METH_POST;
$httpRequest->setBody($request->getRawData());
if (!isset($headers['Content-Type'])) {
$headers['Content-Type'] = 'text/xml; charset=utf-8';
}
break;
case Solarium_Client_Request::METHOD_HEAD:
$method = HTTP_METH_HEAD;
break;
default:
throw new Solarium_Exception(
'Unsupported method: ' . $request->getMethod()
);
}
$httpRequest->setMethod($method);
$httpRequest->setOptions(array('timeout' => $this->getTimeout()));
$httpRequest->setHeaders($headers);
return $httpRequest;
}
}
......@@ -58,7 +58,7 @@ class Solarium_Client_ResponseParser_MoreLikeThis extends Solarium_Client_Respon
$query = $result->getQuery();
$parseResult = parent::parse($result);
if (isset($data['interestingTerms']) && 'none' != $query->getInterestingTerms()) {
if (isset($data['interestingTerms']) and 'none' != $query->getInterestingTerms()) {
$terms = $data['interestingTerms'];
if ('details' == $query->getInterestingTerms()) {
$tempTerms = array();
......@@ -72,7 +72,7 @@ class Solarium_Client_ResponseParser_MoreLikeThis extends Solarium_Client_Respon
if (isset($data['match']['docs'][0]) && true == $query->getMatchInclude()) {
$matchData = $data['match']['docs'][0];
$documentClass = $query->getOption('documentclass');
$fields = (array)$matchData;
$parseResult['match'] = new $documentClass($fields);
......
......@@ -42,42 +42,127 @@ class Solarium_Client_Adapter_PeclHttpTest extends PHPUnit_Framework_TestCase
$this->markTestSkipped('Pecl_http not available, skipping PeclHttp adapter tests');
}
$this->_adapter = new Solarium_Client_Adapter_PeclHttp();
$this->_adapter = new Solarium_Client_Adapter_PeclHttp(array('timeout' => 10));
}
public function testCheck()
/**
* @dataProvider requestProvider
*/
public function testToHttpRequestWithMethod($request, $method, $support)
{
try {
$httpRequest = $this->_adapter->toHttpRequest($request);
$this->assertEquals($httpRequest->getMethod(), $method);
} catch (Solarium_Exception $e) {
if ($support) {
$this->fail("Unsupport method: {$request->getMethod()}");
}
}
}
public function requestProvider()
{
$data = 'data';
$headers = array('X-dummy: data');
$methods = array(
Solarium_Client_Request::METHOD_GET => array(
'method' => HTTP_METH_GET,
'support' => true
),
Solarium_Client_Request::METHOD_POST => array(
'method' => HTTP_METH_POST,
'support' => true
),
Solarium_Client_Request::METHOD_HEAD => array(
'method' => HTTP_METH_HEAD,
'support' => true
),
'PUT' => array(
'method' => HTTP_METH_PUT,
'support' => false
),
'DELETE' => array(
'method' => HTTP_METH_DELETE,
'support' => false
),
);
// this should be ok, no exception
$this->_adapter->check($data, $headers);
foreach ($methods as $method => $options) {
$request = new Solarium_Client_Request;
$request->setMethod($method);
$data[] = array_merge(array($request), $options);
}
$data = '';
$headers = array();
return $data;
}
public function testToHttpRequestWithHeaders()
{
$request = new Solarium_Client_Request(array(
'header' => array(
'Content-Type: application/json',
'User-Agent: Foo'
)
));
$this->setExpectedException('Solarium_Exception');
$this->_adapter->check($data, $headers);
$httpRequest = $this->_adapter->toHttpRequest($request);
$this->assertEquals(array(
'timeout' => 10,
'headers' => array(
'Content-Type' => 'application/json',
'User-Agent' => 'Foo'
)
), $httpRequest->getOptions());
}
public function testToHttpRequestWithDefaultContentType()
{
$request = new Solarium_Client_Request;
$request->setMethod(Solarium_Client_Request::METHOD_POST);
$httpRequest = $this->_adapter->toHttpRequest($request);
$this->assertEquals(array(
'timeout' => 10,
'headers' => array(
'Content-Type' => 'text/xml; charset=utf-8',
)
), $httpRequest->getOptions());
}
public function testExecute()
{
$headers = array('HTTP/1.0 200 OK');
$statusCode = 200;
$statusMessage = 'OK';
$body = 'data';
$data = array($body, $headers);
$data = <<<EOF
HTTP/1.1 $statusCode $statusMessage
$body
EOF;
$message = new HttpMessage($data);
$request = new Solarium_Client_Request();
$mock = $this->getMock('Solarium_Client_Adapter_PeclHttp', array('_getData'));
$mockHttpRequest = $this->getMock('HttpRequest');
$mockHttpRequest->expects($this->once())
->method('send')
->will($this->returnValue(new HttpMessage($data)));
$mock = $this->getMock('Solarium_Client_Adapter_PeclHttp', array('toHttpRequest'));
$mock->expects($this->once())
->method('_getData')
->with($request)
->will($this->returnValue($data));
->method('toHttpRequest')
->with($request)
->will($this->returnValue($mockHttpRequest));
$response = $mock->execute($request);
$this->assertEquals($body, $response->getBody());
$this->assertEquals($statusCode, $response->getStatusCode());
$this->assertEquals($statusMessage, $response->getStatusMessage());
}
$this->assertEquals($body,$response->getBody());
$this->assertEquals($headers,$response->getHeaders());
/**
* @expectedException Solarium_Client_HttpException
*/
public function testExecuteWithException()
{
$request = new Solarium_Client_Request();
$this->_adapter->execute($request);
}
}
\ No newline at end of file
}
......@@ -63,7 +63,7 @@ class Solarium_Client_RequestBuilder_AnalysisTest extends PHPUnit_Framework_Test
array(
'wt' => 'json',
'analysis.query' => $query,
'analysis.showmatch' => $showMatch,
'analysis.showmatch' => 'true',
),
$request->getParams()
);
......@@ -72,4 +72,4 @@ class Solarium_Client_RequestBuilder_AnalysisTest extends PHPUnit_Framework_Test
}
}
\ No newline at end of file
}
......@@ -48,4 +48,4 @@ class Solarium_Client_RequestBuilder_PingTest extends PHPUnit_Framework_TestCase
);
}
}
\ No newline at end of file
}
......@@ -53,7 +53,7 @@ class Solarium_Client_RequestBuilder_Select_Component_MoreLikeThisTest extends P
$this->assertEquals(
array(
'mlt' => true,
'mlt' => 'true',
'mlt.fl' => 'description,name',
'mlt.mintf' => 1,
'mlt.mindf' => 3,
......@@ -61,7 +61,7 @@ class Solarium_Client_RequestBuilder_Select_Component_MoreLikeThisTest extends P
'mlt.maxwl' => 15,
'mlt.maxqt' => 4,
'mlt.maxntp' => 5,
'mlt.boost' => true,
'mlt.boost' => 'true',
'mlt.qf' => 'description',
'mlt.count' => 6,
),
......@@ -70,4 +70,4 @@ class Solarium_Client_RequestBuilder_Select_Component_MoreLikeThisTest extends P
}
}
\ No newline at end of file
}
......@@ -46,7 +46,7 @@ class Solarium_Client_RequestBuilder_Select_Component_StatsTest extends PHPUnit_
$this->assertEquals(
array(
'stats' => true,
'stats' => 'true',
'stats.facet' => array(
'facetA',
'facetB',
......@@ -62,4 +62,4 @@ class Solarium_Client_RequestBuilder_Select_Component_StatsTest extends PHPUnit_
}
}
\ No newline at end of file
}
......@@ -45,4 +45,4 @@ class Solarium_Result_PingTest extends PHPUnit_Framework_TestCase
);
}
}
\ No newline at end of file
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment