Skip to content

Commit

Permalink
Migrate to guzzlehttp/guzzle (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrliptontea authored May 15, 2024
1 parent 7484c6d commit ab15262
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 38 deletions.
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@ workflows:
php_version:
- '7.3'
- '7.4'
# - '8.0'
# - '8.3'
- '8.0'
- '8.3'
13 changes: 6 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,23 @@
"homepage": "https://github.com/talis/SRUclient-php",
"type": "library",
"license": "MIT",
"authors":[
"authors": [
{
"name":"Ross Singer",
"name": "Ross Singer",
"email": "[email protected]",
"homepage":"http://engineering.talis.com"
"homepage": "http://engineering.talis.com"
}
],
"require":{
"require": {
"php": ">=7.3",
"monolog/monolog": ">=1.5.0",
"guzzle/guzzle":"~3.7"
"guzzlehttp/guzzle": "^6.5.8 || ^7.4.5"
},
"require-dev": {
"squizlabs/php_codesniffer": "3.*",
"phpunit/phpunit": "^8.5.38",
"pear/file_marc": "1.1.2"
},
"autoload":{
"autoload": {
"psr-4": {
"SRU\\": "src/SRU/"
}
Expand Down
56 changes: 28 additions & 28 deletions src/SRU/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace SRU;

use GuzzleHttp\RequestOptions;

class Client
{
/**
Expand Down Expand Up @@ -35,7 +37,7 @@ class Client
private $defaultSRUVersion = "1.1";

/**
* @var \Guzzle\Http\Client
* @var \GuzzleHttp\Client
*/
private $httpClient;

Expand Down Expand Up @@ -71,13 +73,14 @@ public function explain($raw = false)
$explainResponse = $this->fetch(
['version' => $this->getDefaultSRUVersion(), 'operation' => 'explain']
);
$body = (string) $explainResponse->getBody();
if ($raw) {
$explain = $explainResponse->getBody();
} else {
$explain = new \DOMDocument();
$explain->loadXML($explainResponse->getBody());
return $body;
}

$explain = new \DOMDocument();
$explain->loadXML($body);

return $explain;
}

Expand Down Expand Up @@ -118,15 +121,16 @@ public function searchRetrieve($query, $options = [], $raw = false)
$searchRetrieveResponse = $this->fetch(
array_merge($defaultOptions, $options, ['operation' => 'searchRetrieve', 'query' => $query])
);
$body = (string) $searchRetrieveResponse->getBody();
if ($raw) {
$searchRetrieve = $searchRetrieveResponse->getBody();
} else {
$searchXML = new \DOMDocument();
$searchXML->loadXML($searchRetrieveResponse->getBody());

$searchRetrieve = new SearchRetrieveResponse($searchXML);
return $body;
}

$searchXML = new \DOMDocument();
$searchXML->loadXML($body);

$searchRetrieve = new SearchRetrieveResponse($searchXML);

return $searchRetrieve;
}

Expand All @@ -149,11 +153,12 @@ public function scan($scanClause, $options = [], $raw = false)
$scanResponse = $this->fetch(
array_merge($defaultOptions, $options, ['operation' => 'scan', 'scanClause' => $scanClause])
);
$body = (string) $scanResponse->getBody();
if ($raw) {
$scan = $scanResponse->getBody();
} else {
$scan = new ScanResponse($scanResponse->getBody());
return $body;
}

$scan = new ScanResponse($body);
return $scan;
}

Expand Down Expand Up @@ -211,10 +216,9 @@ public function indexes()
/**
* Performs the HTTP request based on the defined request method
* @param array $args
* @param array $options
* @return \Guzzle\Http\Message\Response
* @return \Psr\Http\Message\ResponseInterface
*/
protected function fetch(array $args = null, array $options = [])
protected function fetch(array $args = [])
{
$client = $this->getHttpClient();

Expand All @@ -225,16 +229,12 @@ protected function fetch(array $args = null, array $options = [])
$url = $this->getBaseUrl() . '?' . http_build_query($args);
}

$request = $client->get($url, $args, $options);
$response = $request->send();

return $response;
} else {
$request = $client->post($this->getBaseUrl(), array(), $args, $options);
$response = $request->send();

return $response;
return $client->get($url);
}

return $client->post($this->getBaseUrl(), [
RequestOptions::FORM_PARAMS => $args,
]);
}

/**
Expand Down Expand Up @@ -314,12 +314,12 @@ public function setDefaultHttpMethod($defaultHttpMethod)

/**
* Lazy loader for the GuzzleClient
* @return \Guzzle\Http\Client
* @return \GuzzleHttp\Client
*/
protected function getHttpClient()
{
if (!$this->httpClient) {
$this->httpClient = new \Guzzle\Http\Client();
$this->httpClient = new \GuzzleHttp\Client();
}
return $this->httpClient;
}
Expand Down
44 changes: 43 additions & 1 deletion tests/unit/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace SRU\tests;

use SRU\Client;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ServerException;
use PHPUnit\Framework\TestCase;
use SRU\Client;

class ClientTest extends TestCase
{
Expand Down Expand Up @@ -113,4 +115,44 @@ public function scanDataProvider(): iterable
yield 'Override default maximumTerms' => ['GET', ['maximumRecords' => '42']];
yield 'Call options' => ['GET', ['version' => '2.0'], ['version' => '3.0', 'responsePosition' => 5]];
}

/**
* @dataProvider clientErrorStatusProvider
*/
public function testThrowsClientException(int $status)
{
$this->expectException(ClientException::class);
$this->expectExceptionCode($status);

$client = new Client(self::$httpbinHost . '/status/' . $status);
$client->explain(true);
}

public function clientErrorStatusProvider(): iterable
{
yield 'Bad Request' => [400];
yield 'Unauthorized' => [401];
yield 'Forbidden' => [403];
yield 'Not Found' => [404];
}

/**
* @dataProvider serverErrorStatusProvider
*/
public function testThrowsServerException(int $status)
{
$this->expectException(ServerException::class);
$this->expectExceptionCode($status);

$client = new Client(self::$httpbinHost . '/status/' . $status);
$client->explain(true);
}

public function serverErrorStatusProvider(): iterable
{
yield 'Internal Server Error' => [500];
yield 'Bad Gateway' => [502];
yield 'Service Unavailable' => [503];
yield 'Gateway Timeout' => [504];
}
}

0 comments on commit ab15262

Please sign in to comment.