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

267 lines
7.8 KiB
PHP
Raw Normal View History

<?php
2015-08-13 01:02:41 +02:00
namespace Amp\File\Test;
use Amp\ByteStream\ClosedException;
use Amp\File;
2015-08-13 01:02:41 +02:00
abstract class FileTest extends FilesystemTest
{
2020-06-30 21:45:09 +02:00
/** @var File\Driver */
private $driver;
2019-08-23 20:59:26 +02:00
public function testWrite(): \Generator
{
2019-08-23 20:59:26 +02:00
$path = Fixture::path() . "/write";
2020-06-30 21:45:09 +02:00
/** @var File\File $handle */
$handle = yield $this->driver->openFile($path, "c+");
2019-08-23 20:59:26 +02:00
$this->assertSame(0, $handle->tell());
2015-08-13 01:02:41 +02:00
2019-08-23 20:59:26 +02:00
$handle->write("foo");
yield $handle->write("bar");
yield $handle->seek(0);
$contents = yield $handle->read();
$this->assertSame(6, $handle->tell());
$this->assertTrue($handle->eof());
$this->assertSame("foobar", $contents);
2015-08-13 01:02:41 +02:00
2019-08-23 20:59:26 +02:00
yield $handle->close();
2015-08-13 01:02:41 +02:00
}
2019-08-23 20:59:26 +02:00
public function testEmptyWrite(): \Generator
{
2019-08-23 20:59:26 +02:00
$path = Fixture::path() . "/write";
2020-06-30 21:45:09 +02:00
$handle = yield $this->driver->openFile($path, "c+");
2019-08-23 20:59:26 +02:00
$this->assertSame(0, $handle->tell());
2019-08-23 20:59:26 +02:00
yield $handle->write("");
$this->assertSame(0, $handle->tell());
2019-08-23 20:59:26 +02:00
yield $handle->close();
}
2019-08-23 20:59:26 +02:00
public function testWriteAfterClose(): \Generator
{
2019-08-23 20:59:26 +02:00
$path = Fixture::path() . "/write";
2020-06-30 21:45:09 +02:00
/** @var File\File $handle */
$handle = yield $this->driver->openFile($path, "c+");
2019-08-23 20:59:26 +02:00
yield $handle->close();
2019-08-23 20:59:26 +02:00
$this->expectException(ClosedException::class);
yield $handle->write("bar");
}
2019-08-23 20:59:26 +02:00
public function testDoubleClose(): \Generator
{
2019-08-23 20:59:26 +02:00
$path = Fixture::path() . "/write";
2020-06-30 21:45:09 +02:00
/** @var File\File $handle */
$handle = yield $this->driver->openFile($path, "c+");
2019-08-23 20:59:26 +02:00
yield $handle->close();
$this->assertNull(yield $handle->close());
2017-06-21 14:07:49 +02:00
}
2019-08-23 20:59:26 +02:00
public function testWriteAfterEnd(): \Generator
{
2019-08-23 20:59:26 +02:00
$path = Fixture::path() . "/write";
2020-06-30 21:45:09 +02:00
/** @var File\File $handle */
$handle = yield $this->driver->openFile($path, "c+");
2019-08-23 20:59:26 +02:00
$this->assertSame(0, $handle->tell());
yield $handle->end("foo");
2019-08-23 20:59:26 +02:00
$this->expectException(ClosedException::class);
yield $handle->write("bar");
}
2019-08-23 20:59:26 +02:00
public function testWriteInAppendMode(): \Generator
2019-03-01 17:38:04 +01:00
{
2019-08-23 20:59:26 +02:00
$path = Fixture::path() . "/write";
2020-06-30 21:45:09 +02:00
/** @var File\File $handle */
$handle = yield $this->driver->openFile($path, "a+");
2019-08-23 20:59:26 +02:00
$this->assertSame(0, $handle->tell());
yield $handle->write("bar");
yield $handle->write("foo");
yield $handle->write("baz");
$this->assertSame(9, $handle->tell());
yield $handle->seek(0);
$this->assertSame(0, $handle->tell());
$this->assertSame("barfoobaz", yield $handle->read());
2019-03-01 17:38:04 +01:00
}
2019-08-23 20:59:26 +02:00
public function testReadingToEof(): \Generator
{
2020-06-30 21:45:09 +02:00
/** @var File\File $handle */
$handle = yield $this->driver->openFile(__FILE__, "r");
2019-08-23 20:59:26 +02:00
$contents = "";
$position = 0;
2015-08-13 01:02:41 +02:00
2020-07-01 07:23:23 +02:00
$stat = yield $this->driver->getStatus(__FILE__);
2019-08-23 20:59:26 +02:00
$chunkSize = (int) \floor(($stat["size"] / 5));
2015-08-13 01:02:41 +02:00
2019-08-23 20:59:26 +02:00
while (!$handle->eof()) {
$chunk = yield $handle->read($chunkSize);
$contents .= $chunk;
$position += \strlen($chunk);
$this->assertSame($position, $handle->tell());
}
2015-08-13 01:02:41 +02:00
2019-08-23 20:59:26 +02:00
$this->assertNull(yield $handle->read());
2020-07-01 07:23:23 +02:00
$this->assertSame(yield $this->driver->read(__FILE__), $contents);
2019-08-23 20:59:26 +02:00
yield $handle->close();
2015-08-13 01:02:41 +02:00
}
2019-08-23 20:59:26 +02:00
public function testSequentialReads(): \Generator
{
2020-06-30 21:45:09 +02:00
/** @var File\File $handle */
$handle = yield $this->driver->openFile(__FILE__, "r");
2015-08-13 01:02:41 +02:00
2019-08-23 20:59:26 +02:00
$contents = "";
$contents .= yield $handle->read(10);
$contents .= yield $handle->read(10);
2015-08-13 01:02:41 +02:00
2020-07-01 07:23:23 +02:00
$expected = \substr(yield $this->driver->read(__FILE__), 0, 20);
2019-08-23 20:59:26 +02:00
$this->assertSame($expected, $contents);
2019-08-23 20:59:26 +02:00
yield $handle->close();
2015-08-13 01:02:41 +02:00
}
2019-08-23 20:59:26 +02:00
public function testReadingFromOffset(): \Generator
{
2020-06-30 21:45:09 +02:00
/** @var File\File $handle */
$handle = yield $this->driver->openFile(__FILE__, "r");
2019-08-23 20:59:26 +02:00
$this->assertSame(0, $handle->tell());
yield $handle->seek(10);
$this->assertSame(10, $handle->tell());
$chunk = yield $handle->read(90);
$this->assertSame(100, $handle->tell());
2020-07-01 07:23:23 +02:00
$expected = \substr(yield $this->driver->read(__FILE__), 10, 90);
2019-08-23 20:59:26 +02:00
$this->assertSame($expected, $chunk);
2019-08-23 20:59:26 +02:00
yield $handle->close();
2015-08-13 01:02:41 +02:00
}
2019-08-23 20:59:26 +02:00
public function testSeekThrowsOnInvalidWhence(): \Generator
{
2019-08-23 20:23:33 +02:00
$this->expectException(\Error::class);
2019-08-23 20:59:26 +02:00
try {
2020-06-30 21:45:09 +02:00
/** @var File\File $handle */
$handle = yield $this->driver->openFile(__FILE__, "r");
2019-08-23 20:59:26 +02:00
yield $handle->seek(0, 99999);
} finally {
yield $handle->close();
2019-08-23 20:59:26 +02:00
}
2015-08-13 01:02:41 +02:00
}
2019-08-23 20:59:26 +02:00
public function testSeekSetCur(): \Generator
{
2020-06-30 21:45:09 +02:00
/** @var File\File $handle */
$handle = yield $this->driver->openFile(__FILE__, "r");
2019-08-23 20:59:26 +02:00
$this->assertSame(0, $handle->tell());
yield $handle->seek(10);
$this->assertSame(10, $handle->tell());
yield $handle->seek(-10, \SEEK_CUR);
$this->assertSame(0, $handle->tell());
yield $handle->close();
2015-08-13 01:02:41 +02:00
}
2019-08-23 20:59:26 +02:00
public function testSeekSetEnd(): \Generator
{
2020-07-01 07:23:23 +02:00
$size = \filesize(__FILE__);
2020-06-30 21:45:09 +02:00
/** @var File\File $handle */
$handle = yield $this->driver->openFile(__FILE__, "r");
2019-08-23 20:59:26 +02:00
$this->assertSame(0, $handle->tell());
yield $handle->seek(-10, \SEEK_END);
$this->assertSame($size - 10, $handle->tell());
yield $handle->close();
2015-08-13 01:02:41 +02:00
}
2019-08-23 20:59:26 +02:00
public function testPath(): \Generator
{
2020-06-30 21:45:09 +02:00
/** @var File\File $handle */
$handle = yield $this->driver->openFile(__FILE__, "r");
$this->assertSame(__FILE__, $handle->getPath());
2019-08-23 20:59:26 +02:00
yield $handle->close();
2015-08-13 01:02:41 +02:00
}
2019-08-23 20:59:26 +02:00
public function testMode(): \Generator
{
2020-06-30 21:45:09 +02:00
/** @var File\File $handle */
$handle = yield $this->driver->openFile(__FILE__, "r");
$this->assertSame("r", $handle->getMode());
2019-08-23 20:59:26 +02:00
yield $handle->close();
}
public function testClose(): \Generator
{
2020-06-30 21:45:09 +02:00
/** @var File\File $handle */
$handle = yield $this->driver->openFile(__FILE__, "r");
2019-08-23 20:59:26 +02:00
yield $handle->close();
2019-08-23 20:59:26 +02:00
$this->expectException(ClosedException::class);
yield $handle->read();
2015-08-13 01:02:41 +02:00
}
/**
* @depends testWrite
*/
2019-08-23 20:59:26 +02:00
public function testTruncateToSmallerSize(): \Generator
{
2019-08-23 20:59:26 +02:00
$path = Fixture::path() . "/write";
2020-06-30 21:45:09 +02:00
/** @var File\File $handle */
$handle = yield $this->driver->openFile($path, "c+");
2019-08-23 20:59:26 +02:00
$handle->write("foo");
yield $handle->write("bar");
yield $handle->truncate(4);
yield $handle->seek(0);
$contents = yield $handle->read();
$this->assertTrue($handle->eof());
$this->assertSame("foob", $contents);
yield $handle->write("bar");
$this->assertSame(7, $handle->tell());
yield $handle->seek(0);
$contents = yield $handle->read();
$this->assertSame("foobbar", $contents);
yield $handle->close();
}
/**
* @depends testWrite
*/
2019-08-23 20:59:26 +02:00
public function testTruncateToLargerSize(): \Generator
{
$path = Fixture::path() . "/write";
2020-06-30 21:45:09 +02:00
/** @var File\File $handle */
$handle = yield $this->driver->openFile($path, "c+");
2019-08-23 20:59:26 +02:00
yield $handle->write("foo");
yield $handle->truncate(6);
$this->assertSame(3, $handle->tell());
yield $handle->seek(0);
$contents = yield $handle->read();
$this->assertTrue($handle->eof());
$this->assertSame("foo\0\0\0", $contents);
yield $handle->write("bar");
$this->assertSame(9, $handle->tell());
yield $handle->seek(0);
$contents = yield $handle->read();
$this->assertSame("foo\0\0\0bar", $contents);
yield $handle->close();
}
2020-06-30 21:45:09 +02:00
abstract protected function createDriver(): File\Driver;
protected function setUp(): void
{
parent::setUp();
$this->driver = $this->createDriver();
}
2015-08-13 01:02:41 +02:00
}