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

88 lines
3.0 KiB
PHP
Raw Normal View History

2015-07-11 03:59:39 +02:00
<?php
namespace Amp\Fs\Test;
use Amp\Reactor;
use Amp\Fs\Filesystem;
abstract class DescriptorTest extends \PHPUnit_Framework_TestCase {
2015-07-17 16:27:38 +02:00
abstract protected function getReactor();
abstract protected function getFilesystem(Reactor $reactor);
2015-07-11 03:59:39 +02:00
public function testReadWriteCreate() {
2015-07-17 16:27:38 +02:00
$this->getReactor()->run(function($reactor) {
2015-07-11 03:59:39 +02:00
$path = __DIR__ . "/fixture/new.txt";
$fs = $this->getFilesystem($reactor);
$flags = Filesystem::READ | Filesystem::WRITE | Filesystem::CREATE;
2015-07-17 16:27:38 +02:00
$fh = (yield $fs->open($path, $flags));
2015-07-11 03:59:39 +02:00
yield $fh->write(0, "test");
2015-07-17 16:27:38 +02:00
$data = (yield $fh->read(0, 8192));
2015-07-11 03:59:39 +02:00
$this->assertSame("test", $data);
yield $fh->close();
yield $fs->unlink($path);
});
}
/**
* @expectedException RuntimeException
*/
public function testWriteFailsOnDirectory() {
2015-07-17 16:27:38 +02:00
$this->getReactor()->run(function($reactor) {
2015-07-11 03:59:39 +02:00
$path = __DIR__ . "/fixture/dir";
$fs = $this->getFilesystem($reactor);
$flags = Filesystem::READ | Filesystem::WRITE | Filesystem::CREATE;
2015-07-17 16:27:38 +02:00
$fh = (yield $fs->open($path, $flags));
2015-07-11 03:59:39 +02:00
yield $fh->write(0, "should fail because this is a directory");
});
}
/**
* @expectedException RuntimeException
*/
public function testReadFailsOnDirectory() {
2015-07-17 16:27:38 +02:00
$this->getReactor()->run(function($reactor) {
2015-07-11 03:59:39 +02:00
$path = __DIR__ . "/fixture/dir";
$fs = $this->getFilesystem($reactor);
$flags = Filesystem::READ | Filesystem::WRITE | Filesystem::CREATE;
2015-07-17 16:27:38 +02:00
$fh = (yield $fs->open($path, $flags));
2015-07-11 03:59:39 +02:00
yield $fh->read(0, 8192);
});
}
public function testTruncate() {
2015-07-17 16:27:38 +02:00
$this->getReactor()->run(function($reactor) {
2015-07-11 03:59:39 +02:00
$path = __DIR__ . "/fixture/truncate.txt";
$fs = $this->getFilesystem($reactor);
$flags = Filesystem::READ | Filesystem::WRITE | Filesystem::CREATE;
2015-07-17 16:27:38 +02:00
$fh = (yield $fs->open($path, $flags));
2015-07-11 03:59:39 +02:00
yield $fh->write(0, "test");
2015-07-17 16:27:38 +02:00
$data = (yield $fh->read(0, 8192));
2015-07-11 03:59:39 +02:00
$this->assertSame("test", $data);
yield $fh->truncate();
yield $fh->close();
2015-07-17 16:27:38 +02:00
$stat = (yield $fs->stat($path));
$this->assertEquals(0, $stat["size"]);
2015-07-11 03:59:39 +02:00
yield $fs->unlink($path);
});
}
2015-07-17 16:27:38 +02:00
2015-07-11 03:59:39 +02:00
public function testStat() {
2015-07-17 16:27:38 +02:00
$this->getReactor()->run(function($reactor) {
2015-07-11 03:59:39 +02:00
$fs = $this->getFilesystem($reactor);
// file
2015-07-17 16:27:38 +02:00
$fh = (yield $fs->open(__DIR__ . "/fixture/small.txt"));
$stat = (yield $fh->stat());
2015-07-11 03:59:39 +02:00
$this->assertTrue($stat["isfile"]);
$this->assertFalse($stat["isdir"]);
// directory
2015-07-17 16:27:38 +02:00
$fh = (yield $fs->open(__DIR__ . "/fixture/dir"));
$stat = (yield $fh->stat());
2015-07-11 03:59:39 +02:00
$this->assertFalse($stat["isfile"]);
$this->assertTrue($stat["isdir"]);
});
}
}