Commit 151b7dc3 authored by Bas de Nooijer's avatar Bas de Nooijer

- unittest improvements

parent 1035216c
...@@ -58,9 +58,9 @@ class Solarium_Client extends Solarium_Configurable ...@@ -58,9 +58,9 @@ class Solarium_Client extends Solarium_Configurable
/** /**
* Querytype definitions * Querytype definitions
*/ */
const QUERYTYPE_SELECT = 'Select'; const QUERYTYPE_SELECT = 'select';
const QUERYTYPE_UPDATE = 'Update'; const QUERYTYPE_UPDATE = 'update';
const QUERYTYPE_PING = 'Ping'; const QUERYTYPE_PING = 'ping';
/** /**
* Default options * Default options
......
...@@ -307,8 +307,8 @@ class Solarium_Client_Request extends Solarium_Configurable ...@@ -307,8 +307,8 @@ class Solarium_Client_Request extends Solarium_Configurable
*/ */
public function addHeaders($headers) public function addHeaders($headers)
{ {
foreach ($headers as $key => $value) { foreach ($headers as $header) {
$this->addHeader($key, $value); $this->addHeader($header);
} }
return $this; return $this;
......
...@@ -54,7 +54,7 @@ class Solarium_Client_RequestBuilder_Select_Component_Highlighting ...@@ -54,7 +54,7 @@ class Solarium_Client_RequestBuilder_Select_Component_Highlighting
public function build($component, $request) public function build($component, $request)
{ {
// enable highlighting // enable highlighting
$request->addParam('hl', 'true'); $request->addParam('hl', true);
$request->addParam('hl.fl', $component->getFields()); $request->addParam('hl.fl', $component->getFields());
$request->addParam('hl.snippets', $component->getSnippets()); $request->addParam('hl.snippets', $component->getSnippets());
......
...@@ -54,7 +54,7 @@ class Solarium_Client_RequestBuilder_Select_Component_MoreLikeThis ...@@ -54,7 +54,7 @@ class Solarium_Client_RequestBuilder_Select_Component_MoreLikeThis
public function build($component, $request) public function build($component, $request)
{ {
// enable morelikethis // enable morelikethis
$request->addParam('mlt', 'true'); $request->addParam('mlt', true);
$request->addParam('mlt.fl', $component->getFields()); $request->addParam('mlt.fl', $component->getFields());
$request->addParam('mlt.mintf', $component->getMinimumTermFrequency()); $request->addParam('mlt.mintf', $component->getMinimumTermFrequency());
......
...@@ -72,6 +72,26 @@ class Solarium_Client_AdapterTest extends PHPUnit_Framework_TestCase ...@@ -72,6 +72,26 @@ class Solarium_Client_AdapterTest extends PHPUnit_Framework_TestCase
$this->assertEquals('core1', $this->_adapter->getCore()); $this->assertEquals('core1', $this->_adapter->getCore());
} }
public function testSetAndGetTimeout()
{
$this->_adapter->setTimeout(7);
$this->assertEquals(7, $this->_adapter->getTimeout());
}
public function testGetBaseUri()
{
$this->_adapter->setHost('myserver')->setPath('/mypath')->setPort(123);
$this->assertEquals('http://myserver:123/mypath/', $this->_adapter->getBaseUri());
}
public function testGetBaseUriWithCore()
{
$this->_adapter->setHost('myserver')->setPath('/mypath')->setPort(123)->setCore('mycore');
$this->assertEquals('http://myserver:123/mypath/mycore/', $this->_adapter->getBaseUri());
}
} }
class TestAdapter extends Solarium_Client_Adapter class TestAdapter extends Solarium_Client_Adapter
......
<?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_RequestBuilder_Select_Component_HighlightingTest extends PHPUnit_Framework_TestCase
{
public function testBuild()
{
$builder = new Solarium_Client_RequestBuilder_Select_Component_Highlighting;
$request = new Solarium_Client_Request();
$component = new Solarium_Query_Select_Component_Highlighting();
$component->setFields('fieldA,fieldB');
$component->setSnippets(2);
$component->setFragSize(3);
$component->setMergeContiguous(true);
$component->setRequireFieldMatch(false);
$component->setMaxAnalyzedChars(4);
$component->setAlternateField('fieldC');
$component->setMaxAlternateFieldLength(5);
$component->setFormatter('simple');
$component->setSimplePrefix('<b>');
$component->setSimplePostfix('</b>');
$component->setFragmenter('myFragmenter');
$component->setFragListBuilder('myFragListBuilder');
$component->setFragmentsBuilder('myFragmentsBuilder');
$component->setUsePhraseHighlighter(true);
$component->setUseFastVectorHighlighter(false);
$component->setHighlightMultiTerm(true);
$component->setRegexSlop(1.3);
$component->setRegexPattern('mypattern');
$component->setMaxAnalyzedChars(100);
$request = $builder->build($component, $request);
$this->assertEquals(
array(
'hl' => true,
'hl.fl' => 'fieldA,fieldB',
'hl.snippets' => 2,
'hl.fragsize' => 3,
'hl.mergeContiguous' => true,
'hl.requireFieldMatch' => false,
'hl.maxAnalyzedChars' => 100,
'hl.alternateField' => 'fieldC',
'hl.maxAlternateFieldLength' => 5,
'hl.formatter' => 'simple',
'hl.simple.pre' => '<b>',
'hl.simple.post' => '</b>',
'hl.fragmenter' => 'myFragmenter',
'hl.fragListBuilder' => 'myFragListBuilder',
'hl.fragmentsBuilder' => 'myFragmentsBuilder',
'hl.useFastVectorHighlighter' => false,
'hl.usePhraseHighlighter' => true,
'hl.highlightMultiTerm' => true,
'hl.regex.slop' => 1.3,
'hl.regex.pattern' => 'mypattern',
),
$request->getParams()
);
}
}
<?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_RequestBuilder_Select_Component_MoreLikeThisTest extends PHPUnit_Framework_TestCase
{
public function testBuild()
{
$builder = new Solarium_Client_RequestBuilder_Select_Component_MoreLikeThis;
$request = new Solarium_Client_Request();
$component = new Solarium_Query_Select_Component_MoreLikeThis();
$component->setFields('description,name');
$component->setMinimumTermFrequency(1);
$component->setMinimumDocumentFrequency(3);
$component->setMinimumWordLength(2);
$component->setMaximumWordLength(15);
$component->setMaximumQueryTerms(4);
$component->setMaximumNumberOfTokens(5);
$component->setBoost(true);
$component->setQueryFields('description');
$component->setCount(6);
$request = $builder->build($component, $request);
$this->assertEquals(
array(
'mlt' => true,
'mlt.fl' => 'description,name',
'mlt.mintf' => 1,
'mlt.mindf' => 3,
'mlt.minwl' => 2,
'mlt.maxwl' => 15,
'mlt.maxqt' => 4,
'mlt.maxntp' => 5,
'mlt.boost' => true,
'mlt.qf' => 'description',
'mlt.count' => 6,
),
$request->getParams()
);
}
}
\ No newline at end of file
...@@ -181,33 +181,6 @@ class Solarium_Client_RequestBuilder_SelectTest extends PHPUnit_Framework_TestCa ...@@ -181,33 +181,6 @@ class Solarium_Client_RequestBuilder_SelectTest extends PHPUnit_Framework_TestCa
$request->getUri(); $request->getUri();
} }
public function testSelectUrlWithMoreLikeThis()
{
$mlt = $this->_query->getMoreLikeThis();
$mlt->setFields('description,name');
$mlt->setMinimumTermFrequency(1);
$mlt->setMinimumDocumentFrequency(3);
$mlt->setMinimumWordLength(2);
$mlt->setMaximumWordLength(15);
$mlt->setMaximumQueryTerms(4);
$mlt->setMaximumNumberOfTokens(5);
$mlt->setBoost(true);
$mlt->setQueryFields('description');
$mlt->setCount(6);
$request = $this->_builder->build($this->_query);
$this->assertEquals(
null,
$request->getRawData()
);
$this->assertEquals(
'select?q=*:*&start=0&rows=10&fl=*,score&wt=json&mlt=true&mlt.fl=description,name&mlt.mintf=1&mlt.mindf=3&mlt.minwl=2&mlt.maxwl=15&mlt.maxqt=4&mlt.maxntp=5&mlt.boost=1&mlt.qf=description&mlt.count=6',
urldecode($request->getUri())
);
}
} }
class UnknownFacet extends Solarium_Query_Select_Component_Facet_Field{ class UnknownFacet extends Solarium_Query_Select_Component_Facet_Field{
......
...@@ -32,6 +32,9 @@ ...@@ -32,6 +32,9 @@
class Solarium_Client_RequestTest extends PHPUnit_Framework_TestCase class Solarium_Client_RequestTest extends PHPUnit_Framework_TestCase
{ {
/**
* @var Solarium_Client_Request
*/
protected $_request; protected $_request;
public function setup() public function setup()
...@@ -39,7 +42,7 @@ class Solarium_Client_RequestTest extends PHPUnit_Framework_TestCase ...@@ -39,7 +42,7 @@ class Solarium_Client_RequestTest extends PHPUnit_Framework_TestCase
$this->_request = new Solarium_Client_Request; $this->_request = new Solarium_Client_Request;
} }
public function testGetMethod() public function testGetDefaultMethod()
{ {
$this->assertEquals( $this->assertEquals(
Solarium_Client_Request::METHOD_GET, Solarium_Client_Request::METHOD_GET,
...@@ -47,6 +50,293 @@ class Solarium_Client_RequestTest extends PHPUnit_Framework_TestCase ...@@ -47,6 +50,293 @@ class Solarium_Client_RequestTest extends PHPUnit_Framework_TestCase
); );
} }
public function testSetAndGetMethod()
{
$this->_request->setMethod(Solarium_Client_Request::METHOD_POST);
$this->assertEquals(
Solarium_Client_Request::METHOD_POST,
$this->_request->getMethod()
);
}
public function testSetAndGetHandler()
{
$this->_request->setHandler('myhandler');
$this->assertEquals(
'myhandler',
$this->_request->getHandler()
);
}
public function testSetAndGetParams()
{
$params = array(
'param1' => 1,
'param2' => 2,
);
$this->_request->setParams($params);
$this->assertEquals(
$params,
$this->_request->getParams()
);
}
public function testSetAndGetParam()
{
$params = array(
'param1' => 1,
'param2' => 2,
);
$this->_request->setParams($params);
$this->assertEquals(
2,
$this->_request->getParam('param2')
);
}
public function testAddParam()
{
$params = array(
'param1' => 1,
'param2' => 2,
);
$this->_request->setParams($params);
$this->_request->addParam('param3', 3);
$params['param3'] = 3;
$this->assertEquals(
$params,
$this->_request->getParams()
);
}
public function testAddParamMultivalue()
{
$params = array(
'param1' => 1,
);
$this->_request->setParams($params);
$this->_request->addParam('param2', 2);
$this->_request->addParam('param2', 3);
$params['param2'] = array(2, 3);
$this->assertEquals(
$params,
$this->_request->getParams()
);
}
public function testAddParamNoValue()
{
$params = array(
'param1' => 1,
);
$this->_request->setParams($params);
$this->_request->addParam('param2', 2);
$this->_request->addParam('param2', '');
$this->_request->addParam('param3', '');
$params['param2'] = 2;
$this->assertEquals(
$params,
$this->_request->getParams()
);
}
public function testAddParamOverwrite()
{
$params = array(
'param1' => 1,
);
$this->_request->setParams($params);
$this->_request->addParam('param1', 2, true);
$this->assertEquals(
array('param1' => 2),
$this->_request->getParams()
);
}
public function testAddParams()
{
$params = array(
'param1' => 1,
);
$extraParams = array(
'param1' => 2,
'param2' => 3,
);
$this->_request->setParams($params);
$this->_request->addParams($extraParams);
$this->assertEquals(
array(
'param1' => array(1,2),
'param2' => 3,
),
$this->_request->getParams()
);
}
public function testAddParamsOverwrite()
{
$params = array(
'param1' => 1,
);
$extraParams = array(
'param1' => 2,
'param2' => 3,
);
$this->_request->setParams($params);
$this->_request->addParams($extraParams, true);
$this->assertEquals(
array(
'param1' => 2,
'param2' => 3,
),
$this->_request->getParams()
);
}
public function testRemoveParam()
{
$params = array(
'param1' => 1,
'param2' => 2,
);
$this->_request->setParams($params);
$this->_request->removeParam('param2');
$this->assertEquals(
array('param1' => 1),
$this->_request->getParams()
);
}
public function testClearParams()
{
$params = array(
'param1' => 1,
'param2' => 2,
);
$this->_request->setParams($params);
$this->_request->clearParams();
$this->assertEquals(
array(),
$this->_request->getParams()
);
}
public function testGetAndSetRawData()
{
$data = '1234567890';
$this->_request->setRawData($data);
$this->assertEquals(
$data,
$this->_request->getRawData()
);
}
public function testSetAndGetHeaders()
{
$headers = array(
'User-Agent: My Agent',
'Cache-Control: no-cache'
);
$this->_request->setHeaders($headers);
$this->assertEquals(
$headers,
$this->_request->getHeaders()
);
}
public function testAddHeader()
{
$headers = array(
'User-Agent: My Agent',
);
$this->_request->setHeaders($headers);
$this->_request->addHeader('Cache-Control: no-cache');
$headers[] = 'Cache-Control: no-cache';
$this->assertEquals(
$headers,
$this->_request->getHeaders()
);
}
public function testAddHeaders()
{
$headers = array(
'User-Agent: My Agent',
);
$extraHeaders = array(
'Cache-Control: no-cache',
'X-custom: 123',
);
$this->_request->setHeaders($headers);
$this->_request->addHeaders($extraHeaders);
$this->assertEquals(
array_merge($headers, $extraHeaders),
$this->_request->getHeaders()
);
}
public function testClearHeaders()
{
$headers = array(
'User-Agent: My Agent',
'Cache-Control: no-cache'
);
$this->_request->setHeaders($headers);
$this->assertEquals(
$headers,
$this->_request->getHeaders()
);
$this->_request->clearHeaders();
$this->assertEquals(
array(),
$this->_request->getHeaders()
);
}
public function testGetUri() public function testGetUri()
{ {
$this->assertEquals( $this->assertEquals(
...@@ -55,4 +345,20 @@ class Solarium_Client_RequestTest extends PHPUnit_Framework_TestCase ...@@ -55,4 +345,20 @@ class Solarium_Client_RequestTest extends PHPUnit_Framework_TestCase
); );
} }
public function testGetUriWithHandlerAndParams()
{
$params = array(
'param1' => 1,
'param2' => array(2,3),
);
$this->_request->setHandler('myHandler');
$this->_request->addParams($params);
$this->assertEquals(
'myHandler?param1=1&param2=2&param2=3',
$this->_request->getUri()
);
}
} }
\ No newline at end of file
...@@ -32,43 +32,93 @@ ...@@ -32,43 +32,93 @@
class Solarium_ClientTest extends PHPUnit_Framework_TestCase class Solarium_ClientTest extends PHPUnit_Framework_TestCase
{ {
/**
* @var Solarium_Client
*/
protected $_client;
public function setUp()
{
$this->_client = new Solarium_Client();
}
public function testGetAdapterWithDefaultAdapter() public function testGetAdapterWithDefaultAdapter()
{ {
$client = new Solarium_Client();
$defaultAdapter = $client->getOption('adapter'); $defaultAdapter = $this->_client->getOption('adapter');
$adapter = $client->getAdapter(); $adapter = $this->_client->getAdapter();
$this->assertThat($adapter, $this->isInstanceOf($defaultAdapter)); $this->assertThat($adapter, $this->isInstanceOf($defaultAdapter));
} }
public function testGetAdapterWithString() public function testGetAdapterWithString()
{ {
$adapterClass = 'MyAdapter'; $adapterClass = 'MyAdapter';
$client = new Solarium_Client(); $this->_client->setAdapter($adapterClass);
$client->setAdapter($adapterClass); $this->assertThat($this->_client->getAdapter(), $this->isInstanceOf($adapterClass));
$this->assertThat($client->getAdapter(), $this->isInstanceOf($adapterClass));
} }
public function testGetAdapterWithObject() public function testGetAdapterWithObject()
{ {
$adapterClass = 'MyAdapter'; $adapterClass = 'MyAdapter';
$client = new Solarium_Client(); $this->_client->setAdapter(new $adapterClass);
$client->setAdapter(new $adapterClass); $this->assertThat($this->_client->getAdapter(), $this->isInstanceOf($adapterClass));
$this->assertThat($client->getAdapter(), $this->isInstanceOf($adapterClass));
} }
public function testRegisterQueryTypeAndGetQueryTypes() public function testRegisterQueryTypeAndGetQueryTypes()
{ {
$queryTypes = $this->_client->getQueryTypes();
$this->_client->registerQueryType('myquerytype','mybuilder','myparser');
$queryTypes['myquerytype'] = array(
'requestbuilder' => 'mybuilder',
'responseparser' => 'myparser',
);
$this->assertEquals(
$queryTypes,
$this->_client->getQueryTypes()
);
} }
public function testRegisterAndGetPlugin() public function testRegisterAndGetPlugin()
{ {
$options = array('option1' => 1);
$this->_client->registerPlugin('testplugin','MyClientPlugin',$options);
$plugin = $this->_client->getPlugin('testplugin');
$this->assertThat(
$plugin,
$this->isInstanceOf('MyClientPlugin')
);
$this->assertEquals(
$options,
$plugin->getOptions()
);
} }
public function testRemoveAndGetPlugins() public function testRemoveAndGetPlugins()
{ {
$options = array('option1' => 1);
$this->_client->registerPlugin('testplugin','MyClientPlugin',$options);
$plugin = $this->_client->getPlugin('testplugin');
$plugins = $this->_client->getPlugins();
$this->assertEquals(
array('testplugin' => $plugin),
$plugins
);
$this->_client->removePlugin('testplugin');
$plugins = $this->_client->getPlugins();
$this->assertEquals(
array(),
$plugins
);
} }
public function testCreateRequest() public function testCreateRequest()
...@@ -81,12 +131,12 @@ class Solarium_ClientTest extends PHPUnit_Framework_TestCase ...@@ -81,12 +131,12 @@ class Solarium_ClientTest extends PHPUnit_Framework_TestCase
} }
public function testCreateRequestWithOverridingPlugin() public function testCreateRequestPlugin()
{ {
} }
public function testCreateRequestPostPlugin() public function testCreateRequestWithOverridingPlugin()
{ {
} }
...@@ -101,12 +151,12 @@ class Solarium_ClientTest extends PHPUnit_Framework_TestCase ...@@ -101,12 +151,12 @@ class Solarium_ClientTest extends PHPUnit_Framework_TestCase
} }
public function testCreateResultWithOverridingPlugin() public function testCreateResultPlugin()
{ {
} }
public function testCreateResultPostPlugin() public function testCreateResultWithOverridingPlugin()
{ {
} }
...@@ -151,3 +201,7 @@ class myConfig{ ...@@ -151,3 +201,7 @@ class myConfig{
return $this->_options; return $this->_options;
} }
} }
class MyClientPlugin extends Solarium_Plugin_Abstract{
}
\ 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