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

- added config mode support to Solarium_Client_Request

- added config mode support to Solarium_Client (including some required refactoring and new methods)
- improved unittests for config mode
parent 36770638
......@@ -124,6 +124,28 @@ class Solarium_Client extends Solarium_Configurable
*/
protected $_requestBuilders;
/**
* Initialization hook
*/
protected function _init()
{
foreach ($this->_options AS $name => $value) {
switch ($name) {
case 'adapteroptions':
$this->_setOption('adapteroptions', $value);
$adapter = $this->getAdapter();
if ($adapter) $adapter->setOptions($value);
break;
case 'querytype':
$this->registerQueryTypes($value);
break;
case 'plugin':
$this->registerPlugins($value);
break;
}
}
}
/**
* Set the adapter
*
......@@ -174,7 +196,7 @@ class Solarium_Client extends Solarium_Configurable
{
$adapterClass = $this->getOption('adapter');
$this->_adapter = new $adapterClass;
$this->_adapter->setOptions($this->_options);
$this->_adapter->setOptions($this->getOption('adapteroptions'));
}
/**
......@@ -183,11 +205,12 @@ class Solarium_Client extends Solarium_Configurable
* If {@see $_adapter} doesn't hold an instance a new one will be created by
* calling {@see _createAdapter()}
*
* @param boolean $autoload
* @return Solarium_Client_Adapter
*/
public function getAdapter()
public function getAdapter($autoload = true)
{
if (null === $this->_adapter) {
if (null === $this->_adapter && $autoload) {
$this->_createAdapter();
}
......@@ -200,13 +223,15 @@ 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 $query
* @param string|object $requestBuilder
* @param string|object $responseParser
* @return Solarium_Client Provides fluent interface
*/
public function registerQueryType($type, $requestBuilder, $responseParser)
public function registerQueryType($type, $query, $requestBuilder, $responseParser)
{
$this->_queryTypes[$type] = array(
'query' => $query,
'requestbuilder' => $requestBuilder,
'responseparser' => $responseParser,
);
......@@ -214,6 +239,27 @@ class Solarium_Client extends Solarium_Configurable
return $this;
}
/**
* Register multiple querytypes
*
* @param array $queryTypes
* @return Solarium_Client Provides fluent interface
*/
public function registerQueryTypes($queryTypes)
{
foreach ($queryTypes as $type => $queryType) {
if (!isset($queryType['type'])) $queryType['type'] = $type;
$this->registerQueryType(
$queryType['type'],
$queryType['query'],
$queryType['requestbuilder'],
$queryType['responseparser']
);
}
}
/**
* Get all registered querytypes
*
......@@ -249,6 +295,28 @@ class Solarium_Client extends Solarium_Configurable
return $this;
}
/**
* Register multiple plugins
*
* @param array $plugins
* @return Solarium_Client Provides fluent interface
*/
public function registerPlugins($plugins)
{
foreach ($plugins as $key => $plugin) {
if (!isset($plugin['key'])) $plugin['key'] = $key;
$this->registerPlugin(
$plugin['key'],
$plugin['plugin'],
$plugin['options']
);
}
return $this;
}
/**
* Get all registered querytypes
*
......
......@@ -82,6 +82,26 @@ class Solarium_Client_Request extends Solarium_Configurable
*/
protected $_rawData;
/**
* Initialization hook
*/
protected function _init()
{
foreach ($this->_options AS $name => $value) {
switch ($name) {
case 'rawdata':
$this->setRawData($value);
break;
case 'param':
$this->setParams($value);
break;
case 'header':
$this->setHeaders($value);
break;
}
}
}
/**
* Set request handler
*
......
......@@ -32,14 +32,29 @@
class Solarium_Client_AdapterTest extends PHPUnit_Framework_TestCase
{
/**
* @var Solarium_Client_Adapter
* @var TestAdapter
*/
protected $_adapter;
public function setUp()
{
$client = new Solarium_Client();
$this->_adapter = $client->getAdapter();
$this->_adapter = new TestAdapter();
}
public function testConfigMode()
{
$options = array(
'host' => '192.168.0.1',
'port' => 123,
'path' => '/mysolr/',
'core' => 'mycore',
'timeout' => 3,
);
$this->_adapter->setOptions($options);
$options['path'] = '/mysolr'; //expected trimming of trailing slash
$this->assertEquals($options, $this->_adapter->getOptions());
}
public function testSetAndGetHost()
......
......@@ -42,6 +42,52 @@ class Solarium_Client_RequestTest extends PHPUnit_Framework_TestCase
$this->_request = new Solarium_Client_Request;
}
public function testConfigMode()
{
$options = array(
'method' => Solarium_Client_Request::METHOD_POST,
'handler' => 'myHandler',
'param' => array(
'param1' => 1,
'param2' => 'test',
),
'rawdata' => 'raw post data here',
'header' => array(
'myHeader1' => 'X-myHeader1: value1',
'myHeader2' => 'X-myHeader2: value2',
),
);
$this->_request->setOptions($options);
$this->assertEquals(
$options['method'],
$this->_request->getMethod()
);
$this->assertEquals(
$options['handler'],
$this->_request->getHandler()
);
$this->assertEquals(
$options['rawdata'],
$this->_request->getRawData()
);
$this->assertEquals(
$options['param'],
$this->_request->getParams()
);
$this->assertEquals(
array(
$options['header']['myHeader1'],
$options['header']['myHeader2']
),
$this->_request->getHeaders()
);
}
public function testGetDefaultMethod()
{
$this->assertEquals(
......
......@@ -42,21 +42,117 @@ class Solarium_ClientTest extends PHPUnit_Framework_TestCase
$this->_client = new Solarium_Client();
}
public function testGetAdapterWithDefaultAdapter()
public function testConfigMode()
{
$options = array(
'adapter' => 'MyAdapter',
'adapteroptions' => array(
'host' => 'myhost',
'port' => 8080,
),
'querytype' => array(
'myquerytype' => array(
'query' => 'MyQuery',
'requestbuilder' => 'MyRequestBuilder',
'responseparser' => 'MyResponseParser'
)
),
'plugin' => array(
'myplugin' => array(
'plugin' => 'MyClientPlugin',
'options' => array(
'option1' => 'value1',
'option2' => 'value2',
)
)
),
);
$this->_client->setOptions($options);
$adapter = $this->_client->getAdapter();
$this->assertThat($adapter, $this->isInstanceOf('MyAdapter'));
$this->assertEquals(8080, $adapter->getPort());
$queryTypes = $this->_client->getQueryTypes();
$this->assertEquals(
$options['querytype']['myquerytype'],
$queryTypes['myquerytype']
);
$plugin = $this->_client->getPlugin('myplugin');
$this->assertThat($plugin, $this->isInstanceOf('MyClientPlugin'));
$this->assertEquals($options['plugin']['myplugin']['options'], $plugin->getOptions());
}
public function testConfigModeWithoutKeys()
{
$options = array(
'adapter' => 'MyAdapter',
'adapteroptions' => array(
'host' => 'myhost',
'port' => 8080,
),
'querytype' => array(
array(
'type' => 'myquerytype',
'query' => 'MyQuery',
'requestbuilder' => 'MyRequestBuilder',
'responseparser' => 'MyResponseParser',
)
),
'plugin' => array(
array(
'key' => 'myplugin',
'plugin' => 'MyClientPlugin',
'options' => array(
'option1' => 'value1',
'option2' => 'value2',
)
)
),
);
$this->_client->setOptions($options);
$adapter = $this->_client->getAdapter();
$this->assertThat($adapter, $this->isInstanceOf('MyAdapter'));
$this->assertEquals(8080, $adapter->getPort());
$queryTypes = $this->_client->getQueryTypes();
$this->assertEquals(
array(
'requestbuilder' => 'MyRequestBuilder',
'responseparser' => 'MyResponseParser',
'query' => 'MyQuery',
),
$queryTypes['myquerytype']
);
$plugin = $this->_client->getPlugin('myplugin');
$this->assertThat($plugin, $this->isInstanceOf('MyClientPlugin'));
$this->assertEquals($options['plugin'][0]['options'], $plugin->getOptions());
}
public function testSetAndGetAdapterWithDefaultAdapter()
{
$defaultAdapter = $this->_client->getOption('adapter');
$adapter = $this->_client->getAdapter();
$this->assertThat($adapter, $this->isInstanceOf($defaultAdapter));
}
public function testGetAdapterWithString()
public function testSetAndGetAdapterWithString()
{
$adapterClass = 'MyAdapter';
$this->_client->setAdapter($adapterClass);
$this->assertThat($this->_client->getAdapter(), $this->isInstanceOf($adapterClass));
}
public function testGetAdapterWithObject()
public function testSetAndGetAdapterWithObject()
{
$adapterClass = 'MyAdapter';
$this->_client->setAdapter(new $adapterClass);
......@@ -67,9 +163,10 @@ class Solarium_ClientTest extends PHPUnit_Framework_TestCase
{
$queryTypes = $this->_client->getQueryTypes();
$this->_client->registerQueryType('myquerytype','mybuilder','myparser');
$this->_client->registerQueryType('myquerytype','myquery','mybuilder','myparser');
$queryTypes['myquerytype'] = array(
'query' => 'myquery',
'requestbuilder' => 'mybuilder',
'responseparser' => 'myparser',
);
......@@ -146,7 +243,7 @@ class Solarium_ClientTest extends PHPUnit_Framework_TestCase
->method('build')
->with($this->equalTo($queryStub));
$this->_client->registerQueryType('testquerytype', $observer, '');
$this->_client->registerQueryType('testquerytype', 'Solarium_Query_Select', $observer, '');
$this->_client->createRequest($queryStub);
}
......
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