2016-12-30 03:59:59 +01:00
|
|
|
<?php
|
2015-08-13 01:02:41 +02:00
|
|
|
|
|
|
|
namespace Amp\File\Test;
|
|
|
|
|
2017-06-17 01:12:42 +02:00
|
|
|
use Amp\File;
|
2017-05-18 05:58:39 +02:00
|
|
|
use Amp\PHPUnit\TestCase;
|
2015-08-13 01:02:41 +02:00
|
|
|
|
2017-05-18 05:58:39 +02:00
|
|
|
abstract class HandleTest extends TestCase {
|
2017-06-17 01:12:42 +02:00
|
|
|
protected function setUp() {
|
2015-08-13 01:02:41 +02:00
|
|
|
Fixture::init();
|
2017-06-17 01:12:42 +02:00
|
|
|
File\StatCache::clear();
|
2015-08-13 01:02:41 +02:00
|
|
|
}
|
|
|
|
|
2017-06-17 01:12:42 +02:00
|
|
|
protected function tearDown() {
|
2015-08-13 01:02:41 +02:00
|
|
|
Fixture::clear();
|
|
|
|
}
|
|
|
|
|
2017-06-17 01:12:42 +02:00
|
|
|
abstract protected function execute(callable $cb);
|
2016-07-21 01:33:03 +02:00
|
|
|
|
2015-08-13 01:02:41 +02:00
|
|
|
public function testWrite() {
|
2017-06-17 01:12:42 +02:00
|
|
|
$this->execute(function () {
|
2015-08-13 01:02:41 +02:00
|
|
|
$path = Fixture::path() . "/write";
|
2017-06-17 01:12:42 +02:00
|
|
|
$handle = yield File\open($path, "c+");
|
2015-08-13 01:02:41 +02:00
|
|
|
$this->assertSame(0, $handle->tell());
|
|
|
|
|
|
|
|
yield $handle->write("foo");
|
|
|
|
yield $handle->write("bar");
|
|
|
|
$handle->seek(0);
|
|
|
|
$contents = (yield $handle->read(8192));
|
|
|
|
$this->assertSame(6, $handle->tell());
|
|
|
|
$this->assertTrue($handle->eof());
|
|
|
|
$this->assertSame("foobar", $contents);
|
|
|
|
|
|
|
|
yield $handle->close();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testReadingToEof() {
|
2017-06-17 01:12:42 +02:00
|
|
|
$this->execute(function () {
|
|
|
|
$handle = yield File\open(__FILE__, "r");
|
2015-08-13 01:02:41 +02:00
|
|
|
$contents = "";
|
|
|
|
$position = 0;
|
|
|
|
|
2017-06-17 01:12:42 +02:00
|
|
|
$stat = yield File\stat(__FILE__);
|
2016-08-24 07:01:41 +02:00
|
|
|
$chunkSize = (int) \floor(($stat["size"] / 5));
|
2015-08-13 01:02:41 +02:00
|
|
|
|
|
|
|
while (!$handle->eof()) {
|
2017-06-17 01:12:42 +02:00
|
|
|
$chunk = yield $handle->read($chunkSize);
|
2015-08-13 01:02:41 +02:00
|
|
|
$contents .= $chunk;
|
|
|
|
$position += \strlen($chunk);
|
|
|
|
$this->assertSame($position, $handle->tell());
|
|
|
|
}
|
|
|
|
|
2017-06-17 01:12:42 +02:00
|
|
|
$this->assertSame((yield File\get(__FILE__)), $contents);
|
2017-05-18 17:31:20 +02:00
|
|
|
|
|
|
|
yield $handle->close();
|
2015-08-13 01:02:41 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testQueuedReads() {
|
2017-06-17 01:12:42 +02:00
|
|
|
$this->execute(function () {
|
|
|
|
$handle = yield File\open(__FILE__, "r");
|
2015-08-13 01:02:41 +02:00
|
|
|
|
|
|
|
$contents = "";
|
|
|
|
$read1 = $handle->read(10);
|
|
|
|
$handle->seek(10);
|
|
|
|
$read2 = $handle->read(10);
|
|
|
|
|
|
|
|
$contents .= (yield $read1);
|
|
|
|
$contents .= (yield $read2);
|
|
|
|
|
2017-06-17 01:12:42 +02:00
|
|
|
$expected = \substr((yield File\get(__FILE__)), 0, 20);
|
2015-08-13 01:02:41 +02:00
|
|
|
$this->assertSame($expected, $contents);
|
2017-05-18 17:31:20 +02:00
|
|
|
|
|
|
|
yield $handle->close();
|
2015-08-13 01:02:41 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testReadingFromOffset() {
|
2017-06-17 01:12:42 +02:00
|
|
|
$this->execute(function () {
|
|
|
|
$handle = yield File\open(__FILE__, "r");
|
2015-08-13 01:02:41 +02:00
|
|
|
$this->assertSame(0, $handle->tell());
|
2016-08-30 21:05:14 +02:00
|
|
|
yield $handle->seek(10);
|
2015-08-13 01:02:41 +02:00
|
|
|
$this->assertSame(10, $handle->tell());
|
|
|
|
$chunk = (yield $handle->read(90));
|
|
|
|
$this->assertSame(100, $handle->tell());
|
2017-06-17 01:12:42 +02:00
|
|
|
$expected = \substr((yield File\get(__FILE__)), 10, 90);
|
2015-08-13 01:02:41 +02:00
|
|
|
$this->assertSame($expected, $chunk);
|
2017-05-18 17:31:20 +02:00
|
|
|
|
|
|
|
yield $handle->close();
|
2015-08-13 01:02:41 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-30 21:05:14 +02:00
|
|
|
* @expectedException \Error
|
2015-08-13 01:02:41 +02:00
|
|
|
*/
|
|
|
|
public function testSeekThrowsOnInvalidWhence() {
|
2017-06-17 01:12:42 +02:00
|
|
|
$this->execute(function () {
|
2017-05-18 17:31:20 +02:00
|
|
|
try {
|
2017-06-17 01:12:42 +02:00
|
|
|
$handle = yield File\open(__FILE__, "r");
|
2017-05-18 17:31:20 +02:00
|
|
|
yield $handle->seek(0, 99999);
|
|
|
|
} finally {
|
|
|
|
yield $handle->close();
|
|
|
|
}
|
2015-08-13 01:02:41 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSeekSetCur() {
|
2017-06-17 01:12:42 +02:00
|
|
|
$this->execute(function () {
|
|
|
|
$handle = yield File\open(__FILE__, "r");
|
2015-08-13 01:02:41 +02:00
|
|
|
$this->assertSame(0, $handle->tell());
|
2016-08-30 21:05:14 +02:00
|
|
|
yield $handle->seek(10);
|
2015-08-13 01:02:41 +02:00
|
|
|
$this->assertSame(10, $handle->tell());
|
2016-08-30 21:05:14 +02:00
|
|
|
yield $handle->seek(-10, \SEEK_CUR);
|
2015-08-13 01:02:41 +02:00
|
|
|
$this->assertSame(0, $handle->tell());
|
2017-05-18 17:31:20 +02:00
|
|
|
yield $handle->close();
|
2015-08-13 01:02:41 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSeekSetEnd() {
|
2017-06-17 01:12:42 +02:00
|
|
|
$this->execute(function () {
|
|
|
|
$size = yield File\size(__FILE__);
|
|
|
|
$handle = yield File\open(__FILE__, "r");
|
2015-08-13 01:02:41 +02:00
|
|
|
$this->assertSame(0, $handle->tell());
|
2016-08-30 21:05:14 +02:00
|
|
|
yield $handle->seek(-10, \SEEK_END);
|
2015-08-13 01:02:41 +02:00
|
|
|
$this->assertSame($size - 10, $handle->tell());
|
2017-05-18 17:31:20 +02:00
|
|
|
yield $handle->close();
|
2015-08-13 01:02:41 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testPath() {
|
2017-06-17 01:12:42 +02:00
|
|
|
$this->execute(function () {
|
|
|
|
$handle = yield File\open(__FILE__, "r");
|
2015-08-13 01:02:41 +02:00
|
|
|
$this->assertSame(__FILE__, $handle->path());
|
2017-05-18 17:31:20 +02:00
|
|
|
yield $handle->close();
|
2015-08-13 01:02:41 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMode() {
|
2017-06-17 01:12:42 +02:00
|
|
|
$this->execute(function () {
|
|
|
|
$handle = yield File\open(__FILE__, "r");
|
2015-08-13 01:02:41 +02:00
|
|
|
$this->assertSame("r", $handle->mode());
|
2017-05-18 17:31:20 +02:00
|
|
|
yield $handle->close();
|
2015-08-13 01:02:41 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-17 01:21:56 +02:00
|
|
|
/**
|
|
|
|
* @expectedException \Amp\File\FilesystemException
|
|
|
|
*/
|
2015-08-13 01:02:41 +02:00
|
|
|
public function testClose() {
|
2017-06-17 01:12:42 +02:00
|
|
|
$this->execute(function () {
|
|
|
|
$handle = yield File\open(__FILE__, "r");
|
2015-08-13 01:02:41 +02:00
|
|
|
yield $handle->close();
|
2017-06-17 01:21:56 +02:00
|
|
|
yield $handle->read();
|
2015-08-13 01:02:41 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|