Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 31 additions & 10 deletions src/EdurepSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,34 @@ public function __construct(DefaultSearchConfig $config, ?SearchClient $searchCl
$this->searchClient = $searchClient;
}

private function isCatalogusStrategy(StrategyType $strategyType): bool
{
return $strategyType instanceof \Kennisnet\Edurep\Strategy\CatalogusStrategyType;
}

/**
* @param int $value
*
* @return self
*/
public function setMaximumRecords(int $value): self
{
if ($value >= 0 && $value <= self::MAX_RECORDS) {
$this->parameters["maximumRecords"] = $value;
} else {
throw new UnexpectedValueException("The value for maximumRecords should be between 0 and 100.", 22);
$isCatalogusStrategy = $this->isCatalogusStrategy($this->config->getStrategy());

if (self::isMaximumRecordsOutsideSelectedRange($value) && !$isCatalogusStrategy) {
throw new UnexpectedValueException("The value for maximumRecords should be between 0 and " . self::MAX_RECORDS . ".", 22);
}

$this->parameters["maximumRecords"] = $value;

return $this;
}

static private function isMaximumRecordsOutsideSelectedRange(int $value): bool
{
return ($value < 0 || $value > self::MAX_RECORDS);
}

public function setRecordpacking(string $recordPacking): self
{
$this->parameters["recordPacking"] = $recordPacking;
Expand Down Expand Up @@ -142,17 +154,24 @@ public function addFilterPart(string $filterId, string $comparator, string $valu
*/
public function setStartRecord(int $value): self
{
if ($value >= 1 && $value <= self::EDUREP_MAX_STARTRECORD) {
$this->parameters["startRecord"] = $value;
$this->availablestartrecords = self::EDUREP_MAX_STARTRECORD - $value;
} else {
$isCatalogusStrategy = $this->isCatalogusStrategy($this->config->getStrategy());

if (self::isStartRecordOutsideSelectedRange($value) && !$isCatalogusStrategy) {
throw new UnexpectedValueException("The value for startRecords should be between 1 and " . self::EDUREP_MAX_STARTRECORD . ".",
23);
23);
}

$this->parameters["startRecord"] = $value;
$this->availablestartrecords = self::EDUREP_MAX_STARTRECORD - $value;

return $this;
}

static private function isStartRecordOutsideSelectedRange(int $value): bool
{
return ($value < 1 || $value > self::EDUREP_MAX_STARTRECORD);
}

public function addXRecordSchema(string $value): self
{
$this->recordschemas[] = $value;
Expand Down Expand Up @@ -209,7 +228,9 @@ public function getQuery(?string $searchType = null): string
{
# making sure the startRecord/maximumRecord combo does
# not trigger an exception.
if ($this->availablestartrecords < $this->parameters["maximumRecords"]) {
$isNotCatalogusStrategy = !$this->isCatalogusStrategy($this->config->getStrategy());

if (($this->availablestartrecords < $this->parameters["maximumRecords"]) && $isNotCatalogusStrategy) {
$this->parameters["maximumRecords"] = $this->availablestartrecords;
}

Expand Down
74 changes: 65 additions & 9 deletions tests/EdurepSearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Tests\Kennisnet\Edurep;


use Kennisnet\Edurep\DefaultSearchConfig;
use Kennisnet\Edurep\EdurepSearch;
use Kennisnet\Edurep\SearchClient;
Expand All @@ -13,30 +12,87 @@
class EdurepSearchTest extends TestCase
{

public function testInvalidMax(): void
public function test_exception_thrown_on_setting_invalid_maximum_records_value(): void
{
$strategy = new EdurepStrategyType();
$config = new DefaultSearchConfig($strategy, "http://wszoeken.edurep.kennisnet.nl:8000/");
$edurep = new EdurepSearch($config);

$this->expectExceptionMessage('The value for maximumRecords should be between 0 and 100.');

$edurep->setMaximumRecords(-1);
}

public function test_exception_thrown_on_missing_query_when_getting_request_url(): void
{
$strategy = new EdurepStrategyType();
$config = new DefaultSearchConfig($strategy, "http://wszoeken.edurep.kennisnet.nl:8000/");
$edurep = new EdurepSearch($config);

$edurep->setMaximumRecords(-1);
$this->expectExceptionMessage('Missing query');

$edurep->getRequestUrl();
}

public function testNoQuery(): void
public function test_catalogus_can_set_infinitely_high_start_record(): void
{
$this->expectExceptionMessage('Missing query');
$strategy = new CatalogusStrategyType();
$config = new DefaultSearchConfig($strategy, "https://staging.catalogusservice.edurep.nl/");
$edurep = new EdurepSearch($config);

$edurep->setStartRecord(3000);
$edurep->setQuery("mbo");

$this->assertEquals(
'https://staging.catalogusservice.edurep.nl/sru?operation=searchRetrieve&version=1.2&recordPacking=xml&query=mbo&maximumRecords=100&startRecord=3000',
$edurep->getRequestUrl(),
"Test catalogus URL with startRecord 3000"
);

$this->assertEquals(3000, $edurep->getParameters()['startRecord'], "Test catalogus can use unrestrained start record");
}

public function test_catalogus_can_set_infinitely_high_maximum_records(): void
{
$strategy = new CatalogusStrategyType();
$config = new DefaultSearchConfig($strategy, "https://staging.catalogusservice.edurep.nl/");
$edurep = new EdurepSearch($config);

$edurep->setMaximumRecords(1000);
$edurep->setQuery("mbo");

$this->assertEquals(
'https://staging.catalogusservice.edurep.nl/sru?operation=searchRetrieve&version=1.2&recordPacking=xml&query=mbo&maximumRecords=1000',
$edurep->getRequestUrl(),
"Test catalogus URL with maximumRecords 1000"
);

$this->assertEquals(1000, $edurep->getParameters()['maximumRecords'], "Test catalogus can use unrestrained maxiumum records");
}

public function test_edurep_cant_use_unrestrained_maximum_records(): void
{
$strategy = new EdurepStrategyType();
$config = new DefaultSearchConfig($strategy, "http://wszoeken.edurep.kennisnet.nl:8000/");
$edurep = new EdurepSearch($config);

$edurep->getRequestUrl();
$this->expectExceptionMessage('The value for maximumRecords should be between 0 and 100.');

$edurep->setMaximumRecords(1000);
}

public function test_edurep_cant_use_unrestrained_start_record(): void
{
$strategy = new EdurepStrategyType();
$config = new DefaultSearchConfig($strategy, "http://wszoeken.edurep.kennisnet.nl:8000/");
$edurep = new EdurepSearch($config);

$this->expectExceptionMessage('The value for startRecords should be between 1 and 1000.');

$edurep->setStartRecord(3000);
}

public function testEdurepQuery(): void
public function test_edurep_query(): void
{
$strategy = new EdurepStrategyType();
$config = new DefaultSearchConfig($strategy, "http://wszoeken.edurep.kennisnet.nl:8000/");
Expand Down Expand Up @@ -76,7 +132,7 @@ public function testEdurepQuery(): void
);
}

public function testCatalogQuery(): void
public function test_catalogus_query(): void
{
$strategy = new CatalogusStrategyType();
$config = new DefaultSearchConfig($strategy, "https://staging.catalogusservice.edurep.nl/");
Expand All @@ -101,7 +157,7 @@ public function testCatalogQuery(): void
);
}

public function testDefaultSearch()
public function test_default_search()
{
$strategy = new EdurepStrategyType();
$config = new DefaultSearchConfig($strategy, "http://wszoeken.edurep.kennisnet.nl:8000/");
Expand Down