diff --git a/module/VuFindSearch/src/VuFindSearch/Service.php b/module/VuFindSearch/src/VuFindSearch/Service.php index 905ec0b89a62d80cca3252032ac3e69e1c993286..848684a705e294e8def71dd42dcfab33bfe41e7c 100644 --- a/module/VuFindSearch/src/VuFindSearch/Service.php +++ b/module/VuFindSearch/src/VuFindSearch/Service.php @@ -289,12 +289,15 @@ class Service $params = $params ?: new ParamBag(); $context = __FUNCTION__; $args = compact('backend', 'id', 'params', 'context'); - $backend = $this->resolve($backend, $args); - $args['backend_instance'] = $backend; + $backendInstance = $this->resolve($backend, $args); + $args['backend_instance'] = $backendInstance; - $this->triggerPre($backend, $args); + $this->triggerPre($backendInstance, $args); try { - $response = $backend->similar($id, $params); + if (!($backendInstance instanceof Feature\SimilarInterface)) { + throw new BackendException("$backend does not support similar()"); + } + $response = $backendInstance->similar($id, $params); } catch (BackendException $e) { $this->triggerError($e, $args); throw $e; diff --git a/module/VuFindSearch/tests/unit-tests/src/VuFindTest/SearchServiceTest.php b/module/VuFindSearch/tests/unit-tests/src/VuFindTest/SearchServiceTest.php index 17b817d42dde25aacfc392479802c80510c387b6..cb4bcdb55490f36fe3c766e13a8ed8fc72493a46 100644 --- a/module/VuFindSearch/tests/unit-tests/src/VuFindTest/SearchServiceTest.php +++ b/module/VuFindSearch/tests/unit-tests/src/VuFindTest/SearchServiceTest.php @@ -35,6 +35,7 @@ use VuFindSearch\Backend\BackendInterface; use VuFindSearch\Backend\Exception\BackendException; use VuFindSearch\Feature\RetrieveBatchInterface; use VuFindSearch\Feature\RandomInterface; +use VuFindSearch\Feature\SimilarInterface; use VuFindSearch\Query\Query; use VuFindSearch\Response\AbstractRecordCollection; @@ -585,6 +586,20 @@ class SearchServiceTest extends TestCase unset($this->backend); } + /** + * Test similar action on bad backend. + * + * @return void + * @expectedException VuFindSearch\Backend\Exception\BackendException + * @expectedExceptionMessage foo does not support similar() + */ + public function testSimilarOnNonSupportingBackend() + { + $service = $this->getService(); + $params = new ParamBag(array('x' => 'y')); + $service->similar('foo', 'bar', $params); + } + /** * Test exception-throwing similar action. * @@ -690,9 +705,8 @@ abstract class TestClassForRetrieveBatchInterface * Stub class to test similar. */ abstract class TestBackendClassForSimilar - implements BackendInterface + implements BackendInterface, SimilarInterface { - abstract function similar(); } /**