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

369 lines
11 KiB
PHP
Raw Normal View History

<?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 {
2015-07-19 16:38:40 +02:00
private static $fixtureId;
2016-10-04 14:09:19 +02:00
private static $umask;
2015-07-19 16:38:40 +02:00
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();
2016-10-04 14:09:19 +02:00
self::$umask = umask(0022);
2015-07-19 16:38:40 +02:00
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();
2016-10-04 14:09:19 +02:00
umask(self::$umask);
2015-07-19 16:38:40 +02:00
}
2015-08-08 16:09:07 +02:00
protected function setUp() {
file\StatCache::clear();
}
2017-01-11 14:22:06 +01:00
2016-07-21 01:33:03 +02:00
abstract protected function lRun(callable $cb);
2015-08-08 16:09:07 +02:00
2015-07-11 03:59:39 +02:00
public function testScandir() {
2016-07-21 01:33:03 +02:00
$this->lRun(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() {
2016-07-21 01:33:03 +02:00
$this->lRun(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() {
2016-07-21 01:33:03 +02:00
$this->lRun(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() {
2016-07-21 01:33:03 +02:00
$this->lRun(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));
$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() {
2016-07-21 01:33:03 +02:00
$this->lRun(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() {
2016-07-21 01:33:03 +02:00
$this->lRun(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() {
2016-07-21 01:33:03 +02:00
$this->lRun(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() {
2016-07-21 01:33:03 +02:00
$this->lRun(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() {
2016-07-21 01:33:03 +02:00
$this->lRun(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() {
2016-07-21 01:33:03 +02:00
$this->lRun(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() {
2016-07-21 01:33:03 +02:00
$this->lRun(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() {
2016-07-21 01:33:03 +02:00
$this->lRun(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() {
2016-07-21 01:33:03 +02:00
$this->lRun(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() {
2016-07-21 01:33:03 +02:00
$this->lRun(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() {
2016-07-21 01:33:03 +02:00
$this->lRun(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() {
2016-07-21 01:33:03 +02:00
$this->lRun(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() {
2016-07-21 01:33:03 +02:00
$this->lRun(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() {
2016-07-21 01:33:03 +02:00
$this->lRun(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() {
2016-07-21 01:33:03 +02:00
$this->lRun(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() {
2016-07-21 01:33:03 +02:00
$this->lRun(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");
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() {
2016-07-21 01:33:03 +02:00
$this->lRun(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);
2017-01-11 07:05:18 +01:00
$stat = yield file\stat($dir);
$this->assertTrue(($stat["mode"] & 0777) === 0644);
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, 0764, true); // the umask is 022 by default
2017-01-11 07:05:18 +01:00
$stat = yield file\stat($dir);
2016-10-04 14:09:19 +02:00
$this->assertTrue(($stat["mode"] & 0777) == 0744);
2015-08-08 17:46:34 +02:00
});
2015-08-08 16:09:07 +02:00
}
public function testMtime() {
2016-07-21 01:33:03 +02:00
$this->lRun(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() {
2016-07-21 01:33:03 +02:00
$this->lRun(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() {
2016-07-21 01:33:03 +02:00
$this->lRun(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() {
2016-07-21 01:33:03 +02:00
$this->lRun(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() {
2016-07-21 01:33:03 +02:00
$this->lRun(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() {
2016-07-21 01:33:03 +02:00
$this->lRun(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() {
2016-07-21 01:33:03 +02:00
$this->lRun(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
}