Commit aa98efa4 authored by Bas de Nooijer's avatar Bas de Nooijer

- unittest improvements

parent 168e6a09
......@@ -63,7 +63,7 @@ class Solarium_Client_Adapter_ZendHttp extends Solarium_Client_Adapter
*
* Overrides any existing values.
*
* If the options array has an 'adapteroptions' entry it is forwarded to the
* If the options array has an 'options' entry it is forwarded to the
* Zend_Http_Client. See the Zend_Http_Clientdocs for the many config
* options available.
*
......@@ -81,14 +81,14 @@ class Solarium_Client_Adapter_ZendHttp extends Solarium_Client_Adapter
if (null !== $this->_zendHttp) {
// forward timeout setting
$this->_zendHttp->setConfig(
array('timeout' => $this->getOption('timeout'))
);
$adapterOptions = array('timeout' => $this->getTimeout());
// forward adapter options if available
if (isset($this->_options['adapteroptions'])) {
$this->_zendHttp->setConfig($this->_options['adapteroptions']);
if (isset($this->_options['options'])) {
$adapterOptions = array_merge($adapterOptions, $this->_options['options']);
}
$this->_zendHttp->setConfig($adapterOptions);
}
return $this;
......@@ -126,10 +126,12 @@ class Solarium_Client_Adapter_ZendHttp extends Solarium_Client_Adapter
{
if (null == $this->_zendHttp) {
$options = array('timeout' => $this->getOption('timeout'));
if (isset($this->_options['adapteroptions'])) {
// forward zendhttp options
if (isset($this->_options['options'])) {
$options = array_merge(
$options,
$this->_options['adapteroptions']
$this->_options['options']
);
}
......@@ -150,7 +152,7 @@ class Solarium_Client_Adapter_ZendHttp extends Solarium_Client_Adapter
$client = $this->getZendHttp();
$client->setMethod($request->getMethod());
$client->setUri($request->getUri());
$client->setUri($this->getBaseUri() . $request->getUri());
$client->setRawData($request->getRawData());
$response = $client->request();
......@@ -163,7 +165,7 @@ class Solarium_Client_Adapter_ZendHttp extends Solarium_Client_Adapter
);
}
if ($request->getMethod() == Solarium_Client_Request::HEAD) {
if ($request->getMethod() == Solarium_Client_Request::METHOD_HEAD) {
$data = '';
} else {
$data = $response->getBody();
......
......@@ -38,8 +38,6 @@
/**
* Class for describing a request
*
* @todo support config mode
*
* @package Solarium
* @subpackage Client
*/
......
......@@ -88,7 +88,6 @@ class Solarium_Client_RequestBuilder_Select extends Solarium_Client_RequestBuild
foreach ($query->getComponents() as $component) {
$componentBuilderClass = $types[$component->getType()]['requestbuilder'];
if (!empty($componentBuilderClass)) {
// todo add caching?
$componentBuilder = new $componentBuilderClass;
$request = $componentBuilder->build($component, $request);
}
......
......@@ -55,9 +55,6 @@ class Solarium_Client_ResponseParser_Select extends Solarium_Client_ResponsePars
$data = $result->getData();
$query = $result->getQuery();
// reset arrays
$this->_components = array();
// create document instances
$documentClass = $query->getOption('documentclass');
$documents = array();
......@@ -74,7 +71,6 @@ class Solarium_Client_ResponseParser_Select extends Solarium_Client_ResponsePars
foreach ($query->getComponents() as $component) {
$componentParserClass = $types[$component->getType()]['responseparser'];
if (!empty($componentParserClass)) {
// todo add caching?
$componentParser = new $componentParserClass;
$components[$component->getType()] = $componentParser->parse($query, $component, $data);
}
......
......@@ -38,8 +38,6 @@
/**
* QueryType result
*
* TODO intro
*
* @package Solarium
* @subpackage Result
*/
......
<?php
/**
* Copyright 2011 Bas de Nooijer. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this listof conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER 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 HOLDER 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.
*
* The views and conclusions contained in the software and documentation are
* those of the authors and should not be interpreted as representing official
* policies, either expressed or implied, of the copyright holder.
*/
class Solarium_Client_Adapter_ZendHttpTest extends PHPUnit_Framework_TestCase
{
/**
* @var Solarium_Client_Adapter_ZendHttp
*/
protected $_adapter;
public function setUp()
{
if (!class_exists('Zend_Loader_Autoloader') && (include_once 'Zend/Loader/Autoloader.php') !== 'OK') {
$this->markTestSkipped('ZF not in include_path, skipping ZendHttp adapter tests');
}
Zend_Loader_Autoloader::getInstance();
$this->_adapter = new Solarium_Client_Adapter_ZendHttp();
}
public function testForwardingToZendHttpInSetOptions()
{
$options = array('timeout' => 10, 'optionZ' => 123, 'options' => array('optionX' => 'Y'));
$adapterOptions = array('timeout' => 10, 'optionX' => 'Y');
$mock = $this->getMock('Zend_Http_Client');
$mock->expects($this->once())
->method('setConfig')
->with($this->equalTo($adapterOptions));
$this->_adapter->setZendHttp($mock);
$this->_adapter->setOptions($options);
}
public function testSetAndGetZendHttp()
{
$dummy = new StdClass();
$this->_adapter->setZendHttp($dummy);
$this->assertEquals(
$dummy,
$this->_adapter->getZendHttp()
);
}
public function testGetZendHttpAutoload()
{
$this->_adapter->setOptions(array('myoption', 123));
$zendHttp = $this->_adapter->getZendHttp();
$this->assertThat($zendHttp, $this->isInstanceOf('Zend_Http_Client'));
}
public function testExecute()
{
$method = Solarium_Client_Request::METHOD_GET;
$rawData = 'xyz';
$responseData = 'abc';
$handler = 'myhandler';
$request = new Solarium_Client_Request();
$request->setMethod($method);
$request->setHandler($handler);
$request->setRawData($rawData);
$response = new Zend_Http_Response(200, array('status' => 'HTTP 1.1 200 OK'), $responseData);
$mock = $this->getMock('Zend_Http_Client');
$mock->expects($this->once())
->method('setMethod')
->with($this->equalTo($method));
$mock->expects($this->once())
->method('setUri')
->with($this->equalTo('http://127.0.0.1:8983/solr/myhandler?'));
$mock->expects($this->once())
->method('setRawData')
->with($this->equalTo($rawData));
$mock->expects($this->once())
->method('request')
->will($this->returnValue($response));
$this->_adapter->setZendHttp($mock);
$adapterResponse = $this->_adapter->execute($request);
$this->assertEquals(
$responseData,
$adapterResponse->getBody()
);
}
public function testExecuteErrorResponse()
{
$request = new Solarium_Client_Request();
$response = new Zend_Http_Response(404, array(), '');
$mock = $this->getMock('Zend_Http_Client');
$mock->expects($this->once())
->method('request')
->will($this->returnValue($response));
$this->_adapter->setZendHttp($mock);
$this->setExpectedException('Solarium_Client_HttpException');
$this->_adapter->execute($request);
}
public function testExecuteHeadRequestReturnsNoData()
{
$request = new Solarium_Client_Request();
$request->setMethod(Solarium_Client_Request::METHOD_HEAD);
$response = new Zend_Http_Response(200, array('status' => 'HTTP 1.1 200 OK'), 'data');
$mock = $this->getMock('Zend_Http_Client');
$mock->expects($this->once())
->method('request')
->will($this->returnValue($response));
$this->_adapter->setZendHttp($mock);
$response = $this->_adapter->execute($request);
$this->assertEquals(
'',
$response->getBody()
);
}
}
\ No newline at end of file
......@@ -486,6 +486,114 @@ class Solarium_ClientTest extends PHPUnit_Framework_TestCase
$observer->update($query);
}
public function testCreateQuery()
{
$options = array('optionA' => 1, 'optionB' => 2);
$query = $this->_client->createQuery(Solarium_Client::QUERYTYPE_SELECT, $options);
// check class mapping
$this->assertThat($query, $this->isInstanceOf('Solarium_Query_Select'));
// check option forwarding
$queryOptions = $query->getOptions();
$this->assertEquals(
$options['optionB'],
$queryOptions['optionB']
);
}
public function testCreateQueryWithInvalidQueryType()
{
$this->setExpectedException('Solarium_Exception');
$this->_client->createQuery('invalidtype');
}
public function testCreateQueryPrePlugin()
{
$type = Solarium_Client::QUERYTYPE_SELECT;
$options = array('optionA' => 1, 'optionB' => 2);
$observer = $this->getMock('Solarium_Plugin_Abstract', array(), array($this->_client,array()));
$observer->expects($this->once())
->method('preCreateQuery')
->with($this->equalTo($type), $this->equalTo($options));
$this->_client->registerPlugin('testplugin', $observer);
$this->_client->createQuery($type, $options);
}
public function testCreateQueryWithOverridingPlugin()
{
$type = Solarium_Client::QUERYTYPE_SELECT;
$options = array('optionA' => 1, 'optionB' => 2);
$dummyvalue = 'test123';
$observer = $this->getMock('Solarium_Plugin_Abstract', array(), array($this->_client,array()));
$observer->expects($this->once())
->method('preCreateQuery')
->with($this->equalTo($type), $this->equalTo($options))
->will($this->returnValue($dummyvalue));
$this->_client->registerPlugin('testplugin', $observer);
$query = $this->_client->createQuery($type, $options);
$this->assertEquals(
$dummyvalue,
$query
);
}
public function testCreateQueryPostPlugin()
{
$type = Solarium_Client::QUERYTYPE_SELECT;
$options = array('optionA' => 1, 'optionB' => 2);
$query = $this->_client->createQuery($type, $options);
$observer = $this->getMock('Solarium_Plugin_Abstract', array(), array($this->_client,array()));
$observer->expects($this->once())
->method('postCreateQuery')
->with($this->equalTo($type), $this->equalTo($options), $this->equalTo($query));
$this->_client->registerPlugin('testplugin', $observer);
$this->_client->createQuery($type, $options);
}
public function testCreateSelect()
{
$options = array('optionA' => 1, 'optionB' => 2);
$observer = $this->getMock('Solarium_Client', array('createQuery'));
$observer->expects($this->once())
->method('createQuery')
->with($this->equalTo(Solarium_Client::QUERYTYPE_SELECT), $this->equalTo($options));
$observer->createSelect($options);
}
public function testCreateUpdate()
{
$options = array('optionA' => 1, 'optionB' => 2);
$observer = $this->getMock('Solarium_Client', array('createQuery'));
$observer->expects($this->once())
->method('createQuery')
->with($this->equalTo(Solarium_Client::QUERYTYPE_UPDATE), $this->equalTo($options));
$observer->createUpdate($options);
}
public function testCreatePing()
{
$options = array('optionA' => 1, 'optionB' => 2);
$observer = $this->getMock('Solarium_Client', array('createQuery'));
$observer->expects($this->once())
->method('createQuery')
->with($this->equalTo(Solarium_Client::QUERYTYPE_PING), $this->equalTo($options));
$observer->createPing($options);
}
}
class MyAdapter extends Solarium_Client_Adapter_Http{
......
......@@ -55,7 +55,7 @@ class Solarium_Plugin_AbstractTest extends PHPUnit_Framework_TestCase
}
public function testEventHooks()
public function testEventHooksEmpty()
{
$this->assertEquals(null, $this->_plugin->preCreateRequest(null));
$this->assertEquals(null, $this->_plugin->postCreateRequest(null,null));
......@@ -65,6 +65,8 @@ class Solarium_Plugin_AbstractTest extends PHPUnit_Framework_TestCase
$this->assertEquals(null, $this->_plugin->postExecute(null,null));
$this->assertEquals(null, $this->_plugin->preCreateResult(null,null));
$this->assertEquals(null, $this->_plugin->postCreateResult(null,null,null));
$this->assertEquals(null, $this->_plugin->preCreateQuery(null,null));
$this->assertEquals(null, $this->_plugin->postCreateQuery(null,null,null));
}
}
......
......@@ -230,4 +230,78 @@ class Solarium_Query_Select_Component_FacetSetTest extends PHPUnit_Framework_Tes
);
}
public function testCreateFacet()
{
$type = Solarium_Query_Select_Component_FacetSet::FACET_FIELD;
$options = array('optionA' => 1, 'optionB' => 2);
$facet = $this->_facetSet->createFacet($type, $options);
// check class mapping
$this->assertEquals(
$type,
$facet->getType()
);
// check option forwarding
$facetOptions = $facet->getOptions();
$this->assertEquals(
$options['optionB'],
$facetOptions['optionB']
);
}
public function testCreateFacetWithInvalidType()
{
$this->setExpectedException('Solarium_Exception');
$this->_facetSet->createFacet('invalidtype');
}
public function testCreateFacetField()
{
$options = array('optionA' => 1, 'optionB' => 2);
$observer = $this->getMock('Solarium_Query_Select_Component_FacetSet', array('createFacet'));
$observer->expects($this->once())
->method('createFacet')
->with($this->equalTo(Solarium_Query_Select_Component_FacetSet::FACET_FIELD), $this->equalTo($options));
$observer->createFacetField($options);
}
public function testCreateFacetQuery()
{
$options = array('optionA' => 1, 'optionB' => 2);
$observer = $this->getMock('Solarium_Query_Select_Component_FacetSet', array('createFacet'));
$observer->expects($this->once())
->method('createFacet')
->with($this->equalTo(Solarium_Query_Select_Component_FacetSet::FACET_QUERY), $this->equalTo($options));
$observer->createFacetQuery($options);
}
public function testCreateFacetMultiQuery()
{
$options = array('optionA' => 1, 'optionB' => 2);
$observer = $this->getMock('Solarium_Query_Select_Component_FacetSet', array('createFacet'));
$observer->expects($this->once())
->method('createFacet')
->with($this->equalTo(Solarium_Query_Select_Component_FacetSet::FACET_MULTIQUERY), $this->equalTo($options));
$observer->createFacetMultiQuery($options);
}
public function testCreateFacetRange()
{
$options = array('optionA' => 1, 'optionB' => 2);
$observer = $this->getMock('Solarium_Query_Select_Component_FacetSet', array('createFacet'));
$observer->expects($this->once())
->method('createFacet')
->with($this->equalTo(Solarium_Query_Select_Component_FacetSet::FACET_RANGE), $this->equalTo($options));
$observer->createFacetRange($options);
}
}
......@@ -520,4 +520,20 @@ class Solarium_Query_SelectTest extends PHPUnit_Framework_TestCase
$this->_query->getComponentTypes()
);
}
public function testCreateFilterQuery()
{
$options = array('optionA' => 1, 'optionB' => 2);
$fq = $this->_query->createFilterQuery($options);
// check class
$this->assertThat($fq, $this->isInstanceOf('Solarium_Query_Select_FilterQuery'));
// check option forwarding
$fqOptions = $fq->getOptions();
$this->assertEquals(
$options['optionB'],
$fqOptions['optionB']
);
}
}
\ No newline at end of file
......@@ -365,4 +365,30 @@ class Solarium_Query_UpdateTest extends PHPUnit_Framework_TestCase
new Solarium_Query_Update($config);
}
public function testCreateCommand()
{
$type = Solarium_Query_Update::COMMAND_ROLLBACK;
$options = array('optionA' => 1, 'optionB' => 2);
$command = $this->_query->createCommand($type, $options);
// check command type
$this->assertEquals(
$type,
$command->getType()
);
// check option forwarding
$commandOptions = $command->getOptions();
$this->assertEquals(
$options['optionB'],
$commandOptions['optionB']
);
}
public function testCreateCommandWithInvalidQueryType()
{
$this->setExpectedException('Solarium_Exception');
$this->_query->createCommand('invalidtype');
}
}
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