Skip to content

Commit 9ed6035

Browse files
committed
Restructure and let Client return a Response object instead of a QuiteSimpleXMLElement object
1 parent 71d7391 commit 9ed6035

File tree

6 files changed

+132
-28
lines changed

6 files changed

+132
-28
lines changed

composer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,10 @@
2525
"Scriptotek\\": "src/"
2626
}
2727
},
28+
"autoload-dev": {
29+
"psr-4": {
30+
"Scriptotek\\Tests\\": "tests/"
31+
}
32+
},
2833
"minimum-stability": "dev"
2934
}

src/Scriptotek/SruClient.php renamed to src/Scriptotek/Sru/Client.php

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

33
use Danmichaelo\QuiteSimpleXMLElement\QuiteSimpleXMLElement;
44
use \Guzzle\Http\Client as HttpClient;
55

6-
class SRUClient {
6+
class Client {
77

88
protected $httpClient;
99
protected $url;
@@ -25,7 +25,7 @@ class SRUClient {
2525
protected $credentials;
2626

2727
/**
28-
* Create a new client object
28+
* Create a new client
2929
*
3030
* @param string $url
3131
* @param array $options Associative array of options
@@ -123,14 +123,7 @@ public function search($cql, $start = 1, $count = 10) {
123123
$res = $this->httpClient->get($url, $options)->send();
124124
$body = $res->getBody(true);
125125

126-
try {
127-
$xml = new QuiteSimpleXMLElement($body);
128-
} catch (\Exception $e) {
129-
throw new \Exception('Invalid response received from SRU service');
130-
}
131-
132-
$xml->registerXPathNamespaces($this->namespaces);
133-
return $xml;
126+
return new Response($body, $this->namespaces);
134127

135128
}
136129

src/Scriptotek/Sru/Response.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php namespace Scriptotek\Sru;
2+
3+
use Danmichaelo\QuiteSimpleXMLElement\QuiteSimpleXMLElement;
4+
5+
class Response {
6+
7+
public $records;
8+
public $error;
9+
10+
protected $rawResponse;
11+
12+
/**
13+
* Create a new response
14+
*
15+
* @param string $text
16+
*/
17+
public function __construct($text)
18+
{
19+
$this->rawResponse = $text;
20+
try {
21+
$doc = new QuiteSimpleXMLElement($text);
22+
} catch (\Exception $e) {
23+
throw new \Exception('Invalid XML received');
24+
}
25+
26+
$doc->registerXPathNamespaces(array(
27+
'srw' => 'http://www.loc.gov/zing/srw/',
28+
'd' => 'http://www.loc.gov/zing/srw/diagnostic/'
29+
));
30+
31+
$this->records = array();
32+
foreach ($doc->xpath('/srw:searchRetrieveResponse/srw:records/srw:record') as $record) {
33+
$this->records[] = $record;
34+
}
35+
36+
//doc->xpath('/srw:searchRetrieveResponse/
37+
38+
}
39+
40+
public function asXml()
41+
{
42+
return $this->rawResponse;
43+
}
44+
45+
public function next()
46+
{
47+
# code...
48+
}
49+
50+
}
51+

tests/SruClientTest.php renamed to tests/ClientTest.php

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
<?php namespace Scriptotek;
1+
<?php namespace Scriptotek\Tests;
22

3-
require 'SruTestCase.php';
4-
5-
use \Guzzle\Http\Message\Response;
3+
use \Guzzle\Http\Message\Response as HttpResponse;
64
use \Mockery as m;
5+
use \Scriptotek\Sru\Client as SruClient;
76

8-
class SruClientTest extends SruTestCase {
7+
class ClientTest extends TestCase {
98

109
protected $url = 'http://sru.my_fictive_host.net';
1110

@@ -15,36 +14,41 @@ class SruClientTest extends SruTestCase {
1514

1615
public function testUrlTo()
1716
{
17+
$sru1 = new SruClient($this->url);
1818
$expectedUrl1 = $this->url . '?version=1.1&operation=searchRetrieve&recordSchema=marcxml&maximumRecords=10&query=isbn%3D123';
1919
$expectedUrl2 = $this->url . '?version=1.1&operation=searchRetrieve&recordSchema=marcxml&maximumRecords=50&query=isbn%3D123&startRecord=2';
20+
21+
$sru3 = new SruClient($this->url, array('schema' => 'CUSTOMSCHEMA'));
2022
$expectedUrl3 = $this->url . '?version=1.1&operation=searchRetrieve&recordSchema=CUSTOMSCHEMA&maximumRecords=10&query=isbn%3D123';
23+
24+
$sru4 = new SruClient($this->url, array('version' => '0.9'));
2125
$expectedUrl4 = $this->url . '?version=0.9&operation=searchRetrieve&recordSchema=marcxml&maximumRecords=10&query=isbn%3D123';
22-
$sru1 = new SruClient($this->url);
23-
$sru2 = new SruClient($this->url, array('schema' => 'CUSTOMSCHEMA'));
24-
$sru3 = new SruClient($this->url, array('version' => '0.9'));
2526

2627
$this->assertEquals($expectedUrl1, $sru1->urlTo('isbn=123'));
2728
$this->assertEquals($expectedUrl2, $sru1->urlTo('isbn=123', 2, 50));
28-
$this->assertEquals($expectedUrl3, $sru2->urlTo('isbn=123'));
29-
$this->assertEquals($expectedUrl4, $sru3->urlTo('isbn=123'));
29+
$this->assertEquals($expectedUrl3, $sru3->urlTo('isbn=123'));
30+
$this->assertEquals($expectedUrl4, $sru4->urlTo('isbn=123'));
3031
}
3132

3233
public function testSearch()
3334
{
3435
$http = $this->basicHttpMock($this->simple_response);
3536
$sru = new SruClient($this->url, null, $http);
3637

37-
$this->assertXmlStringEqualsXmlString($this->simple_response, $sru->search('test')->asXml());
38+
$this->assertXmlStringEqualsXmlString(
39+
$this->simple_response,
40+
$sru->search('test')->asXml()
41+
);
3842
}
3943

40-
public function testSearchAuth()
44+
public function testSearchWithAuth()
4145
{
4246
$credentials = array('secretuser', 'secretpass');
4347

4448
$request = m::mock();
4549
$request->shouldReceive('send')
4650
->once()
47-
->andReturn(new Response(200, null, $this->simple_response));
51+
->andReturn(new HttpResponse(200, null, $this->simple_response));
4852

4953
$http = m::mock();
5054
$http->shouldReceive('get')

tests/ResponseTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php namespace Scriptotek\Tests;
2+
3+
use \Guzzle\Http\Message\Response as HttpResponse;
4+
use \Mockery as m;
5+
use \Scriptotek\Sru\Response as SruResponse;
6+
7+
class ResponseTest extends TestCase {
8+
9+
public function testParse()
10+
{
11+
$res = new SruResponse('<?xml version="1.0" encoding="UTF-8" ?>
12+
<srw:searchRetrieveResponse
13+
xmlns:srw="http://www.loc.gov/zing/srw/"
14+
xmlns:xcql="http://www.loc.gov/zing/cql/xcql/"
15+
>
16+
<srw:version>1.1</srw:version>
17+
<srw:numberOfRecords>303</srw:numberOfRecords>
18+
<srw:records>
19+
<srw:record>
20+
<srw:recordSchema>marcxchange</srw:recordSchema>
21+
<srw:recordPacking>xml</srw:recordPacking>
22+
<srw:recordPosition>1</srw:recordPosition>
23+
<srw:recordData>Record 1</srw:recordData>
24+
</srw:record>
25+
<srw:record>
26+
<srw:recordSchema>marcxchange</srw:recordSchema>
27+
<srw:recordPacking>xml</srw:recordPacking>
28+
<srw:recordPosition>2</srw:recordPosition>
29+
<srw:recordData>Record 2</srw:recordData>
30+
</srw:record>
31+
</srw:records>
32+
<srw:nextRecordPosition>3</srw:nextRecordPosition>
33+
<srw:echoedSearchRetrieveRequest>
34+
<srw:operation>searchRetrieve</srw:operation>
35+
<srw:version>1.1</srw:version>
36+
<srw:query>bs.avdelingsamling = &quot;urealastr&quot; AND bs.lokal-klass = &quot;k C11?&quot;</srw:query>
37+
<srw:startRecord>1</srw:startRecord>
38+
<srw:maximumRecords>2</srw:maximumRecords>
39+
<srw:recordSchema>marcxchange</srw:recordSchema>
40+
</srw:echoedSearchRetrieveRequest>
41+
<srw:extraResponseData>
42+
<responseDate>2014-03-28T12:09:50Z</responseDate>
43+
</srw:extraResponseData>
44+
</srw:searchRetrieveResponse>');
45+
46+
$this->assertCount(2, $res->records);
47+
$this->assertNull($res->error);
48+
49+
}
50+
51+
}

tests/SruTestCase.php renamed to tests/TestCase.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
<?php namespace Scriptotek;
1+
<?php namespace Scriptotek\Tests;
22

3-
use \Guzzle\Http\Message\Response;
3+
use \Guzzle\Http\Message\Response as HttpResponse;
44
use \Mockery as m;
55

6-
class SruTestCase extends \PHPUnit_Framework_TestCase {
6+
class TestCase extends \PHPUnit_Framework_TestCase {
77

88

99
protected function basicHttpMock($response)
1010
{
1111
$request = m::mock();
1212
$request->shouldReceive('send')
1313
->once()
14-
->andReturn(new Response(200, null, $response));
14+
->andReturn(new HttpResponse(200, null, $response));
1515

1616
$http = m::mock();
1717
$http->shouldReceive('get')

0 commit comments

Comments
 (0)