Skip to content
Snippets Groups Projects
Commit 05f50fb8 authored by David Maus's avatar David Maus
Browse files

New class: Abstract base class of handler maps

* VuFindSearch/Backend/AbstractHandlerMap.php: New class. Abstract
  base class of handler maps.
parent 1e8490b4
No related merge requests found
<?php
/**
* Base class for search backend handler maps.
*
* PHP version 5
*
* Copyright (C) Villanova University 2010.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* @category VuFind2
* @package Search
* @author David Maus <maus@hab.de>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link http://vufind.org
*/
namespace VuFindSearch\Backend;
use VuFindSearch\ParamBag;
/**
* Base class for search backend handler maps.
*
* The handler map maps search functions to parameterizable backend request
* handlers. The base class implements the parameter preparation method which
* applies query defaults, appends, and invariants to an existing set of
* parameters.
*
* @category VuFind2
* @package Search
* @author David Maus <maus@hab.de>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link http://vufind.org
*/
abstract class AbstractHandlerMap
{
/**
* Prepare final set of parameters for search function.
*
* Applies the query defaults, appends, and invariants.
*
* The concept of defaults, appends, and invariants follows SOLR with
* regards to the order of the application process: Invariants come last
* and overwrite runtime parameters, defaults, and appends.
*
* @param string $function Name of search function
* @param ParamBag $params Parameters
*
* @return void
*/
final public function prepare($function, ParamBag $params)
{
$final = $params->getArrayCopy();
$defaults = $this->getDefaults($function);
$invariants = $this->getInvariants($function);
$appends = $this->getAppends($function);
$final = array_replace($defaults, $final);
$final = array_merge_recursive($final, $appends);
$final = array_replace($final, $invariants);
$params->exchangeArray($final);
}
/**
* Return query invariants for search function.
*
* @param string $function Name of search function
*
* @return array Query invariants
*/
abstract protected function getInvariants($function);
/**
* Return query defaults for search function.
*
* @param string $function Name of search function
*
* @return array Query defaults
*/
abstract protected function getDefaults($function);
/**
* Return query appends for search function.
*
* @param string $function Name of search function
*
* @return array Query appends
*/
abstract protected function getAppends($function);
}
\ No newline at end of file
<?php
/**
* Unit tests for handler map base class.
*
* PHP version 5
*
* Copyright (C) Villanova University 2010.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* @category VuFind2
* @package Search
* @author David Maus <maus@hab.de>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link http://vufind.org
*/
namespace VuFindTest\Backend;
use VuFindSearch\Backend\AbstractHandlerMap;
use VuFindSearch\ParamBag;
use PHPUnit_Framework_TestCase as TestCase;
/**
* Unit tests for handler map base class.
*
* @category VuFind2
* @package Search
* @author David Maus <maus@hab.de>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link http://vufind.org
*/
class AbstractHandlerMapTest extends TestCase
{
/**
* Test parameter preparation, defaults.
*
* @return void
*/
public function testPrepareDefaults()
{
$map = $this->getMockForAbstractClass('VuFindSearch\Backend\AbstractHandlerMap');
$map->expects($this->once())
->method('getDefaults')
->will($this->returnValue(array('p1' => array('default'), 'p2' => array('default'))));
$map->expects($this->once())
->method('getAppends')
->will($this->returnValue(array()));
$map->expects($this->once())
->method('getInvariants')
->will($this->returnValue(array()));
$params = new ParamBag(array('p2' => array('non-default')));
$map->prepare('f', $params);
$this->assertTrue($params->contains('p1', 'default'));
$this->assertFalse($params->contains('p2', 'default'));
$this->assertTrue($params->contains('p2', 'non-default'));
}
/**
* Test parameter preparation, appends.
*
* @return void
*/
public function testPrepareAppends()
{
$map = $this->getMockForAbstractClass('VuFindSearch\Backend\AbstractHandlerMap');
$map->expects($this->once())
->method('getDefaults')
->will($this->returnValue(array()));
$map->expects($this->once())
->method('getAppends')
->will($this->returnValue(array('p1' => 'append')));
$map->expects($this->once())
->method('getInvariants')
->will($this->returnValue(array()));
$params = new ParamBag(array('p1' => array('something')));
$map->prepare('f', $params);
$this->assertTrue($params->contains('p1', 'something'));
$this->assertTrue($params->contains('p1', 'append'));
}
/**
* Test parameter preparation, invariants.
*
* @return void
*/
public function testPrepareInvariants()
{
$map = $this->getMockForAbstractClass('VuFindSearch\Backend\AbstractHandlerMap');
$map->expects($this->once())
->method('getDefaults')
->will($this->returnValue(array()));
$map->expects($this->once())
->method('getAppends')
->will($this->returnValue(array('p1' => array('append'))));
$map->expects($this->once())
->method('getInvariants')
->will($this->returnValue(array('p1' => array('invariant'))));
$params = new ParamBag(array('p1' => array('something')));
$map->prepare('f', $params);
$this->assertFalse($params->contains('p1', 'something'));
$this->assertFalse($params->contains('p1', 'append'));
$this->assertTrue($params->contains('p1', 'invariant'));
}
}
\ No newline at end of file
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