Skip to content
This repository has been archived by the owner on May 17, 2022. It is now read-only.

Commit

Permalink
Implement the method \Martial\Transmission\API\RpcClient::freeSpace
Browse files Browse the repository at this point in the history
  • Loading branch information
cyb3rd4d committed Jan 7, 2016
1 parent 3d3ab19 commit 992f964
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/RpcClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ public function queueMoveBottom($sessionId, array $ids)
*/
public function freeSpace($sessionId, $path)
{
// TODO: Implement freeSpace() method.
$this->sendRequest($sessionId, $this->buildRequestBody('free-space', ['path' => $path]));
}

/**
Expand Down
81 changes: 81 additions & 0 deletions tests/RpcClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use GuzzleHttp\Exception\ClientException;
use Martial\Transmission\API\CSRFException;
use Martial\Transmission\API\RpcClient;
use Martial\Transmission\API\TransmissionException;
use Mockery as m;

class RpcClientTest extends \PHPUnit_Framework_TestCase
Expand Down Expand Up @@ -1621,6 +1622,86 @@ public function testQueueMoveBottomShouldThrowAnExceptionWithAnInvalidSessionId(
}
}

public function testFreeSpaceWithSuccess()
{
$requestBody = '{"method":"free-space","arguments":{"path":"/var/lib/transmission-daemon/downloads"}}';

$this
->sendRequest($requestBody)
->andReturn($this->guzzleResponse);

$jsonResponse = '{"arguments":{"path":"/var/lib/transmission-daemon/downloads","size-bytes":37548523520},"result":"success"}';
$this->setResponseBody($jsonResponse);
$this->rpcClient->freeSpace($this->sessionId, '/var/lib/transmission-daemon/downloads');
}

public function testFreeSpaceShouldThrowAnExceptionWhenThePathIsInvalid()
{
$requestBody = '{"method":"free-space","arguments":{"path":"/invalid/path"}}';

$this
->sendRequest($requestBody)
->andReturn($this->guzzleResponse);

$jsonResponse = '{"arguments":{"path":"/invalid/path","size-bytes":-1},"result":"No such file or directory"}';
$this->setResponseBody($jsonResponse);

try {
$success = false;
$this->rpcClient->freeSpace($this->sessionId, '/invalid/path');
} catch (TransmissionException $e) {
$this->assertSame('No such file or directory', $e->getResult());
$success = true;
}

if (!$success) {
$this->fail('An invalid path should throw an exception.');
}
}

/**
* @expectedException \Martial\Transmission\API\TransmissionException
*/
public function testFreeSpaceShouldThrowAnExceptionWhenTheServerReturnsAnError500()
{
$requestBody = '{"method":"free-space","arguments":{"path":"/var/lib/transmission-daemon/downloads"}}';

$this
->sendRequest($requestBody)
->andThrow(m::mock('\GuzzleHttp\Exception\RequestException'));

$this->rpcClient->freeSpace($this->sessionId, '/var/lib/transmission-daemon/downloads');
}

/**
* @expectedException \GuzzleHttp\Exception\ClientException
*/
public function testFreeSpaceShouldThrowAnExceptionWhenTheRequestFails()
{
$requestBody = '{"method":"free-space","arguments":{"path":"/var/lib/transmission-daemon/downloads"}}';

$this
->sendRequest($requestBody)
->andThrow(m::mock('\GuzzleHttp\Exception\ClientException'));

$this->rpcClient->freeSpace($this->sessionId, '/var/lib/transmission-daemon/downloads');
}

public function testFreeSpaceShouldThrowAnExceptionWithAnInvalidSessionId()
{
$requestBody = '{"method":"free-space","arguments":{"path":"/var/lib/transmission-daemon/downloads"}}';

$this
->sendRequest($requestBody)
->andThrow($this->generateCSRFException());

try {
$this->rpcClient->freeSpace($this->sessionId, '/var/lib/transmission-daemon/downloads');
} catch (CSRFException $e) {
$this->assertSame($this->sessionId, $e->getSessionId());
}
}

/**
* @return ClientException
*/
Expand Down

0 comments on commit 992f964

Please sign in to comment.