1
0
mirror of https://github.com/danog/file.git synced 2024-12-02 09:17:57 +01:00
file/test/DriverTest.php

360 lines
10 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
2018-07-27 21:00:26 +02:00
use Amp\File;
2017-05-18 05:58:39 +02:00
use Amp\PHPUnit\TestCase;
2015-07-11 03:59:39 +02:00
2018-07-27 21:00:26 +02:00
abstract class DriverTest extends TestCase
{
protected function setUp()
{
Fixture::init();
File\StatCache::clear();
2015-07-19 16:38:40 +02:00
}
2018-07-27 21:00:26 +02:00
protected function tearDown()
{
Fixture::clear();
2015-08-08 16:09:07 +02:00
}
2017-01-11 14:22:06 +01:00
2018-07-27 21:00:26 +02:00
abstract protected function execute(callable $callback);
2015-08-08 16:09:07 +02:00
2018-07-27 21:00:26 +02:00
public function testScandir(): void
{
$this->execute(function () {
$fixtureDir = Fixture::path();
2018-07-27 21:00:26 +02:00
$actual = 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
}
2018-07-27 21:00:26 +02:00
public function testScandirThrowsIfPathNotADirectory(): void
{
$this->execute(function () {
2018-07-27 21:00:26 +02:00
$this->expectException(File\FilesystemException::class);
File\scandir(__FILE__);
2015-08-08 17:46:34 +02:00
});
2015-08-08 16:09:07 +02:00
}
2018-07-27 21:00:26 +02:00
public function testScandirThrowsIfPathDoesntExist(): void
{
$this->execute(function () {
$path = Fixture::path() . "/nonexistent";
2018-07-27 21:00:26 +02:00
$this->expectException(File\FilesystemException::class);
File\scandir($path);
2015-08-08 17:46:34 +02:00
});
2015-08-08 16:09:07 +02:00
}
2018-07-27 21:00:26 +02:00
public function testSymlink(): void
{
$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";
2018-07-27 21:00:26 +02:00
File\symlink($original, $link);
$this->assertTrue(\is_link($link));
2018-07-27 21:00:26 +02:00
File\unlink($link);
2015-08-08 17:46:34 +02:00
});
2015-07-11 03:59:39 +02:00
}
2018-07-27 21:00:26 +02:00
public function testLstat(): void
{
$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";
2018-07-27 21:00:26 +02:00
File\symlink($target, $link);
$this->assertInternalType('array', File\lstat($link));
File\unlink($link);
2015-08-08 17:46:34 +02:00
});
2015-07-11 03:59:39 +02:00
}
2018-07-27 21:00:26 +02:00
public function testFileStat(): void
{
$this->execute(function () {
$fixtureDir = Fixture::path();
2018-07-27 21:00:26 +02:00
$stat = 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
2018-07-27 21:00:26 +02:00
public function testDirStat(): void
{
$this->execute(function () {
$fixtureDir = Fixture::path();
2018-07-27 21:00:26 +02:00
$stat = 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
}
2018-07-27 21:00:26 +02:00
public function testNonexistentPathStatResolvesToNull(): void
{
$this->execute(function () {
$fixtureDir = Fixture::path();
2018-07-27 21:00:26 +02:00
$stat = 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
2018-07-27 21:00:26 +02:00
public function testExists(): void
{
$this->execute(function () {
$fixtureDir = Fixture::path();
2018-07-27 21:00:26 +02:00
$this->assertFalse(File\exists("{$fixtureDir}/nonexistent"));
$this->assertTrue(File\exists("{$fixtureDir}/small.txt"));
2015-08-08 17:46:34 +02:00
});
2015-07-19 15:24:52 +02:00
}
2018-07-27 21:00:26 +02:00
public function testGet(): void
{
2017-06-17 15:36:51 +02:00
$this->execute(function () {
$fixtureDir = Fixture::path();
2018-07-27 21:00:26 +02:00
$this->assertSame("small", File\get("{$fixtureDir}/small.txt"));
2017-06-17 15:36:51 +02:00
});
}
2018-07-27 21:00:26 +02:00
public function testSize(): void
{
$this->execute(function () {
$fixtureDir = Fixture::path();
2015-08-08 16:09:07 +02:00
$path = "{$fixtureDir}/small.txt";
2018-07-27 21:00:26 +02:00
$stat = File\stat($path);
2015-08-08 16:09:07 +02:00
$size = $stat["size"];
File\StatCache::clear($path);
2018-07-27 21:00:26 +02:00
$this->assertSame($size, 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
2018-07-27 21:00:26 +02:00
public function testSizeFailsOnNonexistentPath(): void
{
$this->execute(function () {
$fixtureDir = Fixture::path();
2015-08-08 16:09:07 +02:00
$path = "{$fixtureDir}/nonexistent";
2018-07-27 21:00:26 +02:00
$this->expectException(File\FilesystemException::class);
File\size($path);
2015-08-08 17:46:34 +02:00
});
2015-08-08 16:09:07 +02:00
}
2018-07-27 21:00:26 +02:00
public function testSizeFailsOnDirectoryPath(): void
{
$this->execute(function () {
$fixtureDir = Fixture::path();
2015-08-08 16:09:07 +02:00
$path = "{$fixtureDir}/dir";
2018-07-27 21:00:26 +02:00
$this->assertTrue(File\isDir($path));
File\StatCache::clear($path);
2018-07-27 21:00:26 +02:00
$this->expectException(File\FilesystemException::class);
File\size($path);
2015-08-08 17:46:34 +02:00
});
2015-08-08 16:09:07 +02:00
}
2018-07-27 21:00:26 +02:00
public function testIsDirResolvesTrueOnDirectoryPath(): void
{
$this->execute(function () {
$fixtureDir = Fixture::path();
2015-08-08 16:09:07 +02:00
$path = "{$fixtureDir}/dir";
2018-07-27 21:00:26 +02:00
$this->assertTrue(File\isDir($path));
2015-08-08 17:46:34 +02:00
});
2015-08-08 16:09:07 +02:00
}
2018-07-27 21:00:26 +02:00
public function testIsDirResolvesFalseOnFilePath(): void
{
$this->execute(function () {
$fixtureDir = Fixture::path();
2015-08-08 16:09:07 +02:00
$path = "{$fixtureDir}/small.txt";
2018-07-27 21:00:26 +02:00
$this->assertFalse(File\isDir($path));
2015-08-08 17:46:34 +02:00
});
2015-08-08 16:09:07 +02:00
}
2018-07-27 21:00:26 +02:00
public function testIsDirResolvesFalseOnNonexistentPath(): void
{
$this->execute(function () {
$fixtureDir = Fixture::path();
2015-08-08 16:09:07 +02:00
$path = "{$fixtureDir}/nonexistent";
2018-07-27 21:00:26 +02:00
$this->assertFalse(File\isDir($path));
2015-08-08 17:46:34 +02:00
});
2015-08-08 16:09:07 +02:00
}
2018-07-27 21:00:26 +02:00
public function testIsFileResolvesTrueOnFilePath(): void
{
$this->execute(function () {
$fixtureDir = Fixture::path();
2015-08-08 16:09:07 +02:00
$path = "{$fixtureDir}/small.txt";
2018-07-27 21:00:26 +02:00
$this->assertTrue(File\isFile($path));
2015-08-08 17:46:34 +02:00
});
2015-08-08 16:09:07 +02:00
}
2018-07-27 21:00:26 +02:00
public function testIsFileResolvesFalseOnDirectoryPath(): void
{
$this->execute(function () {
$fixtureDir = Fixture::path();
2015-08-08 16:09:07 +02:00
$path = "{$fixtureDir}/dir";
2018-07-27 21:00:26 +02:00
$this->assertFalse(File\isFile($path));
2015-08-08 17:46:34 +02:00
});
2015-08-08 16:09:07 +02:00
}
2018-07-27 21:00:26 +02:00
public function testIsFileResolvesFalseOnNonexistentPath(): void
{
$this->execute(function () {
$fixtureDir = Fixture::path();
2015-08-08 16:09:07 +02:00
$path = "{$fixtureDir}/nonexistent";
2018-07-27 21:00:26 +02:00
$this->assertFalse(File\isFile($path));
2015-08-08 17:46:34 +02:00
});
2015-07-11 03:59:39 +02:00
}
2018-07-27 21:00:26 +02:00
public function testRename(): void
{
$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
2018-07-27 21:00:26 +02:00
File\put($old, $contents1);
File\rename($old, $new);
$contents2 = File\get($new);
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
}
2018-07-27 21:00:26 +02:00
public function testUnlink(): void
{
$this->execute(function () {
$fixtureDir = Fixture::path();
2015-07-19 16:38:40 +02:00
$toUnlink = "{$fixtureDir}/unlink";
2018-07-27 21:00:26 +02:00
File\put($toUnlink, "unlink me");
File\unlink($toUnlink);
$this->assertNull(File\stat($toUnlink));
2015-08-08 17:46:34 +02:00
});
2015-07-11 03:59:39 +02:00
}
2018-07-27 21:00:26 +02:00
public function testMkdirRmdir(): void
{
$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
2018-03-15 18:35:10 +01:00
\umask(0022);
2018-07-27 21:00:26 +02:00
File\mkdir($dir);
$stat = File\stat($dir);
2018-03-15 18:35:10 +01:00
$this->assertSame('0755', $this->getPermissionsFromStat($stat));
2018-07-27 21:00:26 +02:00
File\rmdir($dir);
$this->assertNull(File\stat($dir));
2016-09-28 12:39:24 +02:00
// test for 0, because previous array_filter made that not work
$dir = "{$fixtureDir}/newdir/with/recursive/creation/0/1/2";
2016-09-28 12:39:24 +02:00
2018-07-27 21:00:26 +02:00
File\mkdir($dir, 0764, true);
$stat = File\stat($dir);
2018-03-15 18:35:10 +01:00
$this->assertSame('0744', $this->getPermissionsFromStat($stat));
2015-08-08 17:46:34 +02:00
});
2015-08-08 16:09:07 +02:00
}
2018-07-27 21:00:26 +02:00
public function testMtime(): void
{
$this->execute(function () {
$fixtureDir = Fixture::path();
2015-08-08 16:09:07 +02:00
$path = "{$fixtureDir}/small.txt";
2018-07-27 21:00:26 +02:00
$stat = File\stat($path);
2015-08-08 16:09:07 +02:00
$statMtime = $stat["mtime"];
File\StatCache::clear($path);
2018-07-27 21:00:26 +02:00
$this->assertSame($statMtime, File\mtime($path));
2015-08-08 17:46:34 +02:00
});
2015-08-08 16:09:07 +02:00
}
2018-07-27 21:00:26 +02:00
public function testMtimeFailsOnNonexistentPath(): void
{
$this->execute(function () {
$fixtureDir = Fixture::path();
2015-08-08 16:09:07 +02:00
$path = "{$fixtureDir}/nonexistent";
2018-07-27 21:00:26 +02:00
$this->expectException(File\FilesystemException::class);
File\mtime($path);
2015-08-08 17:46:34 +02:00
});
2015-08-08 16:09:07 +02:00
}
2018-07-27 21:00:26 +02:00
public function testAtime(): void
{
$this->execute(function () {
$fixtureDir = Fixture::path();
2015-08-08 16:09:07 +02:00
$path = "{$fixtureDir}/small.txt";
2018-07-27 21:00:26 +02:00
$stat = File\stat($path);
2015-08-08 16:09:07 +02:00
$statAtime = $stat["atime"];
File\StatCache::clear($path);
2018-07-27 21:00:26 +02:00
$this->assertSame($statAtime, File\atime($path));
2015-08-08 17:46:34 +02:00
});
2015-08-08 16:09:07 +02:00
}
2018-07-27 21:00:26 +02:00
public function testAtimeFailsOnNonexistentPath(): void
{
$this->execute(function () {
$fixtureDir = Fixture::path();
2015-08-08 16:09:07 +02:00
$path = "{$fixtureDir}/nonexistent";
2018-07-27 21:00:26 +02:00
$this->expectException(File\FilesystemException::class);
File\atime($path);
2015-08-08 17:46:34 +02:00
});
2015-08-08 16:09:07 +02:00
}
2018-07-27 21:00:26 +02:00
public function testCtime(): void
{
$this->execute(function () {
$fixtureDir = Fixture::path();
2015-08-08 16:09:07 +02:00
$path = "{$fixtureDir}/small.txt";
2018-07-27 21:00:26 +02:00
$stat = File\stat($path);
2015-08-08 16:09:07 +02:00
$statCtime = $stat["ctime"];
File\StatCache::clear($path);
2018-07-27 21:00:26 +02:00
$this->assertSame($statCtime, File\ctime($path));
2015-08-08 17:46:34 +02:00
});
2015-08-08 16:09:07 +02:00
}
2018-07-27 21:00:26 +02:00
public function testCtimeFailsOnNonexistentPath(): void
{
$this->execute(function () {
$fixtureDir = Fixture::path();
2015-08-08 16:09:07 +02:00
$path = "{$fixtureDir}/nonexistent";
2018-07-27 21:00:26 +02:00
$this->expectException(File\FilesystemException::class);
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
*/
2018-07-27 21:00:26 +02:00
public function testTouch(): void
{
$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";
2018-07-27 21:00:26 +02:00
File\put($touch, "touch me");
2015-07-18 20:53:46 +02:00
2018-07-27 21:00:26 +02:00
$oldStat = File\stat($touch);
/** @noinspection PotentialMalwareInspection */
File\touch($touch, \time() + 10, \time() + 20);
File\StatCache::clear($touch);
2018-07-27 21:00:26 +02:00
$newStat = File\stat($touch);
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
}
2018-03-15 18:35:10 +01:00
/**
* @param array $stat
2018-07-27 21:00:26 +02:00
*
2018-03-15 18:35:10 +01:00
* @return string
*/
2018-07-27 21:00:26 +02:00
private function getPermissionsFromStat(array $stat): string
{
2018-03-15 18:35:10 +01:00
return \substr(\decoct($stat["mode"]), 1);
}
2015-07-11 03:59:39 +02:00
}