|
1
|
|
|
<?php namespace Scriptotek\Sru; |
|
2
|
|
|
|
|
3
|
|
|
use GuzzleHttp\Psr7\Response as HttpResponse; |
|
4
|
|
|
use function GuzzleHttp\Psr7\stream_for; |
|
5
|
|
|
use Http\Mock\Client as MockHttp; |
|
6
|
|
|
|
|
7
|
|
|
class TestCase extends \PHPUnit\Framework\TestCase |
|
8
|
|
|
{ |
|
9
|
|
|
protected $recordTpl = '<srw:record> |
|
10
|
|
|
<srw:recordSchema>marcxchange</srw:recordSchema> |
|
11
|
|
|
<srw:recordPacking>xml</srw:recordPacking> |
|
12
|
|
|
<srw:recordPosition>{{position}}</srw:recordPosition> |
|
13
|
|
|
<srw:recordData>{{data}}</srw:recordData> |
|
14
|
|
|
</srw:record>'; |
|
15
|
|
|
|
|
16
|
|
|
protected $mainTpl = '<?xml version="1.0" encoding="UTF-8" ?> |
|
17
|
|
|
<srw:searchRetrieveResponse |
|
18
|
|
|
xmlns:srw="http://www.loc.gov/zing/srw/" |
|
19
|
|
|
xmlns:xcql="http://www.loc.gov/zing/cql/xcql/" |
|
20
|
|
|
> |
|
21
|
|
|
<srw:version>1.1</srw:version> |
|
22
|
|
|
<srw:numberOfRecords>{{numberOfRecords}}</srw:numberOfRecords> |
|
23
|
|
|
<srw:records> |
|
24
|
|
|
{{records}} |
|
25
|
|
|
</srw:records> |
|
26
|
|
|
<srw:echoedSearchRetrieveRequest> |
|
27
|
|
|
<srw:operation>searchRetrieve</srw:operation> |
|
28
|
|
|
<srw:version>1.1</srw:version> |
|
29
|
|
|
<srw:query>{{cql}}</srw:query> |
|
30
|
|
|
<srw:startRecord>{{startRecord}}</srw:startRecord> |
|
31
|
|
|
<srw:maximumRecords>{{maxRecords}}</srw:maximumRecords> |
|
32
|
|
|
<srw:recordSchema>marcxchange</srw:recordSchema> |
|
33
|
|
|
</srw:echoedSearchRetrieveRequest> |
|
34
|
|
|
<srw:extraResponseData> |
|
35
|
|
|
<responseDate>2014-03-28T12:09:50Z</responseDate> |
|
36
|
|
|
</srw:extraResponseData> |
|
37
|
|
|
</srw:searchRetrieveResponse>'; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Get an item from an array using "dot" notation. |
|
41
|
|
|
* Source: http://laravel.com/api/source-function-array_get.html#226-251 |
|
42
|
|
|
* |
|
43
|
|
|
* @param array $array |
|
44
|
|
|
* @param string $key |
|
45
|
|
|
* @param mixed $default |
|
46
|
|
|
* @return mixed |
|
47
|
|
|
*/ |
|
48
|
|
|
private function array_get($array, $key, $default = null) |
|
49
|
|
|
{ |
|
50
|
|
|
if (is_null($key)) { |
|
|
|
|
|
|
51
|
|
|
return $array; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
if (isset($array[$key])) { |
|
55
|
|
|
return $array[$key]; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
foreach (explode('.', $key) as $segment) { |
|
59
|
|
|
if (! is_array($array) or ! array_key_exists($segment, $array)) { |
|
60
|
|
|
return $default; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$array = $array[$segment]; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return $array; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* numberOfRecords : Total number of records in response |
|
71
|
|
|
*/ |
|
72
|
|
|
public function makeDummyResponse($numberOfRecords = 10, $options = array()) |
|
73
|
|
|
{ |
|
74
|
|
|
// Request: CQL |
|
75
|
|
|
$cql = $this->array_get($options, 'cql', 'dummy'); |
|
76
|
|
|
|
|
77
|
|
|
// Request: First record to fetch |
|
78
|
|
|
$startRecord = $this->array_get($options, 'startRecord', 1); |
|
79
|
|
|
|
|
80
|
|
|
// Request: Max number of records to return |
|
81
|
|
|
$maxRecords = $this->array_get($options, 'maxRecords', 10); |
|
82
|
|
|
|
|
83
|
|
|
$endRecord = $startRecord + min($maxRecords - 1, $numberOfRecords - $startRecord); |
|
84
|
|
|
|
|
85
|
|
|
$recordTpl = $this->recordTpl; |
|
86
|
|
|
$records = implode('', array_map(function ($n) use ($recordTpl) { |
|
87
|
|
|
return str_replace( |
|
88
|
|
|
array('{{position}}', '{{data}}'), |
|
89
|
|
|
array($n, 'RecordData #' . $n), |
|
90
|
|
|
$recordTpl |
|
91
|
|
|
); |
|
92
|
|
|
}, range($startRecord, $endRecord))); |
|
93
|
|
|
|
|
94
|
|
|
return str_replace( |
|
95
|
|
|
array('{{records}}', '{{cql}}', '{{startRecord}}', '{{maxRecords}}', '{{numberOfRecords}}'), |
|
96
|
|
|
array($records, $cql, $startRecord, $maxRecords, $numberOfRecords), |
|
97
|
|
|
$this->mainTpl |
|
98
|
|
|
); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Returns a series of responses (no matter what request) |
|
103
|
|
|
*/ |
|
104
|
|
|
protected function httpMockWithResponses($bodies) |
|
105
|
|
|
{ |
|
106
|
|
|
if (!is_array($bodies)) { |
|
107
|
|
|
$bodies = [$bodies]; |
|
108
|
|
|
} |
|
109
|
|
|
$http = new MockHttp(); |
|
110
|
|
|
foreach ($bodies as $body) { |
|
111
|
|
|
$http->addResponse( |
|
112
|
|
|
(new HttpResponse()) |
|
113
|
|
|
->withBody(stream_for($body)) |
|
|
|
|
|
|
114
|
|
|
); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
return $http; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|