1
0
mirror of https://github.com/danog/file.git synced 2024-12-03 09:47:54 +01:00
file/test/DescriptorTest.php

87 lines
3.0 KiB
PHP
Raw Normal View History

2015-07-11 03:59:39 +02:00
<?php
namespace Amp\Fs\Test;
use Amp\Reactor;
use Amp\Fs\Filesystem;
abstract class DescriptorTest extends \PHPUnit_Framework_TestCase {
abstract protected function getReactor(): Reactor;
abstract protected function getFilesystem(Reactor $reactor): Filesystem;
public function testReadWriteCreate() {
($this->getReactor())->run(function($reactor) {
$path = __DIR__ . "/fixture/new.txt";
$fs = $this->getFilesystem($reactor);
$flags = Filesystem::READ | Filesystem::WRITE | Filesystem::CREATE;
$fh = yield $fs->open($path, $flags);
yield $fh->write(0, "test");
$data = yield $fh->read(0, 8192);
$this->assertSame("test", $data);
yield $fh->close();
yield $fs->unlink($path);
});
}
/**
* @expectedException RuntimeException
*/
public function testWriteFailsOnDirectory() {
($this->getReactor())->run(function($reactor) {
$path = __DIR__ . "/fixture/dir";
$fs = $this->getFilesystem($reactor);
$flags = Filesystem::READ | Filesystem::WRITE | Filesystem::CREATE;
$fh = yield $fs->open($path, $flags);
yield $fh->write(0, "should fail because this is a directory");
});
}
/**
* @expectedException RuntimeException
*/
public function testReadFailsOnDirectory() {
($this->getReactor())->run(function($reactor) {
$path = __DIR__ . "/fixture/dir";
$fs = $this->getFilesystem($reactor);
$flags = Filesystem::READ | Filesystem::WRITE | Filesystem::CREATE;
$fh = yield $fs->open($path, $flags);
yield $fh->read(0, 8192);
});
}
public function testTruncate() {
($this->getReactor())->run(function($reactor) {
$path = __DIR__ . "/fixture/truncate.txt";
$fs = $this->getFilesystem($reactor);
$flags = Filesystem::READ | Filesystem::WRITE | Filesystem::CREATE;
$fh = yield $fs->open($path, $flags);
yield $fh->write(0, "test");
$data = yield $fh->read(0, 8192);
$this->assertSame("test", $data);
yield $fh->truncate();
yield $fh->close();
$this->assertEquals(0, (yield $fs->stat($path))["size"]);
yield $fs->unlink($path);
});
}
public function testStat() {
($this->getReactor())->run(function($reactor) {
$fs = $this->getFilesystem($reactor);
// file
$fh = yield $fs->open(__DIR__ . "/fixture/small.txt");
$stat = yield $fh->stat();
$this->assertTrue($stat["isfile"]);
$this->assertFalse($stat["isdir"]);
// directory
$fh = yield $fs->open(__DIR__ . "/fixture/dir");
$stat = yield $fh->stat();
$this->assertFalse($stat["isfile"]);
$this->assertTrue($stat["isdir"]);
});
}
}