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

Added unittest coverage for per-field highlighting settings

parent 711073d2
......@@ -38,7 +38,19 @@ class Solarium_Client_RequestBuilder_Select_Component_HighlightingTest extends P
$request = new Solarium_Client_Request();
$component = new Solarium_Query_Select_Component_Highlighting();
$component->setFields('fieldA,fieldB');
$component->addField('fieldA');
$field = $component->getField('fieldB');
$field->setSnippets(3);
$field->setFragSize(25);
$field->setMergeContiguous(true);
$field->setAlternateField('text');
$field->setFormatter('myFormatter');
$field->setSimplePrefix('<b>');
$field->setSimplePostfix('</b>');
$field->setFragmenter('myFragmenter');
$field->setUseFastVectorHighlighter(true);
$component->setSnippets(2);
$component->setFragSize(3);
$component->setMergeContiguous(true);
......@@ -58,12 +70,7 @@ class Solarium_Client_RequestBuilder_Select_Component_HighlightingTest extends P
$component->setRegexSlop(1.3);
$component->setRegexPattern('mypattern');
$component->setMaxAnalyzedChars(100);
// Per field
$component->field->setSnippets(20)
->setFragsize(200)
->endPerFieldOptions();
$request = $builder->build($component, $request);
$this->assertEquals(
......@@ -88,8 +95,15 @@ class Solarium_Client_RequestBuilder_Select_Component_HighlightingTest extends P
'hl.highlightMultiTerm' => 'true',
'hl.regex.slop' => 1.3,
'hl.regex.pattern' => 'mypattern',
'f.field.hl.snippets' => 20,
'f.field.hl.fragsize' => 200
'f.fieldB.hl.snippets' => 3,
'f.fieldB.hl.fragsize' => 25,
'f.fieldB.hl.mergeContiguous' => 'true',
'f.fieldB.hl.alternateField' => 'text',
'f.fieldB.hl.formatter' => 'myFormatter',
'f.fieldB.hl.simple.pre' => '<b>',
'f.fieldB.hl.simple.post' => '</b>',
'f.fieldB.hl.fragmenter' => 'myFragmenter',
'f.fieldB.hl.useFastVectorHighlighter' => 'true',
),
$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_Query_Select_Component_FieldTest extends PHPUnit_Framework_TestCase
{
/**
* @var Solarium_Query_Select_Component_Highlighting_Field
*/
protected $_fld;
public function setUp()
{
$this->_fld = new Solarium_Query_Select_Component_Highlighting_Field;
}
public function testConfigMode()
{
$options = array(
'snippets' => 3,
'fragsize' => 25,
'mergecontiguous' => true,
'alternatefield' => 'text',
'formatter' => 'myFormatter',
'simpleprefix' => '<b>',
'simplepostfix' => '</b>',
'fragmenter' => 'myFragmenter',
'usefastvectorhighlighter' => true,
);
$this->_fld->setOptions($options);
$this->assertEquals(3, $this->_fld->getSnippets());
$this->assertEquals(25, $this->_fld->getFragSize());
$this->assertEquals(true, $this->_fld->getMergeContiguous());
$this->assertEquals('text', $this->_fld->getAlternateField());
$this->assertEquals('myFormatter', $this->_fld->getFormatter());
$this->assertEquals('<b>', $this->_fld->getSimplePrefix());
$this->assertEquals('</b>', $this->_fld->getSimplePostfix());
$this->assertEquals('myFragmenter', $this->_fld->getFragmenter());
$this->assertEquals(true, $this->_fld->getUseFastVectorHighlighter());
}
public function testSetAndGetName()
{
$value = 'testname';
$this->_fld->setName($value);
$this->assertEquals(
$value,
$this->_fld->getName()
);
}
public function testSetAndGetSnippets()
{
$value = 2;
$this->_fld->setSnippets($value);
$this->assertEquals(
$value,
$this->_fld->getSnippets()
);
}
public function testSetAndGetFragSize()
{
$value = 20;
$this->_fld->setFragsize($value);
$this->assertEquals(
$value,
$this->_fld->getFragSize()
);
}
public function testSetAndGetMergeContiguous()
{
$value = true;
$this->_fld->setMergeContiguous($value);
$this->assertEquals(
$value,
$this->_fld->getMergeContiguous()
);
}
public function testSetAndGetAlternateField()
{
$value = 'description';
$this->_fld->setAlternateField($value);
$this->assertEquals(
$value,
$this->_fld->getAlternateField()
);
}
public function testSetAndGetFormatter()
{
$this->_fld->setFormatter();
$this->assertEquals(
'simple',
$this->_fld->getFormatter()
);
}
public function testSetAndGetSimplePrefix()
{
$value = '<em>';
$this->_fld->setSimplePrefix($value);
$this->assertEquals(
$value,
$this->_fld->getSimplePrefix()
);
}
public function testSetAndGetSimplePostfix()
{
$value = '</em>';
$this->_fld->setSimplePostfix($value);
$this->assertEquals(
$value,
$this->_fld->getSimplePostfix()
);
}
public function testSetAndGetFragmenter()
{
$value = Solarium_Query_Select_Component_Highlighting::FRAGMENTER_REGEX;
$this->_fld->setFragmenter($value);
$this->assertEquals(
$value,
$this->_fld->getFragmenter()
);
}
public function testSetAndGetUseFastVectorHighlighter()
{
$value = true;
$this->_fld->setUseFastVectorHighlighter($value);
$this->assertEquals(
$value,
$this->_fld->getUseFastVectorHighlighter()
);
}
}
......@@ -45,7 +45,13 @@ class Solarium_Query_Select_Component_HighlightingTest extends PHPUnit_Framework
public function testConfigMode()
{
$options = array(
'fields' => 'fieldA,fieldB',
'field' => array(
'fieldA' => array(
'snippets' => 3,
'fragsize' => 25,
),
'fieldB'
),
'snippets' => 2,
'fragsize' => 20,
'mergecontiguous' => true,
......@@ -70,7 +76,10 @@ class Solarium_Query_Select_Component_HighlightingTest extends PHPUnit_Framework
$this->_hlt->setOptions($options);
$this->assertEquals($options['fields'], $this->_hlt->getFields());
$this->assertEquals(array('fieldA','fieldB'), array_keys($this->_hlt->getFields()));
$this->assertEquals($options['field']['fieldA']['snippets'], $this->_hlt->getField('fieldA')->getSnippets());
$this->assertEquals($options['field']['fieldA']['fragsize'], $this->_hlt->getField('fieldA')->getFragSize());
$this->assertEquals(null, $this->_hlt->getField('FieldB')->getSnippets());
$this->assertEquals($options['snippets'], $this->_hlt->getSnippets());
$this->assertEquals($options['fragsize'], $this->_hlt->getFragSize());
$this->assertEquals($options['mergecontiguous'], $this->_hlt->getMergeContiguous());
......@@ -96,17 +105,140 @@ class Solarium_Query_Select_Component_HighlightingTest extends PHPUnit_Framework
$this->assertEquals(Solarium_Query_Select::COMPONENT_HIGHLIGHTING, $this->_hlt->getType());
}
public function testSetAndGetFields()
public function testGetFieldAutocreate()
{
$value = 'name,description';
$this->_hlt->setFields($value);
$name = 'test';
$field = $this->_hlt->getField($name);
$this->assertEquals(
$value,
$this->_hlt->getFields()
$this->assertEquals($name, $field->getName());
}
public function testGetFieldNoAutocreate()
{
$name = 'test';
$field = $this->_hlt->getField($name, false);
$this->assertEquals(null, $field);
}
public function testAddFieldWithObject()
{
$field = new Solarium_Query_Select_Component_Highlighting_Field;
$field->setName('test');
$this->_hlt->addField($field);
$this->assertEquals($field, $this->_hlt->getField('test'));
}
public function testAddFieldWithString()
{
$name = 'test';
$this->_hlt->addField($name);
$this->assertEquals(array($name), array_keys($this->_hlt->getFields()));
}
public function testAddFieldWithArray()
{
$config = array(
'name' => 'fieldA',
'snippets' => 6
);
$this->_hlt->addField($config);
$this->assertEquals(6, $this->_hlt->getField('fieldA')->getSnippets());
}
public function testAddFieldWithObjectWithoutName()
{
$field = new Solarium_Query_Select_Component_Highlighting_Field;
$this->setExpectedException('Solarium_Exception');
$this->_hlt->addField($field);
}
public function testAddsFieldsWithString()
{
$fields = 'test1,test2';
$this->_hlt->addFields($fields);
$this->assertEquals(array('test1','test2'), array_keys($this->_hlt->getFields()));
}
public function testAddsFieldsWithArray()
{
$fields = array(
'test1' => array('snippets' => 2),
'test2' => array('snippets' => 5),
);
$this->_hlt->addFields($fields);
$this->assertEquals(2, $this->_hlt->getField('test1')->getSnippets());
$this->assertEquals(5, $this->_hlt->getField('test2')->getSnippets());
}
public function testRemoveField()
{
$fields = array(
'test1' => array('snippets' => 2),
'test2' => array('snippets' => 5),
);
$this->_hlt->addFields($fields);
$this->assertEquals(array('test1','test2'), array_keys($this->_hlt->getFields()));
$this->_hlt->removeField('test1');
$this->assertEquals(array('test2'), array_keys($this->_hlt->getFields()));
}
public function testRemoveFieldWithInvalidName()
{
$fields = array(
'test1' => array('snippets' => 2),
'test2' => array('snippets' => 5),
);
$this->_hlt->addFields($fields);
$this->assertEquals(array('test1','test2'), array_keys($this->_hlt->getFields()));
$this->_hlt->removeField('test1=3'); // should fail silently and do nothing
$this->assertEquals(array('test1','test2'), array_keys($this->_hlt->getFields()));
}
public function testClearFields()
{
$fields = array(
'test1' => array('snippets' => 2),
'test2' => array('snippets' => 5),
);
$this->_hlt->addFields($fields);
$this->assertEquals(array('test1','test2'), array_keys($this->_hlt->getFields()));
$this->_hlt->clearFields();
$this->assertEquals(array(), array_keys($this->_hlt->getFields()));
}
public function testSetFields()
{
$fields = array(
'test1' => array('snippets' => 2),
'test2' => array('snippets' => 5),
);
$this->_hlt->addFields($fields);
$this->assertEquals(array('test1','test2'), array_keys($this->_hlt->getFields()));
$newFields = array(
'test3' => array('snippets' => 4),
'test4' => array('snippets' => 6),
);
$this->_hlt->setFields($newFields);
$this->assertEquals(array('test3','test4'), array_keys($this->_hlt->getFields()));
}
public function testSetAndGetSnippets()
{
$value = 2;
......@@ -314,32 +446,5 @@ class Solarium_Query_Select_Component_HighlightingTest extends PHPUnit_Framework
$this->_hlt->getRegexMaxAnalyzedChars()
);
}
public function testNonExistentPropertiesWillReturnTheComponentItself()
{
$this->assertInstanceOf('Solarium_Query_Select_Component', $this->_hlt->field);
}
/**
* @depends testNonExistentPropertiesWillReturnTheComponentItself
*/
public function testNonExistentPropertiesWillBeAddedToTheFieldsList()
{
$this->_hlt->field->endPerFieldOptions();
$this->assertContains('field', $this->_hlt->getFields());
}
public function testMethodsCalledAfterUnexistendPropertySetWillBeAddedAsAFieldSpecificOption()
{
$this->_hlt->field->setSnippets(20)->setFragSize(200)->endPerFieldOptions();
$fieldOptions = $this->_hlt->getPerFieldOptions();
$this->assertArrayHasKey('field', $fieldOptions);
$this->assertInternalType('array', $fieldOptions['field']);
$this->assertArrayHasKey('snippets', $fieldOptions['field']);
$this->assertArrayHasKey('fragsize', $fieldOptions['field']);
$this->assertEquals(20, $fieldOptions['field']['snippets']);
$this->assertEquals(200, $fieldOptions['field']['fragsize']);
}
}
......@@ -464,6 +464,10 @@ class Solarium_Query_SelectTest extends PHPUnit_Framework_TestCase
'fields' => array('id','title','category'),
'rows' => 100,
'start' => 200,
'shards' => array(
'shard1' => 'localhost:8983/solr/shard1',
'shard2' => 'localhost:8983/solr/shard2',
),
'filterquery' => array(
array('key' => 'pub', 'tag' => array('pub'),'query' => 'published:true'),
'online' => array('tag' => 'onl','query' => 'online:true')
......@@ -486,6 +490,7 @@ class Solarium_Query_SelectTest extends PHPUnit_Framework_TestCase
$this->assertEquals($config['fields'], $query->getFields());
$this->assertEquals($config['rows'], $query->getRows());
$this->assertEquals($config['start'], $query->getStart());
$this->assertEquals($config['shards'], $query->getShards());
$this->assertEquals($config['documentclass'], $query->getDocumentClass());
$this->assertEquals($config['resultclass'], $query->getResultClass());
$this->assertEquals('published:true', $query->getFilterQuery('pub')->getQuery());
......@@ -617,7 +622,7 @@ class Solarium_Query_SelectTest extends PHPUnit_Framework_TestCase
get_class($grouping)
);
}
public function testRegisterComponentType()
{
$components = $this->_query->getComponentTypes();
......@@ -650,4 +655,14 @@ class Solarium_Query_SelectTest extends PHPUnit_Framework_TestCase
$fqOptions['optionB']
);
}
public function testGetSpellcheck()
{
$spellcheck = $this->_query->getSpellcheck();
$this->assertEquals(
'Solarium_Query_Select_Component_Spellcheck',
get_class($spellcheck)
);
}
}
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