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

224 lines
7.1 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 FilesystemTest 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 testOpen() {
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 = self::getFixturePath();
$descriptor = (yield $fs->open("{$fixtureDir}/small.txt", Filesystem::READ));
2015-07-11 03:59:39 +02:00
$this->assertInstanceOf("Amp\Fs\Descriptor", $descriptor);
});
}
public function testScandir() {
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 = self::getFixturePath();
$actual = (yield $fs->scandir($fixtureDir));
2015-07-11 03:59:39 +02:00
$expected = ["dir", "small.txt"];
$this->assertSame($expected, $actual);
});
}
public function testSymlink() {
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 = self::getFixturePath();
2015-07-11 03:59:39 +02:00
2015-07-19 16:38:40 +02:00
$target = "{$fixtureDir}/small.txt";
$link = "{$fixtureDir}/symlink.txt";
2015-07-11 03:59:39 +02:00
$this->assertTrue(yield $fs->symlink($target, $link));
$this->assertTrue(is_link($link));
yield $fs->unlink($link);
});
}
public function testLstat() {
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 = self::getFixturePath();
2015-07-11 03:59:39 +02:00
2015-07-19 16:38:40 +02:00
$target = "{$fixtureDir}/small.txt";
$link = "{$fixtureDir}/symlink.txt";
2015-07-11 03:59:39 +02:00
$this->assertTrue(yield $fs->symlink($target, $link));
$this->assertTrue(is_array(yield $fs->lstat($link)));
yield $fs->unlink($link);
});
}
/**
* @expectedException RuntimeException
*/
public function testOpenFailsOnNonexistentFile() {
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 = self::getFixturePath();
$descriptor = (yield $fs->open("{$fixtureDir}/nonexistent", Filesystem::READ));
2015-07-11 03:59:39 +02:00
$this->assertInstanceOf("Amp\Fs\Descriptor", $descriptor);
});
}
2015-07-19 15:24:52 +02:00
public function testStatForFile() {
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 = self::getFixturePath();
2015-07-11 03:59:39 +02:00
2015-07-19 16:38:40 +02:00
$stat = (yield $fs->stat("{$fixtureDir}/small.txt"));
2015-07-19 15:24:52 +02:00
$this->assertInternalType("array", $stat);
2015-07-19 16:38:40 +02:00
$this->assertTrue($stat["isfile"]);
$this->assertFalse($stat["isdir"]);
2015-07-19 15:24:52 +02:00
});
}
public function testStatForDirectory() {
$this->getReactor()->run(function($reactor) {
$fs = $this->getFilesystem($reactor);
2015-07-19 16:38:40 +02:00
$fixtureDir = self::getFixturePath();
2015-07-11 03:59:39 +02:00
2015-07-19 16:38:40 +02:00
$stat = (yield $fs->stat("{$fixtureDir}/dir"));
2015-07-19 15:24:52 +02:00
$this->assertInternalType("array", $stat);
2015-07-19 16:38:40 +02:00
$this->assertTrue($stat["isdir"]);
$this->assertFalse($stat["isfile"]);
2015-07-19 15:24:52 +02:00
});
}
public function testStatForNonexistentPath() {
$this->getReactor()->run(function($reactor) {
$fs = $this->getFilesystem($reactor);
2015-07-19 16:38:40 +02:00
$fixtureDir = self::getFixturePath();
2015-07-11 03:59:39 +02:00
2015-07-19 16:38:40 +02:00
$stat = (yield $fs->stat("{$fixtureDir}/nonexistent"));
2015-07-11 03:59:39 +02:00
$this->assertNull($stat);
});
}
public function testRename() {
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 = self::getFixturePath();
2015-07-11 03:59:39 +02:00
$contents1 = "rename test";
2015-07-19 16:38:40 +02:00
$old = "{$fixtureDir}/rename1.txt";
$new = "{$fixtureDir}/rename2.txt";
2015-07-11 03:59:39 +02:00
yield $fs->put($old, $contents1);
yield $fs->rename($old, $new);
2015-07-17 16:27:38 +02:00
$contents2 = (yield $fs->get($new));
2015-07-11 03:59:39 +02:00
yield $fs->unlink($new);
$this->assertSame($contents1, $contents2);
});
}
public function testUnlink() {
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 = self::getFixturePath();
2015-07-11 03:59:39 +02:00
2015-07-19 16:38:40 +02:00
$toUnlink = "{$fixtureDir}/unlink";
2015-07-11 03:59:39 +02:00
2015-07-18 18:08:50 +02:00
yield $fs->put($toUnlink, "unlink me");
2015-07-17 16:27:38 +02:00
$this->assertTrue((bool) (yield $fs->stat($toUnlink)));
2015-07-11 03:59:39 +02:00
yield $fs->unlink($toUnlink);
$this->assertNull(yield $fs->stat($toUnlink));
});
}
public function testMkdirRmdir() {
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 = self::getFixturePath();
2015-07-11 03:59:39 +02:00
2015-07-19 16:38:40 +02:00
$dir = "{$fixtureDir}/newdir";
2015-07-11 03:59:39 +02:00
yield $fs->mkdir($dir);
2015-07-17 16:27:38 +02:00
$stat = (yield $fs->stat($dir));
2015-07-11 03:59:39 +02:00
$this->assertTrue($stat["isdir"]);
$this->assertFalse($stat["isfile"]);
yield $fs->rmdir($dir);
$this->assertNull(yield $fs->stat($dir));
});
}
2015-07-18 20:53:46 +02:00
/**
* @group slow
*/
public function testTouch() {
$this->getReactor()->run(function($reactor) {
$fs = $this->getFilesystem($reactor);
2015-07-19 16:38:40 +02:00
$fixtureDir = self::getFixturePath();
2015-07-18 20:53:46 +02:00
2015-07-19 16:38:40 +02:00
$touch = "{$fixtureDir}/touch";
2015-07-18 20:53:46 +02:00
yield $fs->put($touch, "touch me");
$oldStat = (yield $fs->stat($touch));
sleep(1);
yield $fs->touch($touch);
$newStat = (yield $fs->stat($touch));
yield $fs->unlink($touch);
$this->assertTrue($newStat["atime"] > $oldStat["atime"]);
$this->assertTrue($newStat["mtime"] > $oldStat["mtime"]);
});
}
2015-07-11 03:59:39 +02:00
}