Skip to content
Snippets Groups Projects
Commit 66d7b8ed authored by Hoop, Bert Jan de's avatar Hoop, Bert Jan de
Browse files

L05DOCBST-38 add search_all_records method

parent adb0dedd
No related branches found
No related tags found
No related merge requests found
...@@ -291,6 +291,34 @@ class Webquery { ...@@ -291,6 +291,34 @@ class Webquery {
} }
/**
* run a search request with set request parameters
* continue searches and collect all records until no next element is found
* return all collected records from the search or false if any search fails
*/
public function search_all_records() {
array_push($this->context, 'search_all_records');
if ( ! $this->search()->is_success()) {
array_pop($this->context);
return false;
}
$records = $this->get_records();
while ($ofs = $this->get_next_ofs()) {
if ( ! $this->set_wq_ofs($ofs)->search()->is_success()) {
array_pop($this->context);
return false;
}
array_push($records, $this->get_records());
}
array_pop($this->context);
return $records;
}
/** /**
* return first record from response * return first record from response
*/ */
...@@ -457,13 +485,13 @@ class Webquery { ...@@ -457,13 +485,13 @@ class Webquery {
// check status attribute of webquery error element if available // check status attribute of webquery error element if available
$error_status = $this->get_error_status(); $error_status = $this->get_error_status();
if (isset($error_status) && $error_status != 200) { if (!empty($error_status) && $error_status != 200) {
return false; return false;
} }
// check code attribute of webquery error element if available // check code attribute of webquery error element if available
$error_code = $this->get_error_code(); $error_code = $this->get_error_code();
if (isset($error_code)) { if (!empty($error_code)) {
// check if code is known to be a success indication // check if code is known to be a success indication
return !empty(array_search($error_code, ["WQW_NO_HITS", "WQW_RECORD_NOT_FOUND", "WQW_UPDATE_OK", "WQW_UPDATE_NOCHANGE"])); return !empty(array_search($error_code, ["WQW_NO_HITS", "WQW_RECORD_NOT_FOUND", "WQW_UPDATE_OK", "WQW_UPDATE_NOCHANGE"]));
} }
......
...@@ -182,6 +182,53 @@ EOT; ...@@ -182,6 +182,53 @@ EOT;
} }
public function test_search_all_records() {
// create mock http client that stores requests and responses
$request_history = [];
$record1 = "<record isn='1' crc='11111111'><seq>1</seq></record>";
$record2 = "<record isn='2' crc='22222222'><seq>2</seq></record>";
$record3 = "<record isn='3' crc='33333333'><seq>3</seq></record>";
$resp1 = "<recordset><hits>2</hits>${record1}${record2}<next wq_ofs='2' wq_max='2' /></recordset>";
$resp2 = "<recordset><hits>1</hits>$record3</recordset>";
$resp3 = '<recordset><error code="WQW_NO_HITS"><message>nothing found</message></error></recordset>';
$responses = [
new Response(200, [], $resp1),
new Response(200, [], $resp2),
new Response(404, [], $resp3),
];
$mock_httpclient = get_mock_httpclient($responses, $request_history);
$wq = new \Library\Webquery();
$wq->set_http_client($mock_httpclient);
$wq->set_logger(new \Monolog\Logger('search_all_records'));
$wq->set_host('devel.library.wur.nl')
->set_service('test')
->set_record_name("record")
->set_wq_max(2)
->set_query('record=*');
// first request succeeds
$result = $wq->search_all_records();
$this->assertEquals(3, count($result), 'number of records returned');
$this->assertEquals('200', $wq->get_http_status(), 'response http status last request');
// check request history
$this->assertEquals(2, count($request_history), 'request history count');
// check first request
$request = $request_history[0]['request'];
$this->assertEquals('GET', $request->getMethod(), 'request 1 method');
$expect = 'http://devel.library.wur.nl/WebQuery/test/xml?wq_max=2&record=*';
$this->assertEquals($expect, (string) $request->getUri(), 'request 1 url');
// check second request
$request = $request_history[1]['request'];
$this->assertEquals('GET', $request->getMethod(), 'request 2 method');
$expect = 'http://devel.library.wur.nl/WebQuery/test/xml?wq_ofs=2&wq_max=2&record=*';
$this->assertEquals($expect, (string) $request->getUri(), 'request 2 url');
}
public function test_xml_to_wwwform() { public function test_xml_to_wwwform() {
$xml = get_test_xml(); $xml = get_test_xml();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment