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 {
|
2015-07-17 16:27:38 +02:00
|
|
|
abstract protected function getReactor();
|
|
|
|
abstract protected function getFilesystem(Reactor $reactor);
|
2015-07-11 03:59:39 +02:00
|
|
|
|
|
|
|
public function testReadWriteCreate() {
|
2015-07-17 16:27:38 +02:00
|
|
|
$this->getReactor()->run(function($reactor) {
|
2015-07-11 03:59:39 +02:00
|
|
|
$path = __DIR__ . "/fixture/new.txt";
|
|
|
|
$fs = $this->getFilesystem($reactor);
|
|
|
|
$flags = Filesystem::READ | Filesystem::WRITE | Filesystem::CREATE;
|
2015-07-17 16:27:38 +02:00
|
|
|
$fh = (yield $fs->open($path, $flags));
|
2015-07-11 03:59:39 +02:00
|
|
|
yield $fh->write(0, "test");
|
2015-07-17 16:27:38 +02:00
|
|
|
$data = (yield $fh->read(0, 8192));
|
2015-07-11 03:59:39 +02:00
|
|
|
$this->assertSame("test", $data);
|
|
|
|
yield $fh->close();
|
|
|
|
yield $fs->unlink($path);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testTruncate() {
|
2015-07-17 16:27:38 +02:00
|
|
|
$this->getReactor()->run(function($reactor) {
|
2015-07-11 03:59:39 +02:00
|
|
|
$path = __DIR__ . "/fixture/truncate.txt";
|
|
|
|
$fs = $this->getFilesystem($reactor);
|
|
|
|
$flags = Filesystem::READ | Filesystem::WRITE | Filesystem::CREATE;
|
2015-07-17 16:27:38 +02:00
|
|
|
$fh = (yield $fs->open($path, $flags));
|
2015-07-11 03:59:39 +02:00
|
|
|
yield $fh->write(0, "test");
|
2015-07-17 16:27:38 +02:00
|
|
|
$data = (yield $fh->read(0, 8192));
|
2015-07-11 03:59:39 +02:00
|
|
|
$this->assertSame("test", $data);
|
|
|
|
yield $fh->truncate();
|
|
|
|
yield $fh->close();
|
|
|
|
|
2015-07-17 16:27:38 +02:00
|
|
|
$stat = (yield $fs->stat($path));
|
|
|
|
$this->assertEquals(0, $stat["size"]);
|
2015-07-11 03:59:39 +02:00
|
|
|
yield $fs->unlink($path);
|
|
|
|
});
|
|
|
|
}
|
2015-07-17 16:27:38 +02:00
|
|
|
|
2015-07-11 03:59:39 +02:00
|
|
|
public function testStat() {
|
2015-07-17 16:27:38 +02:00
|
|
|
$this->getReactor()->run(function($reactor) {
|
2015-07-11 03:59:39 +02:00
|
|
|
$fs = $this->getFilesystem($reactor);
|
|
|
|
|
2015-07-17 16:27:38 +02:00
|
|
|
$fh = (yield $fs->open(__DIR__ . "/fixture/small.txt"));
|
|
|
|
$stat = (yield $fh->stat());
|
2015-07-19 15:24:52 +02:00
|
|
|
$this->assertInternalType("array", $stat);
|
2015-07-11 03:59:39 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|