Skip to content

Commit

Permalink
Reduce copy and paste in tests. (#3008)
Browse files Browse the repository at this point in the history
  • Loading branch information
demiankatz committed Jul 28, 2023
1 parent fd07436 commit 350a743
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 228 deletions.
Expand Up @@ -703,16 +703,13 @@ public function testGetProxiedUsers(): void
}

/**
* Test getHolding with HRID-based lookup
* Get expected result of get-holding fixture (shared by multiple tests).
*
* @return void
* @return array
*/
public function testGetHoldingWithHridLookup(): void
protected function getExpectedGetHoldingResult(): array
{
$driverConfig = $this->defaultDriverConfig;
$driverConfig['IDs']['type'] = 'hrid';
$this->createConnector("get-holding", $driverConfig);
$expected = [
return [
[
'callnumber_prefix' => '',
'callnumber' => 'PS2394 .M643 1883',
Expand All @@ -737,7 +734,19 @@ public function testGetHoldingWithHridLookup(): void
'addLink' => true,
],
];
$this->assertEquals($expected, $this->driver->getHolding("foo"));
}

/**
* Test getHolding with HRID-based lookup
*
* @return void
*/
public function testGetHoldingWithHridLookup(): void
{
$driverConfig = $this->defaultDriverConfig;
$driverConfig['IDs']['type'] = 'hrid';
$this->createConnector("get-holding", $driverConfig);
$this->assertEquals($this->getExpectedGetHoldingResult(), $this->driver->getHolding("foo"));
}

/**
Expand All @@ -752,34 +761,7 @@ public function testGetStatuses(): void
$driverConfig = $this->defaultDriverConfig;
$driverConfig['IDs']['type'] = 'hrid';
$this->createConnector("get-holding", $driverConfig);
$expected = [
[
[
'callnumber_prefix' => '',
'callnumber' => 'PS2394 .M643 1883',
'id' => 'foo',
'item_id' => 'itemid',
'holding_id' => 'holdingid',
'number' => 1,
'enumchron' => '',
'barcode' => 'barcode-test',
'status' => 'Available',
'duedate' => '',
'availability' => true,
'is_holdable' => true,
'holdings_notes' => null,
'item_notes' => null,
'summary' => ["foo", "bar baz"],
'supplements' => [],
'indexes' => [],
'location' => 'Special Collections',
'location_code' => 'DCOC',
'reserve' => 'TODO',
'addLink' => true,
],
],
];
$this->assertEquals($expected, $this->driver->getStatuses(["foo"]));
$this->assertEquals([$this->getExpectedGetHoldingResult()], $this->driver->getStatuses(["foo"]));
}

/**
Expand Down
Expand Up @@ -229,11 +229,13 @@ public function testCanInitialiseInDisregardMode()
}

/**
* Test minimum result limit feature
* Get a module configured to return results.
*
* @return void
* @param string $recConfig Recommendation module configuration
*
* @return Random
*/
public function testWillReturnEmptyForMinimumResultLimit()
protected function getConfiguredModule($recConfig): Random
{
$service = $this->getMockBuilder(\VuFindSearch\Service::class)
->disableOriginalConstructor()->getMock();
Expand Down Expand Up @@ -273,9 +275,20 @@ public function testWillReturnEmptyForMinimumResultLimit()
->with($this->callback($checkCommand))
->will($this->returnValue($commandObj));

$recommend->setConfig("Solr:10:mixed:retain:20:facet1:value1:facet2:value2");
$recommend->setConfig($recConfig);
$recommend->init($params, $request);
$recommend->process($results);
return $recommend;
}

/**
* Test minimum result limit feature
*
* @return void
*/
public function testWillReturnEmptyForMinimumResultLimit()
{
$recommend = $this->getConfiguredModule("Solr:10:mixed:retain:20:facet1:value1:facet2:value2");
$output = $recommend->getResults();
$this->assertEmpty($output);
}
Expand All @@ -287,49 +300,9 @@ public function testWillReturnEmptyForMinimumResultLimit()
*/
public function testWillReturnResults()
{
$service = $this->getMockBuilder(\VuFindSearch\Service::class)
->disableOriginalConstructor()->getMock();
$paramManager = $this->createMock(\VuFind\Search\Params\PluginManager::class);
$recommend = new Random($service, $paramManager);
$records = ["1", "2", "3", "4", "5"];

// Use Solr since some Base components are abstract:
$results = $this->getSolrResults();
$params = $results->getParams();
$query = $this->unserializeFixture('query');
$params->setBasicSearch($query->getString(), $query->getHandler());
$request = $this->createMock(\Laminas\Stdlib\Parameters::class);

$results = $this->getMockBuilder(\VuFindSearch\Response\RecordCollectionInterface::class)
->getMock();

$results->expects($this->once())->method('getRecords')
->will($this->returnValue($records));

$commandObj = $this->getMockBuilder(\VuFindSearch\Command\AbstractBase::class)
->disableOriginalConstructor()
->getMock();
$commandObj->expects($this->once())->method('getResult')
->will($this->returnValue($results));

$checkCommand = function ($command) {
return $command::class === \VuFindSearch\Command\RandomCommand::class
&& $command->getTargetIdentifier() === "Solr"
&& $command->getArguments()[0]->getAllTerms() === "john smith"
&& $command->getArguments()[1] === 10
&& $command->getArguments()[2]->getArrayCopy() === ['spellcheck' => ['true'],
'fq' => ['facet1:"value1"',
'facet2:"value2"'], 'hl' => ["false"]];
};
$service->expects($this->once())->method('invoke')
->with($this->callback($checkCommand))
->will($this->returnValue($commandObj));

$recommend->setConfig("Solr:10:mixed:retain:0:facet1:value1:facet2:value2");
$recommend->init($params, $request);
$recommend->process($results);
$recommend = $this->getConfiguredModule("Solr:10:mixed:retain:0:facet1:value1:facet2:value2");
$output = $recommend->getResults();
$this->assertEquals($records, $output);
$this->assertEquals(["1", "2", "3", "4", "5"], $output);
}

/**
Expand Down
Expand Up @@ -78,17 +78,14 @@ class AlmaTest extends \PHPUnit\Framework\TestCase
];

/**
* Test
* Support method for testParseLinks and testParseLinksWithoutIgnoredFiltering.
*
* @return void
* @param bool $filterSet Should we filter the results for testParseLinksWithoutIgnoredFiltering?
*
* @return array
*/
public function testParseLinks()
protected function getExpectedParsedLinks($filterSet = false): array
{
$conn = $this->createConnector('alma.xml');

$openUrl = "url_ver=Z39.88-2004&ctx_ver=Z39.88-2004";
$result = $conn->parseLinks($conn->fetchLinks($openUrl));

$testResult = [
[
'title' => 'Unpaywall',
Expand Down Expand Up @@ -170,7 +167,9 @@ public function testParseLinks()
'authentication' => '',
'service_type' => 'getWebService',
],
[
];
if (!$filterSet) {
$testResult[] = [
'title' => 'ProQuest Safari Tech Books Online',
'coverage' => '',
'access' => 'limited',
Expand All @@ -179,10 +178,22 @@ public function testParseLinks()
'notes' => '',
'authentication' => '',
'service_type' => 'getFullTxt',
],
];
];
}
return $testResult;
}

$this->assertEquals($testResult, $result);
/**
* Test
*
* @return void
*/
public function testParseLinks()
{
$conn = $this->createConnector('alma.xml');
$openUrl = "url_ver=Z39.88-2004&ctx_ver=Z39.88-2004";
$result = $conn->parseLinks($conn->fetchLinks($openUrl));
$this->assertEquals($this->getExpectedParsedLinks(), $result);
}

/**
Expand All @@ -193,94 +204,9 @@ public function testParseLinks()
public function testParseLinksWithoutIgnoredFiltering()
{
$conn = $this->createConnector('alma.xml', ['ignoredFilterReasons' => '']);

$openUrl = "url_ver=Z39.88-2004&ctx_ver=Z39.88-2004";
$result = $conn->parseLinks($conn->fetchLinks($openUrl));

$testResult = [
[
'title' => 'Unpaywall',
'coverage' => '',
'access' => 'open',
'href' => 'https://na01.alma.exlibrisgroup.com/view/action/uresolver.do'
. '?operation=resolveService&package_service_id=1',
'notes' => '',
'authentication' => '',
'service_type' => 'getFullTxt',
],
[
'title' => 'Ebook override',
'coverage' => 'Available from 2019',
'access' => 'limited',
'href' => 'https://na01.alma.exlibrisgroup.com/view/action/uresolver.do'
. '?operation=resolveService&package_service_id=5687861830000561&institutionId=561&customerId=550',
'notes' => '',
'authentication' => '',
'service_type' => 'getFullTxt',
],
[
'title' => 'ebrary Academic Complete Subscription UKI Edition',
'coverage' => '',
'access' => 'limited',
'href' => 'https://na01.alma.exlibrisgroup.com/view/action/uresolver.do'
. '?operation=resolveService&package_service_id=5687861800000561&institutionId=561&customerId=550',
'notes' => '',
'authentication' => '',
'service_type' => 'getFullTxt',
],
[
'title' => 'ebrary Science & Technology Subscription',
'coverage' => '',
'access' => 'limited',
'href' => 'https://na01.alma.exlibrisgroup.com/view/action/uresolver.do'
. '?operation=resolveService&package_service_id=5687861790000561&institutionId=561&customerId=550',
'notes' => '',
'authentication' => '',
'service_type' => 'getFullTxt',
],
[
'title' => 'EBSCOhost Academic eBook Collection (North America)',
'coverage' => '',
'access' => 'open',
'href' => 'https://na01.alma.exlibrisgroup.com/view/action/uresolver.do'
. '?operation=resolveService&package_service_id=5687861770000561&institutionId=561&customerId=550',
'notes' => 'notessssssssssss SERVICE LEVEL PUBLIC NOTE',
'authentication' => 'collection level auth SERVICE LEVEL AUTHE NOTE',
'service_type' => 'getFullTxt',
],
[
'title' => 'EBSCOhost eBook Community College Collection',
'coverage' => '',
'access' => 'limited',
'href' => 'https://na01.alma.exlibrisgroup.com/view/action/uresolver.do'
. '?operation=resolveService&package_service_id=5687861780000561&institutionId=561&customerId=550',
'notes' => '',
'authentication' => '',
'service_type' => 'getHolding',
],
[
'title' => 'Elsevier ScienceDirect Books',
'coverage' => '',
'access' => 'limited',
'href' => 'https://na01.alma.exlibrisgroup.com/view/action/uresolver.do'
. '?operation=resolveService&package_service_id=5687861820000561&institutionId=561&customerId=550',
'notes' => '',
'authentication' => '',
'service_type' => 'getFullTxt',
],
[
'title' => 'Request Assistance for this Resource!',
'coverage' => '',
'access' => '',
'href' => 'https://www.google.com/search?Testingrft.oclcnum=437189463'
. '&q=Fundamental+Data+Compression&rft.archive=9942811800561',
'notes' => '',
'authentication' => '',
'service_type' => 'getWebService',
],
];

$this->assertEquals($testResult, $result);
$this->assertEquals($this->getExpectedParsedLinks(true), $result);
}

/**
Expand Down

0 comments on commit 350a743

Please sign in to comment.