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

358 lines
11 KiB
PHP
Raw Normal View History

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 16:09:07 +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 16:09:07 +02:00
/**
* @expectedException \Amp\File\FilesystemException
*/
public function testScandirThrowsIfPathNotADirectory() {
amp\run(function () {
(yield file\scandir(__FILE__));
});
}
/**
* @expectedException \Amp\File\FilesystemException
*/
public function testScandirThrowsIfPathDoesntExist() {
amp\run(function () {
$path = self::getFixturePath() . "/nonexistent";
(yield file\scandir($path));
});
}
2015-07-11 03:59:39 +02:00
public function testSymlink() {
2015-08-08 16:09:07 +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));
$this->assertTrue(\is_link($link));
2015-08-08 16:09:07 +02:00
yield file\unlink($link);
2015-07-11 03:59:39 +02:00
});
}
public function testLstat() {
2015-08-08 16:09:07 +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-07-11 03:59:39 +02:00
});
}
2015-08-08 16:09:07 +02:00
public function testFileStat() {
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-07-11 03:59:39 +02:00
2015-08-08 16:09:07 +02:00
public function testDirStat() {
amp\run(function () {
$fixtureDir = self::getFixturePath();
$stat = (yield file\stat("{$fixtureDir}/dir"));
2015-07-19 15:24:52 +02:00
$this->assertInternalType("array", $stat);
});
}
2015-08-08 16:09:07 +02:00
public function testNonexistentPathStatResolvesToNull() {
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-07-11 03:59:39 +02:00
2015-08-08 16:09:07 +02:00
public function testExists() {
amp\run(function () {
$fixtureDir = self::getFixturePath();
$this->assertFalse(yield file\exists("{$fixtureDir}/nonexistent"));
$this->assertTrue(yield file\exists("{$fixtureDir}/small.txt"));
2015-07-19 15:24:52 +02:00
});
}
2015-08-08 16:09:07 +02:00
public function testSize() {
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-07-11 03:59:39 +02:00
2015-08-08 16:09:07 +02:00
/**
* @expectedException \Amp\File\FilesystemException
*/
public function testSizeFailsOnNonexistentPath() {
amp\run(function () {
$fixtureDir = self::getFixturePath();
$path = "{$fixtureDir}/nonexistent";
yield file\size($path);
});
}
/**
* @expectedException \Amp\File\FilesystemException
*/
public function testSizeFailsOnDirectoryPath() {
amp\run(function () {
$fixtureDir = self::getFixturePath();
$path = "{$fixtureDir}/dir";
$this->assertTrue(yield file\isdir($path));
file\StatCache::clear($path);
yield file\size($path);
});
}
public function testIsdirResolvesTrueOnDirectoryPath() {
amp\run(function () {
$fixtureDir = self::getFixturePath();
$path = "{$fixtureDir}/dir";
$this->assertTrue(yield file\isdir($path));
});
}
public function testIsdirResolvesFalseOnFilePath() {
amp\run(function () {
$fixtureDir = self::getFixturePath();
$path = "{$fixtureDir}/small.txt";
$this->assertFalse(yield file\isdir($path));
});
}
public function testIsdirResolvesFalseOnNonexistentPath() {
amp\run(function () {
$fixtureDir = self::getFixturePath();
$path = "{$fixtureDir}/nonexistent";
$this->assertFalse(yield file\isdir($path));
});
}
public function testIsfileResolvesTrueOnFilePath() {
amp\run(function () {
$fixtureDir = self::getFixturePath();
$path = "{$fixtureDir}/small.txt";
$this->assertTrue(yield file\isfile($path));
});
}
public function testIsfileResolvesFalseOnDirectoryPath() {
amp\run(function () {
$fixtureDir = self::getFixturePath();
$path = "{$fixtureDir}/dir";
$this->assertFalse(yield file\isfile($path));
});
}
public function testIsfileResolvesFalseOnNonexistentPath() {
amp\run(function () {
$fixtureDir = self::getFixturePath();
$path = "{$fixtureDir}/nonexistent";
$this->assertFalse(yield file\isfile($path));
2015-07-11 03:59:39 +02:00
});
}
public function testRename() {
2015-08-08 16:09:07 +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);
});
}
public function testUnlink() {
2015-08-08 16:09:07 +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-07-11 03:59:39 +02:00
});
}
public function testMkdirRmdir() {
2015-08-08 16:09:07 +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);
$stat = (yield file\stat($dir));
yield file\rmdir($dir);
$this->assertNull(yield file\stat($dir));
});
}
public function testMtime() {
amp\run(function () {
$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)));
});
}
/**
* @expectedException \Amp\File\FilesystemException
*/
public function testMtimeFailsOnNonexistentPath() {
amp\run(function () {
$fixtureDir = self::getFixturePath();
$path = "{$fixtureDir}/nonexistent";
yield file\mtime($path);
});
}
public function testAtime() {
amp\run(function () {
$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)));
});
}
/**
* @expectedException \Amp\File\FilesystemException
*/
public function testAtimeFailsOnNonexistentPath() {
amp\run(function () {
$fixtureDir = self::getFixturePath();
$path = "{$fixtureDir}/nonexistent";
yield file\atime($path);
});
}
public function testCtime() {
amp\run(function () {
$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)));
});
}
/**
* @expectedException \Amp\File\FilesystemException
*/
public function testCtimeFailsOnNonexistentPath() {
amp\run(function () {
$fixtureDir = self::getFixturePath();
$path = "{$fixtureDir}/nonexistent";
yield file\ctime($path);
2015-07-11 03:59:39 +02:00
});
}
2015-07-18 20:53:46 +02:00
/**
* @group slow
*/
public function testTouch() {
2015-08-08 16:09:07 +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-07-11 03:59:39 +02:00
}