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

- unittest improvements

parent 36440380
<?php
/**
* Copyright 2011 Bas de Nooijer. All rights reserved.
......@@ -196,8 +197,8 @@ class Solarium_Client extends Solarium_Configurable
* You can also use this method to override any existing querytype with a new mapping
*
* @param string $type
* @param string $requestBuilder
* @param string $responseParser
* @param string|object $requestBuilder
* @param string|object $responseParser
* @return Solarium_Client Provides fluent interface
*/
public function registerQueryType($type, $requestBuilder, $responseParser)
......@@ -224,13 +225,16 @@ class Solarium_Client extends Solarium_Configurable
* Register a plugin
*
* @param string $key
* @param string $class
* @param string|object $plugin
* @param array $options
* @return Solarium_Client Provides fluent interface
*/
public function registerPlugin($key, $class, $options = array())
public function registerPlugin($key, $plugin, $options = array())
{
$plugin = new $class($this, $options);
if (is_string($plugin)) {
$plugin = new $plugin($this, $options);
}
if (!($plugin instanceof Solarium_Plugin_Abstract)) {
throw new Solarium_Exception('All plugins must extend Solarium_Plugin_Abstract');
}
......@@ -316,8 +320,10 @@ class Solarium_Client extends Solarium_Configurable
throw new Solarium_Exception('No requestbuilder registered for querytype: '. $queryType);
}
$requestBuilderClass = $this->_queryTypes[$queryType]['requestbuilder'];
$requestBuilder = new $requestBuilderClass;
$requestBuilder = $this->_queryTypes[$queryType]['requestbuilder'];
if (is_string($requestBuilder)) {
$requestBuilder = new $requestBuilder;
}
$request = $requestBuilder->build($query);
$this->_callPlugins('postCreateRequest', array($query, $request));
......
......@@ -137,22 +137,76 @@ class Solarium_ClientTest extends PHPUnit_Framework_TestCase
public function testCreateRequest()
{
$queryStub = $this->getMock('Solarium_Query_Select');
$queryStub->expects($this->any())
->method('getType')
->will($this->returnValue('testquerytype'));
$observer = $this->getMock('Solarium_Client_RequestBuilder', array('build'));
$observer->expects($this->once())
->method('build')
->with($this->equalTo($queryStub));
$this->_client->registerQueryType('testquerytype', $observer, '');
$this->_client->createRequest($queryStub);
}
public function testCreateRequestInvalidQueryType()
{
$queryStub = $this->getMock('Solarium_Query_Select');
$queryStub->expects($this->any())
->method('getType')
->will($this->returnValue('testquerytype'));
$this->setExpectedException('Solarium_Exception');
$this->_client->createRequest($queryStub);
}
public function testCreateRequestPrePlugin()
{
$query = new Solarium_Query_Select();
$observer = $this->getMock('Solarium_Plugin_Abstract', array(), array($this->_client,array()));
$observer->expects($this->once())
->method('preCreateRequest')
->with($this->equalTo($query));
$this->_client->registerPlugin('testplugin', $observer);
$this->_client->createRequest($query);
}
public function testCreateRequestPlugin()
public function testCreateRequestPostPlugin()
{
$query = new Solarium_Query_Select();
$request = $this->_client->createRequest($query);
$observer = $this->getMock('Solarium_Plugin_Abstract', array(), array($this->_client,array()));
$observer->expects($this->once())
->method('postCreateRequest')
->with($this->equalTo($query),$this->equalTo($request));
$this->_client->registerPlugin('testplugin', $observer);
$this->_client->createRequest($query);
}
public function testCreateRequestWithOverridingPlugin()
{
$overrideValue = 'dummyvalue';
$query = new Solarium_Query_Select();
$observer = $this->getMock('Solarium_Plugin_Abstract', array(), array($this->_client,array()));
$observer->expects($this->once())
->method('preCreateRequest')
->with($this->equalTo($query))
->will($this->returnValue($overrideValue));
$this->_client->registerPlugin('testplugin', $observer);
$request = $this->_client->createRequest($query);
$this->assertEquals(
$overrideValue,
$request
);
}
public function testCreateResult()
......@@ -165,7 +219,12 @@ class Solarium_ClientTest extends PHPUnit_Framework_TestCase
}
public function testCreateResultPlugin()
public function testCreateResultPrePlugin()
{
}
public function testCreateResultPostPlugin()
{
}
......
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