Skip to content

Commit

Permalink
Merge pull request #423 from matslindh/make-localized-storage-tests-b…
Browse files Browse the repository at this point in the history
…etter

Make storage drivers throw exceptions as expected in StorageOperations
  • Loading branch information
rexxars committed Dec 10, 2015
2 parents 9a94505 + d79010b commit c44391e
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 2 deletions.
8 changes: 7 additions & 1 deletion library/Imbo/Storage/Doctrine.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,18 @@ public function store($user, $imageIdentifier, $imageData) {
]);
}

return (boolean) $this->getConnection()->insert($this->getTableName($user, $imageIdentifier), [
$inserted = $this->getConnection()->insert($this->getTableName($user, $imageIdentifier), [
'user' => $user,
'imageIdentifier' => $imageIdentifier,
'data' => $imageData,
'updated' => $now,
]);

if (!$inserted) {
throw new StorageException('Unable to persist image data to database.', 500);
}

return true;
}

/**
Expand Down
9 changes: 8 additions & 1 deletion library/Imbo/Storage/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,14 @@ public function store($user, $imageIdentifier, $imageData) {

$imagePath = $imageDir . '/' . $imageIdentifier;

return (bool) file_put_contents($imagePath, $imageData);
$bytesWritten = file_put_contents($imagePath, $imageData);

// if write failed or 0 bytes were written (0 byte input == fail), or we wrote less than expected
if (!$bytesWritten || ($bytesWritten < strlen($imageData))) {
throw new StorageException('Failed writing file (disk full? zero bytes input?) to disk: ' . $imagePath, 507);
}

return true;
}

/**
Expand Down
8 changes: 8 additions & 0 deletions tests/phpunit/ImboIntegrationTest/Storage/FilesystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ public function tearDown() {
parent::tearDown();
}

/**
* @expectedException Imbo\Exception\StorageException
* @expectedExceptionCode 507
*/
public function testStoringEmptyDataFails() {
$this->getDriverActive()->store($this->getUser(), "this_identifier_left_empty", "");
}

/**
* Recursively delete the test directory
*
Expand Down
18 changes: 18 additions & 0 deletions tests/phpunit/ImboIntegrationTest/Storage/StorageTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,24 @@ abstract class StorageTests extends \PHPUnit_Framework_TestCase {
*/
abstract protected function getDriver();

/**
* Get the currently instanced, active driver in inherited tests
*
* @return string
*/
protected function getDriverActive() {
return $this->driver;
}

/**
* Get the user name in inherited tests
*
* @return string
*/
protected function getUser() {
return $this->user;
}

/**
* Set up
*/
Expand Down
22 changes: 22 additions & 0 deletions tests/phpunit/ImboUnitTest/Storage/DoctrineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ class DoctrineTest extends \PHPUnit_Framework_TestCase {
*/
private $connection;

/**
* @var string
*/
private $user = 'key';

/**
* Set up the driver
*/
Expand All @@ -50,6 +55,23 @@ public function tearDown() {
$this->driver = null;
}

/**
* @covers Imbo\Storage\Doctrine::store
* @expectedException Imbo\Exception\StorageException
* @expectedExceptionCode 500
*/
public function testStoreExceptionOnInsertFailure() {
$this->connection->expects($this->once())->method('insert')->will($this->returnValue(0));

$driverMock = $this->getMockBuilder('Imbo\Storage\Doctrine')
->setConstructorArgs(array([], $this->connection))
->setMethods(array('imageExists'))
->getMock();

$driverMock->expects($this->once())->method('imageExists')->will($this->returnValue(false));
$driverMock->store($this->user, 'image_identifier_not_used', 'image_data_not_used');
}

/**
* @covers Imbo\Storage\Doctrine::getStatus
*/
Expand Down

0 comments on commit c44391e

Please sign in to comment.