From 623d82eb37c8bfa023227bf0e4487157d4d394be Mon Sep 17 00:00:00 2001 From: Jared Whiklo Date: Wed, 26 Apr 2023 16:18:26 -0500 Subject: [PATCH] Update dependencies (#99) * Update guzzle and easyrdf versions * Update php requirement * Update test dependencies * Need the base uri passed in to store it. * Fix actions * Remove mockery * Allow passing in a handler stack * Allowing string, resource or stream to saveResource/createResource --- .github/workflows/build-2.x.yml | 82 +++++++++++ composer.json | 19 ++- src/FedoraApi.php | 253 +++++++++++++------------------- src/IFedoraApi.php | 127 +++++++++------- test/ChulloTestBase.php | 32 ++++ test/CreateClientTest.php | 42 ++++++ test/CreateGraphTest.php | 36 +++-- test/CreateResourceTest.php | 35 ++--- test/CreateVersionTest.php | 69 +++++---- test/DeleteResourceTest.php | 35 +++-- test/GetBaseUriTest.php | 21 ++- test/GetGraphTest.php | 28 ++-- test/GetResourceHeadersTest.php | 33 ++--- test/GetResourceOptionsTest.php | 28 ++-- test/GetResourceTest.php | 65 ++++---- test/GetTimemapURITest.php | 32 ++-- test/GetVersionsTest.php | 80 +++++----- test/ModifyResourceTest.php | 34 +++-- test/SaveGraphTest.php | 31 ++-- test/SaveResourceTest.php | 31 ++-- 20 files changed, 635 insertions(+), 478 deletions(-) create mode 100644 .github/workflows/build-2.x.yml create mode 100644 test/ChulloTestBase.php create mode 100644 test/CreateClientTest.php diff --git a/.github/workflows/build-2.x.yml b/.github/workflows/build-2.x.yml new file mode 100644 index 0000000..70a164b --- /dev/null +++ b/.github/workflows/build-2.x.yml @@ -0,0 +1,82 @@ +# This is a basic workflow to help you get started with Actions + +name: CI + +# Controls when the action will run. +on: + # Triggers the workflow on push or pull request events but only for the 1.x branch + push: + branches: [ 2.x ] + pull_request: + branches: [ 2.x ] + + # Allows you to run this workflow manually from the Actions tab + workflow_dispatch: + +# A workflow run is made up of one or more jobs that can run sequentially or in parallel +jobs: + # This workflow contains a single job called "build" + build: + # The type of runner that the job will run on + runs-on: ubuntu-latest + continue-on-error: ${{ matrix.experimental }} + strategy: + fail-fast: false + matrix: + php-versions: ["7.4", "8.0", "8.1"] + experimental: [false] + + name: PHP ${{ matrix.php-versions }} + + # Steps represent a sequence of tasks that will be executed as part of the job + steps: + # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it + - name: Checkout code + uses: actions/checkout@v3 + with: + path: build_dir + + - name: Checkout islandora_ci + uses: actions/checkout@v3 + with: + repository: islandora/islandora_ci + ref: github-actions + path: islandora_ci + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php-versions }} + tools: composer:v2 + + - name: Set environment variables + run: | + echo "SCRIPT_DIR=$GITHUB_WORKSPACE/islandora_ci" >> $GITHUB_ENV + + - name: Get composer cache directory + id: composer-cache + run: echo "composer-cache-dir=$(composer config cache-files-dir)" >> $GITHUB_ENV + + - name: Cache Composer dependencies + uses: actions/cache@v3 + with: + path: ${{ env.composer-cache-dir }} + key: ${{ runner.os }}-composer-${{ matrix.php-version }}-${{ hashFiles('**/composer.lock') }} + restore-keys: ${{ runner.os }}-composer-${{ matrix.php-version }} + + - name: composer install + run: | + cd $GITHUB_WORKSPACE/build_dir + composer install + + - name: Line endings + run: $SCRIPT_DIR/line_endings.sh $GITHUB_WORKSPACE + + - name: Test scripts + run: | + cd $GITHUB_WORKSPACE/build_dir + composer test + + - name: Codecov + uses: codecov/codecov-action@v3 + diff --git a/composer.json b/composer.json index 78c75cb..d3823c4 100644 --- a/composer.json +++ b/composer.json @@ -7,16 +7,16 @@ "issues": "https://github.com/Islandora/documentation/issues" }, "require": { - "php": ">=7.3", - "guzzlehttp/guzzle": "^6.1.0", - "easyrdf/easyrdf": "^0.9 || ^1", + "php": ">=7.4", + "guzzlehttp/guzzle": "^6.5 || ^7.4", + "easyrdf/easyrdf": "^1", "ml/json-ld": "^1.0.4" }, "require-dev": { + "donatj/mock-webserver": "^2.6", "phpunit/phpunit": "^9.0", "squizlabs/php_codesniffer": "^3.0", - "sebastian/phpcpd": "^6.0", - "mockery/mockery": "^0.9" + "sebastian/phpcpd": "^6.0" }, "scripts": { "check": [ @@ -48,6 +48,13 @@ } ], "autoload": { - "psr-4": {"Islandora\\Chullo\\": "src/"} + "psr-4": { + "Islandora\\Chullo\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + "Islandora\\Chullo\\Test\\": "test/" + } } } diff --git a/src/FedoraApi.php b/src/FedoraApi.php index 83b95cc..106a2b8 100644 --- a/src/FedoraApi.php +++ b/src/FedoraApi.php @@ -18,68 +18,79 @@ namespace Islandora\Chullo; +use EasyRdf\Graph; use GuzzleHttp\Client; +use GuzzleHttp\HandlerStack; use GuzzleHttp\Psr7; use Psr\Http\Message\ResponseInterface; -use Symfony\Component\HttpFoundation\Response; use \RuntimeException; -// phpcs:disable -if (class_exists('\EasyRdf_Graph')) { - class_alias('\EasyRdf_Graph', ' \EasyRdf\Graph'); -} -// phpcs:enable - -// /** * Default implementation of IFedoraApi using Guzzle. */ class FedoraApi implements IFedoraApi { - protected $client; + /** + * The client. + * + * @var \GuzzleHttp\Client + */ + private Client $client; + + /** + * Fedora Base URI. + * + * @var string + */ + private string $base_uri; /** * @codeCoverageIgnore */ - public function __construct(Client $client) + private function __construct(string $uri, ?HandlerStack $stack) { - $this->client = $client; + $normalized = rtrim($uri); + $normalized = rtrim($normalized, '/') . '/'; + $guzzle_opts = ['base_uri' => $normalized]; + if (!is_null($stack)) { + $guzzle_opts['handler'] = $stack; + } + $this->client = new Client($guzzle_opts); + $this->base_uri = $normalized; } /** * @codeCoverageIgnore */ - public static function create($fedora_rest_url) + public static function create(string $fedora_rest_url): self { - $normalized = rtrim($fedora_rest_url); - $normalized = rtrim($normalized, '/') . '/'; - $guzzle = new Client(['base_uri' => $normalized]); - return new static($guzzle); + return new static($fedora_rest_url, null); } /** - * Gets the Fedora base uri (e.g. http://localhost:8080/fcrepo/rest) - * - * @return string + * @codeCoverageIgnore */ - public function getBaseUri() + public static function createWithHandler(string $fedora_rest_url, HandlerStack $stack): self { - return $this->client->getConfig('base_uri'); + return new static($fedora_rest_url, $stack); } /** - * Gets a Fedora resource. - * - * @param string $uri Resource URI - * @param array $headers HTTP Headers - * - * @return ResponseInterface + * @inheritDoc + */ + public function getBaseUri(): string + { + return $this->base_uri; + } + + /** + * @inheritDoc */ public function getResource( - $uri = "", - $headers = [] - ) { + string $uri = "", + array $headers = [] + ): ResponseInterface { // Set headers $options = ['http_errors' => false, 'headers' => $headers]; @@ -92,18 +103,12 @@ public function getResource( } /** - * Gets a Fedora resoure's headers. - * - * @param string $uri Resource URI - * @param array $headers HTTP Headers - * - * @return ResponseInterface + * @inheritDoc */ public function getResourceHeaders( - $uri = "", - $headers = [] - ) { - + string $uri = "", + array $headers = [] + ): ResponseInterface { // Send the request. return $this->client->request( 'HEAD', @@ -113,17 +118,12 @@ public function getResourceHeaders( } /** - * Gets information about the supported HTTP methods, etc., for a Fedora resource. - * - * @param string $uri Resource URI - * @param array $headers HTTP Headers - * - * @return ResponseInterface + * @inheritDoc */ public function getResourceOptions( - $uri = "", - $headers = [] - ) { + string $uri = "", + array $headers = [] + ): ResponseInterface { return $this->client->request( 'OPTIONS', $uri, @@ -132,19 +132,13 @@ public function getResourceOptions( } /** - * Creates a new resource in Fedora. - * - * @param string $uri Resource URI - * @param string $content String or binary content - * @param array $headers HTTP Headers - * - * @return ResponseInterface + * @inheritDoc */ public function createResource( - $uri = "", + string $uri = "", $content = null, - $headers = [] - ) { + array $headers = [] + ): ResponseInterface { $options = ['http_errors' => false]; // Set content. @@ -161,19 +155,13 @@ public function createResource( } /** - * Saves a resource in Fedora. - * - * @param string $uri Resource URI - * @param string $content String or binary content - * @param array $headers HTTP Headers - * - * @return ResponseInterface + * @inheritDoc */ public function saveResource( - $uri, + string $uri, $content = null, - $headers = [] - ) { + array $headers = [] + ): ResponseInterface { $options = ['http_errors' => false]; // Set content. @@ -190,19 +178,13 @@ public function saveResource( } /** - * Modifies a resource using a SPARQL Update query. - * - * @param string $uri Resource URI - * @param string $sparql SPARQL Update query - * @param array $headers HTTP Headers - * - * @return ResponseInterface + * @inheritDoc */ public function modifyResource( - $uri, - $sparql = "", - $headers = [] - ) { + string $uri, + string $sparql = "", + array $headers = [] + ): ResponseInterface { $options = ['http_errors' => false]; // Set content. @@ -220,17 +202,12 @@ public function modifyResource( } /** - * Issues a DELETE request to Fedora. - * - * @param string $uri Resource URI - * @param array $headers HTTP Headers - * - * @return ResponseInterface + * @inheritDoc */ public function deleteResource( - $uri = '', - $headers = [] - ) { + string $uri = '', + array $headers = [] + ): ResponseInterface { $options = ['http_errors' => false, 'headers' => $headers]; return $this->client->request( @@ -241,19 +218,13 @@ public function deleteResource( } /** - * Saves RDF in Fedora. - * - * @param EasyRdf_Resource $graph Graph to save - * @param string $uri Resource URI - * @param array $headers HTTP Headers - * - * @return ResponseInterface + * @inheritDoc */ public function saveGraph( - \EasyRdf\Graph $graph, - $uri = '', - $headers = [] - ) { + Graph $graph, + string $uri = '', + array $headers = [] + ): ResponseInterface { // Serialze the rdf. $turtle = $graph->serialise('turtle'); @@ -269,20 +240,14 @@ public function saveGraph( } /** - * Creates RDF in Fedora. - * - * @param EasyRdf_Resource $graph Graph to save - * @param string $uri Resource URI - * @param array $headers HTTP Headers - * - * @return ResponseInterface + * @inheritDoc */ public function createGraph( - \EasyRdf\Graph $graph, - $uri = '', - $headers = [] - ) { - // Serialze the rdf. + Graph $graph, + string $uri = '', + array $headers = [] + ): ResponseInterface { + // Serialize the rdf. $turtle = $graph->serialise('turtle'); // Checksum it. @@ -296,19 +261,14 @@ public function createGraph( return $this->createResource($uri, $turtle, $headers); } - /** - * Gets RDF in Fedora. - * - * @param ResponseInterface $request Response received - * - * @return \EasyRdf\Graph + * @inheritDoc */ - public function getGraph(ResponseInterface $response) + public function getGraph(ResponseInterface $response): Graph { // Extract rdf as response body and return Easy_RDF Graph object. $rdf = $response->getBody()->getContents(); - $graph = new \EasyRdf\Graph(); + $graph = new Graph(); if (!empty($rdf)) { $graph->parse($rdf, 'jsonld'); } @@ -316,23 +276,19 @@ public function getGraph(ResponseInterface $response) } /** - * Creates version in Fedora. - * @param string $uri Fedora Resource URI - * @param string $timestamp Timestamp for Memento version - * @param string $content String or binary content - * @param array $header HTTP Headers - * - * @return ResponseInterface + * @inheritDoc */ public function createVersion( $uri = '', $timestamp = '', $content = null, $headers = [] - ) { + ): ResponseInterface { $timemap_uri = $this->getTimemapURI($uri, $headers); if ($timemap_uri == null) { - throw new \RuntimeException('Timemap URI is null, cannot create version'); + throw new RuntimeException( + 'Timemap URI is null, cannot create version' + ); } $options = ['http_errors' => false]; if ($timestamp != '' && $content != null) { @@ -349,44 +305,43 @@ public function createVersion( } /** - * Gets list of versions in Fedora. - * @param string $uri Fedora Resource URI - * @param array $header HTTP Headers - * - * @return ResponseInterface + * @inheritDoc */ public function getVersions( $uri = '', $headers = [] - ) { + ): ResponseInterface { $timemap_uri = $this->getTimemapURI($uri, $headers); if ($timemap_uri == null) { - throw new \RuntimeException('Timemap URI is null, cannot create version'); + throw new RuntimeException( + 'Timemap URI is null, cannot create version' + ); } - $options = ['http_errors' => false, 'headers' => $headers]; - return $this->client->request( - 'GET', - $timemap_uri, - $options - ); + + return $this->getResource($timemap_uri, $headers); } /** * Helper method to get the Headers for a resource * and parse the timemap header from it * @param string $uri Fedora Resource URI - * @param array $header HTTP Headers + * @param array $headers HTTP Headers * - * @return string + * @return string|null */ public function getTimemapURI( - $uri = '', - $headers = [] - ) { + string $uri = '', + array $headers = [] + ): ?string { $resource_headers = $this->getResourceHeaders($uri, $headers); - $parsed_link_headers = Psr7\parse_header($resource_headers->getHeader('Link')); + $parsed_link_headers = Psr7\Header::parse( + $resource_headers->getHeader('Link') + ); $timemap_uri = null; - $timemap_index = array_search('timemap', array_column($parsed_link_headers, 'rel')); + $timemap_index = array_search( + 'timemap', + array_column($parsed_link_headers, 'rel') + ); if (is_int($timemap_index)) { $timemap_uri = $parsed_link_headers[$timemap_index][0]; $timemap_uri = trim($timemap_uri, "<> \t\n\r\0\x0B"); diff --git a/src/IFedoraApi.php b/src/IFedoraApi.php index f08587e..c2ce595 100644 --- a/src/IFedoraApi.php +++ b/src/IFedoraApi.php @@ -17,14 +17,9 @@ namespace Islandora\Chullo; +use EasyRdf\Graph; use Psr\Http\Message\ResponseInterface; -// phpcs:disable -if (class_exists('\EasyRdf_Graph') && !class_exists('\EasyRdf\Graph')) { - class_alias('\EasyRdf_Graph', '\EasyRdf\Graph'); -} -// phpcs:enable - /** * Interface for Fedora interaction. All functions return a PSR-7 response. */ @@ -35,19 +30,19 @@ interface IFedoraApi * * @return string */ - public function getBaseUri(); + public function getBaseUri(): string; /** * Gets a Fedora resource. * - * @param string $uri Resource URI - * @param array $headers HTTP Headers + * @param string $uri Resource URI + * @param array $headers HTTP Headers * @return \Psr\Http\Message\ResponseInterface */ public function getResource( - $uri = "", - $headers = [] - ); + string $uri = "", + array $headers = [] + ): ResponseInterface; /** * Gets a Fedora resoure's headers. @@ -57,9 +52,9 @@ public function getResource( * @return \Psr\Http\Message\ResponseInterface */ public function getResourceHeaders( - $uri = "", - $headers = [] - ); + string $uri = "", + array $headers = [] + ): ResponseInterface; /** * Gets information about the supported HTTP methods, etc., for a Fedora resource. @@ -69,37 +64,37 @@ public function getResourceHeaders( * @return \Psr\Http\Message\ResponseInterface */ public function getResourceOptions( - $uri = "", - $headers = [] - ); + string $uri = "", + array $headers = [] + ): ResponseInterface; /** * Creates a new resource in Fedora. * - * @param string $uri Resource URI - * @param string $content String or binary content - * @param array $headers HTTP Headers + * @param string $uri Resource URI + * @param mixed|null $content String, resource from fopen() or stream content + * @param array $headers HTTP Headers * @return \Psr\Http\Message\ResponseInterface */ public function createResource( - $uri = "", + string $uri = "", $content = null, - $headers = [] - ); + array $headers = [] + ): ResponseInterface; /** * Saves a resource in Fedora. * - * @param string $uri Resource URI - * @param string $content String or binary content - * @param array $headers HTTP Headers + * @param string $uri Resource URI + * @param mixed|null $content String, resource from fopen() or stream content + * @param array $headers HTTP Headers * @return \Psr\Http\Message\ResponseInterface */ public function saveResource( - $uri, + string $uri, $content = null, - $headers = [] - ); + array $headers = [] + ): ResponseInterface; /** * Modifies a resource using a SPARQL Update query. @@ -110,10 +105,10 @@ public function saveResource( * @return \Psr\Http\Message\ResponseInterface */ public function modifyResource( - $uri, - $sparql = "", - $headers = [] - ); + string $uri, + string $sparql = "", + array $headers = [] + ): ResponseInterface; /** * Issues a DELETE request to Fedora. @@ -123,38 +118,64 @@ public function modifyResource( * @return \Psr\Http\Message\ResponseInterface */ public function deleteResource( - $uri = "", - $headers = [] - ); + string $uri = "", + array $headers = [] + ): ResponseInterface; /** * Saves RDF in Fedora. * - * @param EasyRdf_Resource $rdf Graph to save + * @param \EasyRdf\Graph $graph Graph to save * @param string $uri Resource URI * @param array $headers HTTP Headers * @return \Psr\Http\Message\ResponseInterface */ public function saveGraph( - \EasyRdf\Graph $graph, - $uri = "", - $headers = [] - ); + Graph $graph, + string $uri = "", + array $headers = [] + ): ResponseInterface; + + /** + * Creates RDF in Fedora. + * + * @param \EasyRdf\Graph $graph Graph to save + * @param string $uri Resource URI + * @param array $headers HTTP Headers + * + * @return ResponseInterface + */ + public function createGraph( + Graph $graph, + string $uri = '', + array $headers = [] + ): ResponseInterface; + + /** + * Gets RDF in Fedora. + * + * @param ResponseInterface $response Response received + * + * @return \EasyRdf\Graph + * @throws \EasyRdf\Exception Unable to parse graph. + */ + public function getGraph(ResponseInterface $response): Graph; /** * Creates a version of the resource in Fedora. * - * @param string $uri Resource URI - * @param string $timestamp Timestamp in RFC-1123 format - * @param array $headers HTTP Headers + * @param string $uri Resource URI + * @param string $timestamp Timestamp in RFC-1123 format + * @param string|null $content Content for the version + * @param array $headers HTTP Headers * @return \Psr\Http\Message\ResponseInterface */ public function createVersion( - $uri = "", - $timestamp = "", - $content = null, - $headers = [] - ); + string $uri = "", + string $timestamp = "", + ?string $content = null, + array $headers = [] + ): ResponseInterface; /** * Creates a version of the resource in Fedora. @@ -164,7 +185,7 @@ public function createVersion( * @return \Psr\Http\Message\ResponseInterface */ public function getVersions( - $uri = "", - $headers = [] - ); + string $uri = "", + array $headers = [] + ): ResponseInterface; } diff --git a/test/ChulloTestBase.php b/test/ChulloTestBase.php new file mode 100644 index 0000000..3aa8e7d --- /dev/null +++ b/test/ChulloTestBase.php @@ -0,0 +1,32 @@ +start(); + } + + /** + * @inheritDoc + */ + public static function tearDownAfterClass(): void + { + self::$webserver->stop(); + } +} diff --git a/test/CreateClientTest.php b/test/CreateClientTest.php new file mode 100644 index 0000000..b5bd5b5 --- /dev/null +++ b/test/CreateClientTest.php @@ -0,0 +1,42 @@ +setResponseOfPath( + "/rest/path/to/resource", + new Response( + "SOME CONTENT", + ['X-FOO' => 'Fedora4'], + 200 + ) + ); + + $stack = HandlerStack::create(); + $stack->setHandler(new CurlHandler()); + + $stack->push(Middleware::mapResponse(function (ResponseInterface $response) { + return $response->withHeader('X-ISLANDORA', 'my header'); + })); + + $api = FedoraApi::createWithHandler(self::$webserver->getHost(), $stack); + + $result = $api->getResource($test_uri); + $this->assertSame((string)$result->getBody(), "SOME CONTENT"); + $this->assertSame($result->getHeader('X-FOO'), ['Fedora4']); + $this->assertSame($result->getHeader('X-ISLANDORA'), ['my header']); + } +} diff --git a/test/CreateGraphTest.php b/test/CreateGraphTest.php index c89f7ad..1e6c4d0 100644 --- a/test/CreateGraphTest.php +++ b/test/CreateGraphTest.php @@ -1,32 +1,36 @@ setResponseOfPath( + "/test_create", + new ResponseByMethod([ + ResponseByMethod::METHOD_POST => + new Response( + "", + [], + 204 + ) + ]) + ); - $handler = HandlerStack::create($mock); - $guzzle = new Client(['handler' => $handler]); - $api = new FedoraApi($guzzle); + $api = FedoraApi::create(self::$webserver->getHost()); - $result = $api->createGraph(new \EasyRdf\Graph()); + $result = $api->createGraph(new Graph(), $test_uri); $this->assertEquals(204, $result->getStatusCode()); } } diff --git a/test/CreateResourceTest.php b/test/CreateResourceTest.php index 9457ccd..9ee8d4c 100644 --- a/test/CreateResourceTest.php +++ b/test/CreateResourceTest.php @@ -1,34 +1,35 @@ "SOME URI"]), - ] + $test_uri = self::$webserver->setResponseOfPath( + '/some_uri', + new ResponseByMethod([ + ResponseByMethod::METHOD_POST => + new Response( + "", + ['Location' => "SOME URI"], + 201 + ) + ]) ); - $handler = HandlerStack::create($mock); - $guzzle = new Client(['handler' => $handler]); - $api = new FedoraApi($guzzle); + $api = FedoraApi::create(self::$webserver->getHost()); - $result = $api->createResource(""); + $result = $api->createResource($test_uri); $this->assertEquals($result->getHeaderLine("Location"), "SOME URI"); $this->assertEquals(201, $result->getStatusCode(), "Expected a 201 response."); } diff --git a/test/CreateVersionTest.php b/test/CreateVersionTest.php index 81ec84f..be86ff8 100644 --- a/test/CreateVersionTest.php +++ b/test/CreateVersionTest.php @@ -1,60 +1,65 @@ ';rel="timemap"']), - new Response(201, ['Location' => "SOME URI"]) - ] + $test_uri_timemap = self::$webserver->setResponseOfPath( + "/rest/path/to/resource/fcr:versions", + new ResponseByMethod([ + ResponseByMethod::METHOD_POST => new Response("", ['Location' => "SOME URI"], 201) + ]) + ); + $test_uri = self::$webserver->setResponseOfPath( + "/rest/path/to/resource", + new ResponseByMethod([ + ResponseByMethod::METHOD_HEAD => new Response( + "", + ['Link' => "<$test_uri_timemap>; rel=\"timemap\""], + 200 + ) + ]) ); - $handler = HandlerStack::create($mock); - $guzzle = new Client(['handler' => $handler]); - $api = new FedoraApi($guzzle); + $api = FedoraApi::create(self::$webserver->getHost()); $date = new DateTime(); $timestamp = $date->format("D, d M Y H:i:s O"); $content = "test"; - $result = $api->createVersion('', $timestamp, $content); + $result = $api->createVersion($test_uri, $timestamp, $content); $this->assertEquals(201, $result->getStatusCode()); } /** - * @covers Islandora\Chullo\FedoraApi::createVersion Exception - * @uses GuzzleHttp\Client + * @covers \Islandora\Chullo\FedoraApi::createVersion Exception + * @uses \GuzzleHttp\Client */ public function testThrowsExceptionWithoutTimemapUri() { - $mock = new MockHandler( - [ - new Response(200, []), - new Response(201, ['Location' => "SOME URI"]) - ] + $test_uri = self::$webserver->setResponseOfPath( + "/rest/path/to/resource", + new ResponseByMethod([ + ResponseByMethod::METHOD_GET => new Response( + "", + [], + 200 + ) + ]) ); - - $handler = HandlerStack::create($mock); - $guzzle = new Client(['handler' => $handler]); - $api = new FedoraApi($guzzle); + $api = FedoraApi::create(self::$webserver->getHost()); $this->expectException(\RuntimeException::class); - $result = $api->createVersion(''); + $api->createVersion($test_uri); } } diff --git a/test/DeleteResourceTest.php b/test/DeleteResourceTest.php index f43b1bb..d04b0e1 100644 --- a/test/DeleteResourceTest.php +++ b/test/DeleteResourceTest.php @@ -1,32 +1,35 @@ setResponseOfPath( + '/rest/some/path', + new ResponseByMethod([ + ResponseByMethod::METHOD_DELETE => + new Response( + "", + [], + 204 + ), + ]) + ); - $handler = HandlerStack::create($mock); - $guzzle = new Client(['handler' => $handler]); - $api = new FedoraApi($guzzle); + $api = FedoraApi::create(self::$webserver->getHost()); - $result = $api->deleteResource(""); + $result = $api->deleteResource($test_uri); $this->assertEquals(204, $result->getStatusCode()); } } diff --git a/test/GetBaseUriTest.php b/test/GetBaseUriTest.php index dceb259..b0ac242 100644 --- a/test/GetBaseUriTest.php +++ b/test/GetBaseUriTest.php @@ -1,27 +1,24 @@ 'http://localhost:8080/fcrepo/rest']); - $api = new FedoraApi($guzzle); + $input = 'http://localhost:8080/fcrepo/rest'; + $api = FedoraApi::create($input); + # Base Uri always has a forward slash on the end. + $expected = 'http://localhost:8080/fcrepo/rest/'; $baseUri = $api->getBaseUri(); - $this->assertEquals($baseUri, 'http://localhost:8080/fcrepo/rest'); + $this->assertEquals($expected, $baseUri); } } diff --git a/test/GetGraphTest.php b/test/GetGraphTest.php index 6e14932..d1502e9 100644 --- a/test/GetGraphTest.php +++ b/test/GetGraphTest.php @@ -1,20 +1,21 @@ 'application/ld+json'], $fixture), - ]); + $test_uri = self::$webserver->setResponseOfPath( + "/rest/path/to/resource", + new Response( + $fixture, + ['Content-Type' => 'application/ld+json'], + 200 + ) + ); - $handler = HandlerStack::create($mock); - $guzzle = new Client(['handler' => $handler]); - $api = new FedoraApi($guzzle); + $api = FedoraApi::create(self::$webserver->getHost()); - $result = $api->getResource(""); + $result = $api->getResource($test_uri); $graph = $api->getGraph($result); $title = (string)$graph->get( diff --git a/test/GetResourceHeadersTest.php b/test/GetResourceHeadersTest.php index 5b86384..2e29e0d 100644 --- a/test/GetResourceHeadersTest.php +++ b/test/GetResourceHeadersTest.php @@ -1,24 +1,19 @@ '200 OK', 'ETag' => "bbdd92e395800153a686773f773bcad80a51f47b", @@ -31,18 +26,18 @@ public function testReturnsHeadersOn200() 'multipart/form-data,application/sparql-update', 'Allow' => 'MOVE,COPY,DELETE,POST,HEAD,GET,PUT,PATCH,OPTIONS', ]; - - $mock = new MockHandler( - [ - new Response(200, $headers) - ] + $test_uri = self::$webserver->setResponseOfPath( + "/rest/path/to/resource", + new Response( + "", + $headers, + 200 + ) ); - $handler = HandlerStack::create($mock); - $guzzle = new Client(['handler' => $handler]); - $api = new FedoraApi($guzzle); + $api = FedoraApi::create(self::$webserver->getHost()); - $result = $api->getResourceHeaders(""); + $result = $api->getResourceHeaders($test_uri); $this->assertEquals(200, $result->getStatusCode()); $this->assertTrue($result->hasHeader("etag")); diff --git a/test/GetResourceOptionsTest.php b/test/GetResourceOptionsTest.php index 752fa02..bba90ea 100644 --- a/test/GetResourceOptionsTest.php +++ b/test/GetResourceOptionsTest.php @@ -1,20 +1,16 @@ 'text/turtle,text/rdf+n3,application/n3,text/n3,application/rdf+xml,' . 'application/n-triples,multipart/form-data,application/sparql-update', ]; - $mock = new MockHandler([ - new Response(200, $headers), - ]); + $test_uri = self::$webserver->setResponseOfPath( + "/test_options", + new Response("", $headers, 200) + ); + $api = FedoraApi::create(self::$webserver->getHost()); - $handler = HandlerStack::create($mock); - $guzzle = new Client(['handler' => $handler]); - $api = new FedoraApi($guzzle); - - $result = $api->getResourceOptions(""); + $result = $api->getResourceOptions($test_uri); $this->assertEquals(200, $result->getStatusCode()); $this->assertEquals($headers['Allow'], $result->getHeaderLine('allow')); $this->assertEquals($headers['Accept-Patch'], $result->getHeaderLine('accept-patch')); diff --git a/test/GetResourceTest.php b/test/GetResourceTest.php index 39ea629..d17a826 100644 --- a/test/GetResourceTest.php +++ b/test/GetResourceTest.php @@ -1,58 +1,67 @@ 'Fedora4'], "SOME CONTENT"), - ]); - - $handler = HandlerStack::create($mock); - $guzzle = new Client(['handler' => $handler]); - $api = new FedoraApi($guzzle); - $result = $api->getResource(""); + $test_uri = self::$webserver->setResponseOfPath( + "/rest/path/to/resource", + new Response( + "SOME CONTENT", + ['X-FOO' => 'Fedora4'], + 200 + ) + ); + + $api = FedoraApi::create(self::$webserver->getHost()); + $result = $api->getResource($test_uri); $this->assertSame((string)$result->getBody(), "SOME CONTENT"); $this->assertSame($result->getHeader('X-FOO'), ['Fedora4']); } /** - * @covers Islandora\Chullo\FedoraApi::getResource - * @uses GuzzleHttp\Client + * @covers \Islandora\Chullo\FedoraApi::getResource + * @uses \GuzzleHttp\Client * * TODO: Is this useful anymore? */ public function testReturnsNullOtherwise() { - $mock = new MockHandler([ - new Response(304), - new Response(404), - ]); + $test_uri1 = self::$webserver->setResponseOfPath( + "/rest/path/to/resource1", + new Response( + "", + [], + 304 + ) + ); + $test_uri2 = self::$webserver->setResponseOfPath( + "/rest/path/to/resource2", + new Response( + "", + [], + 404 + ) + ); - $handler = HandlerStack::create($mock); - $guzzle = new Client(['handler' => $handler]); - $api = new FedoraApi($guzzle); + $api = FedoraApi::create(self::$webserver->getHost()); //304 - $result = $api->getResource(""); + $result = $api->getResource($test_uri1); $this->assertEquals(304, $result->getStatusCode()); //404 - $result = $api->getResource(""); + $result = $api->getResource($test_uri2); $this->assertEquals(404, $result->getStatusCode()); } } diff --git a/test/GetTimemapURITest.php b/test/GetTimemapURITest.php index a2b9fbf..8079104 100644 --- a/test/GetTimemapURITest.php +++ b/test/GetTimemapURITest.php @@ -1,20 +1,16 @@ ';rel="type"', 'Link' => ';rel="timemap"', ]; - - $mock = new MockHandler( - [ - new Response(200, $headers) - ] + $test_uri = self::$webserver->setResponseOfPath( + "/rest/path/to/resource", + new Response( + "", + $headers, + 200 + ) ); - $handler = HandlerStack::create($mock); - $guzzle = new Client(['handler' => $handler]); - $api = new FedoraApi($guzzle); + $api = FedoraApi::create(self::$webserver->getHost()); - $timemapuri = $api->getTimemapURI(""); + $timemapuri = $api->getTimemapURI($test_uri); $this->assertEquals("http://localhost:8080/rest/path/to/resource/fcr:versions", $timemapuri); } diff --git a/test/GetVersionsTest.php b/test/GetVersionsTest.php index ca8275d..967ab7a 100644 --- a/test/GetVersionsTest.php +++ b/test/GetVersionsTest.php @@ -1,66 +1,78 @@ setResponseOfPath( + "/rest/path/to/resource/fcr:versions", + new ResponseByMethod([ + ResponseByMethod::METHOD_GET => + new Response( + "", + [], + 200 + ) + ]) + ); $headers = [ - 'Status' => '200 OK', - 'Link' => ';rel="timemap"' + 'Status' => '200 OK', + 'Link' => "<$test_uri_timemap>;rel=\"timemap\"" ]; - - $mock = new MockHandler( - [ - new Response(200, $headers), - new Response(200, $headers) - ] + $test_uri = self::$webserver->setResponseOfPath( + "/rest/path/to/resource", + new ResponseByMethod([ + ResponseByMethod::METHOD_HEAD => + new Response( + "", + $headers, + 200 + ) + ]) ); - $handler = HandlerStack::create($mock); - $guzzle = new Client(['handler' => $handler]); - $api = new FedoraApi($guzzle); + $api = FedoraApi::create(self::$webserver->getHost()); - $result = $api->getVersions(); + $result = $api->getVersions($test_uri); $this->assertEquals(200, $result->getStatusCode()); } /** - * @covers Islandora\Chullo\FedoraApi::getVersions Exception - * @uses GuzzleHttp\Client + * @covers \Islandora\Chullo\FedoraApi::getVersions Exception + * @uses \GuzzleHttp\Client */ public function testThrowErrorWithNoTimemapURI() { $headers = [ 'Status' => '200 OK' ]; - - $mock = new MockHandler( - [ - new Response(200, $headers) - ] + $test_uri = self::$webserver->setResponseOfPath( + "/rest/path/to/resource", + new ResponseByMethod([ + ResponseByMethod::METHOD_HEAD => + new Response( + "", + $headers, + 200 + ) + ]) ); - $handler = HandlerStack::create($mock); - $guzzle = new Client(['handler' => $handler]); - $api = new FedoraApi($guzzle); + + $api = FedoraApi::create(self::$webserver->getHost()); $this->expectException(\RuntimeException::class); - $result = $api->getVersions(); + $api->getVersions($test_uri); } } diff --git a/test/ModifyResourceTest.php b/test/ModifyResourceTest.php index 52a0063..07c6076 100644 --- a/test/ModifyResourceTest.php +++ b/test/ModifyResourceTest.php @@ -1,32 +1,34 @@ setResponseOfPath( + "/rest/path/to/resource", + new ResponseByMethod([ + ResponseByMethod::METHOD_PATCH => new Response( + "", + [], + 204 + ), + ]) + ); - $handler = HandlerStack::create($mock); - $guzzle = new Client(['handler' => $handler]); - $api = new FedoraApi($guzzle); + $api = FedoraApi::create(self::$webserver->getHost()); - $result = $api->modifyResource(""); + $result = $api->modifyResource($test_uri); $this->assertEquals(204, $result->getStatusCode()); } } diff --git a/test/SaveGraphTest.php b/test/SaveGraphTest.php index 5590f38..e6a3f84 100644 --- a/test/SaveGraphTest.php +++ b/test/SaveGraphTest.php @@ -1,32 +1,31 @@ setResponseOfPath( + "/rest/path/to/resource", + new ResponseByMethod([ + ResponseByMethod::METHOD_PUT => new Response("", [], 204), + ]) + ); - $handler = HandlerStack::create($mock); - $guzzle = new Client(['handler' => $handler]); - $api = new FedoraApi($guzzle); + $api = FedoraApi::create(self::$webserver->getHost()); - $result = $api->saveGraph(new \EasyRdf\Graph()); + $result = $api->saveGraph(new Graph(), $test_uri); $this->assertEquals(204, $result->getStatusCode()); } } diff --git a/test/SaveResourceTest.php b/test/SaveResourceTest.php index fe42eef..6c20681 100644 --- a/test/SaveResourceTest.php +++ b/test/SaveResourceTest.php @@ -1,32 +1,29 @@ setResponseOfPath( + "/rest/path/to/resource", + new ResponseByMethod([ + ResponseByMethod::METHOD_PUT => new Response("", [], 204), + ]) + ); + $api = FedoraApi::create(self::$webserver->getHost()); - $handler = HandlerStack::create($mock); - $guzzle = new Client(['handler' => $handler]); - $api = new FedoraApi($guzzle); - - $result = $api->saveResource(""); + $result = $api->saveResource($test_uri); $this->assertEquals(204, $result->getStatusCode()); } }