Skip to content
Snippets Groups Projects
Commit 507e0702 authored by Luke O'Sullivan's avatar Luke O'Sullivan Committed by Demian Katz
Browse files

Added missing error check; expanded tests.

parent 7bd229ee
No related merge requests found
...@@ -57,13 +57,173 @@ class RandomRecommendTest extends TestCase ...@@ -57,13 +57,173 @@ class RandomRecommendTest extends TestCase
} }
/** /**
* Test load * Test settings
*
* @return void
*/
public function testCanSetSettings()
{
//[backend]:[limit]:[display mode]:[random mode]:[minimumset]:[facet1]:[facetvalue1]
$this->recommend->setConfig("Solr:10:mixed:disregard:20:facet1:value1:facet2:value2");
$this->assertEquals(
"Solr", \PHPUnit_Framework_Assert::readAttribute(
$this->recommend, 'backend'
)
);
$this->assertEquals(
"10", \PHPUnit_Framework_Assert::readAttribute(
$this->recommend, 'limit'
)
);
$this->assertEquals(
"mixed", \PHPUnit_Framework_Assert::readAttribute(
$this->recommend, 'displayMode'
)
);
$this->assertEquals(
"disregard", \PHPUnit_Framework_Assert::readAttribute(
$this->recommend, 'mode'
)
);
$this->assertEquals(
"20", \PHPUnit_Framework_Assert::readAttribute(
$this->recommend, 'minimum'
)
);
$filters = \PHPUnit_Framework_Assert::readAttribute($this->recommend, 'filters');
$this->assertInternalType("array", $filters);
$this->assertCount(2, $filters);
$this->assertEquals("facet1:value1", $filters[0]);
$this->assertEquals("facet2:value2", $filters[1]);
}
/**
* Test initialisation
*
* @return void
*/
public function testCanInitialise()
{
$service = $this->getMock('VuFindSearch\Service');
$paramManager = $this->getMock('VuFind\Search\Params\PluginManager');
$recommend = new Random($service, $paramManager);
// Use Solr since some Base components are abstract:
$params = $this->getServiceManager()->get('VuFind\SearchParamsPluginManager')
->get('Solr');
$query = $this->getFixture('query');
$params->setBasicSearch($query->getString(), $query->getHandler());
$request = $this->getMock('\Zend\StdLib\Parameters');
$service->expects($this->once())->method('random')
->with(
$this->equalTo("Solr"),
$this->equalTo($params->getQuery()),
$this->equalTo(10)
)->will($this->returnValue($this->getMock('VuFindTest\Recommend\TestSearchResultsForRandom')));
$recommend->setConfig("Solr:10:mixed:retain:20:facet1:value1:facet2:value2");
$recommend->init($params, $request);
}
/**
* Test initialisation
*
* @return void
*/
public function testCanInitialiseInDisregardMode()
{
$service = $this->getMock('VuFindSearch\Service');
$paramManager = $this->getMock('VuFind\Search\Params\PluginManager');
$recommend = new Random($service, $paramManager);
$paramManager->expects($this->once())->method('get')
->with($this->equalTo("Solr"))
->will($this->returnValue(
$this->getServiceManager()->get('VuFind\SearchParamsPluginManager')
->get('Solr')
)
);
// Use Solr since some Base components are abstract:
$params = $this->getServiceManager()->get('VuFind\SearchParamsPluginManager')
->get('Solr');
$query = $this->getFixture('query');
$params->setBasicSearch($query->getString(), $query->getHandler());
$request = $this->getMock('\Zend\StdLib\Parameters');
$service->expects($this->once())->method('random')
->with($this->equalTo("Solr"))
->will($this->returnValue($this->getMock('VuFindTest\Recommend\TestSearchResultsForRandom')));
$recommend->setConfig("Solr:10:mixed:disregard:20:facet1:value1:facet2:value2");
$recommend->init($params, $request);
}
/**
* Test initialisation
*
* @return void
*/
public function testWillReturnEmptyForMinimumResultLimit()
{
$service = $this->getMock('VuFindSearch\Service');
$paramManager = $this->getMock('VuFind\Search\Params\PluginManager');
$recommend = new Random($service, $paramManager);
$records = array("1", "2", "3", "4", "5");
// Use Solr since some Base components are abstract:
$params = $this->getServiceManager()->get('VuFind\SearchParamsPluginManager')
->get('Solr');
$query = $this->getFixture('query');
$params->setBasicSearch($query->getString(), $query->getHandler());
$request = $this->getMock('\Zend\StdLib\Parameters');
$results = $this->getMock('VuFindTest\Recommend\TestSearchResultsForRandom');
$results->expects($this->once())->method('getRecords')
->will($this->returnValue($records));
$service->expects($this->once())->method('random')
->with(
$this->equalTo("Solr"),
$this->equalTo($params->getQuery()),
$this->equalTo(10)
)->will($this->returnValue($results));
$recommend->setConfig("Solr:10:mixed:retain:20:facet1:value1:facet2:value2");
$recommend->init($params, $request);
$output = $recommend->getResults();
$this->assertEmpty($output);
}
/**
* Test displaymode
* *
* @return void * @return void
*/ */
public function testCanSetDisplayMode() public function testCanSetDisplayMode()
{ {
$this->recommend->setConfig("Solr:10:disregard"); $this->recommend->setConfig("Solr:10:mixed");
$this->assertEquals("disregard", $this->recommend->getDisplayMode()); $this->assertEquals("mixed", $this->recommend->getDisplayMode());
}
/**
* Get a fixture object
*
* @return mixed
*/
protected function getFixture($file)
{
$fixturePath = realpath(__DIR__ . '/../../../../fixtures/searches/basic') . '/';
return unserialize(file_get_contents($fixturePath . $file));
} }
} }
/**
* Stub class to test random.
*/
abstract class TestSearchResultsForRandom
{
abstract function getRecords();
}
...@@ -256,9 +256,14 @@ class Service ...@@ -256,9 +256,14 @@ class Service
$nextIndex = rand(0, $total_records - 1); $nextIndex = rand(0, $total_records - 1);
} }
$retrievedIndexes[] = $nextIndex; $retrievedIndexes[] = $nextIndex;
$currentBatch = $backend->search( try {
$query, $nextIndex, 1, $params $currentBatch = $backend->search(
); $query, $nextIndex, 1, $params
);
} catch (BackendException $e) {
$this->triggerError($e, $args);
throw $e;
}
if (!$response) { if (!$response) {
$response = $currentBatch; $response = $currentBatch;
} else if ($record = $currentBatch->first()) { } else if ($record = $currentBatch->first()) {
......
...@@ -452,7 +452,7 @@ class SearchServiceTest extends TestCase ...@@ -452,7 +452,7 @@ class SearchServiceTest extends TestCase
* @expectedException VuFindSearch\Backend\Exception\BackendException * @expectedException VuFindSearch\Backend\Exception\BackendException
* @expectedExceptionMessage test * @expectedExceptionMessage test
*/ */
public function testRandomNoInterfaceWithException() public function testRandomNoInterfaceWithExceptionAtFirstSearch()
{ {
$service = $this->getService(); $service = $this->getService();
$backend = $this->getBackend(); $backend = $this->getBackend();
...@@ -460,6 +460,7 @@ class SearchServiceTest extends TestCase ...@@ -460,6 +460,7 @@ class SearchServiceTest extends TestCase
$params = new ParamBag(array('x' => 'y')); $params = new ParamBag(array('x' => 'y'));
$query = new Query('test'); $query = new Query('test');
// Exception at first search
$backend->expects($this->once())->method('search') $backend->expects($this->once())->method('search')
->will($this->throwException($exception)); ->will($this->throwException($exception));
$em = $service->getEventManager(); $em = $service->getEventManager();
...@@ -470,6 +471,92 @@ class SearchServiceTest extends TestCase ...@@ -470,6 +471,92 @@ class SearchServiceTest extends TestCase
$service->random('foo', $query, "10", $params); $service->random('foo', $query, "10", $params);
} }
/**
* Test random (without RandomInterface) exception at item retrieval search.
*
* @return void
* @expectedException VuFindSearch\Backend\Exception\BackendException
* @expectedExceptionMessage test
*/
public function testRandomNoInterfaceWithExceptionAtItemSearch()
{
$limit = 10;
$total = 20;
$service = $this->getService();
$backend = $this->getBackend();
$responseForZero = $this->getRecordCollection();
$exception = new BackendException('test');
$params = new ParamBag(array('x' => 'y'));
$query = new Query('test');
// First Search Grabs 0 records but uses get total method
$backend->expects($this->at(0))->method('search')
->with(
$this->equalTo($query),
$this->equalTo("0"),
$this->equalTo("0"),
$this->equalTo($params)
)->will($this->returnValue($responseForZero));
$responseForZero->expects($this->once())->method('getTotal')
->will($this->returnValue($total));
// Exception at item search
$backend->expects($this->at(1))->method('search')
->will($this->throwException($exception));
$em = $service->getEventManager();
$em->expects($this->at(0))->method('trigger')
->with($this->equalTo('pre'), $this->equalTo($backend));
$em->expects($this->at(1))->method('trigger')
->with($this->equalTo('error'), $this->equalTo($exception));
$service->random('foo', $query, "10", $params);
}
/**
* Test random (without RandomInterface) exception with less results than limit.
*
* @return void
* @expectedException VuFindSearch\Backend\Exception\BackendException
* @expectedExceptionMessage test
*/
public function testRandomNoInterfaceExceptionWithLessResultsThanLimit()
{
$limit = 10;
$total = 5;
$service = $this->getService();
$backend = $this->getBackend();
$responseForZero = $this->getRecordCollection();
$response = $this->getRecordCollection();
$exception = new BackendException('test');
$params = new ParamBag(array('x' => 'y'));
$query = new Query('test');
// First Search Grabs 0 records but uses get total method
$backend->expects($this->at(0))->method('search')
->with(
$this->equalTo($query),
$this->equalTo("0"),
$this->equalTo("0"),
$this->equalTo($params)
)->will($this->returnValue($responseForZero));
$responseForZero->expects($this->once())->method('getTotal')
->will($this->returnValue($total));
// Second search grabs all the records
$backend->expects($this->at(1))->method('search')
->will($this->throwException($exception));
$em = $service->getEventManager();
$em->expects($this->at(0))->method('trigger')
->with($this->equalTo('pre'), $this->equalTo($backend));
$em->expects($this->at(1))->method('trigger')
->with($this->equalTo('error'), $this->equalTo($exception));
$service->random('foo', $query, $limit, $params);
}
/** /**
* Test similar action. * Test similar action.
* *
......
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