Skip to content

Commit

Permalink
Merge pull request #20 from ruebot/issue-10
Browse files Browse the repository at this point in the history
Implement copyResource -- Address #10.
  • Loading branch information
daniel-dgi committed Dec 7, 2015
2 parents 41d4a60 + 2c5ce73 commit 33edb8f
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 1 deletion.
34 changes: 34 additions & 0 deletions src/Chullo.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,40 @@ protected function prepareUri($uri, $transaction = "") {
return implode([$base_uri, $transaction, $relative_path], '/');
}

/**
* Issues a COPY request to Fedora.
*
* @param string $uri Resource URI
* @param array $destination Destination URI
* @param string $transaction Transaction id
*
* @return string
*/
public function copyResource($uri,
$destination,
$transaction = "") {
// Ensure uri takes transaction into account.
$uri = $this->prepareUri($uri, $transaction);
// Create destinsation URI
$destination_uri = $this->prepareUri($destination, $transaction);
// Create destination array
$options = [
'headers' => [
'Destination' => $destination_uri,
'Overwrite' => 'T'
],
];
$response = $this->client->request(
'COPY',
$uri,
$options
);

// Return the value of the location header
$locations = $response->getHeader('Location');
return reset($locations);
}

/**
* Issues a MOVE request to Fedora.
*
Expand Down
14 changes: 13 additions & 1 deletion src/IFedoraClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,26 @@ public function modifyResource($uri,
public function deleteResource($uri,
$transaction = "");
/**
* Issues a MOVE request to Fedora.
* Issues a COPY request to Fedora.
*
* @param string $uri Resource URI
* @param array $destination Destination URI
* @param string $transaction Transaction id
*
* @return string
*/
public function copyResource($uri,
$destination,
$transaction = "");
/**
* Issues a MOVE request to Fedora.
*
* @param string $uri Resource URI
* @param array $destination Destination URI
* @param string $transaction Transaction id
*
* @return string
*/
public function moveResource($uri,
$destination,
$transaction = "");
Expand Down
78 changes: 78 additions & 0 deletions test/CopyResourceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use Islandora\Chullo\Chullo;

class CopyResourceTest extends \PHPUnit_Framework_TestCase {

/**
* @covers Islandora\Fedora\Chullo::copyResource
* @uses GuzzleHttp\Client
*/
public function testReturnsUriOn201() {
$mock = new MockHandler([
new Response(201, ['Location' => "http://localhost:8080/fcrepo/rest/SOME_URI"]),
]);

$handler = HandlerStack::create($mock);
$guzzle = new Client(['handler' => $handler, 'base_uri' => 'http://localhost:8080/fcrepo/rest']);
$client = new Chullo($guzzle);

$result = $client->copyResource("","");
$this->assertSame($result, "http://localhost:8080/fcrepo/rest/SOME_URI");
}

/**
* @covers Islandora\Fedora\Chullo::copyResource
* @uses GuzzleHttp\Client
* @expectedException GuzzleHttp\Exception\ClientException
*/
public function testThrowsExceptionOn404() {
$mock = new MockHandler([
new Response(404),
]);

$handler = HandlerStack::create($mock);
$guzzle = new Client(['handler' => $handler, 'base_uri' => 'http://localhost:8080/fcrepo/rest']);
$client = new Chullo($guzzle);

$result = $client->copyResource("","");
}

/**
* @covers Islandora\Fedora\Chullo::copyResource
* @uses GuzzleHttp\Client
* @expectedException GuzzleHttp\Exception\ClientException
*/
public function testThrowsExceptionOn409() {
$mock = new MockHandler([
new Response(409),
]);

$handler = HandlerStack::create($mock);
$guzzle = new Client(['handler' => $handler, 'base_uri' => 'http://localhost:8080/fcrepo/rest']);
$client = new Chullo($guzzle);

$result = $client->copyResource("","");
}

/**
* @covers Islandora\Fedora\Chullo::copyResource
* @uses GuzzleHttp\Client
* @expectedException GuzzleHttp\Exception\ServerException
*/
public function testThrowsExceptionOn502() {
$mock = new MockHandler([
new Response(502),
]);

$handler = HandlerStack::create($mock);
$guzzle = new Client(['handler' => $handler, 'base_uri' => 'http://localhost:8080/fcrepo/rest']);
$client = new Chullo($guzzle);

$result = $client->copyResource("","");
}
}

0 comments on commit 33edb8f

Please sign in to comment.