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

implemented new functionality and added unittests

parent ad59cf26
...@@ -496,12 +496,30 @@ class Solarium_Query_Select extends Solarium_Query ...@@ -496,12 +496,30 @@ class Solarium_Query_Select extends Solarium_Query
/** /**
* Create a filterquery instance * Create a filterquery instance
* *
* If you supply a string as the first arguments ($options) it will be used as the key for the filterquery
* and it will be added to this query.
* If you supply an options array/object that contains a key the filterquery will also be added to the query.
*
* When no key is supplied the filterquery cannot be added, in that case you will need to add it manually
* after setting the key, by using the addFilterQuery method.
*
* @param mixed $options * @param mixed $options
* @return Solarium_Query_Select_FilterQuery * @return Solarium_Query_Select_FilterQuery
*/ */
public function createFilterQuery($options = null) public function createFilterQuery($options = null)
{ {
return new Solarium_Query_Select_FilterQuery($options); if (is_string($options)) {
$fq = new Solarium_Query_Select_FilterQuery;
$fq->setKey($options);
} else {
$fq = new Solarium_Query_Select_FilterQuery($options);
}
if ($fq->getKey() !== null) {
$this->addFilterQuery($fq);
}
return $fq;
} }
/** /**
...@@ -526,11 +544,16 @@ class Solarium_Query_Select extends Solarium_Query ...@@ -526,11 +544,16 @@ class Solarium_Query_Select extends Solarium_Query
} }
if (array_key_exists($key, $this->_filterQueries)) { if (array_key_exists($key, $this->_filterQueries)) {
throw new Solarium_Exception('A filterquery must have a unique key' if($this->_filterQueries[$key] === $filterQuery) {
. ' value within a query'); //double add calls for the same FQ are ignored
//@todo add trigger_error with a notice?
} else {
throw new Solarium_Exception('A filterquery must have a unique key value within a query');
} }
} else {
$this->_filterQueries[$key] = $filterQuery; $this->_filterQueries[$key] = $filterQuery;
}
return $this; return $this;
} }
......
...@@ -257,7 +257,7 @@ class Solarium_Query_Select_Component_FacetSet extends Solarium_Query_Select_Com ...@@ -257,7 +257,7 @@ class Solarium_Query_Select_Component_FacetSet extends Solarium_Query_Select_Com
public function addFacet($facet) public function addFacet($facet)
{ {
if (is_array($facet)) { if (is_array($facet)) {
$facet = $this->createFacet($facet['type'], $facet); $facet = $this->createFacet($facet['type'], $facet, false);
} }
$key = $facet->getKey(); $key = $facet->getKey();
...@@ -267,11 +267,16 @@ class Solarium_Query_Select_Component_FacetSet extends Solarium_Query_Select_Com ...@@ -267,11 +267,16 @@ class Solarium_Query_Select_Component_FacetSet extends Solarium_Query_Select_Com
} }
if (array_key_exists($key, $this->_facets)) { if (array_key_exists($key, $this->_facets)) {
throw new Solarium_Exception('A facet must have a unique key value' if($this->_facets[$key] === $facet) {
. ' within a query'); //double add calls for the same facet are ignored
//@todo add trigger_error with a notice?
} else {
throw new Solarium_Exception('A facet must have a unique key value within a query');
} }
} else {
$this->_facets[$key] = $facet; $this->_facets[$key] = $facet;
}
return $this; return $this;
} }
...@@ -367,11 +372,21 @@ class Solarium_Query_Select_Component_FacetSet extends Solarium_Query_Select_Com ...@@ -367,11 +372,21 @@ class Solarium_Query_Select_Component_FacetSet extends Solarium_Query_Select_Com
} }
/** /**
* Create a facet instance
*
* If you supply a string as the first arguments ($options) it will be used as the key for the facet
* and it will be added to this query.
* If you supply an options array/object that contains a key the facet will also be added to the query.
*
* When no key is supplied the facet cannot be added, in that case you will need to add it manually
* after setting the key, by using the addFacet method.
*
* @param string $type * @param string $type
* @param array|object|null $options * @param array|object|null $options
* @param boolean $add
* @return Solarium_Query_Select_Component_Facet * @return Solarium_Query_Select_Component_Facet
*/ */
public function createFacet($type, $options = null) public function createFacet($type, $options = null, $add = true)
{ {
$type = strtolower($type); $type = strtolower($type);
...@@ -380,7 +395,19 @@ class Solarium_Query_Select_Component_FacetSet extends Solarium_Query_Select_Com ...@@ -380,7 +395,19 @@ class Solarium_Query_Select_Component_FacetSet extends Solarium_Query_Select_Com
} }
$class = $this->_facetTypes[$type]; $class = $this->_facetTypes[$type];
return new $class($options);
if (is_string($options)) {
$facet = new $class;
$facet->setKey($options);
} else {
$facet = new $class($options);
}
if ($add && $facet->getKey() !== null) {
$this->addFacet($facet);
}
return $facet;
} }
/** /**
......
...@@ -39,12 +39,10 @@ class Solarium_Client_ResponseParser_Select_Component_FacetSetTest extends PHPUn ...@@ -39,12 +39,10 @@ class Solarium_Client_ResponseParser_Select_Component_FacetSetTest extends PHPUn
$this->_parser = new Solarium_Client_ResponseParser_Select_Component_FacetSet; $this->_parser = new Solarium_Client_ResponseParser_Select_Component_FacetSet;
$this->_facetSet = new Solarium_Query_Select_Component_FacetSet(); $this->_facetSet = new Solarium_Query_Select_Component_FacetSet();
$this->_facetSet->addFacets(array( $this->_facetSet->createFacet('field', array('key' => 'keyA', 'field' => 'fieldA'));
$this->_facetSet->createFacet('field', array('key' => 'keyA', 'field' => 'fieldA')), $this->_facetSet->createFacet('query', array('key' => 'keyB'));
$this->_facetSet->createFacet('query', array('key' => 'keyB')), $this->_facetSet->createFacet('multiquery', array('key' => 'keyC', 'query' => array('keyC_A' => array('query' => 'id:1'), 'keyC_B' => array('query' => 'id:2'))));
$this->_facetSet->createFacet('multiquery', array('key' => 'keyC', 'query' => array('keyC_A' => array('query' => 'id:1'), 'keyC_B' => array('query' => 'id:2')))), $this->_facetSet->createFacet('range', array('key' => 'keyD'));
$this->_facetSet->createFacet('range', array('key' => 'keyD')),
));
} }
public function testParse() public function testParse()
......
...@@ -294,6 +294,24 @@ class Solarium_Query_Select_Component_FacetSetTest extends PHPUnit_Framework_Tes ...@@ -294,6 +294,24 @@ class Solarium_Query_Select_Component_FacetSetTest extends PHPUnit_Framework_Tes
); );
} }
public function testCreateFacetAdd()
{
$type = Solarium_Query_Select_Component_FacetSet::FACET_FIELD;
$options = array('key' => 'mykey','optionA' => 1, 'optionB' => 2);
$facet = $this->_facetSet->createFacet($type, $options);
$this->assertEquals($facet, $this->_facetSet->getFacet('mykey'));
}
public function testCreateFacetAddWithString()
{
$type = Solarium_Query_Select_Component_FacetSet::FACET_FIELD;
$options = 'mykey';
$facet = $this->_facetSet->createFacet($type, $options);
$this->assertEquals($facet, $this->_facetSet->getFacet('mykey'));
}
public function testCreateFacetWithInvalidType() public function testCreateFacetWithInvalidType()
{ {
$this->setExpectedException('Solarium_Exception'); $this->setExpectedException('Solarium_Exception');
......
...@@ -222,6 +222,24 @@ class Solarium_Query_SelectTest extends PHPUnit_Framework_TestCase ...@@ -222,6 +222,24 @@ class Solarium_Query_SelectTest extends PHPUnit_Framework_TestCase
); );
} }
public function testAddAndGetFilterQueryWithKey()
{
$key = 'fq1';
$fq = $this->_query->createFilterQuery($key, true);
$fq->setQuery('category:1');
$this->assertEquals(
$key,
$fq->getKey()
);
$this->assertEquals(
$fq,
$this->_query->getFilterQuery('fq1')
);
}
public function testAddFilterQueryWithoutKey() public function testAddFilterQueryWithoutKey()
{ {
$fq = new Solarium_Query_Select_FilterQuery; $fq = new Solarium_Query_Select_FilterQuery;
......
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