Commit 0536b35a authored by Richard Weinhold's avatar Richard Weinhold 🎩

adds File::moveTo() unittests

- fixes some bugs from move/copy refactoring
parent d6412172
Pipeline #5409 passed with stages
in 12 seconds
......@@ -152,7 +152,7 @@ class File extends FileSystem
* @return self new File-object
* @throws AccessDeniedException|FileNotFoundException
*/
public function moveTo(Storage\Disk $destination, ?int $constraints = null): self
public function moveTo(Storage $destination, ?int $constraints = null): self
{
$destination->setConstraints(($constraints !== null) ? $constraints : $this->storage->getConstraints());
......@@ -169,6 +169,12 @@ class File extends FileSystem
throw new AccessDeniedException('unable to write to destination directory', 403);
}
// ensure the destination-path points to a filename
if ($destination instanceof Storage\Disk && $destination->isDir()) {
$destination = clone $destination;
$destination->cd([$this->storage instanceof Storage\Disk ? $this->path()->filename : (uniqid() . '.file')]);
}
// actual move file to file: use native functions if possible
if (!$this->storage->moveFileTo($destination)) {
throw new AccessDeniedException('unable to move file', 403);
......
......@@ -18,7 +18,7 @@ class Memory extends Storage
{
/**
* @var string
* @var string|null
*/
protected $content = '';
......@@ -45,7 +45,7 @@ class Memory extends Storage
*/
public function isFile(): bool
{
return true;
return $this->content !== null;
}
/**
......@@ -94,10 +94,10 @@ class Memory extends Storage
public function readFile(?int $offset = null, ?int $length = null, int $mode = LOCK_SH): string
{
if ($offset !== null && $length !== null) {
return mb_substr($this->content, $offset, $length, '8bit');
return mb_substr($this->content ?? '', $offset, $length, '8bit');
}
return $this->content;
return $this->content ?? '';
}
/**
......@@ -114,6 +114,10 @@ class Memory extends Storage
*/
public function writeFile(string $content, bool $append = false, int $mode = 0): bool
{
if ($this->content === null) {
$this->content = '';
}
if ($append) {
$this->content .= $content;
} else {
......@@ -128,7 +132,7 @@ class Memory extends Storage
*/
public function removeFile():bool
{
$this->content = '';
$this->content = null;
return true;
}
......@@ -137,7 +141,7 @@ class Memory extends Storage
*/
public function getSize(): int
{
return mb_strlen($this->content, '8bit');
return ($this->content === null) ? 0 : mb_strlen($this->content, '8bit');
}
/**
......@@ -145,7 +149,7 @@ class Memory extends Storage
*/
public function getFileType(bool $withEncoding = false): string
{
return (new \finfo($withEncoding ? FILEINFO_MIME : FILEINFO_MIME_TYPE))->buffer($this->content);
return (new \finfo($withEncoding ? FILEINFO_MIME : FILEINFO_MIME_TYPE))->buffer($this->content ?? '');
}
/**
......@@ -154,7 +158,7 @@ class Memory extends Storage
public function getFileHash(int $mode = Hash::CONTENT, string $algo = 'sha256'): string
{
switch ($mode) {
case Hash::CONTENT: return hash($algo, $this->content, false);
case Hash::CONTENT: return hash($algo, $this->content ?? '', false);
case Hash::FILENAME: case Hash::FILEPATH: throw new RuntimeException('unable to calculate filepath/name hash for in-memory-files', 500);
default: throw new RuntimeException('unknown hashing-mode', 500);
}
......@@ -211,8 +215,10 @@ class Memory extends Storage
}
// pre-fill stream
\fwrite($stream, $this->content);
\rewind($stream);
if ($this->content !== null) {
\fwrite($stream, $this->content);
\rewind($stream);
}
return $stream;
}
......
......@@ -3,6 +3,10 @@
namespace ricwein\FileSystem\Tests\File;
use PHPUnit\Framework\TestCase;
use ricwein\FileSystem\File;
use ricwein\FileSystem\Storage;
use ricwein\FileSystem\Directory;
use ricwein\FileSystem\Helper\Constraint;
/**
* test FileSyst\File bases
......@@ -14,9 +18,91 @@ class MoveTest extends TestCase
/**
* @return void
*/
public function testCopyFromDiskToDisk()
public function testMoveFromDiskToDisk()
{
// @TODO
$this->assertSame(true, true);
$source = (new File(new Storage\Disk(__DIR__, '../_examples', 'test.txt'), Constraint::LOOSE))->copyTo(new Storage\Disk\Temp());
$destination = $source->moveTo(new Storage\Disk\Temp);
$this->assertInstanceOf(Storage\Disk\Temp::class, $destination->storage());
$this->assertFalse($source->isFile());
$this->assertTrue($destination->isFile());
$this->assertSame(file_get_contents(__DIR__ . '/../_examples/test.txt'), $destination->read());
}
/**
* @return void
*/
public function testMoveFromDiskToMemory()
{
$source = (new File(new Storage\Disk(__DIR__, '../_examples', 'test.txt'), Constraint::LOOSE))->copyTo(new Storage\Disk\Temp());
$destination = $source->moveTo(new Storage\Memory);
$this->assertInstanceOf(Storage\Memory::class, $destination->storage());
$this->assertFalse($source->isFile());
$this->assertTrue($destination->isFile());
$this->assertSame(file_get_contents(__DIR__ . '/../_examples/test.txt'), $destination->read());
}
/**
* @return void
*/
public function testMoveFromMemoryToDisk()
{
$source = new File(new Storage\Memory(file_get_contents(__DIR__ . '/../_examples/test.txt')));
$destination = $source->moveTo(new Storage\Disk\Temp);
$this->assertInstanceOf(Storage\Disk\Temp::class, $destination->storage());
$this->assertFalse($source->isFile());
$this->assertSame(file_get_contents(__DIR__ . '/../_examples/test.txt'), $destination->read());
}
/**
* @return void
*/
public function testMoveFromMemoryToMemory()
{
$source = new File(new Storage\Memory(file_get_contents(__DIR__ . '/../_examples/test.txt')));
$destination = $source->moveTo(new Storage\Memory);
$this->assertInstanceOf(Storage\Memory::class, $destination->storage());
$this->assertFalse($source->isFile());
$this->assertSame(file_get_contents(__DIR__ . '/../_examples/test.txt'), $destination->read());
}
/**
* @return void
*/
public function testMoveToDir()
{
$source = (new File(new Storage\Disk(__DIR__, '../_examples', 'test.txt'), Constraint::LOOSE))->copyTo(new Storage\Disk\Temp());
$destination = new Directory(new Storage\Disk\Temp());
$this->assertTrue($destination->isDir());
$this->assertTrue($destination->storage()->isDir());
$retFile = $source->moveTo($destination->storage());
$this->assertFalse($source->isFile());
$this->assertSame($destination->path()->raw, $retFile->path()->directory);
$this->assertSame(file_get_contents(__DIR__ . '/../_examples/test.txt'), $retFile->read());
}
/**
* @return void
*/
public function testMoveMemoryToDir()
{
$source = new File(new Storage\Memory(file_get_contents(__DIR__ . '/../_examples/test.txt')));
$destination = new Directory(new Storage\Disk\Temp());
$this->assertTrue($destination->isDir());
$this->assertTrue($destination->storage()->isDir());
$retFile = $source->moveTo($destination->storage());
$this->assertFalse($source->isFile());
$this->assertTrue(strpos($retFile->path()->filename, '.file') !== false);
$this->assertSame($destination->path()->raw, $retFile->path()->directory);
$this->assertSame(file_get_contents(__DIR__ . '/../_examples/test.txt'), $retFile->read());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment