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

187 lines
5.8 KiB
PHP
Raw Normal View History

2015-07-11 03:59:39 +02:00
<?php
2015-07-30 16:10:53 +02:00
namespace Amp\Filesystem\Test;
2015-07-11 03:59:39 +02:00
2015-07-30 16:10:53 +02:00
use Amp\Filesystem\Driver;
2015-07-11 03:59:39 +02:00
2015-07-30 16:10:53 +02:00
abstract class DriverTest extends \PHPUnit_Framework_TestCase {
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;
}
2015-07-30 16:10:53 +02:00
if (\stripos(\PHP_OS, "win") === 0) {
\system('rd /Q /S "' . $fixtureDir . '"');
2015-07-19 16:38:40 +02:00
} else {
2015-07-30 16:10:53 +02:00
\system('/bin/rm -rf ' . escapeshellarg($fixtureDir));
2015-07-19 16:38:40 +02:00
}
}
public static function setUpBeforeClass() {
$fixtureDir = self::getFixturePath();
self::clearFixtureDir();
2015-07-30 16:10:53 +02:00
if (!\mkdir($fixtureDir, $mode = 0777, $recursive = true)) {
2015-07-19 16:38:40 +02:00
throw new \RuntimeException(
"Failed creating temporary test fixture directory: {$fixtureDir}"
);
}
2015-07-30 16:10:53 +02:00
if (!\mkdir($fixtureDir . "/dir", $mode = 0777, $recursive = true)) {
2015-07-19 16:38:40 +02:00
throw new \RuntimeException(
"Failed creating temporary test fixture directory"
);
}
2015-07-30 16:10:53 +02:00
if (!\file_put_contents($fixtureDir . "/small.txt", "small")) {
2015-07-19 16:38:40 +02:00
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 testScandir() {
\Amp\run(function () {
2015-07-19 16:38:40 +02:00
$fixtureDir = self::getFixturePath();
2015-07-30 16:10:53 +02:00
$actual = (yield \Amp\Filesystem\scandir($fixtureDir));
2015-07-11 03:59:39 +02:00
$expected = ["dir", "small.txt"];
$this->assertSame($expected, $actual);
});
}
public function testSymlink() {
\Amp\run(function () {
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-30 16:10:53 +02:00
$this->assertTrue(yield \Amp\Filesystem\symlink($target, $link));
$this->assertTrue(\is_link($link));
2015-07-30 16:10:53 +02:00
yield \Amp\Filesystem\unlink($link);
2015-07-11 03:59:39 +02:00
});
}
public function testLstat() {
\Amp\run(function () {
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-30 16:10:53 +02:00
$this->assertTrue(yield \Amp\Filesystem\symlink($target, $link));
$this->assertTrue(is_array(yield \Amp\Filesystem\lstat($link)));
yield \Amp\Filesystem\unlink($link);
2015-07-11 03:59:39 +02:00
});
}
2015-07-19 15:24:52 +02:00
public function testStatForFile() {
\Amp\run(function () {
2015-07-19 16:38:40 +02:00
$fixtureDir = self::getFixturePath();
2015-07-11 03:59:39 +02:00
2015-07-30 16:10:53 +02:00
$stat = (yield \Amp\Filesystem\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() {
\Amp\run(function () {
2015-07-19 16:38:40 +02:00
$fixtureDir = self::getFixturePath();
2015-07-11 03:59:39 +02:00
2015-07-30 16:10:53 +02:00
$stat = (yield \Amp\Filesystem\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() {
\Amp\run(function () {
2015-07-19 16:38:40 +02:00
$fixtureDir = self::getFixturePath();
2015-07-11 03:59:39 +02:00
2015-07-30 16:10:53 +02:00
$stat = (yield \Amp\Filesystem\stat("{$fixtureDir}/nonexistent"));
2015-07-11 03:59:39 +02:00
$this->assertNull($stat);
});
}
public function testRename() {
\Amp\run(function () {
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
2015-07-30 16:10:53 +02:00
yield \Amp\Filesystem\put($old, $contents1);
yield \Amp\Filesystem\rename($old, $new);
$contents2 = (yield \Amp\Filesystem\get($new));
yield \Amp\Filesystem\unlink($new);
2015-07-11 03:59:39 +02:00
$this->assertSame($contents1, $contents2);
});
}
public function testUnlink() {
\Amp\run(function () {
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-30 16:10:53 +02:00
yield \Amp\Filesystem\put($toUnlink, "unlink me");
$this->assertTrue((bool) (yield \Amp\Filesystem\stat($toUnlink)));
yield \Amp\Filesystem\unlink($toUnlink);
$this->assertNull(yield \Amp\Filesystem\stat($toUnlink));
2015-07-11 03:59:39 +02:00
});
}
public function testMkdirRmdir() {
\Amp\run(function () {
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
2015-07-30 16:10:53 +02:00
yield \Amp\Filesystem\mkdir($dir);
$stat = (yield \Amp\Filesystem\stat($dir));
2015-07-11 03:59:39 +02:00
$this->assertTrue($stat["isdir"]);
$this->assertFalse($stat["isfile"]);
2015-07-30 16:10:53 +02:00
yield \Amp\Filesystem\rmdir($dir);
$this->assertNull(yield \Amp\Filesystem\stat($dir));
2015-07-11 03:59:39 +02:00
});
}
2015-07-18 20:53:46 +02:00
/**
* @group slow
*/
public function testTouch() {
\Amp\run(function () {
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-30 16:10:53 +02:00
yield \Amp\Filesystem\put($touch, "touch me");
2015-07-18 20:53:46 +02:00
2015-07-30 16:10:53 +02:00
$oldStat = (yield \Amp\Filesystem\stat($touch));
2015-07-18 20:53:46 +02:00
sleep(1);
2015-07-30 16:10:53 +02:00
yield \Amp\Filesystem\touch($touch);
$newStat = (yield \Amp\Filesystem\stat($touch));
yield \Amp\Filesystem\unlink($touch);
2015-07-18 20:53:46 +02:00
$this->assertTrue($newStat["atime"] > $oldStat["atime"]);
$this->assertTrue($newStat["mtime"] > $oldStat["mtime"]);
});
}
2015-07-11 03:59:39 +02:00
}