2015-07-11 03:59:39 +02:00
|
|
|
<?php
|
|
|
|
|
2015-08-05 16:55:56 +02:00
|
|
|
namespace Amp\File\Test;
|
2015-07-11 03:59:39 +02:00
|
|
|
|
2015-08-08 16:09:07 +02:00
|
|
|
use Amp as amp;
|
|
|
|
use Amp\File as file;
|
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)) {
|
2015-08-08 16:09:07 +02:00
|
|
|
self::$fixtureId = \uniqid();
|
2015-07-19 16:38:40 +02:00
|
|
|
}
|
|
|
|
|
2015-08-08 16:09:07 +02:00
|
|
|
return \sys_get_temp_dir() . "/amphp_file_fixture/" . __CLASS__ . self::$fixtureId;
|
2015-07-19 16:38:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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-08-08 16:09:07 +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-08-08 16:09:07 +02:00
|
|
|
protected function setUp() {
|
|
|
|
file\StatCache::clear();
|
|
|
|
}
|
|
|
|
|
2015-07-11 03:59:39 +02:00
|
|
|
public function testScandir() {
|
2015-08-08 17:46:34 +02:00
|
|
|
amp\run(function () {
|
2015-07-19 16:38:40 +02:00
|
|
|
$fixtureDir = self::getFixturePath();
|
2015-08-08 16:09:07 +02:00
|
|
|
$actual = (yield file\scandir($fixtureDir));
|
2015-07-11 03:59:39 +02:00
|
|
|
$expected = ["dir", "small.txt"];
|
|
|
|
$this->assertSame($expected, $actual);
|
2015-08-08 17:46:34 +02:00
|
|
|
});
|
2015-07-11 03:59:39 +02:00
|
|
|
}
|
|
|
|
|
2015-08-08 16:09:07 +02:00
|
|
|
/**
|
|
|
|
* @expectedException \Amp\File\FilesystemException
|
|
|
|
*/
|
|
|
|
public function testScandirThrowsIfPathNotADirectory() {
|
2015-08-08 17:46:34 +02:00
|
|
|
amp\run(function () {
|
2015-08-08 16:09:07 +02:00
|
|
|
(yield file\scandir(__FILE__));
|
2015-08-08 17:46:34 +02:00
|
|
|
});
|
2015-08-08 16:09:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Amp\File\FilesystemException
|
|
|
|
*/
|
|
|
|
public function testScandirThrowsIfPathDoesntExist() {
|
2015-08-08 17:46:34 +02:00
|
|
|
amp\run(function () {
|
2015-08-08 16:09:07 +02:00
|
|
|
$path = self::getFixturePath() . "/nonexistent";
|
|
|
|
(yield file\scandir($path));
|
2015-08-08 17:46:34 +02:00
|
|
|
});
|
2015-08-08 16:09:07 +02:00
|
|
|
}
|
|
|
|
|
2015-07-11 03:59:39 +02:00
|
|
|
public function testSymlink() {
|
2015-08-08 17:46:34 +02:00
|
|
|
amp\run(function () {
|
2015-07-19 16:38:40 +02:00
|
|
|
$fixtureDir = self::getFixturePath();
|
2015-07-11 03:59:39 +02:00
|
|
|
|
2015-08-08 16:09:07 +02:00
|
|
|
$original = "{$fixtureDir}/small.txt";
|
2015-07-19 16:38:40 +02:00
|
|
|
$link = "{$fixtureDir}/symlink.txt";
|
2015-08-08 16:09:07 +02:00
|
|
|
$this->assertTrue(yield file\symlink($original, $link));
|
2015-07-30 15:07:01 +02:00
|
|
|
$this->assertTrue(\is_link($link));
|
2015-08-08 16:09:07 +02:00
|
|
|
yield file\unlink($link);
|
2015-08-08 17:46:34 +02:00
|
|
|
});
|
2015-07-11 03:59:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testLstat() {
|
2015-08-08 17:46:34 +02:00
|
|
|
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-08-08 16:09:07 +02:00
|
|
|
$this->assertTrue(yield file\symlink($target, $link));
|
|
|
|
$this->assertTrue(is_array(yield file\lstat($link)));
|
|
|
|
yield file\unlink($link);
|
2015-08-08 17:46:34 +02:00
|
|
|
});
|
2015-07-11 03:59:39 +02:00
|
|
|
}
|
|
|
|
|
2015-08-08 16:09:07 +02:00
|
|
|
public function testFileStat() {
|
2015-08-08 17:46:34 +02:00
|
|
|
amp\run(function () {
|
2015-07-19 16:38:40 +02:00
|
|
|
$fixtureDir = self::getFixturePath();
|
2015-08-08 16:09:07 +02:00
|
|
|
$stat = (yield file\stat("{$fixtureDir}/small.txt"));
|
|
|
|
$this->assertInternalType("array", $stat);
|
2015-08-08 17:46:34 +02:00
|
|
|
});
|
2015-08-08 16:09:07 +02:00
|
|
|
}
|
2015-07-11 03:59:39 +02:00
|
|
|
|
2015-08-08 16:09:07 +02:00
|
|
|
public function testDirStat() {
|
2015-08-08 17:46:34 +02:00
|
|
|
amp\run(function () {
|
2015-08-08 16:09:07 +02:00
|
|
|
$fixtureDir = self::getFixturePath();
|
|
|
|
$stat = (yield file\stat("{$fixtureDir}/dir"));
|
2015-07-19 15:24:52 +02:00
|
|
|
$this->assertInternalType("array", $stat);
|
2015-08-08 17:46:34 +02:00
|
|
|
});
|
2015-07-19 15:24:52 +02:00
|
|
|
}
|
|
|
|
|
2015-08-08 16:09:07 +02:00
|
|
|
public function testNonexistentPathStatResolvesToNull() {
|
2015-08-08 17:46:34 +02:00
|
|
|
amp\run(function () {
|
2015-07-19 16:38:40 +02:00
|
|
|
$fixtureDir = self::getFixturePath();
|
2015-08-08 16:09:07 +02:00
|
|
|
$stat = (yield file\stat("{$fixtureDir}/nonexistent"));
|
|
|
|
$this->assertNull($stat);
|
2015-08-08 17:46:34 +02:00
|
|
|
});
|
2015-08-08 16:09:07 +02:00
|
|
|
}
|
2015-07-11 03:59:39 +02:00
|
|
|
|
2015-08-08 16:09:07 +02:00
|
|
|
public function testExists() {
|
2015-08-08 17:46:34 +02:00
|
|
|
amp\run(function () {
|
2015-08-08 16:09:07 +02:00
|
|
|
$fixtureDir = self::getFixturePath();
|
|
|
|
$this->assertFalse(yield file\exists("{$fixtureDir}/nonexistent"));
|
|
|
|
$this->assertTrue(yield file\exists("{$fixtureDir}/small.txt"));
|
2015-08-08 17:46:34 +02:00
|
|
|
});
|
2015-07-19 15:24:52 +02:00
|
|
|
}
|
|
|
|
|
2015-08-08 16:09:07 +02:00
|
|
|
public function testSize() {
|
2015-08-08 17:46:34 +02:00
|
|
|
amp\run(function () {
|
2015-07-19 16:38:40 +02:00
|
|
|
$fixtureDir = self::getFixturePath();
|
2015-08-08 16:09:07 +02:00
|
|
|
$path = "{$fixtureDir}/small.txt";
|
|
|
|
$stat = (yield file\stat($path));
|
|
|
|
$size = $stat["size"];
|
|
|
|
file\StatCache::clear($path);
|
|
|
|
$this->assertSame($size, (yield file\size($path)));
|
2015-08-08 17:46:34 +02:00
|
|
|
});
|
2015-08-08 16:09:07 +02:00
|
|
|
}
|
2015-07-11 03:59:39 +02:00
|
|
|
|
2015-08-08 16:09:07 +02:00
|
|
|
/**
|
|
|
|
* @expectedException \Amp\File\FilesystemException
|
|
|
|
*/
|
|
|
|
public function testSizeFailsOnNonexistentPath() {
|
2015-08-08 17:46:34 +02:00
|
|
|
amp\run(function () {
|
2015-08-08 16:09:07 +02:00
|
|
|
$fixtureDir = self::getFixturePath();
|
|
|
|
$path = "{$fixtureDir}/nonexistent";
|
|
|
|
yield file\size($path);
|
2015-08-08 17:46:34 +02:00
|
|
|
});
|
2015-08-08 16:09:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Amp\File\FilesystemException
|
|
|
|
*/
|
|
|
|
public function testSizeFailsOnDirectoryPath() {
|
2015-08-08 17:46:34 +02:00
|
|
|
amp\run(function () {
|
2015-08-08 16:09:07 +02:00
|
|
|
$fixtureDir = self::getFixturePath();
|
|
|
|
$path = "{$fixtureDir}/dir";
|
|
|
|
$this->assertTrue(yield file\isdir($path));
|
|
|
|
file\StatCache::clear($path);
|
|
|
|
yield file\size($path);
|
2015-08-08 17:46:34 +02:00
|
|
|
});
|
2015-08-08 16:09:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testIsdirResolvesTrueOnDirectoryPath() {
|
2015-08-08 17:46:34 +02:00
|
|
|
amp\run(function () {
|
2015-08-08 16:09:07 +02:00
|
|
|
$fixtureDir = self::getFixturePath();
|
|
|
|
$path = "{$fixtureDir}/dir";
|
|
|
|
$this->assertTrue(yield file\isdir($path));
|
2015-08-08 17:46:34 +02:00
|
|
|
});
|
2015-08-08 16:09:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testIsdirResolvesFalseOnFilePath() {
|
2015-08-08 17:46:34 +02:00
|
|
|
amp\run(function () {
|
2015-08-08 16:09:07 +02:00
|
|
|
$fixtureDir = self::getFixturePath();
|
|
|
|
$path = "{$fixtureDir}/small.txt";
|
|
|
|
$this->assertFalse(yield file\isdir($path));
|
2015-08-08 17:46:34 +02:00
|
|
|
});
|
2015-08-08 16:09:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testIsdirResolvesFalseOnNonexistentPath() {
|
2015-08-08 17:46:34 +02:00
|
|
|
amp\run(function () {
|
2015-08-08 16:09:07 +02:00
|
|
|
$fixtureDir = self::getFixturePath();
|
|
|
|
$path = "{$fixtureDir}/nonexistent";
|
|
|
|
$this->assertFalse(yield file\isdir($path));
|
2015-08-08 17:46:34 +02:00
|
|
|
});
|
2015-08-08 16:09:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testIsfileResolvesTrueOnFilePath() {
|
2015-08-08 17:46:34 +02:00
|
|
|
amp\run(function () {
|
2015-08-08 16:09:07 +02:00
|
|
|
$fixtureDir = self::getFixturePath();
|
|
|
|
$path = "{$fixtureDir}/small.txt";
|
|
|
|
$this->assertTrue(yield file\isfile($path));
|
2015-08-08 17:46:34 +02:00
|
|
|
});
|
2015-08-08 16:09:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testIsfileResolvesFalseOnDirectoryPath() {
|
2015-08-08 17:46:34 +02:00
|
|
|
amp\run(function () {
|
2015-08-08 16:09:07 +02:00
|
|
|
$fixtureDir = self::getFixturePath();
|
|
|
|
$path = "{$fixtureDir}/dir";
|
|
|
|
$this->assertFalse(yield file\isfile($path));
|
2015-08-08 17:46:34 +02:00
|
|
|
});
|
2015-08-08 16:09:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testIsfileResolvesFalseOnNonexistentPath() {
|
2015-08-08 17:46:34 +02:00
|
|
|
amp\run(function () {
|
2015-08-08 16:09:07 +02:00
|
|
|
$fixtureDir = self::getFixturePath();
|
|
|
|
$path = "{$fixtureDir}/nonexistent";
|
|
|
|
$this->assertFalse(yield file\isfile($path));
|
2015-08-08 17:46:34 +02:00
|
|
|
});
|
2015-07-11 03:59:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testRename() {
|
2015-08-08 17:46:34 +02:00
|
|
|
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-08-08 16:09:07 +02:00
|
|
|
yield file\put($old, $contents1);
|
|
|
|
yield file\rename($old, $new);
|
|
|
|
$contents2 = (yield file\get($new));
|
|
|
|
yield file\unlink($new);
|
2015-07-11 03:59:39 +02:00
|
|
|
|
|
|
|
$this->assertSame($contents1, $contents2);
|
2015-08-08 17:46:34 +02:00
|
|
|
});
|
2015-07-11 03:59:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testUnlink() {
|
2015-08-08 17:46:34 +02:00
|
|
|
amp\run(function () {
|
2015-07-19 16:38:40 +02:00
|
|
|
$fixtureDir = self::getFixturePath();
|
|
|
|
$toUnlink = "{$fixtureDir}/unlink";
|
2015-08-08 16:09:07 +02:00
|
|
|
yield file\put($toUnlink, "unlink me");
|
|
|
|
$this->assertTrue((bool) (yield file\stat($toUnlink)));
|
|
|
|
yield file\unlink($toUnlink);
|
|
|
|
$this->assertNull(yield file\stat($toUnlink));
|
2015-08-08 17:46:34 +02:00
|
|
|
});
|
2015-07-11 03:59:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testMkdirRmdir() {
|
2015-08-08 17:46:34 +02:00
|
|
|
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-08-08 16:09:07 +02:00
|
|
|
yield file\mkdir($dir);
|
2016-09-28 12:39:24 +02:00
|
|
|
$this->assertNotNull(yield file\stat($dir));
|
2015-08-08 16:09:07 +02:00
|
|
|
yield file\rmdir($dir);
|
|
|
|
$this->assertNull(yield file\stat($dir));
|
2016-09-28 12:39:24 +02:00
|
|
|
|
|
|
|
$dir = "{$fixtureDir}/newdir/with/recursive/creation";
|
|
|
|
|
|
|
|
yield file\mkdir($dir, 0744, true);
|
|
|
|
$this->assertNotNull(yield file\stat($dir));
|
2015-08-08 17:46:34 +02:00
|
|
|
});
|
2015-08-08 16:09:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testMtime() {
|
2015-08-08 17:46:34 +02:00
|
|
|
amp\run(function () {
|
2015-08-08 16:09:07 +02:00
|
|
|
$fixtureDir = self::getFixturePath();
|
|
|
|
$path = "{$fixtureDir}/small.txt";
|
|
|
|
$stat = (yield file\stat($path));
|
|
|
|
$statMtime = $stat["mtime"];
|
|
|
|
file\StatCache::clear($path);
|
|
|
|
$this->assertSame($statMtime, (yield file\mtime($path)));
|
2015-08-08 17:46:34 +02:00
|
|
|
});
|
2015-08-08 16:09:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Amp\File\FilesystemException
|
|
|
|
*/
|
|
|
|
public function testMtimeFailsOnNonexistentPath() {
|
2015-08-08 17:46:34 +02:00
|
|
|
amp\run(function () {
|
2015-08-08 16:09:07 +02:00
|
|
|
$fixtureDir = self::getFixturePath();
|
|
|
|
$path = "{$fixtureDir}/nonexistent";
|
|
|
|
yield file\mtime($path);
|
2015-08-08 17:46:34 +02:00
|
|
|
});
|
2015-08-08 16:09:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAtime() {
|
2015-08-08 17:46:34 +02:00
|
|
|
amp\run(function () {
|
2015-08-08 16:09:07 +02:00
|
|
|
$fixtureDir = self::getFixturePath();
|
|
|
|
$path = "{$fixtureDir}/small.txt";
|
|
|
|
$stat = (yield file\stat($path));
|
|
|
|
$statAtime = $stat["atime"];
|
|
|
|
file\StatCache::clear($path);
|
|
|
|
$this->assertSame($statAtime, (yield file\atime($path)));
|
2015-08-08 17:46:34 +02:00
|
|
|
});
|
2015-08-08 16:09:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Amp\File\FilesystemException
|
|
|
|
*/
|
|
|
|
public function testAtimeFailsOnNonexistentPath() {
|
2015-08-08 17:46:34 +02:00
|
|
|
amp\run(function () {
|
2015-08-08 16:09:07 +02:00
|
|
|
$fixtureDir = self::getFixturePath();
|
|
|
|
$path = "{$fixtureDir}/nonexistent";
|
|
|
|
yield file\atime($path);
|
2015-08-08 17:46:34 +02:00
|
|
|
});
|
2015-08-08 16:09:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testCtime() {
|
2015-08-08 17:46:34 +02:00
|
|
|
amp\run(function () {
|
2015-08-08 16:09:07 +02:00
|
|
|
$fixtureDir = self::getFixturePath();
|
|
|
|
$path = "{$fixtureDir}/small.txt";
|
|
|
|
$stat = (yield file\stat($path));
|
|
|
|
$statCtime = $stat["ctime"];
|
|
|
|
file\StatCache::clear($path);
|
|
|
|
$this->assertSame($statCtime, (yield file\ctime($path)));
|
2015-08-08 17:46:34 +02:00
|
|
|
});
|
2015-08-08 16:09:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Amp\File\FilesystemException
|
|
|
|
*/
|
|
|
|
public function testCtimeFailsOnNonexistentPath() {
|
2015-08-08 17:46:34 +02:00
|
|
|
amp\run(function () {
|
2015-08-08 16:09:07 +02:00
|
|
|
$fixtureDir = self::getFixturePath();
|
|
|
|
$path = "{$fixtureDir}/nonexistent";
|
|
|
|
yield file\ctime($path);
|
2015-08-08 17:46:34 +02:00
|
|
|
});
|
2015-07-11 03:59:39 +02:00
|
|
|
}
|
2015-07-18 20:53:46 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @group slow
|
|
|
|
*/
|
|
|
|
public function testTouch() {
|
2015-08-08 17:46:34 +02:00
|
|
|
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-08-08 16:09:07 +02:00
|
|
|
yield file\put($touch, "touch me");
|
2015-07-18 20:53:46 +02:00
|
|
|
|
2015-08-08 16:09:07 +02:00
|
|
|
$oldStat = (yield file\stat($touch));
|
2015-07-18 20:53:46 +02:00
|
|
|
sleep(1);
|
2015-08-08 16:09:07 +02:00
|
|
|
yield file\touch($touch);
|
|
|
|
file\StatCache::clear($touch);
|
|
|
|
$newStat = (yield file\stat($touch));
|
|
|
|
yield file\unlink($touch);
|
2015-07-18 20:53:46 +02:00
|
|
|
|
|
|
|
$this->assertTrue($newStat["atime"] > $oldStat["atime"]);
|
|
|
|
$this->assertTrue($newStat["mtime"] > $oldStat["mtime"]);
|
2015-08-08 17:46:34 +02:00
|
|
|
});
|
2015-07-18 20:53:46 +02:00
|
|
|
}
|
2015-07-11 03:59:39 +02:00
|
|
|
}
|