Skip to content

Commit

Permalink
Added test init
Browse files Browse the repository at this point in the history
Changed write test
  • Loading branch information
vitalyiegorov committed Nov 24, 2014
1 parent bb4d327 commit 32d51e3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/LocalFileService.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function write($data, $filename = '', $uploadDir = '')

// Put file and return true if at least one byte is written
if (file_put_contents($path, $data) !== false) {
return $uploadDir.'/';
return $uploadDir.'/'.$filename;
} else { // We have failed my lord..
return false;
}
Expand Down
51 changes: 18 additions & 33 deletions tests/MainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,39 @@
*/
class EventTest extends \PHPUnit_Framework_TestCase
{
/** @var \samson\fs\FileService Pointer to file service */
/** @var \samson\fs\LocalFileService Pointer to file service */
public $fileService;

/** Tests init */
public function setUp()
{
// Get instance using services factory as error will signal other way
$this->fileService = \samson\core\Service::getInstance('samson\fs\LocalFileService');
}

/** Test reading */
public function testRead()
{
// Create instance
$this->fileService = new LocalFileService(__DIR__.'../');

// Read current file data
$data = $this->fileService->read(__FILE__);

// Compare current file with data readed
$this->assertStringEqualsFile(__FILE__, $data, 'File service read failed');
}

/** Test file service writing and reading */
public function testWriteRead()
/** Test file service writing */
public function testWrite()
{
// Get instance using services factory as error will signal other way
$this->fileService = \samson\core\Service::getInstance('samson\fs\LocalFileService');

// Create temporary file
$path = tempnam(sys_get_temp_dir(), 'test');

// Create test dir
$testDir = sys_get_temp_dir().'/testDir/';
mkdir($testDir, 0777);

// Write data to temporary file
$this->fileService->write('123', $path);
$this->fileService->write('123', $path, $testDir);


// Read data from file
$data = $this->fileService->read($path);
Expand All @@ -48,28 +54,19 @@ public function testWriteRead()
/** Test file service writing failed */
public function testFailWrite()
{
// Get instance using services factory as error will signal other way
$this->fileService = \samson\core\Service::getInstance('samson\fs\LocalFileService');

// Create temporary file
$path = __DIR__.'/test/test.txt';

// Write data to temporary file
$this->fileService->write('123', $path);

// Read data from file
$data = $this->fileService->read($path);
$writtenFile = $this->fileService->write('123', $path);

// Perform test
$this->assertNotEquals('123', $data, 'File service failed writing failed');
$this->assertStringEqualsFile($writtenFile, '123');
}

/** Test file service deleting */
public function testDelete()
{
// Get instance using services factory as error will signal other way
$this->fileService = \samson\core\Service::getInstance('samson\fs\LocalFileService');

// Create temporary file
$path = tempnam(sys_get_temp_dir(), 'test');

Expand All @@ -83,9 +80,6 @@ public function testDelete()
/** Test file service existing */
public function testExists()
{
// Get instance using services factory as error will signal other way
$this->fileService = \samson\core\Service::getInstance('samson\fs\LocalFileService');

// Create temporary file
$path = tempnam(sys_get_temp_dir(), 'test');

Expand All @@ -99,9 +93,6 @@ public function testExists()
/** Test file service moving */
public function testMove()
{
// Get instance using services factory as error will signal other way
$this->fileService = \samson\core\Service::getInstance('samson\fs\LocalFileService');

// Create temporary file
$path = tempnam(sys_get_temp_dir(), 'test');

Expand All @@ -120,9 +111,6 @@ public function testMove()
/** Test file service moving to existing file */
public function testMoveToExisting()
{
// Get instance using services factory as error will signal other way
$this->fileService = \samson\core\Service::getInstance('samson\fs\LocalFileService');

// Create temporary file
$path = tempnam(sys_get_temp_dir(), 'test');

Expand All @@ -136,9 +124,6 @@ public function testMoveToExisting()
/** Test file service extension method */
public function testExtension()
{
// Get instance using services factory as error will signal other way
$this->fileService = \samson\core\Service::getInstance('samson\fs\LocalFileService');

// Move file to a new dir
$extension = $this->fileService->extension(__FILE__);

Expand Down

0 comments on commit 32d51e3

Please sign in to comment.