Commit 9e9885fb authored by Bas de Nooijer's avatar Bas de Nooijer

Fix for issue #82

parent bcaa7493
...@@ -38,6 +38,7 @@ ...@@ -38,6 +38,7 @@
*/ */
namespace Solarium\QueryType\Select\Query\Component; namespace Solarium\QueryType\Select\Query\Component;
use Solarium\Core\Configurable; use Solarium\Core\Configurable;
use Solarium\Core\Query\Query;
/** /**
* Query component base class * Query component base class
...@@ -45,6 +46,11 @@ use Solarium\Core\Configurable; ...@@ -45,6 +46,11 @@ use Solarium\Core\Configurable;
abstract class Component extends Configurable abstract class Component extends Configurable
{ {
/**
* @var Query
*/
protected $queryInstance;
/** /**
* Get component type * Get component type
* *
...@@ -66,4 +72,25 @@ abstract class Component extends Configurable ...@@ -66,4 +72,25 @@ abstract class Component extends Configurable
*/ */
abstract public function getResponseParser(); abstract public function getResponseParser();
/**
* Set parent query instance
*
* @return self Provides fluent interface
*/
public function setQueryInstance(Query $instance)
{
$this->queryInstance = $instance;
return $this;
}
/**
* Get parent query instance
*
* @return Query
*/
public function getQueryInstance()
{
return $this->queryInstance;
}
} }
...@@ -79,16 +79,19 @@ class Spellcheck extends Component ...@@ -79,16 +79,19 @@ class Spellcheck extends Component
} }
/** /**
* Set query option * Set spellcheck query option
*
* Query to spellcheck
* *
* @param string $query * @param string $query
* @param array $bind Bind values for placeholders in the query string
* @return self Provides fluent interface * @return self Provides fluent interface
*/ */
public function setQuery($query) public function setQuery($query, $bind = null)
{ {
return $this->setOption('query', $query); if (!is_null($bind)) {
$query = $this->getQueryInstance()->getHelper()->assemble($query, $bind);
}
return $this->setOption('query', trim($query));
} }
/** /**
......
...@@ -817,11 +817,12 @@ class Query extends BaseQuery ...@@ -817,11 +817,12 @@ class Query extends BaseQuery
* This overwrites any existing component registered with the same key. * This overwrites any existing component registered with the same key.
* *
* @param string $key * @param string $key
* @param object|null $value * @param object|null $component
* @return self Provides fluent interface * @return self Provides fluent interface
*/ */
public function setComponent($key, $value) public function setComponent($key, $value)
{ {
$value->setQueryInstance($this);
$this->components[$key] = $value; $this->components[$key] = $value;
return $this; return $this;
......
...@@ -42,6 +42,14 @@ class ComponentTest extends \PHPUnit_Framework_TestCase ...@@ -42,6 +42,14 @@ class ComponentTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('testtype', $component->getType()); $this->assertEquals('testtype', $component->getType());
} }
public function testSetAndGetQueryInstance()
{
$query = new Query;
$component = new TestComponent();
$component->setQueryInstance($query);
$this->assertEquals($query, $component->getQueryInstance());
}
} }
class TestComponent extends Component class TestComponent extends Component
......
...@@ -44,6 +44,7 @@ class SpellcheckTest extends \PHPUnit_Framework_TestCase ...@@ -44,6 +44,7 @@ class SpellcheckTest extends \PHPUnit_Framework_TestCase
public function setUp() public function setUp()
{ {
$this->spellCheck = new Spellcheck; $this->spellCheck = new Spellcheck;
$this->spellCheck->setQueryInstance(new Query);
} }
public function testGetType() public function testGetType()
...@@ -72,6 +73,12 @@ class SpellcheckTest extends \PHPUnit_Framework_TestCase ...@@ -72,6 +73,12 @@ class SpellcheckTest extends \PHPUnit_Framework_TestCase
); );
} }
public function testSetAndGetQueryWithBind()
{
$this->spellCheck->setQuery('id:%1%', array(678));
$this->assertEquals('id:678', $this->spellCheck->getQuery());
}
public function testSetAndGetBuild() public function testSetAndGetBuild()
{ {
$value = true; $value = true;
......
...@@ -494,6 +494,17 @@ class QueryTest extends \PHPUnit_Framework_TestCase ...@@ -494,6 +494,17 @@ class QueryTest extends \PHPUnit_Framework_TestCase
); );
} }
public function testSetAndGetComponentQueryInstance()
{
$mlt = new MoreLikeThis;
$this->query->setComponent('mlt',$mlt);
$this->assertEquals(
$this->query,
$this->query->getComponent('mlt')->getQueryInstance()
);
}
public function testGetInvalidComponent() public function testGetInvalidComponent()
{ {
$this->assertEquals( $this->assertEquals(
......
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