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

187 lines
5.6 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-05 16:55:56 +02:00
use Amp\File\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-08-05 16:55:56 +02:00
$actual = (yield \Amp\File\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-08-05 16:55:56 +02:00
$this->assertTrue(yield \Amp\File\symlink($target, $link));
$this->assertTrue(\is_link($link));
2015-08-05 16:55:56 +02:00
yield \Amp\File\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-08-05 16:55:56 +02:00
$this->assertTrue(yield \Amp\File\symlink($target, $link));
$this->assertTrue(is_array(yield \Amp\File\lstat($link)));
yield \Amp\File\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-08-05 16:55:56 +02:00
$stat = (yield \Amp\File\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-08-05 16:55:56 +02:00
$stat = (yield \Amp\File\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-08-05 16:55:56 +02:00
$stat = (yield \Amp\File\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-08-05 16:55:56 +02:00
yield \Amp\File\put($old, $contents1);
yield \Amp\File\rename($old, $new);
$contents2 = (yield \Amp\File\get($new));
yield \Amp\File\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-08-05 16:55:56 +02:00
yield \Amp\File\put($toUnlink, "unlink me");
$this->assertTrue((bool) (yield \Amp\File\stat($toUnlink)));
yield \Amp\File\unlink($toUnlink);
$this->assertNull(yield \Amp\File\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-08-05 16:55:56 +02:00
yield \Amp\File\mkdir($dir);
$stat = (yield \Amp\File\stat($dir));
2015-07-11 03:59:39 +02:00
$this->assertTrue($stat["isdir"]);
$this->assertFalse($stat["isfile"]);
2015-08-05 16:55:56 +02:00
yield \Amp\File\rmdir($dir);
$this->assertNull(yield \Amp\File\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-08-05 16:55:56 +02:00
yield \Amp\File\put($touch, "touch me");
2015-07-18 20:53:46 +02:00
2015-08-05 16:55:56 +02:00
$oldStat = (yield \Amp\File\stat($touch));
2015-07-18 20:53:46 +02:00
sleep(1);
2015-08-05 16:55:56 +02:00
yield \Amp\File\touch($touch);
$newStat = (yield \Amp\File\stat($touch));
yield \Amp\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
}