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
|
|
|
|
2015-07-19 16:38:40 +02:00
|
|
|
private static $fixtureId;
|
|
|
|
|
|
|
|
private static function getFixturePath() {
|
|
|
|
if (empty(self::$fixtureId)) {
|
|
|
|
self::$fixtureId = uniqid();
|
|
|
|
}
|
|
|
|
|
|
|
|
return \sys_get_temp_dir() . "/amp_fs_fixture/" . __CLASS__ . self::$fixtureId;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static function clearFixtureDir() {
|
|
|
|
$fixtureDir = self::getFixturePath();
|
|
|
|
if (!file_exists($fixtureDir)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stripos(\PHP_OS, "win") === 0) {
|
|
|
|
system('rd /Q /S "' . $fixtureDir . '"');
|
|
|
|
} else {
|
|
|
|
system('/bin/rm -rf ' . escapeshellarg($fixtureDir));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function setUpBeforeClass() {
|
|
|
|
$fixtureDir = self::getFixturePath();
|
|
|
|
|
|
|
|
self::clearFixtureDir();
|
|
|
|
|
|
|
|
if (!mkdir($fixtureDir, $mode = 0777, $recursive = true)) {
|
|
|
|
throw new \RuntimeException(
|
|
|
|
"Failed creating temporary test fixture directory: {$fixtureDir}"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (!mkdir($fixtureDir . "/dir", $mode = 0777, $recursive = true)) {
|
|
|
|
throw new \RuntimeException(
|
|
|
|
"Failed creating temporary test fixture directory"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (!file_put_contents($fixtureDir . "/small.txt", "small")) {
|
|
|
|
throw new \RuntimeException(
|
|
|
|
"Failed creating temporary test fixture file"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function tearDownAfterClass() {
|
|
|
|
self::clearFixtureDir();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
$fs = $this->getFilesystem($reactor);
|
2015-07-19 16:38:40 +02:00
|
|
|
$fixtureDir = $this->getFixturePath();
|
|
|
|
|
|
|
|
$path = "{$fixtureDir}/new.txt";
|
2015-07-11 03:59:39 +02:00
|
|
|
$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
|
|
|
$fs = $this->getFilesystem($reactor);
|
2015-07-19 16:38:40 +02:00
|
|
|
$fixtureDir = $this->getFixturePath();
|
|
|
|
|
|
|
|
$path = "{$fixtureDir}/truncate.txt";
|
2015-07-11 03:59:39 +02:00
|
|
|
$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-19 16:38:40 +02:00
|
|
|
$fixtureDir = $this->getFixturePath();
|
2015-07-11 03:59:39 +02:00
|
|
|
|
2015-07-19 16:38:40 +02:00
|
|
|
$fh = (yield $fs->open("{$fixtureDir}/small.txt"));
|
2015-07-17 16:27:38 +02:00
|
|
|
$stat = (yield $fh->stat());
|
2015-07-19 15:24:52 +02:00
|
|
|
$this->assertInternalType("array", $stat);
|
2015-07-11 03:59:39 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|