Skip to content

Commit b1d2373

Browse files
committed
Expand ExplainResponse and add tests
1 parent af7b025 commit b1d2373

File tree

4 files changed

+168
-0
lines changed

4 files changed

+168
-0
lines changed

src/ExplainResponse.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@
77
*/
88
class ExplainResponse extends Response implements ResponseInterface {
99

10+
/** @var string Server hostname */
11+
public $host;
12+
13+
/** @var int Server port */
14+
public $port;
15+
16+
/** @var object Server database */
17+
public $database;
18+
19+
/** @var array Server indexes */
20+
public $indexes;
21+
1022
/**
1123
* Create a new explain response
1224
*
@@ -17,6 +29,34 @@ public function __construct($text, &$client = null)
1729
{
1830
parent::__construct($text, $client);
1931

32+
$explain = $this->response->first('/srw:explainResponse/srw:record/sru:recordData/exp:explain');
33+
$serverInfo = $explain->first('exp:serverInfo');
34+
$dbInfo = $explain->first('exp:databaseInfo');
35+
$indexInfo = $explain->first('exp:indexInfo');
36+
37+
$this->host = $serverInfo->text('exp:host');
38+
$this->port = (int) $serverInfo->text('exp:port');
39+
$this->database = new \StdClass;
40+
$this->database->identifier = $serverInfo->text('exp:database');
41+
$this->database->title = $dbInfo->text('exp:title');
42+
$this->database->description = $dbInfo->text('exp:description');
43+
44+
$this->indexes = array();
45+
foreach ($indexInfo->xpath('exp:index') as $index) {
46+
$ind = new \StdClass;
47+
$ind->scan = ($index->attr('scan') == 'true');
48+
$ind->search = ($index->attr('search') == 'true');
49+
$ind->sort = ($index->attr('sort') == 'true');
50+
$ind->title = $index->text('exp:title');
51+
$ind->maps = array();
52+
foreach ($index->xpath('exp:map') as $map) {
53+
$set = $map->first('exp:name')->attr('set');
54+
$name = $map->text('exp:name');
55+
$ind->maps[] = $set . '.' . $name;
56+
}
57+
$this->indexes[] = $ind;
58+
}
59+
2060
// TODO
2161

2262
}

src/Response.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public function __construct($text, &$client = null)
3939

4040
$this->response->registerXPathNamespaces(array(
4141
'srw' => 'http://www.loc.gov/zing/srw/',
42+
'exp' => 'http://explain.z3950.org/dtd/2.0/',
4243
'd' => 'http://www.loc.gov/zing/srw/diagnostic/'
4344
));
4445

tests/ExplainResponseTest.php

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<?php namespace Scriptotek\Sru;
2+
3+
use \Guzzle\Http\Message\Response as HttpResponse;
4+
use \Mockery as m;
5+
6+
class ExplainResponseTest extends TestCase {
7+
8+
public function testNormalResponse()
9+
{
10+
$res = new ExplainResponse('<?xml version="1.0" encoding="UTF-8"?>
11+
<sru:explainResponse xmlns:sru="http://www.loc.gov/zing/srw/">
12+
<sru:version>1.2</sru:version>
13+
<sru:record>
14+
<sru:recordPacking>xml</sru:recordPacking>
15+
<sru:recordSchema>http://explain.z3950.org/dtd/2.0/</sru:recordSchema>
16+
<sru:recordData>
17+
<explain xmlns="http://explain.z3950.org/dtd/2.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://explain.z3950.org/dtd/2.0/ http://explain.z3950.org/dtd/zeerex-2.0.xsd">
18+
<serverInfo protocol="SRU" transport="http">
19+
<host>sru.bibsys.no</host>
20+
<port>80</port>
21+
<database>biblio</database>
22+
</serverInfo>
23+
<databaseInfo>
24+
<title lang="en" primary="true">BIBSYS Union Catalogue</title>
25+
<description lang="en">SRU access to the BIBSYS Union Catalogue.</description>
26+
<contact lang="en">[email protected]</contact>
27+
<restrictions lang="en">It is prohibited to use this service for bulk downloading of records.</restrictions>
28+
<links>
29+
<link type="icon">http://www.bibsys.no/images/logo/bibsys_logo_medium.jpg</link>
30+
</links>
31+
</databaseInfo>
32+
<metaInfo>
33+
<dateModified>Sat Apr 26 16:47:43 CEST 2014</dateModified>
34+
</metaInfo>
35+
<indexInfo>
36+
<set identifier="info:srw/cql-context-set/1/dc-v1.1" name="dc"/>
37+
<set identifier="info:srw/cql-context-set/2/rec-1.1" name="rec"/>
38+
<set identifier="info:srw/cql-context-set/15/norzig-1.1" name="norzig"/>
39+
<set identifier="info:srw/cql-context-set/1/bib-v1" name="bib"/>
40+
<set identifier="http://www.bibsys.no/context/bs/1.0" name="bs"/>
41+
<set identifier="info:srw/cql-context-set/1/cql-v1.2" name="cql"/>
42+
<index scan="false" search="true" sort="true">
43+
<title>Identifiserer en bibliografisk enhet</title>
44+
<map>
45+
<name set="bs">objektid</name>
46+
</map>
47+
</index>
48+
<index scan="false" search="true" sort="true">
49+
<title>An unambiguous reference to the resource within a given context.</title>
50+
<map>
51+
<name set="dc">identifier</name>
52+
</map>
53+
</index>
54+
<index scan="false" search="true" sort="true">
55+
<title>nlm nummer</title>
56+
<map>
57+
<name set="bs">nlm</name>
58+
</map>
59+
</index>
60+
<index scan="false" search="true" sort="true">
61+
<title>institusjonsspesifikke emneord</title>
62+
<map>
63+
<name set="bs">ubtsy</name>
64+
</map>
65+
</index>
66+
<index scan="false" search="true" sort="true">
67+
<title>en plassering</title>
68+
<map>
69+
<name set="bs">geografisk-emneord</name>
70+
</map>
71+
</index>
72+
<index scan="false" search="true" sort="true">
73+
<title/>
74+
<map>
75+
<name set="bs">antallinstrumenter</name>
76+
</map>
77+
</index>
78+
<index scan="false" search="true" sort="true">
79+
<title>intern</title>
80+
<map>
81+
<name set="bs">issn-annen-manifestasjon</name>
82+
</map>
83+
<map>
84+
<name set="bs">ismn-annen-manifestasjon</name>
85+
</map>
86+
<map>
87+
<name set="bs">isbn-annen-manifestasjon</name>
88+
</map>
89+
</index>
90+
</indexInfo>
91+
<schemaInfo>
92+
<schema identifier="info:srw/schema/1/dc-v1.1" name="dc"/>
93+
<schema identifier="info:lc/xmlns/marcxchange-v1" name="marcxchange"/>
94+
</schemaInfo>
95+
<configInfo>
96+
<default type="relation">=</default>
97+
<default type="retrieveSchema">marcxchange</default>
98+
<default type="numberOfRecords">10</default>
99+
<setting type="maximumRecords">50</setting>
100+
</configInfo>
101+
</explain>
102+
</sru:recordData>
103+
</sru:record>
104+
</sru:explainResponse>');
105+
106+
$this->assertNull($res->error);
107+
$this->assertEquals('1.2', $res->version);
108+
$this->assertEquals('sru.bibsys.no', $res->host);
109+
$this->assertEquals(80, $res->port);
110+
$this->assertEquals('biblio', $res->database->identifier);
111+
$this->assertEquals('BIBSYS Union Catalogue', $res->database->title);
112+
$this->assertEquals('SRU access to the BIBSYS Union Catalogue.', $res->database->description);
113+
$this->assertCount(7, $res->indexes);
114+
$this->assertFalse($res->indexes[0]->scan);
115+
$this->assertTrue($res->indexes[0]->sort);
116+
$this->assertTrue($res->indexes[0]->search);
117+
$this->assertEquals('Identifiserer en bibliografisk enhet', $res->indexes[0]->title);
118+
$this->assertEquals('bs.objektid', $res->indexes[0]->maps[0]);
119+
120+
}
121+
122+
}

tests/RecordsTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,19 @@ public function testIterating()
1515

1616
$client = new Client($uri);
1717
$records = new Records($cql, $client, $http);
18+
$this->assertNull($records->getError());
1819
$records->rewind();
20+
$this->assertNull($records->getError());
1921

2022
$this->assertEquals(1, $records->key());
23+
$this->assertTrue($records->valid());
2124
$records->next();
2225
$records->next();
2326
$this->assertEquals(3, $records->key());
27+
$this->assertTrue($records->valid());
2428
$records->rewind();
2529
$this->assertEquals(1, $records->key());
30+
$this->assertTrue($records->valid());
2631

2732
$i = 0;
2833
foreach ($records as $rec) {

0 commit comments

Comments
 (0)