Skip to content

Commit

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

/**
* 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 = "") {
// 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(
'MOVE',
$uri,
$options
);

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

/**
* Creates a new transaction.
Expand Down
12 changes: 12 additions & 0 deletions src/IFedoraClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,18 @@ public function modifyResource($uri,
*/
public function deleteResource($uri,
$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 = "");

/**
* Creates a new transaction.
Expand Down
78 changes: 78 additions & 0 deletions test/MoveResourceTest.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 MoveResourceTest extends \PHPUnit_Framework_TestCase {

/**
* @covers Islandora\Fedora\Chullo::moveResource
* @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->moveResource("","");
$this->assertSame($result, "http://localhost:8080/fcrepo/rest/SOME_URI");
}

/**
* @covers Islandora\Fedora\Chullo::moveResource
* @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->moveResource("","");
}

/**
* @covers Islandora\Fedora\Chullo::moveResource
* @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->moveResource("","");
}

/**
* @covers Islandora\Fedora\Chullo::moveResource
* @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->moveResource("","");
}
}

0 comments on commit 41d4a60

Please sign in to comment.