Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

More tests, cover edge cases, fix some faulty FileStorage behaviour

  • Loading branch information...
commit fd8ce0c5d69dfd49aa43f32e9ed5cdfcacc1f73c 1 parent e596783
@igorw authored
View
11 src/Igorw/Trashbin/FileStorage.php
@@ -14,14 +14,19 @@ public function __construct($storageDirectory)
public function get($id)
{
if (!file_exists($this->pathTo($id))) {
- return false;
+ return null;
}
- return json_decode(file_get_contents($this->pathTo($id)));
+
+ $json = file_get_contents($this->pathTo($id));
+
+ return json_decode($json, true);
}
public function set($id, array $value)
{
- return file_put_contents($this->pathTo($id), json_encode($value)) !== false;
+ $json = json_encode($value);
+
+ return false !== file_put_contents($this->pathTo($id), $json);
}
private function pathTo($id)
View
27 tests/unit/ArrayStorageTest.php
@@ -4,23 +4,36 @@
class ArrayStorageTest extends \PHPUnit_Framework_TestCase
{
+ private $storage;
+
+ public function setUp()
+ {
+ $this->storage = new ArrayStorage();
+ }
+
public function testGet()
{
- $storage = new ArrayStorage(array('foo' => array('a' => 'b')));
- $this->assertSame(array('a' => 'b'), $storage->get('foo'));
+ $this->storage = new ArrayStorage(array('foo' => array('a' => 'b')));
+ $this->assertSame(array('a' => 'b'), $this->storage->get('foo'));
}
public function testGetOfNonExistentValue()
{
- $storage = new ArrayStorage();
- $this->assertSame(null, $storage->get('foo'));
+ $this->assertSame(null, $this->storage->get('foo'));
}
public function testSet()
{
- $storage = new ArrayStorage();
- $storage->set('foo', array('a' => 'b'));
+ $this->storage->set('foo', array('a' => 'b'));
+
+ $this->assertSame(array('a' => 'b'), $this->storage->get('foo'));
+ }
+
+ public function testSetTwiceShouldOverrideValue()
+ {
+ $this->storage->set('foo', array('a' => 'b'));
+ $this->storage->set('foo', array('c' => 'd'));
- $this->assertSame(array('a' => 'b'), $storage->get('foo'));
+ $this->assertSame(array('c' => 'd'), $this->storage->get('foo'));
}
}
View
51 tests/unit/FileStorageTest.php
@@ -0,0 +1,51 @@
+<?php
+
+namespace Igorw\Trashbin;
+
+class FileStorageTest extends \PHPUnit_Framework_TestCase
+{
+ private $tempDir;
+ private $storage;
+
+ public function setUp()
+ {
+ $this->tempDir = $this->createTempDir();
+ $this->storage = new FileStorage($this->tempDir);
+ }
+
+ public function testGet()
+ {
+ copy(__DIR__.'/Fixtures/FileStorage/foo.json', $this->tempDir.'/foo.json');
+
+ $this->assertSame(array('a' => 'b'), $this->storage->get('foo'));
+ }
+
+ public function testGetOfNonExistentValue()
+ {
+ $this->assertSame(null, $this->storage->get('foo'));
+ }
+
+ public function testSet()
+ {
+ $this->storage->set('foo', array('a' => 'b'));
+
+ $this->assertJsonStringEqualsJsonString('{"a":"b"}', file_get_contents($this->tempDir.'/foo.json'));
+ }
+
+ public function testSetTwiceShouldOverrideValue()
+ {
+ $this->storage->set('foo', array('a' => 'b'));
+ $this->storage->set('foo', array('c' => 'd'));
+
+ $this->assertJsonStringEqualsJsonString('{"c":"d"}', file_get_contents($this->tempDir.'/foo.json'));
+ }
+
+ private function createTempDir()
+ {
+ $tempDir = tempnam(sys_get_temp_dir(), '');
+ unlink($tempDir);
+ mkdir($tempDir, 755, true);
+
+ return $tempDir;
+ }
+}
View
1  tests/unit/Fixtures/FileStorage/foo.json
@@ -0,0 +1 @@
+{"a":"b"}
Please sign in to comment.
Something went wrong with that request. Please try again.