1
0
mirror of https://github.com/danog/file.git synced 2024-11-30 04:19:39 +01:00
file/test/HandleTest.php

144 lines
4.2 KiB
PHP
Raw Normal View History

<?php
2015-08-13 01:02:41 +02:00
namespace Amp\File\Test;
use Amp\File as 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 {
2015-08-13 01:02:41 +02:00
public static function setUpBeforeClass() {
Fixture::init();
}
public static function tearDownAfterClass() {
Fixture::clear();
}
protected function setUp() {
file\StatCache::clear();
}
2016-07-21 01:33:03 +02:00
abstract protected function lRun(callable $cb);
2015-08-13 01:02:41 +02:00
public function testWrite() {
2016-07-21 01:33:03 +02:00
$this->lRun(function () {
2015-08-13 01:02:41 +02:00
$path = Fixture::path() . "/write";
$handle = (yield file\open($path, "c+"));
$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();
yield file\unlink($path);
});
}
public function testReadingToEof() {
2016-07-21 01:33:03 +02:00
$this->lRun(function () {
2015-08-13 01:02:41 +02:00
$handle = (yield file\open(__FILE__, "r"));
$contents = "";
$position = 0;
$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()) {
$chunk = (yield $handle->read($chunkSize));
$contents .= $chunk;
$position += \strlen($chunk);
$this->assertSame($position, $handle->tell());
}
$this->assertSame((yield file\get(__FILE__)), $contents);
});
}
public function testQueuedReads() {
2016-07-21 01:33:03 +02:00
$this->lRun(function () {
2015-08-13 01:02:41 +02:00
$handle = (yield file\open(__FILE__, "r"));
$contents = "";
$read1 = $handle->read(10);
$handle->seek(10);
$read2 = $handle->read(10);
$contents .= (yield $read1);
$contents .= (yield $read2);
$expected = \substr((yield file\get(__FILE__)), 0, 20);
$this->assertSame($expected, $contents);
});
}
public function testReadingFromOffset() {
2016-07-21 01:33:03 +02:00
$this->lRun(function () {
2015-08-13 01:02:41 +02:00
$handle = (yield file\open(__FILE__, "r"));
$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());
$expected = \substr((yield file\get(__FILE__)), 10, 90);
$this->assertSame($expected, $chunk);
});
}
/**
2016-08-30 21:05:14 +02:00
* @expectedException \Error
2015-08-13 01:02:41 +02:00
*/
public function testSeekThrowsOnInvalidWhence() {
2016-07-21 01:33:03 +02:00
$this->lRun(function () {
2015-08-13 01:02:41 +02:00
$handle = (yield file\open(__FILE__, "r"));
2016-08-30 21:05:14 +02:00
yield $handle->seek(0, 99999);
2015-08-13 01:02:41 +02:00
});
}
public function testSeekSetCur() {
2016-07-21 01:33:03 +02:00
$this->lRun(function () {
2015-08-13 01:02:41 +02:00
$handle = (yield file\open(__FILE__, "r"));
$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());
});
}
public function testSeekSetEnd() {
2016-07-21 01:33:03 +02:00
$this->lRun(function () {
2015-08-13 01:02:41 +02:00
$size = (yield file\size(__FILE__));
$handle = (yield file\open(__FILE__, "r"));
$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());
});
}
public function testPath() {
2016-07-21 01:33:03 +02:00
$this->lRun(function () {
2015-08-13 01:02:41 +02:00
$handle = (yield file\open(__FILE__, "r"));
$this->assertSame(__FILE__, $handle->path());
});
}
public function testMode() {
2016-07-21 01:33:03 +02:00
$this->lRun(function () {
2015-08-13 01:02:41 +02:00
$handle = (yield file\open(__FILE__, "r"));
$this->assertSame("r", $handle->mode());
});
}
public function testClose() {
2016-07-21 01:33:03 +02:00
$this->lRun(function () {
2015-08-13 01:02:41 +02:00
$handle = (yield file\open(__FILE__, "r"));
yield $handle->close();
});
}
}