From 61d123bb458b66ad1799a33c12310334aec06ae5 Mon Sep 17 00:00:00 2001 From: Daniel Lowrey Date: Sun, 19 Jul 2015 10:38:40 -0400 Subject: [PATCH] Update tests --- test/DescriptorTest.php | 60 +++++++++++++++++++++++-- test/FilesystemTest.php | 97 ++++++++++++++++++++++++++++++++++------- test/fixture/small.txt | 1 - 3 files changed, 139 insertions(+), 19 deletions(-) delete mode 100644 test/fixture/small.txt diff --git a/test/DescriptorTest.php b/test/DescriptorTest.php index 72a09a4..0e15447 100644 --- a/test/DescriptorTest.php +++ b/test/DescriptorTest.php @@ -9,10 +9,61 @@ abstract class DescriptorTest extends \PHPUnit_Framework_TestCase { abstract protected function getReactor(); abstract protected function getFilesystem(Reactor $reactor); + 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; + } + + if (stripos(\PHP_OS, "win") === 0) { + system('rd /Q /S "' . $fixtureDir . '"'); + } else { + system('/bin/rm -rf ' . escapeshellarg($fixtureDir)); + } + } + + public static function setUpBeforeClass() { + $fixtureDir = self::getFixturePath(); + + self::clearFixtureDir(); + + if (!mkdir($fixtureDir, $mode = 0777, $recursive = true)) { + throw new \RuntimeException( + "Failed creating temporary test fixture directory: {$fixtureDir}" + ); + } + if (!mkdir($fixtureDir . "/dir", $mode = 0777, $recursive = true)) { + throw new \RuntimeException( + "Failed creating temporary test fixture directory" + ); + } + if (!file_put_contents($fixtureDir . "/small.txt", "small")) { + throw new \RuntimeException( + "Failed creating temporary test fixture file" + ); + } + } + + public static function tearDownAfterClass() { + self::clearFixtureDir(); + } + public function testReadWriteCreate() { $this->getReactor()->run(function($reactor) { - $path = __DIR__ . "/fixture/new.txt"; $fs = $this->getFilesystem($reactor); + $fixtureDir = $this->getFixturePath(); + + $path = "{$fixtureDir}/new.txt"; $flags = Filesystem::READ | Filesystem::WRITE | Filesystem::CREATE; $fh = (yield $fs->open($path, $flags)); yield $fh->write(0, "test"); @@ -25,8 +76,10 @@ abstract class DescriptorTest extends \PHPUnit_Framework_TestCase { public function testTruncate() { $this->getReactor()->run(function($reactor) { - $path = __DIR__ . "/fixture/truncate.txt"; $fs = $this->getFilesystem($reactor); + $fixtureDir = $this->getFixturePath(); + + $path = "{$fixtureDir}/truncate.txt"; $flags = Filesystem::READ | Filesystem::WRITE | Filesystem::CREATE; $fh = (yield $fs->open($path, $flags)); yield $fh->write(0, "test"); @@ -44,8 +97,9 @@ abstract class DescriptorTest extends \PHPUnit_Framework_TestCase { public function testStat() { $this->getReactor()->run(function($reactor) { $fs = $this->getFilesystem($reactor); + $fixtureDir = $this->getFixturePath(); - $fh = (yield $fs->open(__DIR__ . "/fixture/small.txt")); + $fh = (yield $fs->open("{$fixtureDir}/small.txt")); $stat = (yield $fh->stat()); $this->assertInternalType("array", $stat); }); diff --git a/test/FilesystemTest.php b/test/FilesystemTest.php index 339c06d..5dff928 100644 --- a/test/FilesystemTest.php +++ b/test/FilesystemTest.php @@ -9,10 +9,61 @@ abstract class FilesystemTest extends \PHPUnit_Framework_TestCase { abstract protected function getReactor(); abstract protected function getFilesystem(Reactor $reactor); + 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; + } + + if (stripos(\PHP_OS, "win") === 0) { + system('rd /Q /S "' . $fixtureDir . '"'); + } else { + system('/bin/rm -rf ' . escapeshellarg($fixtureDir)); + } + } + + public static function setUpBeforeClass() { + $fixtureDir = self::getFixturePath(); + + self::clearFixtureDir(); + + if (!mkdir($fixtureDir, $mode = 0777, $recursive = true)) { + throw new \RuntimeException( + "Failed creating temporary test fixture directory: {$fixtureDir}" + ); + } + if (!mkdir($fixtureDir . "/dir", $mode = 0777, $recursive = true)) { + throw new \RuntimeException( + "Failed creating temporary test fixture directory" + ); + } + if (!file_put_contents($fixtureDir . "/small.txt", "small")) { + throw new \RuntimeException( + "Failed creating temporary test fixture file" + ); + } + } + + public static function tearDownAfterClass() { + self::clearFixtureDir(); + } + public function testOpen() { $this->getReactor()->run(function($reactor) { $fs = $this->getFilesystem($reactor); - $descriptor = (yield $fs->open(__DIR__ . "/fixture/small.txt", Filesystem::READ)); + $fixtureDir = self::getFixturePath(); + + $descriptor = (yield $fs->open("{$fixtureDir}/small.txt", Filesystem::READ)); $this->assertInstanceOf("Amp\Fs\Descriptor", $descriptor); }); } @@ -20,7 +71,8 @@ abstract class FilesystemTest extends \PHPUnit_Framework_TestCase { public function testScandir() { $this->getReactor()->run(function($reactor) { $fs = $this->getFilesystem($reactor); - $actual = (yield $fs->scandir(__DIR__ . "/fixture")); + $fixtureDir = self::getFixturePath(); + $actual = (yield $fs->scandir($fixtureDir)); $expected = ["dir", "small.txt"]; $this->assertSame($expected, $actual); }); @@ -29,9 +81,10 @@ abstract class FilesystemTest extends \PHPUnit_Framework_TestCase { public function testSymlink() { $this->getReactor()->run(function($reactor) { $fs = $this->getFilesystem($reactor); + $fixtureDir = self::getFixturePath(); - $target = __DIR__ . "/fixture/small.txt"; - $link = __DIR__ . "/fixture/symlink.txt"; + $target = "{$fixtureDir}/small.txt"; + $link = "{$fixtureDir}/symlink.txt"; $this->assertTrue(yield $fs->symlink($target, $link)); $this->assertTrue(is_link($link)); yield $fs->unlink($link); @@ -41,9 +94,10 @@ abstract class FilesystemTest extends \PHPUnit_Framework_TestCase { public function testLstat() { $this->getReactor()->run(function($reactor) { $fs = $this->getFilesystem($reactor); + $fixtureDir = self::getFixturePath(); - $target = __DIR__ . "/fixture/small.txt"; - $link = __DIR__ . "/fixture/symlink.txt"; + $target = "{$fixtureDir}/small.txt"; + $link = "{$fixtureDir}/symlink.txt"; $this->assertTrue(yield $fs->symlink($target, $link)); $this->assertTrue(is_array(yield $fs->lstat($link))); yield $fs->unlink($link); @@ -56,7 +110,9 @@ abstract class FilesystemTest extends \PHPUnit_Framework_TestCase { public function testOpenFailsOnNonexistentFile() { $this->getReactor()->run(function($reactor) { $fs = $this->getFilesystem($reactor); - $descriptor = (yield $fs->open(__DIR__ . "/fixture/nonexistent", Filesystem::READ)); + $fixtureDir = self::getFixturePath(); + + $descriptor = (yield $fs->open("{$fixtureDir}/nonexistent", Filesystem::READ)); $this->assertInstanceOf("Amp\Fs\Descriptor", $descriptor); }); } @@ -64,26 +120,33 @@ abstract class FilesystemTest extends \PHPUnit_Framework_TestCase { public function testStatForFile() { $this->getReactor()->run(function($reactor) { $fs = $this->getFilesystem($reactor); + $fixtureDir = self::getFixturePath(); - $stat = (yield $fs->stat(__DIR__ . "/fixture/small.txt")); + $stat = (yield $fs->stat("{$fixtureDir}/small.txt")); $this->assertInternalType("array", $stat); + $this->assertTrue($stat["isfile"]); + $this->assertFalse($stat["isdir"]); }); } public function testStatForDirectory() { $this->getReactor()->run(function($reactor) { $fs = $this->getFilesystem($reactor); + $fixtureDir = self::getFixturePath(); - $stat = (yield $fs->stat(__DIR__ . "/fixture/dir")); + $stat = (yield $fs->stat("{$fixtureDir}/dir")); $this->assertInternalType("array", $stat); + $this->assertTrue($stat["isdir"]); + $this->assertFalse($stat["isfile"]); }); } public function testStatForNonexistentPath() { $this->getReactor()->run(function($reactor) { $fs = $this->getFilesystem($reactor); + $fixtureDir = self::getFixturePath(); - $stat = (yield $fs->stat(__DIR__ . "/fixture/nonexistent")); + $stat = (yield $fs->stat("{$fixtureDir}/nonexistent")); $this->assertNull($stat); }); } @@ -91,10 +154,11 @@ abstract class FilesystemTest extends \PHPUnit_Framework_TestCase { public function testRename() { $this->getReactor()->run(function($reactor) { $fs = $this->getFilesystem($reactor); + $fixtureDir = self::getFixturePath(); $contents1 = "rename test"; - $old = __DIR__ . "/fixture/rename1.txt"; - $new = __DIR__ . "/fixture/rename2.txt"; + $old = "{$fixtureDir}/rename1.txt"; + $new = "{$fixtureDir}/rename2.txt"; yield $fs->put($old, $contents1); yield $fs->rename($old, $new); @@ -108,8 +172,9 @@ abstract class FilesystemTest extends \PHPUnit_Framework_TestCase { public function testUnlink() { $this->getReactor()->run(function($reactor) { $fs = $this->getFilesystem($reactor); + $fixtureDir = self::getFixturePath(); - $toUnlink = __DIR__ . "/fixture/unlink"; + $toUnlink = "{$fixtureDir}/unlink"; yield $fs->put($toUnlink, "unlink me"); $this->assertTrue((bool) (yield $fs->stat($toUnlink))); @@ -121,8 +186,9 @@ abstract class FilesystemTest extends \PHPUnit_Framework_TestCase { public function testMkdirRmdir() { $this->getReactor()->run(function($reactor) { $fs = $this->getFilesystem($reactor); + $fixtureDir = self::getFixturePath(); - $dir = __DIR__ . "/fixture/newdir"; + $dir = "{$fixtureDir}/newdir"; yield $fs->mkdir($dir); $stat = (yield $fs->stat($dir)); @@ -139,8 +205,9 @@ abstract class FilesystemTest extends \PHPUnit_Framework_TestCase { public function testTouch() { $this->getReactor()->run(function($reactor) { $fs = $this->getFilesystem($reactor); + $fixtureDir = self::getFixturePath(); - $touch = __DIR__ . "/fixture/touch"; + $touch = "{$fixtureDir}/touch"; yield $fs->put($touch, "touch me"); $oldStat = (yield $fs->stat($touch)); diff --git a/test/fixture/small.txt b/test/fixture/small.txt deleted file mode 100644 index 64c3ecd..0000000 --- a/test/fixture/small.txt +++ /dev/null @@ -1 +0,0 @@ -small \ No newline at end of file