Commit 84332b28 authored by Bas de Nooijer's avatar Bas de Nooijer

Unittest coverage for suggester query (and related classes)

parent 4c35b3ac
......@@ -42,7 +42,7 @@
* @package Solarium
* @subpackage Result
*/
class Solarium_Result_Suggester_term implements IteratorAggregate, Countable
class Solarium_Result_Suggester_Term implements IteratorAggregate, Countable
{
/**
......
<?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_SuggesterTest extends PHPUnit_Framework_TestCase
{
/**
* @var Solarium_Query_Suggester
*/
protected $_query;
/**
* @var Solarium_Client_RequestBuilder_Suggester
*/
protected $_builder;
public function setUp()
{
$this->_query = new Solarium_Query_Suggester;
$this->_builder = new Solarium_Client_RequestBuilder_Suggester;
}
public function testBuildParams()
{
$this->_query->setCollate(true);
$this->_query->setCount(13);
$this->_query->setDictionary('suggest');
$this->_query->setQuery('ap ip');
$this->_query->setOnlyMorePopular(true);
$request = $this->_builder->build($this->_query);
$this->assertEquals(
array(
'spellcheck' => 'true',
'q' => 'ap ip',
'spellcheck.dictionary' => 'suggest',
'spellcheck.count' => 13,
'spellcheck.onlyMorePopular' => 'true',
'spellcheck.collate' => 'true',
'wt' => 'json',
),
$request->getParams()
);
$this->assertEquals(
Solarium_Client_Request::METHOD_GET,
$request->getMethod()
);
}
}
\ 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_Client_ResponseParser_SuggesterTest extends PHPUnit_Framework_TestCase
{
public function testParse()
{
$data = array(
'responseHeader' => array(
'status' => 1,
'QTime' => 13,
),
'spellcheck' => array(
'suggestions' => array(
'd',
array(
'numFound' => 2,
'startOffset' => 3,
'endOffset' => 7,
'suggestion' => array(
'disk',
'ddr'
)
),
'vid',
array(
'numFound' => 1,
'startOffset' => 2,
'endOffset' => 5,
'suggestion' => array(
'video',
)
),
'collation',
'disk video'
),
),
);
$query = new Solarium_Query_Suggester();
$resultStub = $this->getMock('Solarium_Result_Suggester', array(), array(), '', false);
$resultStub->expects($this->any())
->method('getData')
->will($this->returnValue($data));
$resultStub->expects($this->any())
->method('getQuery')
->will($this->returnValue($query));
$parser = new Solarium_Client_ResponseParser_Suggester;
$result = $parser->parse($resultStub);
$expected = array(
'd' => new Solarium_Result_Suggester_Term(2,3,7,array('disk','ddr')),
'vid' => new Solarium_Result_Suggester_Term(1,2,5,array('video'))
);
$this->assertEquals($expected, $result['results']);
$this->assertEquals('disk video', $result['collation']);
}
}
......@@ -656,6 +656,18 @@ class Solarium_ClientTest extends PHPUnit_Framework_TestCase
$observer->terms($query);
}
public function testSuggester()
{
$query = new Solarium_Query_Suggester();
$observer = $this->getMock('Solarium_Client', array('execute'));
$observer->expects($this->once())
->method('execute')
->with($this->equalTo($query));
$observer->suggester($query);
}
public function testCreateQuery()
{
$options = array('optionA' => 1, 'optionB' => 2);
......@@ -812,6 +824,18 @@ class Solarium_ClientTest extends PHPUnit_Framework_TestCase
$observer->createTerms($options);
}
public function testCreateSuggester()
{
$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_SUGGESTER), $this->equalTo($options));
$observer->createSuggester($options);
}
public function testTriggerEvent()
{
$eventName = 'Test';
......
<?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_SuggesterTest extends PHPUnit_Framework_TestCase
{
/**
* @var Solarium_Query_Suggester
*/
protected $_query;
public function setUp()
{
$this->_query = new Solarium_Query_Suggester;
}
public function testGetType()
{
$this->assertEquals(Solarium_Client::QUERYTYPE_SUGGESTER, $this->_query->getType());
}
public function testSetAndGetQuery()
{
$value = 'testquery';
$this->_query->setQuery($value);
$this->assertEquals(
$value,
$this->_query->getQuery()
);
}
public function testSetAndGetDictionary()
{
$value = 'myDictionary';
$this->_query->setDictionary($value);
$this->assertEquals(
$value,
$this->_query->getDictionary()
);
}
public function testSetAndGetCount()
{
$value = 11;
$this->_query->setCount($value);
$this->assertEquals(
$value,
$this->_query->getCount()
);
}
public function testSetAndGetOnlyMorePopular()
{
$value = false;
$this->_query->setOnlyMorePopular($value);
$this->assertEquals(
$value,
$this->_query->getOnlyMorePopular()
);
}
public function testSetAndGetCollate()
{
$value = false;
$this->_query->setCollate($value);
$this->assertEquals(
$value,
$this->_query->getCollate()
);
}
}
\ 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_Suggester_TermTest extends PHPUnit_Framework_TestCase
{
/**
* @var Solarium_Result_Suggester_Term
*/
protected $_result;
/**
* @var int
*/
protected $_numFound;
/**
* @var int
*/
protected $_startOffset;
/**
* @var int
*/
protected $_endOffset;
/**
* @var array
*/
protected $_suggestions;
public function setUp()
{
$this->_numFound = 5;
$this->_startOffset = 2;
$this->_endOffset = 6;
$this->_suggestions = array(
'suggestion1',
'suggestion2',
);
$this->_result = new Solarium_Result_Suggester_Term(
$this->_numFound, $this->_startOffset, $this->_endOffset, $this->_suggestions
);
}
public function testGetNumFound()
{
$this->assertEquals(
$this->_numFound,
$this->_result->getNumFound()
);
}
public function testGetStartOffset()
{
$this->assertEquals(
$this->_startOffset,
$this->_result->getStartOffset()
);
}
public function testGetEndOffset()
{
$this->assertEquals(
$this->_endOffset,
$this->_result->getEndOffset()
);
}
public function testGetSuggestions()
{
$this->assertEquals(
$this->_suggestions,
$this->_result->getSuggestions()
);
}
public function testCount()
{
$this->assertEquals(count($this->_suggestions), count($this->_result));
}
public function testIterator()
{
$results = array();
foreach($this->_result AS $key => $doc)
{
$results[$key] = $doc;
}
$this->assertEquals($this->_suggestions, $results);
}
}
\ 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_SuggesterTest extends PHPUnit_Framework_TestCase
{
/**
* @var Solarium_Result_Suggester
*/
protected $_result;
/**
* @var array
*/
protected $_data;
/**
* @var string
*/
protected $_collation;
public function setUp()
{
$this->_data = array(
'term1' => 'data1',
'term2' => 'data2',
);
$this->_collation = 'collation result';
$this->_result = new Solarium_Result_SuggesterDummy($this->_data, $this->_collation);
}
public function testGetStatus()
{
$this->assertEquals(
1,
$this->_result->getStatus()
);
}
public function testGetQueryTime()
{
$this->assertEquals(
12,
$this->_result->getQueryTime()
);
}
public function testGetResults()
{
$this->assertEquals($this->_data, $this->_result->getResults());
}
public function testGetTerm()
{
$this->assertEquals($this->_data['term1'], $this->_result->getTerm('term1'));
}
public function testGetTermsWithInvalidFieldName()
{
$this->assertEquals(array(), $this->_result->getTerm('term3'));
}
public function testCount()
{
$this->assertEquals(count($this->_data), count($this->_result));
}
public function testIterator()
{
$results = array();
foreach($this->_result AS $key => $doc)
{
$results[$key] = $doc;
}
$this->assertEquals($this->_data, $results);
}
public function testGetCollation()
{
$this->assertEquals($this->_collation, $this->_result->getCollation());
}
}
class Solarium_Result_SuggesterDummy extends Solarium_Result_Suggester
{
protected $_parsed = true;
public function __construct($results, $collation)
{
$this->_results = $results;
$this->_collation = $collation;
$this->_status = 1;
$this->_queryTime = 12;
}
}
\ 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