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

- added removeComponent method

- renamed maxScore to maximumScore
- added unittests for components and morelikethis
parent 559c9c6d
......@@ -672,7 +672,7 @@ class Solarium_Query_Select extends Solarium_Query
*/
public function getComponent($key, $autoload = null)
{
if (isset($this->_components[$key]) && $this->_components[$key] !== null) {
if (isset($this->_components[$key])) {
return $this->_components[$key];
} else {
if ($autoload !== null) {
......@@ -688,7 +688,6 @@ class Solarium_Query_Select extends Solarium_Query
* Set a component instance
*
* This overwrites any existing component registered with the same key.
* If you want to remove a component use NULL as value.
*
* @param string $key
* @param object|null $value
......@@ -700,6 +699,20 @@ class Solarium_Query_Select extends Solarium_Query
return $this;
}
/**
* Remove a component instance
*
* @param string $key
* @return Solarium_Query_Select Provides fluent interface
*/
public function removeComponent($key)
{
if (isset($this->_components[$key])) {
unset($this->_components[$key]);
}
return $this;
}
/**
* Get a MoreLikeThis component instance
*
......
......@@ -65,7 +65,7 @@ class Solarium_Result_Select_MoreLikeThis_Result implements IteratorAggregate, C
*
* @var float
*/
protected $_maxScore;
protected $_maximumScore;
/**
* Constructor
......@@ -78,7 +78,7 @@ class Solarium_Result_Select_MoreLikeThis_Result implements IteratorAggregate, C
public function __construct($numFound, $maxScore, $documents)
{
$this->_numFound = $numFound;
$this->_maxScore = $maxScore;
$this->_maximumScore = $maxScore;
$this->_documents = $documents;
}
......@@ -100,9 +100,9 @@ class Solarium_Result_Select_MoreLikeThis_Result implements IteratorAggregate, C
*
* @return float
*/
public function getMaxScore()
public function getMaximumScore()
{
return $this->_maxScore;
return $this->_maximumScore;
}
/**
......
......@@ -32,6 +32,9 @@
class Solarium_Client_Request_SelectTest extends PHPUnit_Framework_TestCase
{
/**
* @var Solarium_Query_Select
*/
protected $_query;
protected $_options = array(
......@@ -130,6 +133,34 @@ class Solarium_Client_Request_SelectTest extends PHPUnit_Framework_TestCase
$request = new Solarium_Client_Request_Select($this->_options, $this->_query);
$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 = new Solarium_Client_Request_Select($this->_options, $this->_query);
$this->assertEquals(
null,
$request->getRawData()
);
$this->assertEquals(
'http://127.0.0.1:80/solr/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_Facet_Field{
......
<?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_Query_Select_Component_moreLikeThisTest extends PHPUnit_Framework_TestCase
{
protected $_mlt;
public function setUp()
{
$this->_mlt = new Solarium_Query_Select_Component_MoreLikeThis;
}
public function testSetAndGetFields()
{
$value = 'name,description';
$this->_mlt->setFields($value);
$this->assertEquals(
$value,
$this->_mlt->getFields()
);
}
public function testSetAndGetMinimumTermFrequency()
{
$value = 2;
$this->_mlt->setMinimumTermFrequency($value);
$this->assertEquals(
$value,
$this->_mlt->getMinimumTermFrequency()
);
}
public function testMinimumDocumentFrequency()
{
$value = 4;
$this->_mlt->setMinimumDocumentFrequency($value);
$this->assertEquals(
$value,
$this->_mlt->getMinimumDocumentFrequency()
);
}
public function testSetAndGetMinimumWordLength()
{
$value = 3;
$this->_mlt->setMinimumWordLength($value);
$this->assertEquals(
$value,
$this->_mlt->getMinimumWordLength()
);
}
public function testSetAndGetMaximumWordLength()
{
$value = 15;
$this->_mlt->setMaximumWordLength($value);
$this->assertEquals(
$value,
$this->_mlt->getMaximumWordLength()
);
}
public function testSetAndGetMaximumQueryTerms()
{
$value = 5;
$this->_mlt->setMaximumQueryTerms($value);
$this->assertEquals(
$value,
$this->_mlt->getMaximumQueryTerms()
);
}
public function testSetAndGetMaximumNumberOfTokens()
{
$value = 5;
$this->_mlt->setMaximumNumberOfTokens($value);
$this->assertEquals(
$value,
$this->_mlt->getMaximumNumberOfTokens()
);
}
public function testSetAndGetBoost()
{
$value = true;
$this->_mlt->setBoost($value);
$this->assertEquals(
$value,
$this->_mlt->getBoost()
);
}
public function testSetAndGetQueryFields()
{
$value = 'content,name';
$this->_mlt->setQueryFields($value);
$this->assertEquals(
$value,
$this->_mlt->getQueryFields()
);
}
public function testSetAndGetCount()
{
$value = 8;
$this->_mlt->setCount($value);
$this->assertEquals(
$value,
$this->_mlt->getCount()
);
}
}
<?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_Query_Select_ComponentTest extends PHPUnit_Framework_TestCase
{
public function testGetType()
{
$component = new TestComponent();
$this->assertEquals('testtype', $component->getType());
}
}
class TestComponent extends Solarium_Query_Select_Component{
protected $_type = 'testtype';
}
\ No newline at end of file
......@@ -541,4 +541,62 @@ class Solarium_Query_SelectTest extends PHPUnit_Framework_TestCase
$facets['category13']->getQuery()
);
}
public function testSetAndGetComponents()
{
$mlt = new Solarium_Query_Select_Component_MoreLikeThis;
$this->_query->setComponent('mlt',$mlt);
$this->assertEquals(
array('mlt' => $mlt),
$this->_query->getComponents()
);
}
public function testSetAndGetComponent()
{
$mlt = new Solarium_Query_Select_Component_MoreLikeThis;
$this->_query->setComponent('mlt',$mlt);
$this->assertEquals(
$mlt,
$this->_query->getComponent('mlt')
);
}
public function testGetInvalidComponent()
{
$this->assertEquals(
null,
$this->_query->getComponent('invalid')
);
}
public function testRemoveComponent()
{
$mlt = new Solarium_Query_Select_Component_MoreLikeThis;
$this->_query->setComponent('mlt',$mlt);
$this->assertEquals(
array('mlt' => $mlt),
$this->_query->getComponents()
);
$this->_query->removeComponent('mlt');
$this->assertEquals(
array(),
$this->_query->getComponents()
);
}
public function testGetMoreLikeThis()
{
$mlt = $this->_query->getMoreLikeThis();
$this->assertEquals(
'Solarium_Query_Select_Component_MoreLikeThis',
get_class($mlt)
);
}
}
\ No newline at end of file
<?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_Result_Select_MoreLikeThis_ResultTest extends PHPUnit_Framework_TestCase
{
/**
* @var Solarium_Result_Select_MoreLikeThis_Result
*/
protected $_mltResult;
public function setUp()
{
$this->_docs = array(
new Solarium_Document_ReadOnly(array('id'=>1,'name'=>'test1')),
new Solarium_Document_ReadOnly(array('id'=>2,'name'=>'test2')),
);
$this->_mltResult = new Solarium_Result_Select_MoreLikeThis_Result(2, 5.13, $this->_docs);
}
public function testGetNumFound()
{
$this->assertEquals(2, $this->_mltResult->getNumFound());
}
public function testGetMaximumScore()
{
$this->assertEquals(5.13, $this->_mltResult->getMaximumScore());
}
public function testGetDocuments()
{
$this->assertEquals($this->_docs, $this->_mltResult->getDocuments());
}
public function testIterator()
{
$docs = array();
foreach($this->_mltResult AS $key => $doc)
{
$docs[$key] = $doc;
}
$this->assertEquals($this->_docs, $docs);
}
public function testCount()
{
$this->assertEquals(count($this->_docs), count($this->_mltResult));
}
}
<?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_Result_Select_MoreLikeThisTest extends PHPUnit_Framework_TestCase
{
/**
* @var Solarium_Result_Select_MoreLikeThis
*/
protected $_mlt;
protected $_results;
public function setUp()
{
$docs = array(
new Solarium_Document_ReadOnly(array('id'=>1,'name'=>'test1')),
new Solarium_Document_ReadOnly(array('id'=>2,'name'=>'test2')),
);
$this->_results = array(
'key1' => new Solarium_Result_Select_MoreLikeThis_Result(2, 5.13, $docs),
'key2' => new Solarium_Result_Select_MoreLikeThis_Result(2, 2.3, $docs),
);
$this->_mlt = new Solarium_Result_Select_MoreLikeThis($this->_results);
}
public function testGetResults()
{
$this->assertEquals($this->_results, $this->_mlt->getResults());
}
public function testGetResult()
{
$this->assertEquals(
$this->_results['key1'],
$this->_mlt->getResult('key1')
);
}
public function testIterator()
{
$items = array();
foreach($this->_mlt AS $key => $item)
{
$items[$key] = $item;
}
$this->assertEquals($this->_results, $items);
}
}
......@@ -47,7 +47,11 @@ class Solarium_Result_SelectTest extends Solarium_Result_QueryTest
'f2' => new Solarium_Result_Select_Facet_Field(array('b' => 5)),
);
$this->_result = new Solarium_Result_Select(0,45,100, $this->_docs, $this->_facets);
$this->_components = array(
Solarium_Query_Select_Component::MORELIKETHIS => new Solarium_Result_Select_MoreLikeThis(array())
);
$this->_result = new Solarium_Result_Select(0,45,100, $this->_docs, $this->_facets, $this->_components);
}
public function testGetNumFound()
......@@ -80,6 +84,27 @@ class Solarium_Result_SelectTest extends Solarium_Result_QueryTest
$this->assertEquals(3, count($this->_result));
}
public function testGetComponents()
{
$this->assertEquals($this->_components, $this->_result->getComponents());
}
public function testGetComponent()
{
$this->assertEquals(
$this->_components[Solarium_Query_Select_Component::MORELIKETHIS],
$this->_result->getComponent(Solarium_Query_Select_Component::MORELIKETHIS)
);
}
public function testGetMoreLikeThis()
{
$this->assertEquals(
$this->_components[Solarium_Query_Select_Component::MORELIKETHIS],
$this->_result->getMoreLikeThis()
);
}
public function testIterator()
{
$docs = array();
......
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