Commit 29b2be3a authored by Bas de Nooijer's avatar Bas de Nooijer

Added unittests for MinimumScoreFilter, fixed several bugs discovered using the tests

parent 184cace6
......@@ -39,6 +39,7 @@
namespace Solarium\Plugin\MinimumScoreFilter;
use Solarium\QueryType\Select\Result\DocumentInterface;
use Solarium\Exception\RuntimeException;
/**
* Minimum score filter query result document
......@@ -100,7 +101,7 @@ class Document implements \IteratorAggregate, \Countable, \ArrayAccess
* @return mixed
*/
public function __get($name) {
return parent::__get($name);
return $this->document->__get($name);
}
/**
......@@ -123,18 +124,6 @@ class Document implements \IteratorAggregate, \Countable, \ArrayAccess
return $this->document->count();
}
/**
* ArrayAccess implementation
*
* @param mixed $offset
* @param mixed $value
* @return void
*/
public function offsetSet($offset, $value)
{
return $this->document->offsetGet($offset, $value);
}
/**
* ArrayAccess implementation
*
......@@ -168,4 +157,32 @@ class Document implements \IteratorAggregate, \Countable, \ArrayAccess
return $this->document->offsetGet($offset);
}
/**
* Set field value
*
* Magic method for setting a field as property of this object. Since this
* is a readonly document an exception will be thrown to prevent this.
*
* @throws RuntimeException
* @param string $name
* @param string $value
* @return void
*/
public function __set($name, $value)
{
throw new RuntimeException('A readonly document cannot be altered');
}
/**
* ArrayAccess implementation
*
* @param mixed $offset
* @param mixed $value
* @return void
*/
public function offsetSet($offset, $value)
{
$this->__set($offset, $value);
}
}
......@@ -41,14 +41,10 @@ namespace Solarium\Plugin\MinimumScoreFilter;
use Solarium\Core\Plugin\Plugin;
/**
* PostBigRequest plugin
* MinimumScoreFilter plugin
*
* If you reach the url/header length limit of your servlet container your queries will fail.
* You can increase the limit in the servlet container, but if that's not possible this plugin can automatically
* convert big GET requests into POST requests. A POST request (usually) has a much higher limit.
* Filters results based on score relative to the maxScore
*
* The default maximum querystring length is 1024. This doesn't include the base url or headers.
* For most servlet setups this limit leaves enough room for that extra data. Adjust the limit if needed.
*/
class MinimumScoreFilter extends Plugin
{
......
......@@ -39,6 +39,7 @@
namespace Solarium\Plugin\MinimumScoreFilter;
use Solarium\QueryType\Select\Result\Result as SelectResult;
use Solarium\Exception\OutOfBoundsException;
/**
* Minimumscore filter query result
......@@ -91,7 +92,7 @@ class Result extends SelectResult
}
break;
default:
throw new \OutOfBoundsException('Unknown filter mode in query: ' . $mode);
throw new OutOfBoundsException('Unknown filter mode in query: ' . $mode);
break;
}
......
<?php
/**
* Copyright 2014 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.
*/
namespace Solarium\Tests\Plugin\MinimumScoreFilter;
use Solarium\QueryType\Select\Result\Document;
use Solarium\Plugin\MinimumScoreFilter\Document as FilterDocument;
use Solarium\Tests\QueryType\Select\Result\DocumentTest as SelectDocumentTest;
class DocumentTest extends SelectDocumentTest
{
protected function setUp()
{
$doc = new Document($this->fields);
$this->doc = new FilterDocument($doc, true);
}
public function testMarkedAsLowScore()
{
$this->assertEquals(true, $this->doc->markedAsLowScore());
$doc2 = new Document($this->fields);
$filterDoc2 = new FilterDocument($doc2, false);
$this->assertEquals(false, $filterDoc2->markedAsLowScore());
}
}
<?php
/**
* Copyright 2014 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.
*/
namespace Solarium\Tests\Plugin\MinimumScoreFilter;
use Solarium\Plugin\MinimumScoreFilter\Query;
use Solarium\Tests\QueryType\Select\Query\QueryTest as SelectQueryTest;
class QueryTest extends SelectQueryTest
{
public function setUp()
{
$this->query = new Query;
}
public function testSetAndGetFilterMode()
{
$this->query->setFilterMode(Query::FILTER_MODE_MARK);
$this->assertEquals(Query::FILTER_MODE_MARK, $this->query->getFilterMode());
}
public function testSetAndGetFilterRatio()
{
$this->query->setFilterRatio(0.345);
$this->assertEquals(0.345, $this->query->getFilterRatio());
}
public function testClearFields()
{
$this->query->addField('newfield');
$this->query->clearFields();
$this->assertEquals(array('score'), $this->query->getFields());
}
public function testSetAndGetResultClass()
{
// Should be ignored
$this->query->setResultClass('MyResult');
$this->assertEquals('Solarium\Plugin\MinimumScoreFilter\Result', $this->query->getResultClass());
}
public function testAddFields()
{
$this->query->clearFields();
$this->query->addFields(array('field1', 'field2'));
$this->assertEquals(array('field1', 'field2', 'score'), $this->query->getFields());
}
public function testRemoveField()
{
$this->query->clearFields();
$this->query->addFields(array('field1', 'field2'));
$this->query->removeField('field1');
$this->assertEquals(array('field2', 'score'), $this->query->getFields());
}
public function testSetFields()
{
$this->query->clearFields();
$this->query->addFields(array('field1', 'field2'));
$this->query->setFields(array('field3', 'field4'));
$this->assertEquals(array('field3', 'field4', 'score'), $this->query->getFields());
}
public function testAddFieldsAsStringWithTrim()
{
$this->query->clearFields();
$this->query->addFields('field1, field2');
$this->assertEquals(array('field1', 'field2', 'score'), $this->query->getFields());
}
}
\ No newline at end of file
<?php
/**
* Copyright 2014 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.
*/
namespace Solarium\Tests\Plugin\MinimumScoreFilter;
use Solarium\Plugin\MinimumScoreFilter\Query;
use Solarium\Plugin\MinimumScoreFilter\Result;
use Solarium\QueryType\Select\Result\Document;
use Solarium\Tests\QueryType\Select\Result\ResultTest as SelectResultTest;
class ResultTest extends SelectResultTest
{
public function setUp()
{
parent::setUp();
$this->maxScore = 0.91;
$this->docs = array(
new Document(array('id'=>1, 'title'=>'doc1', 'score' => 0.91)),
new Document(array('id'=>2, 'title'=>'doc2', 'score' => 0.654)),
new Document(array('id'=>3, 'title'=>'doc3', 'score' => 0.23)),
new Document(array('id'=>4, 'title'=>'doc4', 'score' => 0.08)),
);
$this->result = new FilterResultDummy(1, 12, $this->numFound, $this->maxScore, $this->docs, $this->components, Query::FILTER_MODE_MARK);
}
public function testIterator()
{
foreach ($this->result as $key => $doc) {
$this->assertEquals($this->docs[$key]->title, $doc->title);
$this->assertEquals(($key == 3), $doc->markedAsLowScore());
}
}
public function testGetDocuments()
{
$this->assertEquals(count($this->docs), count($this->result->getDocuments()));
}
public function testIteratorWithRemoveFilter()
{
$result = new FilterResultDummy(1, 12, $this->numFound, $this->maxScore, $this->docs, $this->components, Query::FILTER_MODE_REMOVE);
$docs = array();
foreach ($result as $key => $doc) {
$docs[$key] = $doc;
}
$this->assertEquals($docs[0]->title, $this->docs[0]->title);
$this->assertEquals($docs[1]->title, $this->docs[1]->title);
$this->assertEquals($docs[2]->title, $this->docs[2]->title);
$this->assertArrayNotHasKey(3, $docs);
}
public function testGetDocumentsWithRemoveFilter()
{
$result = new FilterResultDummy(1, 12, $this->numFound, $this->maxScore, $this->docs, $this->components, Query::FILTER_MODE_REMOVE);
$docs = $result->getDocuments();
$this->assertEquals(3, count($docs));
$this->assertEquals($docs[0]->title, $this->docs[0]->title);
$this->assertEquals($docs[1]->title, $this->docs[1]->title);
$this->assertEquals($docs[2]->title, $this->docs[2]->title);
}
public function testFilterWithInvalidMode()
{
$this->setExpectedException('Solarium\Exception\OutOfBoundsException');
$result = new FilterResultDummy(1, 12, $this->numFound, $this->maxScore, $this->docs, $this->components, 'invalid_filter_name');
}
}
class FilterResultDummy extends Result
{
protected $parsed = true;
public function __construct($status, $queryTime, $numfound, $maxscore, $docs, $components, $mode)
{
$this->numfound = $numfound;
$this->maxscore = $maxscore;
$this->documents = $docs;
$this->components = $components;
$this->queryTime = $queryTime;
$this->status = $status;
$this->query = new Query();
$this->query->setFilterRatio(0.2)->setFilterMode($mode);
$this->mapData(array('documents' => $this->documents, 'maxscore' => $this->maxscore));
}
}
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