Add unit test for FileStorage

This commit is contained in:
Frederic Guillot 2015-10-03 15:55:20 -04:00
parent aeab662d65
commit d7c0fabcb7
3 changed files with 312 additions and 4 deletions

View File

@ -53,7 +53,6 @@ class FileStorage implements ObjectStorageInterface
* @access public
* @param string $key
* @param string $blob
* @return string
*/
public function put($key, &$blob)
{
@ -78,7 +77,7 @@ class FileStorage implements ObjectStorageInterface
throw new ObjectStorageException('File not found: '.$filename);
}
return readfile($filename);
readfile($filename);
}
/**
@ -141,7 +140,7 @@ class FileStorage implements ObjectStorageInterface
*/
private function createFolder($key)
{
$folder = $this->path.DIRECTORY_SEPARATOR.dirname($key);
$folder = strpos($key, '/') !== false ? $this->path.DIRECTORY_SEPARATOR.dirname($key) : $this->path;
if (! is_dir($folder) && ! mkdir($folder, 0755, true)) {
throw new ObjectStorageException('Unable to create folder: '.$folder);

View File

@ -25,7 +25,6 @@ interface ObjectStorageInterface
* @access public
* @param string $key
* @param string $blob
* @return string
*/
public function put($key, &$blob);

View File

@ -0,0 +1,310 @@
<?php
namespace Core\ObjectStorage;
require_once __DIR__.'/../Base.php';
function file_put_contents($filename, $data)
{
return FileStorageTest::$functions->file_put_contents($filename, $data);
}
function file_get_contents($filename)
{
return FileStorageTest::$functions->file_get_contents($filename);
}
function mkdir($filename, $mode, $recursif)
{
return FileStorageTest::$functions->mkdir($filename, $mode, $recursif);
}
function is_dir($filename)
{
return FileStorageTest::$functions->is_dir($filename);
}
function file_exists($filename)
{
return FileStorageTest::$functions->file_exists($filename);
}
function unlink($filename)
{
return FileStorageTest::$functions->unlink($filename);
}
function readfile($filename)
{
echo FileStorageTest::$functions->readfile($filename);
}
function rename($src, $dst)
{
return FileStorageTest::$functions->rename($src, $dst);
}
function move_uploaded_file($src, $dst)
{
return FileStorageTest::$functions->move_uploaded_file($src, $dst);
}
class FileStorageTest extends \Base
{
public static $functions;
public function setUp()
{
parent::setup();
self::$functions = $this
->getMockBuilder('stdClass')
->setMethods(array(
'file_put_contents',
'file_get_contents',
'file_exists',
'mkdir',
'is_dir',
'unlink',
'rename',
'move_uploaded_file',
'readfile',
))
->getMock();
}
public function tearDown()
{
parent::tearDown();
self::$functions = null;
}
public function testPut()
{
$data = 'data';
$storage = new FileStorage('somewhere');
self::$functions
->expects($this->at(0))
->method('is_dir')
->with(
$this->equalTo('somewhere')
)
->will($this->returnValue(false));
self::$functions
->expects($this->at(1))
->method('mkdir')
->with(
$this->equalTo('somewhere')
)
->will($this->returnValue(true));
self::$functions
->expects($this->at(2))
->method('file_put_contents')
->with(
$this->equalTo('somewhere'.DIRECTORY_SEPARATOR.'mykey'),
$this->equalTo('data')
)
->will($this->returnValue(true));
$storage->put('mykey', $data);
}
public function testPutWithSubfolder()
{
$data = 'data';
$storage = new FileStorage('somewhere');
self::$functions
->expects($this->at(0))
->method('is_dir')
->with(
$this->equalTo('somewhere'.DIRECTORY_SEPARATOR.'my')
)
->will($this->returnValue(false));
self::$functions
->expects($this->at(1))
->method('mkdir')
->with(
$this->equalTo('somewhere'.DIRECTORY_SEPARATOR.'my')
)
->will($this->returnValue(true));
self::$functions
->expects($this->at(2))
->method('file_put_contents')
->with(
$this->equalTo('somewhere'.DIRECTORY_SEPARATOR.'my'.DIRECTORY_SEPARATOR.'key'),
$this->equalTo('data')
)
->will($this->returnValue(true));
$storage->put('my'.DIRECTORY_SEPARATOR.'key', $data);
}
/**
* @expectedException \Core\ObjectStorage\ObjectStorageException
*/
public function testPutWhenNotAbleToCreateFolder()
{
$data = 'data';
$storage = new FileStorage('somewhere');
self::$functions
->expects($this->at(0))
->method('is_dir')
->with(
$this->equalTo('somewhere')
)
->will($this->returnValue(false));
self::$functions
->expects($this->at(1))
->method('mkdir')
->with(
$this->equalTo('somewhere')
)
->will($this->returnValue(false));
$storage->put('mykey', $data);
}
public function testGet()
{
$storage = new FileStorage('somewhere');
self::$functions
->expects($this->at(0))
->method('file_exists')
->with(
$this->equalTo('somewhere'.DIRECTORY_SEPARATOR.'mykey')
)
->will($this->returnValue(true));
self::$functions
->expects($this->at(1))
->method('file_get_contents')
->with(
$this->equalTo('somewhere'.DIRECTORY_SEPARATOR.'mykey')
)
->will($this->returnValue('data'));
$this->assertEquals('data', $storage->get('mykey'));
}
/**
* @expectedException \Core\ObjectStorage\ObjectStorageException
*/
public function testGetWithFileNotFound()
{
$storage = new FileStorage('somewhere');
self::$functions
->expects($this->at(0))
->method('file_exists')
->with(
$this->equalTo('somewhere'.DIRECTORY_SEPARATOR.'mykey')
)
->will($this->returnValue(false));
$this->assertEquals('data', $storage->get('mykey'));
}
public function testOutput()
{
$storage = new FileStorage('somewhere');
self::$functions
->expects($this->at(0))
->method('file_exists')
->with(
$this->equalTo('somewhere'.DIRECTORY_SEPARATOR.'mykey')
)
->will($this->returnValue(true));
self::$functions
->expects($this->at(1))
->method('readfile')
->with(
$this->equalTo('somewhere'.DIRECTORY_SEPARATOR.'mykey')
)
->will($this->returnValue('data'));
$this->expectOutputString('data');
$storage->output('mykey');
}
public function testRemove()
{
$storage = new FileStorage('somewhere');
self::$functions
->expects($this->at(0))
->method('file_exists')
->with(
$this->equalTo('somewhere'.DIRECTORY_SEPARATOR.'mykey')
)
->will($this->returnValue(true));
self::$functions
->expects($this->at(1))
->method('unlink')
->with(
$this->equalTo('somewhere'.DIRECTORY_SEPARATOR.'mykey')
)
->will($this->returnValue(true));
$this->assertTrue($storage->remove('mykey'));
}
public function testMoveFile()
{
$storage = new FileStorage('somewhere');
self::$functions
->expects($this->at(0))
->method('is_dir')
->with(
$this->equalTo('somewhere')
)
->will($this->returnValue(true));
self::$functions
->expects($this->at(1))
->method('rename')
->with(
$this->equalTo('src_file'),
$this->equalTo('somewhere'.DIRECTORY_SEPARATOR.'mykey')
)
->will($this->returnValue(true));
$this->assertTrue($storage->moveFile('src_file', 'mykey'));
}
public function testMoveUploadedFile()
{
$storage = new FileStorage('somewhere');
self::$functions
->expects($this->at(0))
->method('is_dir')
->with(
$this->equalTo('somewhere')
)
->will($this->returnValue(true));
self::$functions
->expects($this->at(1))
->method('move_uploaded_file')
->with(
$this->equalTo('src_file'),
$this->equalTo('somewhere'.DIRECTORY_SEPARATOR.'mykey')
)
->will($this->returnValue(true));
$this->assertTrue($storage->moveUploadedFile('src_file', 'mykey'));
}
}