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

158 lines
4.5 KiB
PHP
Raw Normal View History

<?php
2015-08-13 01:02:41 +02:00
namespace Amp\File\Test;
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 {
protected function setUp() {
2015-08-13 01:02:41 +02:00
Fixture::init();
File\StatCache::clear();
2015-08-13 01:02:41 +02:00
}
protected function tearDown() {
2015-08-13 01:02:41 +02:00
Fixture::clear();
}
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() {
$this->execute(function () {
2015-08-13 01:02:41 +02:00
$path = Fixture::path() . "/write";
$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() {
$this->execute(function () {
$handle = yield File\open(__FILE__, "r");
2015-08-13 01:02:41 +02:00
$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);
2015-08-13 01:02:41 +02:00
$contents .= $chunk;
$position += \strlen($chunk);
$this->assertSame($position, $handle->tell());
}
$this->assertSame((yield File\get(__FILE__)), $contents);
yield $handle->close();
2015-08-13 01:02:41 +02:00
});
}
public function testQueuedReads() {
$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);
$expected = \substr((yield File\get(__FILE__)), 0, 20);
2015-08-13 01:02:41 +02:00
$this->assertSame($expected, $contents);
yield $handle->close();
2015-08-13 01:02:41 +02:00
});
}
public function testReadingFromOffset() {
$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());
$expected = \substr((yield File\get(__FILE__)), 10, 90);
2015-08-13 01:02:41 +02:00
$this->assertSame($expected, $chunk);
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() {
$this->execute(function () {
try {
$handle = yield File\open(__FILE__, "r");
yield $handle->seek(0, 99999);
} finally {
yield $handle->close();
}
2015-08-13 01:02:41 +02:00
});
}
public function testSeekSetCur() {
$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());
yield $handle->close();
2015-08-13 01:02:41 +02:00
});
}
public function testSeekSetEnd() {
$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());
yield $handle->close();
2015-08-13 01:02:41 +02:00
});
}
public function testPath() {
$this->execute(function () {
$handle = yield File\open(__FILE__, "r");
2015-08-13 01:02:41 +02:00
$this->assertSame(__FILE__, $handle->path());
yield $handle->close();
2015-08-13 01:02:41 +02:00
});
}
public function testMode() {
$this->execute(function () {
$handle = yield File\open(__FILE__, "r");
2015-08-13 01:02:41 +02:00
$this->assertSame("r", $handle->mode());
yield $handle->close();
2015-08-13 01:02:41 +02:00
});
}
/**
* @expectedException \Amp\File\FilesystemException
*/
2015-08-13 01:02:41 +02:00
public function testClose() {
$this->execute(function () {
$handle = yield File\open(__FILE__, "r");
2015-08-13 01:02:41 +02:00
yield $handle->close();
yield $handle->read();
2015-08-13 01:02:41 +02:00
});
}
}