Skip to content

Commit 7182c55

Browse files
committed
Allow response classes to be initialized without args
For easier mocking
1 parent 7e068fb commit 7182c55

File tree

7 files changed

+73
-37
lines changed

7 files changed

+73
-37
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"homepage": "http://github.com/scriptotek/sru-client",
66
"keywords": ["sru"],
77
"require": {
8-
"danmichaelo/quitesimplexmlelement": ">=0.2.1",
8+
"danmichaelo/quitesimplexmlelement": ">=0.4.2",
99
"guzzle/guzzle": "~3.8"
1010
},
1111
"require-dev": {

src/ExplainResponse.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php namespace Scriptotek\Sru;
22

3+
use Danmichaelo\QuiteSimpleXMLElement\QuiteSimpleXMLElement;
4+
35
/**
46
* Explain response
57
*/
@@ -23,20 +25,28 @@ class ExplainResponse extends Response implements ResponseInterface
2325
* @param string $text Raw XML response
2426
* @param Client $client SRU client reference (optional)
2527
*/
26-
public function __construct($text, &$client = null)
28+
public function __construct($text = null, &$client = null)
2729
{
2830
parent::__construct($text, $client);
2931

3032
$this->indexes = array();
3133

34+
if (is_null($this->response)) {
35+
return;
36+
}
3237
$explain = $this->response->first('/srw:explainResponse/srw:record/srw:recordData/exp:explain');
3338
if (!$explain) {
3439
return;
3540
}
3641

37-
$serverInfo = $explain->first('exp:serverInfo');
38-
$dbInfo = $explain->first('exp:databaseInfo');
39-
$indexInfo = $explain->first('exp:indexInfo');
42+
$this->parseExplainResponse($explain);
43+
}
44+
45+
protected function parseExplainResponse(QuiteSimpleXMLElement $node)
46+
{
47+
$serverInfo = $node->first('exp:serverInfo');
48+
$dbInfo = $node->first('exp:databaseInfo');
49+
$indexInfo = $node->first('exp:indexInfo');
4050

4151
$this->host = $serverInfo->text('exp:host');
4252
$this->port = (int) $serverInfo->text('exp:port');

src/Response.php

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class Response implements ResponseInterface
9393
);
9494

9595
/** @var string Raw XML response */
96-
protected $rawResponse;
96+
protected $rawResponse = '';
9797

9898
/** @var QuiteSimpleXMLElement XML response */
9999
protected $response;
@@ -110,46 +110,56 @@ class Response implements ResponseInterface
110110
* @param string $text Raw XML response
111111
* @param Client $client SRU client reference (optional)
112112
*/
113-
public function __construct($text, &$client = null)
113+
public function __construct($text = null, &$client = null)
114114
{
115+
$this->client = $client;
116+
117+
if (!is_null($text)) {
118+
$this->initializeFromText($text);
119+
}
120+
}
115121

122+
protected function initializeFromText($text)
123+
{
116124
// Fix missing namespace in Alma records:
117125
$text = str_replace('<record xmlns="">', '<record xmlns="http://www.loc.gov/MARC21/slim">', $text);
118126

119127
$this->rawResponse = $text;
120128

121129
// Throws Danmichaelo\QuiteSimpleXMLElement\InvalidXMLException on invalid xml
122-
$this->response = new QuiteSimpleXMLElement($text);
123-
124-
$this->client = $client;
125-
126-
$this->response->registerXPathNamespaces(array(
130+
$this->response = QuiteSimpleXMLElement::make($text, [
127131
'srw' => 'http://www.loc.gov/zing/srw/',
128132
'exp' => 'http://explain.z3950.org/dtd/2.0/',
129-
'd' => 'http://www.loc.gov/zing/srw/diagnostic/'
130-
));
133+
'd' => 'http://www.loc.gov/zing/srw/diagnostic/',
134+
]);
131135

132136
$this->version = $this->response->text('/srw:*/srw:version');
133137

134-
$diagnostic = $this->response->first('/srw:*/srw:diagnostics/d:diagnostic');
135-
if ($diagnostic) {
136-
// Only the 'uri' field is required, 'message' and 'details' are optional
137-
$uri = $diagnostic->text('d:uri');
138-
if (strlen($uri)) {
139-
$msg = $diagnostic->text('d:message');
140-
$details = $diagnostic->text('d:details');
141-
if (empty($msg)) {
142-
if (isset(self::$errorMessages[$uri])) {
143-
$msg = self::$errorMessages[$uri];
144-
} else {
145-
$msg = 'Unknown error';
146-
}
147-
}
148-
if (!empty($details)) {
149-
$msg .= ' (' . $details . ')';
138+
$this->handleDiagnostic($this->response->first('/srw:*/srw:diagnostics/d:diagnostic'));
139+
}
140+
141+
protected function handleDiagnostic(QuiteSimpleXMLElement $node = null)
142+
{
143+
if (is_null($node)) {
144+
return;
145+
}
146+
147+
// Only the 'uri' field is required, 'message' and 'details' are optional
148+
$uri = $node->text('d:uri');
149+
if (strlen($uri)) {
150+
$msg = $node->text('d:message');
151+
$details = $node->text('d:details');
152+
if (empty($msg)) {
153+
if (isset(self::$errorMessages[$uri])) {
154+
$msg = self::$errorMessages[$uri];
155+
} else {
156+
$msg = 'Unknown error';
150157
}
151-
throw new Exceptions\SruErrorException($msg, $uri);
152158
}
159+
if (!empty($details)) {
160+
$msg .= ' (' . $details . ')';
161+
}
162+
throw new Exceptions\SruErrorException($msg, $uri);
153163
}
154164
}
155165

src/ResponseInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ interface ResponseInterface
1212
* @param Client $client SRU client reference (optional)
1313
* @return void
1414
*/
15-
public function __construct($text, &$client = null);
15+
public function __construct($text = null, &$client = null);
1616

1717
/**
1818
* Get the raw xml response

src/SearchRetrieveResponse.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,31 @@
66
class SearchRetrieveResponse extends Response implements ResponseInterface
77
{
88
/** @var Record[] Array of records */
9-
public $records;
9+
public $records = [];
1010

1111
/** @var int Total number of records in the result set */
12-
public $numberOfRecords;
12+
public $numberOfRecords = 0;
1313

1414
/** @var int Position of next record in the result set, or null if no such record exist */
15-
public $nextRecordPosition;
15+
public $nextRecordPosition = null;
1616

1717
/** @var string The CQL query used to generate the response */
18-
public $query;
18+
public $query = '';
1919

2020
/**
2121
* Create a new searchRetrieve response
2222
*
2323
* @param string $text Raw XML response
2424
* @param Client $client SRU client reference (optional)
2525
*/
26-
public function __construct($text, &$client = null)
26+
public function __construct($text = null, &$client = null)
2727
{
2828
parent::__construct($text, $client);
2929

30+
if (is_null($this->response)) {
31+
return;
32+
}
33+
3034
$this->numberOfRecords = (int) $this->response->text('/srw:searchRetrieveResponse/srw:numberOfRecords');
3135
$this->nextRecordPosition = (int) $this->response->text('/srw:searchRetrieveResponse/srw:nextRecordPosition') ?: null;
3236

tests/ExplainResponseTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,10 @@ public function testNormalResponse()
115115
$this->assertEquals('Identifiserer en bibliografisk enhet', $res->indexes[0]->title);
116116
$this->assertEquals('bs.objektid', $res->indexes[0]->maps[0]);
117117
}
118+
119+
public function testCanBeInitializedWithoutAnyData()
120+
{
121+
$res = new ExplainResponse;
122+
$this->assertNull($res->host);
123+
}
118124
}

tests/SearchRetrieveResponseTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,10 @@ public function testDiagnosticsWithoutError()
158158
</srw:searchRetrieveResponse>');
159159
}
160160

161+
public function testCanBeInitializedWithoutAnyData()
162+
{
163+
$res = new SearchRetrieveResponse;
164+
$this->assertNull($res->version);
165+
}
166+
161167
}

0 commit comments

Comments
 (0)