Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
php-modules
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
library
php-modules
Commits
66d7b8ed
Commit
66d7b8ed
authored
6 years ago
by
Hoop, Bert Jan de
Browse files
Options
Downloads
Patches
Plain Diff
L05DOCBST-38 add search_all_records method
parent
adb0dedd
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/library/Webquery.php
+30
-2
30 additions, 2 deletions
src/library/Webquery.php
tests/library/WebqueryTest.php
+47
-0
47 additions, 0 deletions
tests/library/WebqueryTest.php
with
77 additions
and
2 deletions
src/library/Webquery.php
+
30
−
2
View file @
66d7b8ed
...
@@ -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"
]));
}
}
...
...
This diff is collapsed.
Click to expand it.
tests/library/WebqueryTest.php
+
47
−
0
View file @
66d7b8ed
...
@@ -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
();
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment