2016-12-30 03:59:59 +01:00
|
|
|
<?php
|
2015-07-11 03:59:39 +02:00
|
|
|
|
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\File as file;
|
2017-05-18 05:58:39 +02:00
|
|
|
use Amp\PHPUnit\TestCase;
|
2015-07-11 03:59:39 +02:00
|
|
|
|
2017-05-18 05:58:39 +02:00
|
|
|
abstract class DriverTest extends TestCase {
|
2017-06-17 01:12:42 +02:00
|
|
|
protected function setUp() {
|
|
|
|
Fixture::init();
|
|
|
|
File\StatCache::clear();
|
2015-07-19 16:38:40 +02:00
|
|
|
}
|
|
|
|
|
2017-06-17 01:12:42 +02:00
|
|
|
protected function tearDown() {
|
|
|
|
Fixture::clear();
|
2015-08-08 16:09:07 +02:00
|
|
|
}
|
2017-01-11 14:22:06 +01:00
|
|
|
|
2017-06-17 01:12:42 +02:00
|
|
|
abstract protected function execute(callable $cb);
|
2015-08-08 16:09:07 +02:00
|
|
|
|
2015-07-11 03:59:39 +02:00
|
|
|
public function testScandir() {
|
2017-06-17 01:12:42 +02:00
|
|
|
$this->execute(function () {
|
|
|
|
$fixtureDir = Fixture::path();
|
2017-06-17 15:36:51 +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() {
|
2017-06-17 01:12:42 +02:00
|
|
|
$this->execute(function () {
|
|
|
|
(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() {
|
2017-06-17 01:12:42 +02:00
|
|
|
$this->execute(function () {
|
|
|
|
$path = Fixture::path() . "/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() {
|
2017-06-17 01:12:42 +02:00
|
|
|
$this->execute(function () {
|
|
|
|
$fixtureDir = Fixture::path();
|
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";
|
2017-06-17 01:12:42 +02:00
|
|
|
$this->assertTrue(yield File\symlink($original, $link));
|
2015-07-30 15:07:01 +02:00
|
|
|
$this->assertTrue(\is_link($link));
|
2017-06-17 01:12:42 +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() {
|
2017-06-17 01:12:42 +02:00
|
|
|
$this->execute(function () {
|
|
|
|
$fixtureDir = Fixture::path();
|
2015-07-11 03:59:39 +02:00
|
|
|
|
2015-07-19 16:38:40 +02:00
|
|
|
$target = "{$fixtureDir}/small.txt";
|
|
|
|
$link = "{$fixtureDir}/symlink.txt";
|
2017-06-17 01:12:42 +02:00
|
|
|
$this->assertTrue(yield File\symlink($target, $link));
|
2017-06-17 23:41:57 +02:00
|
|
|
$this->assertInternalType('array', yield File\lstat($link));
|
2017-06-17 01:12:42 +02:00
|
|
|
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() {
|
2017-06-17 01:12:42 +02:00
|
|
|
$this->execute(function () {
|
|
|
|
$fixtureDir = Fixture::path();
|
|
|
|
$stat = (yield File\stat("{$fixtureDir}/small.txt"));
|
2015-08-08 16:09:07 +02:00
|
|
|
$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() {
|
2017-06-17 01:12:42 +02:00
|
|
|
$this->execute(function () {
|
|
|
|
$fixtureDir = Fixture::path();
|
|
|
|
$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() {
|
2017-06-17 01:12:42 +02:00
|
|
|
$this->execute(function () {
|
|
|
|
$fixtureDir = Fixture::path();
|
|
|
|
$stat = (yield File\stat("{$fixtureDir}/nonexistent"));
|
2015-08-08 16:09:07 +02:00
|
|
|
$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() {
|
2017-06-17 01:12:42 +02:00
|
|
|
$this->execute(function () {
|
|
|
|
$fixtureDir = Fixture::path();
|
|
|
|
$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
|
|
|
}
|
|
|
|
|
2017-06-17 15:36:51 +02:00
|
|
|
public function testGet() {
|
|
|
|
$this->execute(function () {
|
|
|
|
$fixtureDir = Fixture::path();
|
|
|
|
$this->assertSame("small", yield File\get("{$fixtureDir}/small.txt"));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-08-08 16:09:07 +02:00
|
|
|
public function testSize() {
|
2017-06-17 01:12:42 +02:00
|
|
|
$this->execute(function () {
|
|
|
|
$fixtureDir = Fixture::path();
|
2015-08-08 16:09:07 +02:00
|
|
|
$path = "{$fixtureDir}/small.txt";
|
2017-06-17 01:12:42 +02:00
|
|
|
$stat = (yield File\stat($path));
|
2015-08-08 16:09:07 +02:00
|
|
|
$size = $stat["size"];
|
2017-06-17 01:12:42 +02:00
|
|
|
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() {
|
2017-06-17 01:12:42 +02:00
|
|
|
$this->execute(function () {
|
|
|
|
$fixtureDir = Fixture::path();
|
2015-08-08 16:09:07 +02:00
|
|
|
$path = "{$fixtureDir}/nonexistent";
|
2017-06-17 01:12:42 +02:00
|
|
|
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() {
|
2017-06-17 01:12:42 +02:00
|
|
|
$this->execute(function () {
|
|
|
|
$fixtureDir = Fixture::path();
|
2015-08-08 16:09:07 +02:00
|
|
|
$path = "{$fixtureDir}/dir";
|
2017-06-17 01:12:42 +02:00
|
|
|
$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() {
|
2017-06-17 01:12:42 +02:00
|
|
|
$this->execute(function () {
|
|
|
|
$fixtureDir = Fixture::path();
|
2015-08-08 16:09:07 +02:00
|
|
|
$path = "{$fixtureDir}/dir";
|
2017-06-17 01:12:42 +02:00
|
|
|
$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() {
|
2017-06-17 01:12:42 +02:00
|
|
|
$this->execute(function () {
|
|
|
|
$fixtureDir = Fixture::path();
|
2015-08-08 16:09:07 +02:00
|
|
|
$path = "{$fixtureDir}/small.txt";
|
2017-06-17 01:12:42 +02:00
|
|
|
$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() {
|
2017-06-17 01:12:42 +02:00
|
|
|
$this->execute(function () {
|
|
|
|
$fixtureDir = Fixture::path();
|
2015-08-08 16:09:07 +02:00
|
|
|
$path = "{$fixtureDir}/nonexistent";
|
2017-06-17 01:12:42 +02:00
|
|
|
$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() {
|
2017-06-17 01:12:42 +02:00
|
|
|
$this->execute(function () {
|
|
|
|
$fixtureDir = Fixture::path();
|
2015-08-08 16:09:07 +02:00
|
|
|
$path = "{$fixtureDir}/small.txt";
|
2017-06-17 01:12:42 +02:00
|
|
|
$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() {
|
2017-06-17 01:12:42 +02:00
|
|
|
$this->execute(function () {
|
|
|
|
$fixtureDir = Fixture::path();
|
2015-08-08 16:09:07 +02:00
|
|
|
$path = "{$fixtureDir}/dir";
|
2017-06-17 01:12:42 +02:00
|
|
|
$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() {
|
2017-06-17 01:12:42 +02:00
|
|
|
$this->execute(function () {
|
|
|
|
$fixtureDir = Fixture::path();
|
2015-08-08 16:09:07 +02:00
|
|
|
$path = "{$fixtureDir}/nonexistent";
|
2017-06-17 01:12:42 +02:00
|
|
|
$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() {
|
2017-06-17 01:12:42 +02:00
|
|
|
$this->execute(function () {
|
|
|
|
$fixtureDir = Fixture::path();
|
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
|
|
|
|
2017-06-17 01:12:42 +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() {
|
2017-06-17 01:12:42 +02:00
|
|
|
$this->execute(function () {
|
|
|
|
$fixtureDir = Fixture::path();
|
2015-07-19 16:38:40 +02:00
|
|
|
$toUnlink = "{$fixtureDir}/unlink";
|
2017-06-17 01:12:42 +02:00
|
|
|
yield File\put($toUnlink, "unlink me");
|
|
|
|
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() {
|
2017-06-17 01:12:42 +02:00
|
|
|
$this->execute(function () {
|
|
|
|
$fixtureDir = Fixture::path();
|
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
|
|
|
|
2017-06-17 01:12:42 +02:00
|
|
|
yield File\mkdir($dir);
|
|
|
|
$stat = yield File\stat($dir);
|
2017-06-17 15:36:51 +02:00
|
|
|
$this->assertSame(0644, $stat["mode"] & 0777);
|
2017-06-17 01:12:42 +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";
|
|
|
|
|
2017-06-17 01:12:42 +02:00
|
|
|
yield File\mkdir($dir, 0764, true); // the umask is 022 by default
|
|
|
|
$stat = yield File\stat($dir);
|
2017-06-17 15:36:51 +02:00
|
|
|
$this->assertSame(0764, $stat["mode"] & 0777);
|
2015-08-08 17:46:34 +02:00
|
|
|
});
|
2015-08-08 16:09:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testMtime() {
|
2017-06-17 01:12:42 +02:00
|
|
|
$this->execute(function () {
|
|
|
|
$fixtureDir = Fixture::path();
|
2015-08-08 16:09:07 +02:00
|
|
|
$path = "{$fixtureDir}/small.txt";
|
2017-06-17 01:12:42 +02:00
|
|
|
$stat = (yield File\stat($path));
|
2015-08-08 16:09:07 +02:00
|
|
|
$statMtime = $stat["mtime"];
|
2017-06-17 01:12:42 +02:00
|
|
|
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() {
|
2017-06-17 01:12:42 +02:00
|
|
|
$this->execute(function () {
|
|
|
|
$fixtureDir = Fixture::path();
|
2015-08-08 16:09:07 +02:00
|
|
|
$path = "{$fixtureDir}/nonexistent";
|
2017-06-17 01:12:42 +02:00
|
|
|
yield File\mtime($path);
|
2015-08-08 17:46:34 +02:00
|
|
|
});
|
2015-08-08 16:09:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAtime() {
|
2017-06-17 01:12:42 +02:00
|
|
|
$this->execute(function () {
|
|
|
|
$fixtureDir = Fixture::path();
|
2015-08-08 16:09:07 +02:00
|
|
|
$path = "{$fixtureDir}/small.txt";
|
2017-06-17 01:12:42 +02:00
|
|
|
$stat = (yield File\stat($path));
|
2015-08-08 16:09:07 +02:00
|
|
|
$statAtime = $stat["atime"];
|
2017-06-17 01:12:42 +02:00
|
|
|
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() {
|
2017-06-17 01:12:42 +02:00
|
|
|
$this->execute(function () {
|
|
|
|
$fixtureDir = Fixture::path();
|
2015-08-08 16:09:07 +02:00
|
|
|
$path = "{$fixtureDir}/nonexistent";
|
2017-06-17 01:12:42 +02:00
|
|
|
yield File\atime($path);
|
2015-08-08 17:46:34 +02:00
|
|
|
});
|
2015-08-08 16:09:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testCtime() {
|
2017-06-17 01:12:42 +02:00
|
|
|
$this->execute(function () {
|
|
|
|
$fixtureDir = Fixture::path();
|
2015-08-08 16:09:07 +02:00
|
|
|
$path = "{$fixtureDir}/small.txt";
|
2017-06-17 01:12:42 +02:00
|
|
|
$stat = (yield File\stat($path));
|
2015-08-08 16:09:07 +02:00
|
|
|
$statCtime = $stat["ctime"];
|
2017-06-17 01:12:42 +02:00
|
|
|
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() {
|
2017-06-17 01:12:42 +02:00
|
|
|
$this->execute(function () {
|
|
|
|
$fixtureDir = Fixture::path();
|
2015-08-08 16:09:07 +02:00
|
|
|
$path = "{$fixtureDir}/nonexistent";
|
2017-06-17 01:12:42 +02:00
|
|
|
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() {
|
2017-06-17 01:12:42 +02:00
|
|
|
$this->execute(function () {
|
|
|
|
$fixtureDir = Fixture::path();
|
2015-07-18 20:53:46 +02:00
|
|
|
|
2015-07-19 16:38:40 +02:00
|
|
|
$touch = "{$fixtureDir}/touch";
|
2017-06-17 01:12:42 +02:00
|
|
|
yield File\put($touch, "touch me");
|
2015-07-18 20:53:46 +02:00
|
|
|
|
2017-06-17 01:12:42 +02:00
|
|
|
$oldStat = (yield File\stat($touch));
|
2015-07-18 20:53:46 +02:00
|
|
|
sleep(1);
|
2017-06-17 01:12:42 +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
|
|
|
}
|