Commit 69604f3f authored by Bas de Nooijer's avatar Bas de Nooijer

- added a curl adapter example and small bugfixes

- added terms query including unittests and an example
- added customize request plugig with unittests and example
parent 303fd1ba
<?php
require('init.php');
htmlHeader();
// create a client instance
$client = new Solarium_Client($config);
// get a terms query instance
$query = $client->createTerms();
$query->setFields('features,name');
$query->setLowerbound('i');
// this executes the query and returns the result
$resultset = $client->terms($query);
// display terms
foreach ($resultset as $field => $terms) {
echo '<h3>' . $field . '</h3>';
foreach ($terms as $term => $count) {
echo $term . ' (' . $count . ')<br/>';
}
echo '<hr/>';
}
htmlFooter();
\ No newline at end of file
<?php
require_once 'Zend/Loader/Autoloader.php';
$loader = Zend_Loader_Autoloader::getInstance();
require('init.php');
htmlHeader();
// create a client instance
$client = new Solarium_Client($config);
// set the adapter to peclhttp
$client->setAdapter('Solarium_Client_Adapter_Curl');
// get a select query instance
$query = $client->createSelect();
// this executes the query and returns the result
$resultset = $client->select($query);
// display the total number of documents found by solr
echo 'NumFound: '.$resultset->getNumFound();
// show documents using the resultset iterator
foreach ($resultset as $document) {
echo '<hr/><table>';
// the documents are also iterable, to get all fields
foreach($document AS $field => $value)
{
// this converts multivalue fields to a comma-separated string
if(is_array($value)) $value = implode(', ', $value);
echo '<tr><th>' . $field . '</th><td>' . $value . '</td></tr>';
}
echo '</table>';
}
htmlFooter();
\ No newline at end of file
<?php
require('init.php');
htmlHeader();
// create a client instance and autoload the customize request plugin
$client = new Solarium_Client($config);
$customizer = $client->getPlugin('customizerequest');
// add a persistent HTTP header (using array input values)
$customizer->createCustomization(array(
'key' => 'auth',
'type' => 'header',
'name' => 'X-my-auth',
'value' => 'mypassword',
'persistent' => true
));
// add a persistent GET param (using fluent interface)
$customizer->createCustomization('session')
->setType('param')
->setName('ssid')
->setValue('md7Nhd86adye6sad46d')
->setPersistent(true);
// add a GET param thats only used for a single request (the default setting is no persistence)
$customizer->createCustomization('id')
->setType('param')
->setName('id')
->setValue(4576);
// create a basic query to execute
$query = $client->createSelect();
// execute query (you should be able to see the extra params in the solr log file)
$resultset = $client->select($query);
// display the total number of documents found by solr
echo 'NumFound: '.$resultset->getNumFound() . '<br/>';
// execute the same query again (this time the 'id' param should no longer show up in the logs)
$resultset = $client->select($query);
// display the total number of documents found by solr
echo 'NumFound: '.$resultset->getNumFound();
htmlFooter();
\ No newline at end of file
...@@ -79,6 +79,8 @@ ...@@ -79,6 +79,8 @@
<li><a href="2.4.1-analysis-document.php">2.4.1 Analysis query for a document</a></li> <li><a href="2.4.1-analysis-document.php">2.4.1 Analysis query for a document</a></li>
<li><a href="2.4.2-analysis-field.php">2.4.2 Analysis query for a field</a></li> <li><a href="2.4.2-analysis-field.php">2.4.2 Analysis query for a field</a></li>
</ul> </ul>
<li><a href="2.5-terms-query.php">2.5 Terms query</a></li>
</ul> </ul>
<li>4. Usage modes</li> <li>4. Usage modes</li>
...@@ -103,8 +105,9 @@ ...@@ -103,8 +105,9 @@
<ul style="list-style:none;"> <ul style="list-style:none;">
<li>6.1 Client adapters</li> <li>6.1 Client adapters</li>
<ul style="list-style:none;"> <ul style="list-style:none;">
<li><a href="6.1.1-zend-http-adapter.php">6.1 Zend_Http adapter</a></li> <li><a href="6.1.1-zend-http-adapter.php">6.1.1 Zend_Http adapter</a></li>
<li><a href="6.1.2-pecl-http-adapter.php">6.2 Pecl_Http adapter</a></li> <li><a href="6.1.2-pecl-http-adapter.php">6.1.2 Pecl_Http adapter</a></li>
<li><a href="6.1.3-curl-adapter.php">6.1.3 Curl adapter</a></li>
</ul> </ul>
<li><a href="6.2-escaping.php">6.2 Escaping</a></li> <li><a href="6.2-escaping.php">6.2 Escaping</a></li>
<li><a href="6.3-placeholder-syntax.php">6.3 Placeholder syntax</a></li> <li><a href="6.3-placeholder-syntax.php">6.3 Placeholder syntax</a></li>
...@@ -114,6 +117,7 @@ ...@@ -114,6 +117,7 @@
<ul style="list-style:none;"> <ul style="list-style:none;">
<li><a href="7.1-plugin-loadbalancer.php">7.1 Loadbalancer</a></li> <li><a href="7.1-plugin-loadbalancer.php">7.1 Loadbalancer</a></li>
<li><a href="7.2-plugin-postbigrequest.php">7.2 Post Big Requests</a></li> <li><a href="7.2-plugin-postbigrequest.php">7.2 Post Big Requests</a></li>
<li><a href="7.3-plugin-customizerequest.php">7.3 customize Requests</a></li>
</ul> </ul>
</ul> </ul>
......
...@@ -87,6 +87,11 @@ class Solarium_Client extends Solarium_Configurable ...@@ -87,6 +87,11 @@ class Solarium_Client extends Solarium_Configurable
*/ */
const QUERYTYPE_ANALYSIS_DOCUMENT = 'analysis-document'; const QUERYTYPE_ANALYSIS_DOCUMENT = 'analysis-document';
/**
* Querytype terms
*/
const QUERYTYPE_TERMS = 'terms';
/** /**
* Default options * Default options
* *
...@@ -132,6 +137,11 @@ class Solarium_Client extends Solarium_Configurable ...@@ -132,6 +137,11 @@ class Solarium_Client extends Solarium_Configurable
'requestbuilder' => 'Solarium_Client_RequestBuilder_Analysis_Field', 'requestbuilder' => 'Solarium_Client_RequestBuilder_Analysis_Field',
'responseparser' => 'Solarium_Client_ResponseParser_Analysis_Field' 'responseparser' => 'Solarium_Client_ResponseParser_Analysis_Field'
), ),
self::QUERYTYPE_TERMS => array(
'query' => 'Solarium_Query_Terms',
'requestbuilder' => 'Solarium_Client_RequestBuilder_Terms',
'responseparser' => 'Solarium_Client_ResponseParser_Terms'
),
); );
/** /**
...@@ -142,6 +152,7 @@ class Solarium_Client extends Solarium_Configurable ...@@ -142,6 +152,7 @@ class Solarium_Client extends Solarium_Configurable
protected $_pluginTypes = array( protected $_pluginTypes = array(
'loadbalancer' => 'Solarium_Plugin_Loadbalancer', 'loadbalancer' => 'Solarium_Plugin_Loadbalancer',
'postbigrequest' => 'Solarium_Plugin_PostBigRequest', 'postbigrequest' => 'Solarium_Plugin_PostBigRequest',
'customizerequest' => 'Solarium_Plugin_CustomizeRequest',
); );
/** /**
...@@ -665,6 +676,20 @@ class Solarium_Client extends Solarium_Configurable ...@@ -665,6 +676,20 @@ class Solarium_Client extends Solarium_Configurable
return $this->execute($query); return $this->execute($query);
} }
/**
* Execute a terms query
*
* @internal This is a convenience method that forwards the query to the
* execute method, thus allowing for an easy to use and clean API.
*
* @param Solarium_Query_Terms $query
* @return Solarium_Result_Terms
*/
public function terms($query)
{
return $this->execute($query);
}
/** /**
* Create a query instance * Create a query instance
* *
...@@ -756,4 +781,15 @@ class Solarium_Client extends Solarium_Configurable ...@@ -756,4 +781,15 @@ class Solarium_Client extends Solarium_Configurable
{ {
return $this->createQuery(self::QUERYTYPE_ANALYSIS_DOCUMENT, $options); return $this->createQuery(self::QUERYTYPE_ANALYSIS_DOCUMENT, $options);
} }
/**
* Create a terms query instance
*
* @param mixed $options
* @return Solarium_Query_Terms
*/
public function createTerms($options = null)
{
return $this->createQuery(self::QUERYTYPE_TERMS, $options);
}
} }
...@@ -43,7 +43,7 @@ ...@@ -43,7 +43,7 @@
* @package Solarium * @package Solarium
* @subpackage Client * @subpackage Client
*/ */
class Solarium_Client_Adapter_CurlHttp extends Solarium_Client_Adapter class Solarium_Client_Adapter_Curl extends Solarium_Client_Adapter
{ {
/** /**
...@@ -129,7 +129,7 @@ class Solarium_Client_Adapter_CurlHttp extends Solarium_Client_Adapter ...@@ -129,7 +129,7 @@ class Solarium_Client_Adapter_CurlHttp extends Solarium_Client_Adapter
$data = $httpResponse; $data = $httpResponse;
$info = curl_getinfo($ch); $info = curl_getinfo($ch);
$headers = array(); $headers = array();
$headers[] = 'HTTP/1.1 ' . $info['http_code']; $headers[] = 'HTTP/1.1 ' . $info['http_code']. ' OK';
} }
return array($data, $headers); return array($data, $headers);
......
<?php
/**
* Copyright 2011 Bas de Nooijer.
* Copyright 2011 Gasol Wu. PIXNET Digital Media Corporation.
* 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.
*
* @copyright Copyright 2011 Bas de Nooijer <solarium@raspberry.nl>
* @copyright Copyright 2011 Gasol Wu <gasol.wu@gmail.com>
* @license http://github.com/basdenooijer/solarium/raw/master/COPYING
* @link http://www.solarium-project.org/
*
* @package Solarium
* @subpackage Client
*/
/**
* Build a Terms query request
*
* @package Solarium
* @subpackage Client
*/
class Solarium_Client_RequestBuilder_Terms extends Solarium_Client_RequestBuilder
{
/**
* Build request for a Terms query
*
* @param Solarium_Query_Terms $query
* @return Solarium_Client_Request
*/
public function build($query)
{
$request = new Solarium_Client_Request;
$request->setHandler($query->getHandler());
$request->addParam('terms', true);
$request->addParam('wt', 'json');
$request->addParam('terms.lower', $query->getLowerbound());
$request->addParam('terms.lower.incl', $query->getLowerboundInclude());
$request->addParam('terms.mincount', $query->getMinCount());
$request->addParam('terms.maxcount', $query->getMaxCount());
$request->addParam('terms.prefix', $query->getPrefix());
$request->addParam('terms.regex', $query->getRegex());
$request->addParam('terms.limit', $query->getLimit());
$request->addParam('terms.upper', $query->getUpperbound());
$request->addParam('terms.upper.incl', $query->getUpperboundInclude());
$request->addParam('terms.raw', $query->getRaw());
$request->addParam('terms.sort', $query->getSort());
$fields = explode(',', $query->getFields());
foreach ($fields as $field) {
$request->addParam('terms.fl', trim($field));
}
if ($query->getRegexFlags() !== null) {
$flags = explode(',', $query->getRegexFlags());
foreach ($flags as $flag) {
$request->addParam('terms.regex.flag', trim($flag));
}
}
return $request;
}
}
<?php
/**
* Copyright 2011 Gasol Wu. PIXNET Digital Media Corporation.
* 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.
*
* @copyright Copyright 2011 Gasol Wu <gasol.wu@gmail.com>
* @license http://github.com/basdenooijer/solarium/raw/master/COPYING
* @link http://www.solarium-project.org/
*
* @package Solarium
* @subpackage Client
*/
/**
* Parse MoreLikeThis response data
*
* @package Solarium
* @subpackage Client
*/
class Solarium_Client_ResponseParser_Terms extends Solarium_Client_ResponseParser
{
/**
* Get result data for the response
*
* @param Solarium_Result_Terms $result
* @return array
*/
public function parse($result)
{
$termResults = array();
$data = $result->getData();
$field = $result->getQuery()->getFields();
$fields = explode(',', $field);
foreach ($fields as $field) {
$field = trim($field);
if (isset($data['terms'][$field])) {
$terms = $data['terms'][$field];
for ($i = 0; $i < count($terms); $i += 2) {
$termResults[$field][$terms[$i]] = $terms[$i + 1];
}
}
}
return array(
'status' => $data['responseHeader']['status'],
'queryTime' => $data['responseHeader']['QTime'],
'results' => $termResults,
);
}
}
<?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.
*
* @copyright Copyright 2011 Bas de Nooijer <solarium@raspberry.nl>
* @license http://github.com/basdenooijer/solarium/raw/master/COPYING
* @link http://www.solarium-project.org/
*
* @package Solarium
*/
/**
* CustomizeRequest plugin
*
* You can use this plugin to customize the requests generated for Solarium queries by adding or overwriting
* params and/or headers.
*
* @package Solarium
* @subpackage Plugin
*/
class Solarium_Plugin_CustomizeRequest extends Solarium_Plugin_Abstract
{
/**
* Holds customizations added to this plugin
*
* @var array
*/
protected $_customizations = array();
/**
* Initialize options
*
* @return void
*/
protected function _init()
{
foreach ($this->_options AS $name => $value) {
switch ($name) {
case 'customization':
$this->addCustomizations($value);
break;
}
}
}
/**
* Create a Customization instance
*
* If you supply a string as the first arguments ($options) it will be used as the key for the Customization
* and it will be added to this plugin.
* If you supply an options array/object that contains a key the Customization will also be added to the plugin.
*
* When no key is supplied the Customization cannot be added, in that case you will need to add it manually
* after setting the key, by using the addCustomization method.
*
* @param mixed $options
* @return Solarium_Plugin_CustomizeRequest_Customization
*/
public function createCustomization($options = null)
{
if (is_string($options)) {
$fq = new Solarium_Plugin_CustomizeRequest_Customization;
$fq->setKey($options);
} else {
$fq = new Solarium_Plugin_CustomizeRequest_Customization($options);
}
if ($fq->getKey() !== null) {
$this->addCustomization($fq);
}
return $fq;
}
/**
* Add a customization
*
* Supports a Customization instance or a config array, in that case a new
* Customization instance wil be created based on the options.
*
* @param Solarium_Plugin_CustomizeRequest_Customization|array $customization
* @return Solarium_Plugin_CustomizeRequest Provides fluent interface
*/
public function addCustomization($customization)
{
if (is_array($customization)) {
$customization = new Solarium_Plugin_CustomizeRequest_Customization($customization);
}
$key = $customization->getKey();
if (0 === strlen($key)) {
throw new Solarium_Exception('A Customization must have a key value');
}
if (array_key_exists($key, $this->_customizations)) {
if ($this->_customizations[$key] === $customization) {
//double add calls for the same customization are ignored
//@todo add trigger_error with a notice?
} else {
throw new Solarium_Exception('A Customization must have a unique key value');
}
} else {
$this->_customizations[$key] = $customization;
}
return $this;
}
/**
* Add multiple Customizations
*
* @param array $customizations
* @return Solarium_Plugin_CustomizeRequest Provides fluent interface
*/
public function addCustomizations(array $customizations)
{
foreach ($customizations AS $key => $customization) {
// in case of a config array: add key to config
if (is_array($customization) && !isset($customization['key'])) {
$customization['key'] = $key;
}
$this->addCustomization($customization);
}
return $this;
}
/**
* Get a Customization
*
* @param string $key
* @return string
*/
public function getCustomization($key)
{
if (isset($this->_customizations[$key])) {
return $this->_customizations[$key];
} else {
return null;
}
}
/**
* Get all Customizations
*
* @return array
*/
public function getCustomizations()
{
return $this->_customizations;
}
/**
* Remove a single Customization
*
* You can remove a Customization by passing it's key, or by passing the Customization instance
*
* @param string|Solarium_Plugin_CustomizeRequest_Customization $customization
* @return Solarium_Plugin_CustomizeRequest Provides fluent interface
*/
public function removeCustomization($customization)
{
if (is_object($customization)) {
$customization = $customization->getKey();
}
if (isset($this->_customizations[$customization])) {
unset($this->_customizations[$customization]);
}
return $this;
}
/**
* Remove all Customizations
*
* @return Solarium_Plugin_CustomizeRequest Provides fluent interface
*/
public function clearCustomizations()
{
$this->_customizations = array();
return $this;
}
/**
* Set multiple Customizations
*
* This overwrites any existing Customizations
*
* @param array $customizations
*/
public function setCustomizations($customizations)
{
$this->clearCustomizations();
$this->addCustomizations($customizations);
}
/**
* Event hook to customize the request object
*
* @param Solarium_Query $query
* @param Solarium_Client_Request $request
* @return void
*/
public function postCreateRequest($query, $request)
{
foreach ($this->getCustomizations() as $key => $customization) {
// first validate
if (!$customization->isValid()) {
throw new Solarium_Exception('Request customization with key "' . $key . '" is invalid' );
}
// apply to request, depending on type
switch ($customization->getType()) {
case Solarium_Plugin_CustomizeRequest_Customization::TYPE_PARAM:
$request->addParam(
$customization->getName(),
$customization->getValue(),
$customization->getOverwrite()
);
break;
case Solarium_Plugin_CustomizeRequest_Customization::TYPE_HEADER:
$request->addHeader($customization->getName() . ': ' . $customization->getValue());
break;
}
// remove single-use customizations after use
if (!$customization->getPersistent()) {
$this->removeCustomization($key);
}
}
}
}
\ No newline at end of file
<?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.
*
* @copyright Copyright 2011 Bas de Nooijer <solarium@raspberry.nl>
* @license http://github.com/basdenooijer/solarium/raw/master/COPYING
* @link http://www.solarium-project.org/
*
* @package Solarium
*/
/**
* Customization value object
*
* @package Solarium
* @subpackage Plugin
*/
class Solarium_Plugin_CustomizeRequest_Customization extends Solarium_Configurable
{
/**
* Type definitions
*/
const TYPE_PARAM = 'param';
const TYPE_HEADER = 'header';
/**
* Default options
*
* @var array
*/
protected $_options = array(
'key' => null,
'type' => null,
'name' => null,
'value' => null,
'persistent' => false,
'overwrite' => true,
);
/**
* Set key value
*
* @param string $value
* @return Solarium_Plugin_CustomizeRequest_Customization
*/
public function setKey($value)
{
$this->_setOption('key', $value);
return $this;
}
/**
* Get key value
*
* @return string
*/
public function getKey()
{
return $this->getOption('key');
}
/**
* Set type value
*
* @param string $value
* @return Solarium_Plugin_CustomizeRequest_Customization
*/
public function setType($value)
{
$this->_setOption('type', $value);
return $this;
}
/**
* Get type value
*
* @return string
*/
public function getType()
{
return $this->getOption('type');
}
/**
* Set name value
*
* @param string $value
* @return Solarium_Plugin_CustomizeRequest_Customization
*/
public function setName($value)
{
$this->_setOption('name', $value);
return $this;
}
/**
* Get name value
*
* @return string
*/
public function getName()
{
return $this->getOption('name');
}
/**
* Set value
*
* @param string $value
* @return Solarium_Plugin_CustomizeRequest_Customization
*/
public function setValue($value)
{
$this->_setOption('value', $value);
return $this;
}
/**
* Get value
*
* @return string
*/
public function getValue()
{
return $this->getOption('value');
}
/**
* Set persistent on/off
*
* @param boolean $value
* @return Solarium_Plugin_CustomizeRequest_Customization
*/
public function setPersistent($value)
{
$this->_setOption('persistent', $value);
return $this;
}
/**
* Get persistent setting
*
* @return boolean
*/
public function getPersistent()
{
return $this->getOption('persistent');
}
/**
* Set overwrite option on/off
*
* @param boolean $value
* @return Solarium_Plugin_CustomizeRequest_Customization
*/
public function setOverwrite($value)
{
$this->_setOption('overwrite', $value);
return $this;
}
/**
* Get overwrite option value
*
* @return boolean
*/
public function getOverwrite()
{
return $this->getOption('overwrite');
}
/**
* Check for all mandatory settings
*
* @return bool
*/
public function isValid()
{
$type = $this->getType();
if ($type !== self::TYPE_PARAM && $type !== self::TYPE_HEADER) return false;
if (null == $this->getKey() || null == $this->getName() || null == $this->getValue()) return false;
return true;
}
}
\ No newline at end of file
<?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.
*
* @copyright Copyright 2011 Bas de Nooijer <solarium@raspberry.nl>
* @license http://github.com/basdenooijer/solarium/raw/master/COPYING
* @link http://www.solarium-project.org/
*
* @package Solarium
* @subpackage Query
*/
/**
* Terns query
*
* A terms query provides access to the indexed terms in a field and the number of documents that match each term.
* This can be useful for doing auto-suggest or other things that operate at the term level instead of the search
* or document level. Retrieving terms in index order is very fast since the implementation directly uses Lucene's
* TermEnum to iterate over the term dictionary.
*
* @package Solarium
* @subpackage Query
*/
class Solarium_Query_Terms extends Solarium_Query
{
/**
* Get type for this query
*
* @return string
*/
public function getType()
{
return Solarium_Client::QUERYTYPE_TERMS;
}
/**
* Default options
*
* @var array
*/
protected $_options = array(
'resultclass' => 'Solarium_Result_Terms',
'handler' => 'terms',
);
/**
* Set the field name(s) to get the terms from
*
* For multiple fields use a comma-separated string
*
* @param string $value
* @return self Provides fluent interface
*/
public function setFields($value)
{
return $this->_setOption('fields', $value);
}
/**
* Get the field name(s) to get the terms from
*
* @return string
*/
public function getFields()
{
return $this->getOption('fields');
}
/**
* Set the lowerbound term to start at
*
* @param string $value
* @return self Provides fluent interface
*/
public function setLowerbound($value)
{
return $this->_setOption('lowerbound', $value);
}
/**
* Get the lowerbound term to start at
*
* @return string
*/
public function getLowerbound()
{
return $this->getOption('lowerbound');
}
/**
* Set lowerboundinclude
*
* @param boolean $value
* @return self Provides fluent interface
*/
public function setLowerboundInclude($value)
{
return $this->_setOption('lowerboundinclude', $value);
}
/**
* Get lowerboundinclude
*
* @return boolean
*/
public function getLowerboundInclude()
{
return $this->getOption('lowerboundinclude');
}
/**
* Set mincount (the minimum doc frequency for terms in order to be included)
*
* @param integer $value
* @return self Provides fluent interface
*/
public function setMinCount($value)
{
return $this->_setOption('mincount', $value);
}
/**
* Get mincount
*
* @return integer
*/
public function getMinCount()
{
return $this->getOption('mincount');
}
/**
* Set maxcount (the maximum doc frequency for terms in order to be included)
*
* @param integer $value
* @return self Provides fluent interface
*/
public function setMaxCount($value)
{
return $this->_setOption('maxcount', $value);
}
/**
* Get maxcount
*
* @return integer
*/
public function getMaxCount()
{
return $this->getOption('maxcount');
}
/**
* Set prefix for terms
*
* @param string $value
* @return self Provides fluent interface
*/
public function setPrefix($value)
{
return $this->_setOption('prefix', $value);
}
/**
* Get maxcount
*
* @return string
*/
public function getPrefix()
{
return $this->getOption('prefix');
}
/**
* Set regex to restrict terms
*
* @param string $value
* @return self Provides fluent interface
*/
public function setRegex($value)
{
return $this->_setOption('regex', $value);
}
/**
* Get regex
*
* @return string
*/
public function getRegex()
{
return $this->getOption('regex');
}
/**
* Set regex flags
*
* Use a comma-separated string for multiple entries
*
* @param string $value
* @return self Provides fluent interface
*/
public function setRegexFlags($value)
{
return $this->_setOption('regexflags', $value);
}
/**
* Get regex flags
*
* @return string
*/
public function getRegexFlags()
{
return $this->getOption('regexflags');
}
/**
* Set limit
*
* If < 0 all terms are included
*
* @param integer $value
* @return self Provides fluent interface
*/
public function setLimit($value)
{
return $this->_setOption('limit', $value);
}
/**
* Get limit
*
* @return integer
*/
public function getLimit()
{
return $this->getOption('limit');
}
/**
* Set the upperbound term to start at
*
* @param string $value
* @return self Provides fluent interface
*/
public function setUpperbound($value)
{
return $this->_setOption('upperbound', $value);
}
/**
* Get the upperbound term to start at
*
* @return string
*/
public function getUpperbound()
{
return $this->getOption('upperbound');
}
/**
* Set upperboundinclude
*
* @param boolean $value
* @return self Provides fluent interface
*/
public function setUpperboundInclude($value)
{
return $this->_setOption('upperboundinclude', $value);
}
/**
* Get upperboundinclude
*
* @return boolean
*/
public function getUpperboundInclude()
{
return $this->getOption('upperboundinclude');
}
/**
* Set raw option
*
* @param boolean $value
* @return self Provides fluent interface
*/
public function setRaw($value)
{
return $this->_setOption('raw', $value);
}
/**
* Get raw option
*
* @return boolean
*/
public function getRaw()
{
return $this->getOption('raw');
}
/**
* Set sort option
*
* @param string $value
* @return self Provides fluent interface
*/
public function setSort($value)
{
return $this->_setOption('sort', $value);
}
/**
* Get sort option
*
* @return string
*/
public function getSort()
{
return $this->getOption('sort');
}
}
\ No newline at end of file
<?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.
*
* @copyright Copyright 2011 Bas de Nooijer <solarium@raspberry.nl>
* @license http://github.com/basdenooijer/solarium/raw/master/COPYING
* @link http://www.solarium-project.org/
*
* @package Solarium
* @subpackage Result
*/
/**
* Terms query result
*
* @package Solarium
* @subpackage Result
*/
class Solarium_Result_Terms extends Solarium_Result_QueryType implements IteratorAggregate, Countable
{
/**
* Status code returned by Solr
*
* @var int
*/
protected $_status;
/**
* Solr index queryTime
*
* This doesn't include things like the HTTP responsetime. Purely the Solr
* query execution time.
*
* @var int
*/
protected $_queryTime;
/**
* Term results
*
* @var array
*/
protected $_results;
/**
* Get Solr status code
*
* This is not the HTTP status code! The normal value for success is 0.
*
* @return int
*/
public function getStatus()
{
$this->_parseResponse();
return $this->_status;
}
/**
* Get Solr query time
*
* This doesn't include things like the HTTP responsetime. Purely the Solr
* query execution time.
*
* @return int
*/
public function getQueryTime()
{
$this->_parseResponse();
return $this->_queryTime;
}
/**
* Get all term results
*
* @return array
*/
public function getResults()
{
$this->_parseResponse();
return $this->_results;
}
/**
* Get term results for a specific field
*
* @param string $field
* @return array
*/
public function getTerms($field)
{
$this->_parseResponse();
if (isset($this->_results[$field])) {
return $this->_results[$field];
} else {
return array();
}
}
/**
* IteratorAggregate implementation
*
* @return ArrayIterator
*/
public function getIterator()
{
$this->_parseResponse();
return new ArrayIterator($this->_results);
}
/**
* Countable implementation
*
* @return int
*/
public function count()
{
$this->_parseResponse();
return count($this->_results);
}
}
\ No newline at end of file
<?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_Adapter_CurlTest extends PHPUnit_Framework_TestCase
{
/**
* @var Solarium_Client_Adapter_Curl
*/
protected $_adapter;
public function setUp()
{
if (!function_exists('curl_init')) {
$this->markTestSkipped('Curl not available, skipping Curl adapter tests');
}
$this->_adapter = new Solarium_Client_Adapter_Curl();
}
public function testCheck()
{
$data = 'data';
$headers = array('X-dummy: data');
// this should be ok, no exception
$this->_adapter->check($data, $headers);
$data = '';
$headers = array();
$this->setExpectedException('Solarium_Exception');
$this->_adapter->check($data, $headers);
}
public function testExecute()
{
$headers = array('HTTP/1.0 200 OK');
$body = 'data';
$data = array($body, $headers);
$request = new Solarium_Client_Request();
$mock = $this->getMock('Solarium_Client_Adapter_Curl', array('_getData'));
$mock->expects($this->once())
->method('_getData')
->with($request)
->will($this->returnValue($data));
$response = $mock->execute($request);
$this->assertEquals($body,$response->getBody());
$this->assertEquals($headers,$response->getHeaders());
}
}
\ No newline at end of file
<?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_RequestBuilder_TermsTest extends PHPUnit_Framework_TestCase
{
/**
* @var Solarium_Query_Terms
*/
protected $_query;
/**
* @var Solarium_Client_RequestBuilder_Terms
*/
protected $_builder;
public function setUp()
{
$this->_query = new Solarium_Query_Terms;
$this->_builder = new Solarium_Client_RequestBuilder_Terms;
}
public function testBuildParams()
{
$this->_query->setFields('fieldA, fieldB');
$this->_query->setLowerbound('d');
$this->_query->setLowerboundInclude(true);
$this->_query->setMinCount(3);
$this->_query->setMaxCount(100);
$this->_query->setPrefix('de');
$this->_query->setRegex('det.*');
$this->_query->setRegexFlags('case_insensitive,comments');
$this->_query->setLimit(50);
$this->_query->setUpperbound('x');
$this->_query->setUpperboundInclude(false);
$this->_query->setRaw(false);
$this->_query->setSort('index');
$request = $this->_builder->build($this->_query);
$this->assertEquals(
array(
'terms' => 'true',
'terms.fl' => array(
'fieldA',
'fieldB',
),
'terms.limit' => 50,
'terms.lower' => 'd',
'terms.lower.incl' => 'true',
'terms.maxcount' => 100,
'terms.mincount' => 3,
'terms.prefix' => 'de',
'terms.raw' => 'false',
'terms.regex' => 'det.*',
'terms.regex.flag' => array(
'case_insensitive',
'comments',
),
'terms.sort' => 'index',
'terms.upper' => 'x',
'terms.upper.incl' => 'false',
'wt' => 'json',
),
$request->getParams()
);
$this->assertEquals(
Solarium_Client_Request::METHOD_GET,
$request->getMethod()
);
}
}
\ No newline at end of file
<?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_ResponseParser_TermsTest extends PHPUnit_Framework_TestCase
{
public function testParse()
{
$data = array(
'responseHeader' => array(
'status' => 1,
'QTime' => 13,
),
'terms' => array(
'fieldA' => array(
'term1',
5,
'term2',
3
),
'fieldB' => array(
'term3',
6,
'term4',
2
)
),
);
$query = new Solarium_Query_Terms();
$query->setFields('fieldA, fieldB');
$resultStub = $this->getMock('Solarium_Result_Terms', array(), array(), '', false);
$resultStub->expects($this->any())
->method('getData')
->will($this->returnValue($data));
$resultStub->expects($this->any())
->method('getQuery')
->will($this->returnValue($query));
$parser = new Solarium_Client_ResponseParser_Terms;
$result = $parser->parse($resultStub);
$expected = array(
'fieldA' => array(
'term1' => 5,
'term2' => 3,
),
'fieldB' => array(
'term3' => 6,
'term4' => 2,
)
);
$this->assertEquals($expected, $result['results']);
$this->assertEquals(2, count($result['results']));
}
}
...@@ -644,6 +644,18 @@ class Solarium_ClientTest extends PHPUnit_Framework_TestCase ...@@ -644,6 +644,18 @@ class Solarium_ClientTest extends PHPUnit_Framework_TestCase
$observer->analyze($query); $observer->analyze($query);
} }
public function testTerms()
{
$query = new Solarium_Query_Terms();
$observer = $this->getMock('Solarium_Client', array('execute'));
$observer->expects($this->once())
->method('execute')
->with($this->equalTo($query));
$observer->terms($query);
}
public function testCreateQuery() public function testCreateQuery()
{ {
$options = array('optionA' => 1, 'optionB' => 2); $options = array('optionA' => 1, 'optionB' => 2);
...@@ -788,6 +800,18 @@ class Solarium_ClientTest extends PHPUnit_Framework_TestCase ...@@ -788,6 +800,18 @@ class Solarium_ClientTest extends PHPUnit_Framework_TestCase
$observer->createAnalysisDocument($options); $observer->createAnalysisDocument($options);
} }
public function testCreateTerms()
{
$options = array('optionA' => 1, 'optionB' => 2);
$observer = $this->getMock('Solarium_Client', array('createQuery'));
$observer->expects($this->once())
->method('createQuery')
->with($this->equalTo(Solarium_Client::QUERYTYPE_TERMS), $this->equalTo($options));
$observer->createTerms($options);
}
public function testTriggerEvent() public function testTriggerEvent()
{ {
$eventName = 'Test'; $eventName = 'Test';
......
<?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_Plugin_CustomizeRequest_CustomizationTest extends PHPUnit_Framework_TestCase
{
/**
* @var Solarium_Plugin_CustomizeRequest_Customization
*/
protected $_instance;
public function setUp()
{
$this->_instance = new Solarium_Plugin_CustomizeRequest_Customization();
}
public function testSetAndGetKey()
{
$value = 'mykey';
$this->_instance->setKey($value);
$this->assertEquals($value, $this->_instance->getKey());
}
public function testSetAndGetName()
{
$value = 'myname';
$this->_instance->setName($value);
$this->assertEquals($value, $this->_instance->getName());
}
public function testSetAndGetType()
{
$value = 'mytype';
$this->_instance->setType($value);
$this->assertEquals($value, $this->_instance->getType());
}
public function testSetAndGetValue()
{
$value = 'myvalue';
$this->_instance->setValue($value);
$this->assertEquals($value, $this->_instance->getValue());
}
public function testSetAndGetPersistence()
{
$value = true;
$this->_instance->setPersistent($value);
$this->assertEquals($value, $this->_instance->getPersistent());
}
public function testSetAndGetOverwrite()
{
$value = false;
$this->_instance->setOverwrite($value);
$this->assertEquals($value, $this->_instance->getOverwrite());
}
public function testIsValid()
{
$this->_instance->setKey('mykey');
$this->_instance->setType('param');
$this->_instance->setName('myname');
$this->_instance->setValue('myvalue');
$this->assertTrue($this->_instance->isValid());
}
public function testIsValidWithInvalidType()
{
$this->_instance->setKey('mykey');
$this->_instance->setType('mytype');
$this->_instance->setName('myname');
$this->_instance->setValue('myvalue');
$this->assertFalse($this->_instance->isValid());
}
public function testIsValidWithMissingValue()
{
$this->_instance->setKey('mykey');
$this->_instance->setType('param');
$this->_instance->setName('myname');
$this->assertFalse($this->_instance->isValid());
}
}
\ No newline at end of file
This diff is collapsed.
<?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_Query_TermsTest extends PHPUnit_Framework_TestCase
{
/**
* @var Solarium_Query_Terms
*/
protected $_query;
public function setUp()
{
$this->_query = new Solarium_Query_Terms;
}
public function testGetType()
{
$this->assertEquals(Solarium_Client::QUERYTYPE_TERMS, $this->_query->getType());
}
public function testSetAndGetFields()
{
$this->_query->setFields('fieldA,fieldB');
$this->assertEquals('fieldA,fieldB', $this->_query->getFields());
}
public function testSetAndGetLowerbound()
{
$this->_query->setLowerbound('f');
$this->assertEquals('f', $this->_query->getLowerbound());
}
public function testSetAndGetLowerboundInclude()
{
$this->_query->setLowerboundInclude(true);
$this->assertEquals(true, $this->_query->getLowerboundInclude());
}
public function testSetAndGetMinCount()
{
$this->_query->setMinCount(3);
$this->assertEquals(3, $this->_query->getMinCount());
}
public function testSetAndGetMaxCount()
{
$this->_query->setMaxCount(25);
$this->assertEquals(25, $this->_query->getMaxCount());
}
public function testSetAndGetPrefix()
{
$this->_query->setPrefix('wat');
$this->assertEquals('wat', $this->_query->getPrefix());
}
public function testSetAndGetRegex()
{
$this->_query->setRegex('at.*');
$this->assertEquals('at.*', $this->_query->getRegex());
}
public function testSetAndGetRegexFlags()
{
$this->_query->setRegexFlags('case_insensitive,comments');
$this->assertEquals('case_insensitive,comments', $this->_query->getRegexFlags());
}
public function testSetAndGetLimit()
{
$this->_query->setLimit(15);
$this->assertEquals(15, $this->_query->getLimit());
}
public function testSetAndGetUpperbound()
{
$this->_query->setUpperbound('x');
$this->assertEquals('x', $this->_query->getUpperbound());
}
public function testSetAndGetUpperboundInclude()
{
$this->_query->setUpperboundInclude(true);
$this->assertEquals(true, $this->_query->getUpperboundInclude());
}
public function testSetAndGetRaw()
{
$this->_query->setRaw(false);
$this->assertEquals(false, $this->_query->getRaw());
}
public function testSetAndGetSort()
{
$this->_query->setSort('index');
$this->assertEquals('index', $this->_query->getSort());
}
}
\ No newline at end of file
<?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_TermsTest extends PHPUnit_Framework_TestCase
{
/**
* @var Solarium_Result_Terms
*/
protected $_result;
/**
* @var array
*/
protected $_data;
public function setUp()
{
$this->_data = array(
'fieldA' => array(
'term1',
11,
'term2',
5,
'term3',
2,
),
'fieldB' => array(
'term4',
4,
'term5',
1,
)
);
$this->_result = new Solarium_Result_TermsDummy($this->_data);
}
public function testGetStatus()
{
$this->assertEquals(
1,
$this->_result->getStatus()
);
}
public function testGetQueryTime()
{
$this->assertEquals(
12,
$this->_result->getQueryTime()
);
}
public function testGetResults()
{
$this->assertEquals($this->_data, $this->_result->getResults());
}
public function testGetTerms()
{
$this->assertEquals($this->_data['fieldA'], $this->_result->getTerms('fieldA'));
}
public function testGetTermsWithInvalidFieldName()
{
$this->assertEquals(array(), $this->_result->getTerms('fieldX'));
}
public function testCount()
{
$this->assertEquals(count($this->_data), count($this->_result));
}
public function testIterator()
{
$results = array();
foreach($this->_result AS $key => $doc)
{
$results[$key] = $doc;
}
$this->assertEquals($this->_data, $results);
}
}
class Solarium_Result_TermsDummy extends Solarium_Result_Terms
{
protected $_parsed = true;
public function __construct($results)
{
$this->_results = $results;
$this->_status = 1;
$this->_queryTime = 12;
}
}
\ No newline at end of file
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