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

- refactoring of Solarium_Client classes

- updated phpdoc
- updated tests
parent 0a08acb8
...@@ -68,7 +68,7 @@ class Solarium_Client extends Solarium_Configurable ...@@ -68,7 +68,7 @@ class Solarium_Client extends Solarium_Configurable
'port' => 8983, 'port' => 8983,
'path' => '/solr', 'path' => '/solr',
'core' => null, 'core' => null,
'adapter' => 'Solarium_Client_Adapter_Stream', 'adapter' => 'Solarium_Client_Adapter_Http',
); );
/** /**
...@@ -289,14 +289,13 @@ class Solarium_Client extends Solarium_Configurable ...@@ -289,14 +289,13 @@ class Solarium_Client extends Solarium_Configurable
* </code> * </code>
* *
* @see Solarium_Query_Ping * @see Solarium_Query_Ping
* @see Solarium_Result_Ping
* *
* @internal This is a convenience method that forwards the query to the adapter and * @internal This is a convenience method that forwards the query to the
* returns the adapter result, thus allowing for an easy to use and clean * adapter and returns the adapter result, thus allowing for an easy to use
* API. * and clean API.
* *
* @param Solarium_Query_Ping $query * @param Solarium_Query_Ping $query
* @return Solarium_Result_Ping * @return boolean
*/ */
public function ping($query) public function ping($query)
{ {
...@@ -317,9 +316,9 @@ class Solarium_Client extends Solarium_Configurable ...@@ -317,9 +316,9 @@ class Solarium_Client extends Solarium_Configurable
* @see Solarium_Query_Update * @see Solarium_Query_Update
* @see Solarium_Result_Update * @see Solarium_Result_Update
* *
* @internal This is a convenience method that forwards the query to the adapter and * @internal This is a convenience method that forwards the query to the
* returns the adapter result, thus allowing for an easy to use and clean * adapter and returns the adapter result, thus allowing for an easy to use
* API. * and clean API.
* *
* @param Solarium_Query_Update $query * @param Solarium_Query_Update $query
* @return Solarium_Result_Update * @return Solarium_Result_Update
...@@ -342,9 +341,9 @@ class Solarium_Client extends Solarium_Configurable ...@@ -342,9 +341,9 @@ class Solarium_Client extends Solarium_Configurable
* @see Solarium_Query_Select * @see Solarium_Query_Select
* @see Solarium_Result_Select * @see Solarium_Result_Select
* *
* @internal This is a convenience method that forwards the query to the adapter and * @internal This is a convenience method that forwards the query to the
* returns the adapter result, thus allowing for an easy to use and clean * adapter and returns the adapter result, thus allowing for an easy to use
* API. * and clean API.
* *
* @param Solarium_Query_Select $query * @param Solarium_Query_Select $query
* @return Solarium_Result_Select * @return Solarium_Result_Select
......
...@@ -41,6 +41,17 @@ ...@@ -41,6 +41,17 @@
abstract class Solarium_Client_Adapter extends Solarium_Configurable abstract class Solarium_Client_Adapter extends Solarium_Configurable
{ {
/**
* Default options
*
* @var array
*/
protected $_options = array(
'adapteroptions' => array(
'timeout' => 5
),
);
/** /**
* Set options (overrides any existing values) * Set options (overrides any existing values)
* *
...@@ -49,7 +60,7 @@ abstract class Solarium_Client_Adapter extends Solarium_Configurable ...@@ -49,7 +60,7 @@ abstract class Solarium_Client_Adapter extends Solarium_Configurable
*/ */
public function setOptions($options) public function setOptions($options)
{ {
$this->_options = $options; $this->_setOptions($options, true);
} }
/** /**
...@@ -70,7 +81,7 @@ abstract class Solarium_Client_Adapter extends Solarium_Configurable ...@@ -70,7 +81,7 @@ abstract class Solarium_Client_Adapter extends Solarium_Configurable
* *
* @abstract * @abstract
* @param Solarium_Query_Ping $query * @param Solarium_Query_Ping $query
* @return Solarium_Result_Ping * @return boolean
*/ */
abstract public function ping($query); abstract public function ping($query);
......
...@@ -38,20 +38,28 @@ ...@@ -38,20 +38,28 @@
/** /**
* A very basic adapter using file_get_contents for retrieving data from Solr * A very basic adapter using file_get_contents for retrieving data from Solr
*/ */
class Solarium_Client_Adapter_Stream extends Solarium_Client_Adapter class Solarium_Client_Adapter_Http extends Solarium_Client_Adapter
{ {
/** /**
* Execute a select query and return a result object * Default options
*
* @var array
*/
protected $_options = array(
'timeout' => 5,
);
/**
* Executes a select query
* *
* @param Solarium_Query_Select $query * @param Solarium_Query_Select $query
* @return Solarium_Result_Select * @return Solarium_Result_Select
*/ */
public function select($query) public function select($query)
{ {
$data = $this->_getSolrData( $request = new Solarium_Client_Request_Select($this->_options, $query);
new Solarium_Client_Request_Select($this->_options, $query) $data = $this->_handleRequest($request);
);
$response = new Solarium_Client_Response_Select($query, $data); $response = new Solarium_Client_Response_Select($query, $data);
return $response->getResult(); return $response->getResult();
...@@ -59,102 +67,93 @@ class Solarium_Client_Adapter_Stream extends Solarium_Client_Adapter ...@@ -59,102 +67,93 @@ class Solarium_Client_Adapter_Stream extends Solarium_Client_Adapter
} }
/** /**
* Execute a ping query and return a result object * Executes a ping query
* *
* @param Solarium_Query_Ping $query * @param Solarium_Query_Ping $query
* @return Solarium_Result_Ping * @return boolean
*/ */
public function ping($query) public function ping($query)
{ {
$data = $this->_getSolrData( $request = new Solarium_Client_Request_Ping($this->_options, $query);
new Solarium_Client_Request($this->_options, $query), return (boolean)$this->_handleRequest($request);
'xml'
);
$response = new Solarium_Client_Response_Ping($query, $data);
return $response->getResult();
} }
/** /**
* Execute an update query and return a result object * Executes an update query
* *
* @param Solarium_Query_Update $query * @param Solarium_Query_Update $query
* @return Solarium_Result_Update * @return Solarium_Result_Update
*/ */
public function update($query) public function update($query)
{ {
$data = $this->_getSolrData( $request = new Solarium_Client_Request_Update($this->_options, $query);
new Solarium_Client_Request_Update($this->_options, $query) $data = $this->_handleRequest($request);
);
$response = new Solarium_Client_Response_Update($query, $data); $response = new Solarium_Client_Response_Update($query, $data);
return $response->getResult(); return $response->getResult();
} }
/** /**
* Handle Solr communication and JSON decode * Handle Solr communication
*
* @todo implement timeout
* @todo check http response code
* *
* @throws Solarium_Exception * @throws Solarium_Exception
* @param Solarium_Client_Request * @param Solarium_Client_Request
* @return array * @return array
*/ */
protected function _getSolrData($request, $mode = 'json') protected function _handleRequest($request)
{ {
if (null !== $request && null !== $request->getPostData()) { $method = $request->getMethod();
$context = stream_context_create( $context = stream_context_create(
array( array('http' => array(
'http' => array( 'method' => $method,
'method' => 'POST', 'timeout' => $this->getOption('timeout')
'content' => $request->getPostData(), ))
'header' => 'Content-Type: text/xml; charset=UTF-8',
),
)
); );
} else {
$context = null; if ($method == Solarium_Client_Request::POST) {
$data = $request->getRawData();
if (null !== $data) {
stream_context_set_option($context, 'http', 'content', $data);
stream_context_set_option($context, 'http', 'header',
'Content-Type: text/xml; charset=UTF-8');
} }
}
$data = @file_get_contents($request->getUri(), false, $context);
$data = @file_get_contents($request->getUrl(), false, $context); if ($method == Solarium_Client_Request::HEAD) {
// HEAD request has no result data
return true;
} else {
if (false === $data) { if (false === $data) {
$error = error_get_last(); $error = error_get_last();
throw new Solarium_Exception($error['message']); throw new Solarium_Exception($error['message']);
} }
if ($mode == 'json') { return $this->_jsonDecode($data);
$data = json_decode($data, true);
if (null === $data) {
throw new Solarium_Exception(
'Solr JSON response could not be decoded');
} }
} else if ($mode == 'xml') {
$data = $this->simplexmlToArray(simplexml_load_string($data));
} else {
throw new Solarium_Exception('Unknown Solr client data mode');
} }
return $data;
}
/**
function simplexmlToArray($xml) * TODO
*
* @throws Solarium_Exception
* @param $data
* @return mixed
*/
protected function _jsonDecode($data)
{ {
if (get_class($xml) == 'SimpleXMLElement') { $data = json_decode($data, true);
$attributes = $xml->attributes(); if (null === $data) {
foreach ($attributes as $k=>$v) { throw new Solarium_Exception(
if ($v) $a[$k] = (string) $v; 'Solr JSON response could not be decoded'
} );
$x = $xml;
$xml = get_object_vars($xml);
}
if (is_array($xml)) {
if (count($xml) == 0) return (string) $x; // for CDATA
foreach ($xml as $key=>$value) {
$r[$key] = $this->simplexmlToArray($value);
}
if (isset($a)) $r['@attributes'] = $a; // Attributes
return $r;
}
return (string) $xml;
} }
return $data;
}
} }
\ No newline at end of file
...@@ -36,19 +36,123 @@ ...@@ -36,19 +36,123 @@
*/ */
/** /**
* Handles Solr ping response, for use in adapters. * An adapter that uses a Zend_Http_Client for Solr request
*
* The Zend Framework HTTP client has many great features and has lots of
* configuration options. For more info see the manual at
* {@link http://framework.zend.com/manual/en/zend.http.html}
*
* To use this adapter you need to have the Zend Framework in your include path,
* autoloader or manually included.
*/ */
class Solarium_Client_Response_Ping extends Solarium_Client_Response class Solarium_Client_Adapter_ZendHttp extends Solarium_Client_Adapter_Http
{ {
public function getResult() /**
* Zend Http instance for communication with Solr
*
* @var Zend_Http_Client
*/
protected $_zendHttp;
/**
* Set options (overrides any existing values)
*
* If the options array has an 'adapteroptions' entry it is forwarded to the
* Zend_Http_Client. See the Zend_Http_Clientdocs for the many config
* options available.
*
* The $options param should be an array or an object that has a toArray
* method, like Zend_Config
*
* @param array|object $options
* @return Solarium_Client_Adapter_ZendHttp Provides fluent interface
*/
public function setOptions($options)
{
parent::setOptions($options);
if (null !== $this->_zendHttp
&& isset($this->_options['adapteroptions'])) {
$this->_zendHttp->setOptions($this->_options['adapteroptions']);
}
return $this;
}
/**
* Set the Zend_Http_Client instance
*
* This method is optional, if you don't set a client it will be created
* upon first use, using default and/or custom options (the most common use
* case)
*
* @param Zend_Http_Client $zendHttp
* @return Solarium_Client_Adapter_ZendHttp Provides fluent interface
*/
public function setZendHttp($zendHttp)
{ {
$resultClass = $this->_query->getOption('resultclass'); $this->_zendHttp = $zendHttp;
return $this;
}
/**
* Get the Zend_Http_Client instance
*
* If no instance is available yet it will be created automatically based on
* options.
*
* You can use this method to get a reference to the client instance to set
* options, get the last response and use many other features offered by the
* Zend_Http_Client API.
*
* @return Zend_Http_Client
*/
public function getZendHttp()
{
if (null == $this->_zendHttp) {
$options = null;
if (isset($this->_options['adapteroptions'])) {
$options = $this->_options['adapteroptions'];
}
$this->_zendHttp = new Zend_Http_Client(null, $options);
}
return $this->_zendHttp;
}
/**
* Execute a Solr request using the Zend_Http_Client instance
*
* @param Solarium_Client_Request $request
* @return string
*/
protected function _handleRequest($request)
{
$client = $this->getZendHttp();
$client->setMethod($request->getMethod());
$client->setUri($request->getUri());
$client->setRawData($request->getRawData());
$response = $client->request();
if ($request->getMethod() == Solarium_Client_Request::HEAD) {
return true;
} else {
$data = $response->getBody();
$type = $response->getHeader('Content-Type');
switch ($type) {
case 'text/plain; charset=utf-8':
return $this->_jsonDecode($data);
break;
default:
throw new Solarium_Exception('Unknown content-type: ' . $type);
break;
}
}
return new $resultClass(
$this->_data['ping']['status'],
0
);
} }
} }
\ No newline at end of file
...@@ -38,26 +38,74 @@ ...@@ -38,26 +38,74 @@
/** /**
* *
*/ */
class Solarium_Client_Request abstract class Solarium_Client_Request
{ {
/**
* Http request methods
*/
const GET = 'GET';
const POST = 'POST';
const HEAD = 'HEAD';
protected $_postData = null; /**
* TODO
protected $_params = array(); *
* @var Solarium_Query
*/
protected $_query;
/**
* TODO
*
* @var array
*/
protected $_options; protected $_options;
/**
* TODO
*
* @var array
*/
protected $_params;
/**
* TODO
*
* @param array|object $options
* @param Solarium_Query $query
*/
public function __construct($options, $query) public function __construct($options, $query)
{ {
$this->_options = $options; $this->_options = $options;
$this->_query = $query; $this->_query = $query;
$this->_init();
} }
protected function _init() /**
* TODO
*
* @return string
*/
public function getMethod()
{ {
return self::GET;
}
/**
* TODO
*
* @abstract
* @return void
*/
abstract public function getUri();
/**
* TODO
*
* @return null
*/
public function getRawData()
{
return null;
} }
/** /**
...@@ -65,7 +113,7 @@ class Solarium_Client_Request ...@@ -65,7 +113,7 @@ class Solarium_Client_Request
* *
* @return string * @return string
*/ */
public function getUrl() public function buildUri()
{ {
$queryString = ''; $queryString = '';
if (count($this->_params) > 0) { if (count($this->_params) > 0) {
...@@ -89,11 +137,6 @@ class Solarium_Client_Request ...@@ -89,11 +137,6 @@ class Solarium_Client_Request
. $queryString; . $queryString;
} }
public function getPostData()
{
return $this->_postData;
}
/** /**
* Render a boolean attribute * Render a boolean attribute
* *
...@@ -154,10 +197,6 @@ class Solarium_Client_Request ...@@ -154,10 +197,6 @@ class Solarium_Client_Request
return $value; return $value;
} }
public function getParams()
{
return $this->_params;
}
public function addParam($name, $value) public function addParam($name, $value)
{ {
......
...@@ -32,10 +32,22 @@ ...@@ -32,10 +32,22 @@
* @license http://github.com/basdenooijer/solarium/raw/master/COPYING * @license http://github.com/basdenooijer/solarium/raw/master/COPYING
* *
* @package Solarium * @package Solarium
* @subpackage Result * @subpackage Client
*/ */
class Solarium_Result_Ping extends Solarium_Result_Query /**
* Builds ping request, for use in adapters.
*/
class Solarium_Client_Request_Ping extends Solarium_Client_Request
{ {
public function getUri()
{
return $this->buildUri();
}
public function getMethod()
{
return self::HEAD;
}
} }
\ No newline at end of file
...@@ -41,7 +41,7 @@ ...@@ -41,7 +41,7 @@
class Solarium_Client_Request_Select extends Solarium_Client_Request class Solarium_Client_Request_Select extends Solarium_Client_Request
{ {
public function _init() public function getUri()
{ {
$this->_params = array( $this->_params = array(
'q' => $this->_query->getQuery(), 'q' => $this->_query->getQuery(),
...@@ -90,6 +90,8 @@ class Solarium_Client_Request_Select extends Solarium_Client_Request ...@@ -90,6 +90,8 @@ class Solarium_Client_Request_Select extends Solarium_Client_Request
} }
} }
} }
return $this->buildUri();
} }
public function addFacetField($facet) public function addFacetField($facet)
......
...@@ -42,13 +42,34 @@ class Solarium_Client_Request_Update extends Solarium_Client_Request ...@@ -42,13 +42,34 @@ class Solarium_Client_Request_Update extends Solarium_Client_Request
{ {
/** /**
* @throws Solarium_Exception * TODO
* @return void *
* @return string
*/
public function getMethod()
{
return self::POST;
}
/**
* TODO
*
* @return string
*/ */
public function _init() public function getUri()
{ {
$this->_params = array('wt' => 'json'); $this->_params = array('wt' => 'json');
return $this->buildUri();
}
/**
* TODO
*
* @throws Solarium_Exception
* @return string
*/
public function getRawData()
{
$xml = '<update>'; $xml = '<update>';
foreach ($this->_query->getCommands() AS $command) { foreach ($this->_query->getCommands() AS $command) {
switch ($command->getType()) { switch ($command->getType()) {
...@@ -74,7 +95,7 @@ class Solarium_Client_Request_Update extends Solarium_Client_Request ...@@ -74,7 +95,7 @@ class Solarium_Client_Request_Update extends Solarium_Client_Request
} }
$xml .= '</update>'; $xml .= '</update>';
$this->_postData = $xml; return $xml;
} }
/** /**
......
...@@ -40,6 +40,7 @@ ...@@ -40,6 +40,7 @@
*/ */
abstract class Solarium_Client_Response abstract class Solarium_Client_Response
{ {
protected $_query; protected $_query;
protected $_data; protected $_data;
......
...@@ -57,20 +57,37 @@ class Solarium_Configurable ...@@ -57,20 +57,37 @@ class Solarium_Configurable
/** /**
* Constructor * Constructor
* *
* If options are passed they will be merged with {@link $_options}, with * If options are passed they will be merged with {@link $_options} using
* new values overwriting any existing (default) values. * the {@link _setOptions()} method.
*
* After handling the options the {@link _init()} method is called.
*
* @throws Solarium_Exception
* @param array|Zend_Config $options
* @return void
*/
public function __construct($options = null)
{
$this->_setOptions($options);
$this->_init();
}
/**
* Set options
* *
* If $options is an object it will be converted into an array by called * If $options is an object it will be converted into an array by called
* it's toArray method. This is compatible with the Zend_Config classes in * it's toArray method. This is compatible with the Zend_Config classes in
* Zend Framework, but can also easily be implemented in any other object. * Zend Framework, but can also easily be implemented in any other object.
* *
* After handling the options the {@link _init()} method is called.
*
* @throws Solarium_Exception * @throws Solarium_Exception
* @param array|Zend_Config $options * @param array|Zend_Config $options
* @param boolean $overwrite True for overwriting existing options, false
* for merging (new values overwrite old ones if needed)
*
* @return void * @return void
*/ */
public function __construct($options = null) protected function _setOptions($options, $overwrite = false)
{ {
if (null !== $options) { if (null !== $options) {
// first convert to array if needed // first convert to array if needed
...@@ -83,11 +100,12 @@ class Solarium_Configurable ...@@ -83,11 +100,12 @@ class Solarium_Configurable
} }
} }
if (true == $overwrite) {
$this->_options = $options;
} else {
$this->_options = array_merge($this->_options, $options); $this->_options = array_merge($this->_options, $options);
} }
}
$this->_init();
} }
/** /**
......
...@@ -38,7 +38,7 @@ ...@@ -38,7 +38,7 @@
/** /**
* Solr query result read-only document * Solr query result read-only document
*/ */
class Solarium_Document_ReadOnly class Solarium_Document_ReadOnly implements IteratorAggregate
{ {
/** /**
...@@ -101,4 +101,14 @@ class Solarium_Document_ReadOnly ...@@ -101,4 +101,14 @@ class Solarium_Document_ReadOnly
throw new Solarium_Exception('A readonly document cannot be altered'); throw new Solarium_Exception('A readonly document cannot be altered');
} }
/**
* IteratorAggregate implementation
*
* @return ArrayIterator
*/
public function getIterator()
{
return new ArrayIterator($this->_fields);
}
} }
\ No newline at end of file
...@@ -56,7 +56,7 @@ class Solarium_Document_ReadWrite extends Solarium_Document_ReadOnly ...@@ -56,7 +56,7 @@ class Solarium_Document_ReadWrite extends Solarium_Document_ReadOnly
protected $_fieldBoosts; protected $_fieldBoosts;
/** /**
* Constructor. * Constructor
* *
* @param array $fields * @param array $fields
*/ */
......
...@@ -38,7 +38,7 @@ ...@@ -38,7 +38,7 @@
* Solarium specific exception * Solarium specific exception
* *
* All exceptions thrown by Solarium are of this type. This way you can easily * All exceptions thrown by Solarium are of this type. This way you can easily
* catch Solarium exception and keep them separate from your own exceptions. * catch Solarium exceptions and keep them separate from your own exceptions.
* *
* @package Solarium * @package Solarium
*/ */
......
...@@ -36,7 +36,10 @@ ...@@ -36,7 +36,10 @@
*/ */
/** /**
* Base class for all queries * Base class for all query types, not intended for direct usage
*
* @package Solarium
* @subpackage Query
*/ */
class Solarium_Query extends Solarium_Configurable class Solarium_Query extends Solarium_Configurable
{ {
...@@ -65,6 +68,13 @@ class Solarium_Query extends Solarium_Configurable ...@@ -65,6 +68,13 @@ class Solarium_Query extends Solarium_Configurable
/** /**
* Set resultclass option * Set resultclass option
* *
* If you set a custom result class it must be available through autoloading
* or a manual require before calling this method. This is your
* responsibility.
*
* Also you need to make sure it extends the orginal result class of the
* query or has an identical API.
*
* @param string $classname * @param string $classname
* @return Solarium_Query Provides fluent interface * @return Solarium_Query Provides fluent interface
*/ */
...@@ -85,6 +95,15 @@ class Solarium_Query extends Solarium_Configurable ...@@ -85,6 +95,15 @@ class Solarium_Query extends Solarium_Configurable
/** /**
* Escape special Solr characters in a value * Escape special Solr characters in a value
*
* This can be used for building Solr query strings. Any (user) input for
* the query can be passed to this function to prevent any issues with
* special characters.
*
* Do mind that you cannot build a complete query first and then pass it to
* this method, the whole query will be escaped. You need to escape only the
* 'content' of your query.
*
* @param string $string * @param string $string
* @return string * @return string
*/ */
......
...@@ -48,7 +48,6 @@ class Solarium_Query_Ping extends Solarium_Query ...@@ -48,7 +48,6 @@ class Solarium_Query_Ping extends Solarium_Query
*/ */
protected $_options = array( protected $_options = array(
'path' => '/admin/ping', 'path' => '/admin/ping',
'resultclass' => 'Solarium_Result_Ping',
); );
} }
\ No newline at end of file
...@@ -37,6 +37,9 @@ ...@@ -37,6 +37,9 @@
/** /**
* Query result * Query result
*
* @package Solarium
* @subpackage Result
*/ */
class Solarium_Result_Query class Solarium_Result_Query
{ {
......
...@@ -39,7 +39,7 @@ ...@@ -39,7 +39,7 @@
* Select query result * Select query result
*/ */
class Solarium_Result_Select extends Solarium_Result_Query class Solarium_Result_Select extends Solarium_Result_Query
implements Iterator, Countable implements IteratorAggregate
{ {
/** /**
...@@ -139,62 +139,13 @@ class Solarium_Result_Select extends Solarium_Result_Query ...@@ -139,62 +139,13 @@ class Solarium_Result_Select extends Solarium_Result_Query
} }
/** /**
* Count method for Countable interface * IteratorAggregate implementation
* *
* @return int * @return ArrayIterator
*/
public function count()
{
return count($this->_documents);
}
/**
* Iterator implementation
*
* @return void
*/
public function rewind()
{
$this->_position = 0;
}
/**
* Iterator implementation
*
* @return Solarium_Result_Select_Document
*/
function current()
{
return $this->_documents[$this->_position];
}
/**
* Iterator implementation
*
* @return integer
*/
public function key()
{
return $this->_position;
}
/**
* Iterator implementation
*
* @return void
*/ */
public function next() public function getIterator()
{ {
++$this->_position; return new ArrayIterator($this->_documents);
} }
/**
* Iterator implementation
*
* @return boolean
*/
public function valid()
{
return isset($this->_documents[$this->_position]);
}
} }
\ No newline at end of file
...@@ -38,7 +38,7 @@ ...@@ -38,7 +38,7 @@
/** /**
* Select query facet result * Select query facet result
*/ */
class Solarium_Result_Select_Facet_Field implements Iterator, Countable class Solarium_Result_Select_Facet_Field implements IteratorAggregate
{ {
/** /**
...@@ -70,63 +70,12 @@ class Solarium_Result_Select_Facet_Field implements Iterator, Countable ...@@ -70,63 +70,12 @@ class Solarium_Result_Select_Facet_Field implements Iterator, Countable
} }
/** /**
* Count method for Countable interface * IteratorAggregate implementation
* *
* @return int * @return ArrayIterator
*/ */
public function count() public function getIterator()
{ {
return count($this->_values); return new ArrayIterator($this->_documents);
} }
/**
* Iterator implementation
*
* @return void
*/
public function rewind()
{
reset($this->_values);
}
/**
* Iterator implementation
*
* @return Solarium_Result_Select_Document
*/
function current()
{
return current($this->_values);
}
/**
* Iterator implementation
*
* @return integer
*/
public function key()
{
return key($this->_values);
}
/**
* Iterator implementation
*
* @return void
*/
public function next()
{
next($this->_values);
}
/**
* Iterator implementation
*
* @return boolean
*/
public function valid()
{
return (current($this->_values) !== false);
}
} }
\ No newline at end of file
...@@ -37,6 +37,9 @@ ...@@ -37,6 +37,9 @@
/** /**
* Update result * Update result
*
* @package Solarium
* @subpackage Result
*/ */
class Solarium_Result_Update extends Solarium_Result_Query class Solarium_Result_Update extends Solarium_Result_Query
{ {
......
...@@ -101,7 +101,7 @@ class Solarium_Version ...@@ -101,7 +101,7 @@ class Solarium_Version
*/ */
static public function checkExact($version) static public function checkExact($version)
{ {
return (substr(self::VERSION,0,strlen($version)) == $version); return (substr(self::VERSION, 0, strlen($version)) == $version);
} }
......
...@@ -49,22 +49,22 @@ class Solarium_Client_RequestTest extends PHPUnit_Framework_TestCase ...@@ -49,22 +49,22 @@ class Solarium_Client_RequestTest extends PHPUnit_Framework_TestCase
return new $class($options, $query); return new $class($options, $query);
} }
public function testGetUrl() public function testGetUri()
{ {
$this->assertEquals( $this->assertEquals(
'http://127.0.0.1:80/solr/mypath?', 'http://127.0.0.1:80/solr/mypath?',
$this->_getRequest($this->_options)->getUrl() $this->_getRequest($this->_options)->getUri()
); );
} }
public function testGetUrlWithCore() public function testGetUriWithCore()
{ {
$options = $this->_options; $options = $this->_options;
$options['core'] = 'core0'; $options['core'] = 'core0';
$this->assertEquals( $this->assertEquals(
'http://127.0.0.1:80/solr/core0/mypath?', 'http://127.0.0.1:80/solr/core0/mypath?',
$this->_getRequest($options)->getUrl() $this->_getRequest($options)->getUri()
); );
} }
...@@ -100,19 +100,19 @@ class Solarium_Client_RequestTest extends PHPUnit_Framework_TestCase ...@@ -100,19 +100,19 @@ class Solarium_Client_RequestTest extends PHPUnit_Framework_TestCase
); );
} }
public function testGetUrlWithParams() public function testGetUriWithParams()
{ {
$this->assertEquals( $this->assertEquals(
'http://127.0.0.1:80/solr/mypath?wt=json&fq=category%3A1&fq=published%3Atrue', 'http://127.0.0.1:80/solr/mypath?wt=json&fq=category%3A1&fq=published%3Atrue',
$this->_getRequest($this->_options, 'TestRequest')->getUrl() $this->_getRequest($this->_options, 'TestRequest')->getUri()
); );
} }
public function testGetPostData() public function testGetRawData()
{ {
$this->assertEquals( $this->assertEquals(
'<data>xyz</data>', '<data>xyz</data>',
$this->_getRequest($this->_options, 'TestRequest')->getPostdata() $this->_getRequest($this->_options, 'TestRequest')->getRawData()
); );
} }
...@@ -173,6 +173,11 @@ class Solarium_Client_RequestTest extends PHPUnit_Framework_TestCase ...@@ -173,6 +173,11 @@ class Solarium_Client_RequestTest extends PHPUnit_Framework_TestCase
class TestRequest extends Solarium_Client_Request class TestRequest extends Solarium_Client_Request
{ {
public function getUri()
{
return $this->buildUri();
}
protected function _init() protected function _init()
{ {
$this->_postData = '<data>xyz</data>'; $this->_postData = '<data>xyz</data>';
......
<?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_Client_Response_PingTest extends PHPUnit_Framework_TestCase
{
public function testGetResult()
{
$query = new Solarium_Query_Ping;
$response = new Solarium_Client_Response_Ping($query);
$this->assertThat($response->getResult(), $this->isInstanceOf($query->getResultClass()));
}
public function testGetResultWithCustomClass()
{
$query = new Solarium_Query_Ping;
$query->setResultClass('MyPingResult');
$response = new Solarium_Client_Response_Ping($query);
$this->assertThat($response->getResult(), $this->isInstanceOf($query->getResultClass()));
}
}
class MyPingResult extends Solarium_Result_Ping{
}
...@@ -34,7 +34,7 @@ class Solarium_Client_ResponseTest extends PHPUnit_Framework_TestCase ...@@ -34,7 +34,7 @@ class Solarium_Client_ResponseTest extends PHPUnit_Framework_TestCase
public function testConstructor() public function testConstructor()
{ {
$query = new Solarium_Query_Ping; $query = new Solarium_Query_Update;
$data = array('response' => null); $data = array('response' => null);
$response = new MyTestResponse($query, $data); $response = new MyTestResponse($query, $data);
......
...@@ -97,11 +97,11 @@ class Solarium_ClientTest extends PHPUnit_Framework_TestCase ...@@ -97,11 +97,11 @@ class Solarium_ClientTest extends PHPUnit_Framework_TestCase
$options = $client->getOptions(); $options = $client->getOptions();
// initialising at adapter creation // initialising at adapter creation
$observer = $this->getMock('Solarium_Client_Adapter_Stream', array('setOptions')); $observer = $this->getMock('Solarium_Client_Adapter_Http', array('setOptions'));
$observer->expects($this->once()) $observer->expects($this->once())
->method('setOptions') ->method('setOptions')
->with($this->equalTo($options)); ->with($this->equalTo($options));
$client->setAdapter($observer)->getAdapter(); $client->setAdapter($observer);
} }
public function testOptionForwardingToAdapterAfterChange() public function testOptionForwardingToAdapterAfterChange()
...@@ -112,7 +112,7 @@ class Solarium_ClientTest extends PHPUnit_Framework_TestCase ...@@ -112,7 +112,7 @@ class Solarium_ClientTest extends PHPUnit_Framework_TestCase
$options = $client->getOptions(); $options = $client->getOptions();
$options['host'] = $newHostValue; $options['host'] = $newHostValue;
$observer = $this->getMock('Solarium_Client_Adapter_Stream', array('setOptions')); $observer = $this->getMock('Solarium_Client_Adapter_Http', array('setOptions'));
$observer->expects($this->at(1)) $observer->expects($this->at(1))
->method('setOptions') ->method('setOptions')
->with($this->equalTo($options)); ->with($this->equalTo($options));
...@@ -127,7 +127,7 @@ class Solarium_ClientTest extends PHPUnit_Framework_TestCase ...@@ -127,7 +127,7 @@ class Solarium_ClientTest extends PHPUnit_Framework_TestCase
$query = new Solarium_Query_Select; $query = new Solarium_Query_Select;
// initialising at adapter creation // initialising at adapter creation
$observer = $this->getMock('Solarium_Client_Adapter_Stream', array('select')); $observer = $this->getMock('Solarium_Client_Adapter_Http', array('select'));
$observer->expects($this->once()) $observer->expects($this->once())
->method('select') ->method('select')
->with($this->equalTo($query)); ->with($this->equalTo($query));
...@@ -142,7 +142,7 @@ class Solarium_ClientTest extends PHPUnit_Framework_TestCase ...@@ -142,7 +142,7 @@ class Solarium_ClientTest extends PHPUnit_Framework_TestCase
$query = new Solarium_Query_Ping; $query = new Solarium_Query_Ping;
// initialising at adapter creation // initialising at adapter creation
$observer = $this->getMock('Solarium_Client_Adapter_Stream', array('ping')); $observer = $this->getMock('Solarium_Client_Adapter_Http', array('ping'));
$observer->expects($this->once()) $observer->expects($this->once())
->method('ping') ->method('ping')
->with($this->equalTo($query)); ->with($this->equalTo($query));
...@@ -157,7 +157,7 @@ class Solarium_ClientTest extends PHPUnit_Framework_TestCase ...@@ -157,7 +157,7 @@ class Solarium_ClientTest extends PHPUnit_Framework_TestCase
$query = new Solarium_Query_Update; $query = new Solarium_Query_Update;
// initialising at adapter creation // initialising at adapter creation
$observer = $this->getMock('Solarium_Client_Adapter_Stream', array('update')); $observer = $this->getMock('Solarium_Client_Adapter_Http', array('update'));
$observer->expects($this->once()) $observer->expects($this->once())
->method('update') ->method('update')
->with($this->equalTo($query)); ->with($this->equalTo($query));
...@@ -168,7 +168,7 @@ class Solarium_ClientTest extends PHPUnit_Framework_TestCase ...@@ -168,7 +168,7 @@ class Solarium_ClientTest extends PHPUnit_Framework_TestCase
} }
class MyAdapter extends Solarium_Client_Adapter_Stream{ class MyAdapter extends Solarium_Client_Adapter_Http{
public function select($query) public function select($query)
{ {
......
<?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_Result_PingTest extends Solarium_Result_QueryTest
{
public function setUp()
{
$this->_result = new Solarium_Result_Ping(0,45);
}
}
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