1
0
mirror of https://github.com/danog/file.git synced 2024-11-26 20:04:51 +01:00

Update tests

This commit is contained in:
Daniel Lowrey 2015-07-19 10:38:40 -04:00
parent 2fd53c6d89
commit 61d123bb45
3 changed files with 139 additions and 19 deletions

View File

@ -9,10 +9,61 @@ abstract class DescriptorTest extends \PHPUnit_Framework_TestCase {
abstract protected function getReactor(); abstract protected function getReactor();
abstract protected function getFilesystem(Reactor $reactor); abstract protected function getFilesystem(Reactor $reactor);
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();
}
public function testReadWriteCreate() { public function testReadWriteCreate() {
$this->getReactor()->run(function($reactor) { $this->getReactor()->run(function($reactor) {
$path = __DIR__ . "/fixture/new.txt";
$fs = $this->getFilesystem($reactor); $fs = $this->getFilesystem($reactor);
$fixtureDir = $this->getFixturePath();
$path = "{$fixtureDir}/new.txt";
$flags = Filesystem::READ | Filesystem::WRITE | Filesystem::CREATE; $flags = Filesystem::READ | Filesystem::WRITE | Filesystem::CREATE;
$fh = (yield $fs->open($path, $flags)); $fh = (yield $fs->open($path, $flags));
yield $fh->write(0, "test"); yield $fh->write(0, "test");
@ -25,8 +76,10 @@ abstract class DescriptorTest extends \PHPUnit_Framework_TestCase {
public function testTruncate() { public function testTruncate() {
$this->getReactor()->run(function($reactor) { $this->getReactor()->run(function($reactor) {
$path = __DIR__ . "/fixture/truncate.txt";
$fs = $this->getFilesystem($reactor); $fs = $this->getFilesystem($reactor);
$fixtureDir = $this->getFixturePath();
$path = "{$fixtureDir}/truncate.txt";
$flags = Filesystem::READ | Filesystem::WRITE | Filesystem::CREATE; $flags = Filesystem::READ | Filesystem::WRITE | Filesystem::CREATE;
$fh = (yield $fs->open($path, $flags)); $fh = (yield $fs->open($path, $flags));
yield $fh->write(0, "test"); yield $fh->write(0, "test");
@ -44,8 +97,9 @@ abstract class DescriptorTest extends \PHPUnit_Framework_TestCase {
public function testStat() { public function testStat() {
$this->getReactor()->run(function($reactor) { $this->getReactor()->run(function($reactor) {
$fs = $this->getFilesystem($reactor); $fs = $this->getFilesystem($reactor);
$fixtureDir = $this->getFixturePath();
$fh = (yield $fs->open(__DIR__ . "/fixture/small.txt")); $fh = (yield $fs->open("{$fixtureDir}/small.txt"));
$stat = (yield $fh->stat()); $stat = (yield $fh->stat());
$this->assertInternalType("array", $stat); $this->assertInternalType("array", $stat);
}); });

View File

@ -9,10 +9,61 @@ abstract class FilesystemTest extends \PHPUnit_Framework_TestCase {
abstract protected function getReactor(); abstract protected function getReactor();
abstract protected function getFilesystem(Reactor $reactor); abstract protected function getFilesystem(Reactor $reactor);
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();
}
public function testOpen() { public function testOpen() {
$this->getReactor()->run(function($reactor) { $this->getReactor()->run(function($reactor) {
$fs = $this->getFilesystem($reactor); $fs = $this->getFilesystem($reactor);
$descriptor = (yield $fs->open(__DIR__ . "/fixture/small.txt", Filesystem::READ)); $fixtureDir = self::getFixturePath();
$descriptor = (yield $fs->open("{$fixtureDir}/small.txt", Filesystem::READ));
$this->assertInstanceOf("Amp\Fs\Descriptor", $descriptor); $this->assertInstanceOf("Amp\Fs\Descriptor", $descriptor);
}); });
} }
@ -20,7 +71,8 @@ abstract class FilesystemTest extends \PHPUnit_Framework_TestCase {
public function testScandir() { public function testScandir() {
$this->getReactor()->run(function($reactor) { $this->getReactor()->run(function($reactor) {
$fs = $this->getFilesystem($reactor); $fs = $this->getFilesystem($reactor);
$actual = (yield $fs->scandir(__DIR__ . "/fixture")); $fixtureDir = self::getFixturePath();
$actual = (yield $fs->scandir($fixtureDir));
$expected = ["dir", "small.txt"]; $expected = ["dir", "small.txt"];
$this->assertSame($expected, $actual); $this->assertSame($expected, $actual);
}); });
@ -29,9 +81,10 @@ abstract class FilesystemTest extends \PHPUnit_Framework_TestCase {
public function testSymlink() { public function testSymlink() {
$this->getReactor()->run(function($reactor) { $this->getReactor()->run(function($reactor) {
$fs = $this->getFilesystem($reactor); $fs = $this->getFilesystem($reactor);
$fixtureDir = self::getFixturePath();
$target = __DIR__ . "/fixture/small.txt"; $target = "{$fixtureDir}/small.txt";
$link = __DIR__ . "/fixture/symlink.txt"; $link = "{$fixtureDir}/symlink.txt";
$this->assertTrue(yield $fs->symlink($target, $link)); $this->assertTrue(yield $fs->symlink($target, $link));
$this->assertTrue(is_link($link)); $this->assertTrue(is_link($link));
yield $fs->unlink($link); yield $fs->unlink($link);
@ -41,9 +94,10 @@ abstract class FilesystemTest extends \PHPUnit_Framework_TestCase {
public function testLstat() { public function testLstat() {
$this->getReactor()->run(function($reactor) { $this->getReactor()->run(function($reactor) {
$fs = $this->getFilesystem($reactor); $fs = $this->getFilesystem($reactor);
$fixtureDir = self::getFixturePath();
$target = __DIR__ . "/fixture/small.txt"; $target = "{$fixtureDir}/small.txt";
$link = __DIR__ . "/fixture/symlink.txt"; $link = "{$fixtureDir}/symlink.txt";
$this->assertTrue(yield $fs->symlink($target, $link)); $this->assertTrue(yield $fs->symlink($target, $link));
$this->assertTrue(is_array(yield $fs->lstat($link))); $this->assertTrue(is_array(yield $fs->lstat($link)));
yield $fs->unlink($link); yield $fs->unlink($link);
@ -56,7 +110,9 @@ abstract class FilesystemTest extends \PHPUnit_Framework_TestCase {
public function testOpenFailsOnNonexistentFile() { public function testOpenFailsOnNonexistentFile() {
$this->getReactor()->run(function($reactor) { $this->getReactor()->run(function($reactor) {
$fs = $this->getFilesystem($reactor); $fs = $this->getFilesystem($reactor);
$descriptor = (yield $fs->open(__DIR__ . "/fixture/nonexistent", Filesystem::READ)); $fixtureDir = self::getFixturePath();
$descriptor = (yield $fs->open("{$fixtureDir}/nonexistent", Filesystem::READ));
$this->assertInstanceOf("Amp\Fs\Descriptor", $descriptor); $this->assertInstanceOf("Amp\Fs\Descriptor", $descriptor);
}); });
} }
@ -64,26 +120,33 @@ abstract class FilesystemTest extends \PHPUnit_Framework_TestCase {
public function testStatForFile() { public function testStatForFile() {
$this->getReactor()->run(function($reactor) { $this->getReactor()->run(function($reactor) {
$fs = $this->getFilesystem($reactor); $fs = $this->getFilesystem($reactor);
$fixtureDir = self::getFixturePath();
$stat = (yield $fs->stat(__DIR__ . "/fixture/small.txt")); $stat = (yield $fs->stat("{$fixtureDir}/small.txt"));
$this->assertInternalType("array", $stat); $this->assertInternalType("array", $stat);
$this->assertTrue($stat["isfile"]);
$this->assertFalse($stat["isdir"]);
}); });
} }
public function testStatForDirectory() { public function testStatForDirectory() {
$this->getReactor()->run(function($reactor) { $this->getReactor()->run(function($reactor) {
$fs = $this->getFilesystem($reactor); $fs = $this->getFilesystem($reactor);
$fixtureDir = self::getFixturePath();
$stat = (yield $fs->stat(__DIR__ . "/fixture/dir")); $stat = (yield $fs->stat("{$fixtureDir}/dir"));
$this->assertInternalType("array", $stat); $this->assertInternalType("array", $stat);
$this->assertTrue($stat["isdir"]);
$this->assertFalse($stat["isfile"]);
}); });
} }
public function testStatForNonexistentPath() { public function testStatForNonexistentPath() {
$this->getReactor()->run(function($reactor) { $this->getReactor()->run(function($reactor) {
$fs = $this->getFilesystem($reactor); $fs = $this->getFilesystem($reactor);
$fixtureDir = self::getFixturePath();
$stat = (yield $fs->stat(__DIR__ . "/fixture/nonexistent")); $stat = (yield $fs->stat("{$fixtureDir}/nonexistent"));
$this->assertNull($stat); $this->assertNull($stat);
}); });
} }
@ -91,10 +154,11 @@ abstract class FilesystemTest extends \PHPUnit_Framework_TestCase {
public function testRename() { public function testRename() {
$this->getReactor()->run(function($reactor) { $this->getReactor()->run(function($reactor) {
$fs = $this->getFilesystem($reactor); $fs = $this->getFilesystem($reactor);
$fixtureDir = self::getFixturePath();
$contents1 = "rename test"; $contents1 = "rename test";
$old = __DIR__ . "/fixture/rename1.txt"; $old = "{$fixtureDir}/rename1.txt";
$new = __DIR__ . "/fixture/rename2.txt"; $new = "{$fixtureDir}/rename2.txt";
yield $fs->put($old, $contents1); yield $fs->put($old, $contents1);
yield $fs->rename($old, $new); yield $fs->rename($old, $new);
@ -108,8 +172,9 @@ abstract class FilesystemTest extends \PHPUnit_Framework_TestCase {
public function testUnlink() { public function testUnlink() {
$this->getReactor()->run(function($reactor) { $this->getReactor()->run(function($reactor) {
$fs = $this->getFilesystem($reactor); $fs = $this->getFilesystem($reactor);
$fixtureDir = self::getFixturePath();
$toUnlink = __DIR__ . "/fixture/unlink"; $toUnlink = "{$fixtureDir}/unlink";
yield $fs->put($toUnlink, "unlink me"); yield $fs->put($toUnlink, "unlink me");
$this->assertTrue((bool) (yield $fs->stat($toUnlink))); $this->assertTrue((bool) (yield $fs->stat($toUnlink)));
@ -121,8 +186,9 @@ abstract class FilesystemTest extends \PHPUnit_Framework_TestCase {
public function testMkdirRmdir() { public function testMkdirRmdir() {
$this->getReactor()->run(function($reactor) { $this->getReactor()->run(function($reactor) {
$fs = $this->getFilesystem($reactor); $fs = $this->getFilesystem($reactor);
$fixtureDir = self::getFixturePath();
$dir = __DIR__ . "/fixture/newdir"; $dir = "{$fixtureDir}/newdir";
yield $fs->mkdir($dir); yield $fs->mkdir($dir);
$stat = (yield $fs->stat($dir)); $stat = (yield $fs->stat($dir));
@ -139,8 +205,9 @@ abstract class FilesystemTest extends \PHPUnit_Framework_TestCase {
public function testTouch() { public function testTouch() {
$this->getReactor()->run(function($reactor) { $this->getReactor()->run(function($reactor) {
$fs = $this->getFilesystem($reactor); $fs = $this->getFilesystem($reactor);
$fixtureDir = self::getFixturePath();
$touch = __DIR__ . "/fixture/touch"; $touch = "{$fixtureDir}/touch";
yield $fs->put($touch, "touch me"); yield $fs->put($touch, "touch me");
$oldStat = (yield $fs->stat($touch)); $oldStat = (yield $fs->stat($touch));

View File

@ -1 +0,0 @@
small