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 @@ ...@@ -16,7 +16,7 @@
you could also write the switches here) you could also write the switches here)
--> -->
<target name="phpunit"> <target name="phpunit">
<exec executable="phpunit" failonerror="true" /> <exec executable="phpunit" />
</target> </target>
<!-- Run pdepend, phpmd, phpcpd, and phpcs in parallel --> <!-- Run pdepend, phpmd, phpcpd, and phpcs in parallel -->
...@@ -33,7 +33,10 @@ you could also write the switches here) ...@@ -33,7 +33,10 @@ you could also write the switches here)
<!-- Generate jdepend.xml and software metrics charts --> <!-- Generate jdepend.xml and software metrics charts -->
<target name="pdepend"> <target name="pdepend">
<exec executable="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> </exec>
</target> </target>
......
...@@ -55,7 +55,7 @@ class Solarium_Client_Adapter_PeclHttp extends Solarium_Client_Adapter ...@@ -55,7 +55,7 @@ class Solarium_Client_Adapter_PeclHttp extends Solarium_Client_Adapter
protected function _init() protected function _init()
{ {
// @codeCoverageIgnoreStart // @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'); 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 ...@@ -71,93 +71,100 @@ class Solarium_Client_Adapter_PeclHttp extends Solarium_Client_Adapter
*/ */
public function execute($request) public function execute($request)
{ {
list($data, $headers) = $this->_getData($request); $httpRequest = $this->toHttpRequest($request);
$this->check($data, $headers);
return new Solarium_Client_Response($data, $headers); 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 * @return array
*/ */
protected function _getData($request) protected function _toRawHeaders($message)
{ {
// @codeCoverageIgnoreStart $headers[] = 'HTTP/' . $message->getHttpVersion()
$uri = $this->getBaseUri() . $request->getUri(); . ' ' . $message->getResponseCode()
$method = $request->getMethod(); . ' ' . $message->getResponseStatus();
$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 = array(); foreach ($message->getHeaders() as $header => $value) {
$data = ''; $headers[] = "$header: $value";
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);
}
} }
return array($data, $headers); return $headers;
// @codeCoverageIgnoreEnd
} }
/** /**
* 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 * @param Solarium_Client_Request $request
* @return array * @param HttpRequest
*/ */
protected function _createOptions($request) public function toHttpRequest($request)
{ {
// @codeCoverageIgnoreStart $url = $this->getBaseUri() . $request->getUri();
$options = array( $httpRequest = new HttpRequest($url);
'timeout' => $this->getTimeout()
); $headers = array();
foreach ($request->getHeaders() as $headerLine) { foreach ($request->getHeaders() as $headerLine) {
list($header, $value) = explode(':', $headerLine); list($header, $value) = explode(':', $headerLine);
if ($header = trim($header)) { if ($header = trim($header)) {
$options['headers'][$header] = trim($value); $headers[$header] = trim($value);
} }
} }
return $options;
// @codeCoverageIgnoreEnd
}
/** switch($request->getMethod()) {
* Check result of a request case Solarium_Client_Request::METHOD_GET:
* $method = HTTP_METH_GET;
* @throws Solarium_Client_HttpException break;
* @param string $data case Solarium_Client_Request::METHOD_POST:
* @param array $headers $method = HTTP_METH_POST;
* @return void $httpRequest->setBody($request->getRawData());
*/ if (!isset($headers['Content-Type'])) {
public function check($data, $headers) $headers['Content-Type'] = 'text/xml; charset=utf-8';
{ }
// if there is no data and there are no headers it's a total failure, break;
// a connection to the host was impossible. case Solarium_Client_Request::METHOD_HEAD:
if (empty($data) && count($headers) == 0) { $method = HTTP_METH_HEAD;
throw new Solarium_Client_HttpException("HTTP request failed"); 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 ...@@ -58,7 +58,7 @@ class Solarium_Client_ResponseParser_MoreLikeThis extends Solarium_Client_Respon
$query = $result->getQuery(); $query = $result->getQuery();
$parseResult = parent::parse($result); $parseResult = parent::parse($result);
if (isset($data['interestingTerms']) && 'none' != $query->getInterestingTerms()) { if (isset($data['interestingTerms']) and 'none' != $query->getInterestingTerms()) {
$terms = $data['interestingTerms']; $terms = $data['interestingTerms'];
if ('details' == $query->getInterestingTerms()) { if ('details' == $query->getInterestingTerms()) {
$tempTerms = array(); $tempTerms = array();
...@@ -72,7 +72,7 @@ class Solarium_Client_ResponseParser_MoreLikeThis extends Solarium_Client_Respon ...@@ -72,7 +72,7 @@ class Solarium_Client_ResponseParser_MoreLikeThis extends Solarium_Client_Respon
if (isset($data['match']['docs'][0]) && true == $query->getMatchInclude()) { if (isset($data['match']['docs'][0]) && true == $query->getMatchInclude()) {
$matchData = $data['match']['docs'][0]; $matchData = $data['match']['docs'][0];
$documentClass = $query->getOption('documentclass'); $documentClass = $query->getOption('documentclass');
$fields = (array)$matchData; $fields = (array)$matchData;
$parseResult['match'] = new $documentClass($fields); $parseResult['match'] = new $documentClass($fields);
......
...@@ -42,42 +42,127 @@ class Solarium_Client_Adapter_PeclHttpTest extends PHPUnit_Framework_TestCase ...@@ -42,42 +42,127 @@ class Solarium_Client_Adapter_PeclHttpTest extends PHPUnit_Framework_TestCase
$this->markTestSkipped('Pecl_http not available, skipping PeclHttp adapter tests'); $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'; $methods = array(
$headers = array('X-dummy: data'); 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 foreach ($methods as $method => $options) {
$this->_adapter->check($data, $headers); $request = new Solarium_Client_Request;
$request->setMethod($method);
$data[] = array_merge(array($request), $options);
}
$data = ''; return $data;
$headers = array(); }
public function testToHttpRequestWithHeaders()
{
$request = new Solarium_Client_Request(array(
'header' => array(
'Content-Type: application/json',
'User-Agent: Foo'
)
));
$this->setExpectedException('Solarium_Exception'); $httpRequest = $this->_adapter->toHttpRequest($request);
$this->_adapter->check($data, $headers); $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() public function testExecute()
{ {
$headers = array('HTTP/1.0 200 OK'); $statusCode = 200;
$statusMessage = 'OK';
$body = 'data'; $body = 'data';
$data = array($body, $headers); $data = <<<EOF
HTTP/1.1 $statusCode $statusMessage
$body
EOF;
$message = new HttpMessage($data);
$request = new Solarium_Client_Request(); $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()) $mock->expects($this->once())
->method('_getData') ->method('toHttpRequest')
->with($request) ->with($request)
->will($this->returnValue($data)); ->will($this->returnValue($mockHttpRequest));
$response = $mock->execute($request); $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 ...@@ -63,7 +63,7 @@ class Solarium_Client_RequestBuilder_AnalysisTest extends PHPUnit_Framework_Test
array( array(
'wt' => 'json', 'wt' => 'json',
'analysis.query' => $query, 'analysis.query' => $query,
'analysis.showmatch' => $showMatch, 'analysis.showmatch' => 'true',
), ),
$request->getParams() $request->getParams()
); );
...@@ -72,4 +72,4 @@ class Solarium_Client_RequestBuilder_AnalysisTest extends PHPUnit_Framework_Test ...@@ -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 ...@@ -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 ...@@ -53,7 +53,7 @@ class Solarium_Client_RequestBuilder_Select_Component_MoreLikeThisTest extends P
$this->assertEquals( $this->assertEquals(
array( array(
'mlt' => true, 'mlt' => 'true',
'mlt.fl' => 'description,name', 'mlt.fl' => 'description,name',
'mlt.mintf' => 1, 'mlt.mintf' => 1,
'mlt.mindf' => 3, 'mlt.mindf' => 3,
...@@ -61,7 +61,7 @@ class Solarium_Client_RequestBuilder_Select_Component_MoreLikeThisTest extends P ...@@ -61,7 +61,7 @@ class Solarium_Client_RequestBuilder_Select_Component_MoreLikeThisTest extends P
'mlt.maxwl' => 15, 'mlt.maxwl' => 15,
'mlt.maxqt' => 4, 'mlt.maxqt' => 4,
'mlt.maxntp' => 5, 'mlt.maxntp' => 5,
'mlt.boost' => true, 'mlt.boost' => 'true',
'mlt.qf' => 'description', 'mlt.qf' => 'description',
'mlt.count' => 6, 'mlt.count' => 6,
), ),
...@@ -70,4 +70,4 @@ class Solarium_Client_RequestBuilder_Select_Component_MoreLikeThisTest extends P ...@@ -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_ ...@@ -46,7 +46,7 @@ class Solarium_Client_RequestBuilder_Select_Component_StatsTest extends PHPUnit_
$this->assertEquals( $this->assertEquals(
array( array(
'stats' => true, 'stats' => 'true',
'stats.facet' => array( 'stats.facet' => array(
'facetA', 'facetA',
'facetB', 'facetB',
...@@ -62,4 +62,4 @@ class Solarium_Client_RequestBuilder_Select_Component_StatsTest extends PHPUnit_ ...@@ -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 ...@@ -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