1
0
mirror of https://github.com/danog/file.git synced 2024-12-03 09:47:54 +01:00
file/test/FileTest.php

251 lines
6.6 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-10-08 16:23:44 +02:00
private File\Driver $driver;
2020-06-30 21:45:09 +02:00
2020-10-08 16:23:44 +02:00
public function testWrite()
{
2019-08-23 20:59:26 +02:00
$path = Fixture::path() . "/write";
2020-10-08 16:23:44 +02:00
$handle = $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");
2020-10-08 16:23:44 +02:00
$handle->write("bar");
$handle->seek(0);
$contents = $handle->read();
2019-08-23 20:59:26 +02:00
$this->assertSame(6, $handle->tell());
$this->assertTrue($handle->eof());
$this->assertSame("foobar", $contents);
2015-08-13 01:02:41 +02:00
2020-10-08 16:23:44 +02:00
$handle->close();
2015-08-13 01:02:41 +02:00
}
2020-10-08 16:23:44 +02:00
public function testEmptyWrite()
{
2019-08-23 20:59:26 +02:00
$path = Fixture::path() . "/write";
2020-10-08 16:23:44 +02:00
$handle = $this->driver->openFile($path, "c+");
2019-08-23 20:59:26 +02:00
$this->assertSame(0, $handle->tell());
2020-10-08 16:23:44 +02:00
$handle->write("");
2019-08-23 20:59:26 +02:00
$this->assertSame(0, $handle->tell());
2020-10-08 16:23:44 +02:00
$handle->close();
}
2020-10-08 16:23:44 +02:00
public function testWriteAfterClose()
{
2019-08-23 20:59:26 +02:00
$path = Fixture::path() . "/write";
2020-10-08 16:23:44 +02:00
$handle = $this->driver->openFile($path, "c+");
$handle->close();
2019-08-23 20:59:26 +02:00
$this->expectException(ClosedException::class);
2020-10-08 16:23:44 +02:00
$handle->write("bar");
}
2020-10-08 16:23:44 +02:00
public function testDoubleClose()
{
2019-08-23 20:59:26 +02:00
$path = Fixture::path() . "/write";
2020-06-30 21:45:09 +02:00
/** @var File\File $handle */
2020-10-08 16:23:44 +02:00
$handle = $this->driver->openFile($path, "c+");
$handle->close();
$this->assertNull($handle->close());
2017-06-21 14:07:49 +02:00
}
2020-10-08 16:23:44 +02:00
public function testWriteAfterEnd()
{
2019-08-23 20:59:26 +02:00
$path = Fixture::path() . "/write";
2020-10-08 16:23:44 +02:00
$handle = $this->driver->openFile($path, "c+");
2019-08-23 20:59:26 +02:00
$this->assertSame(0, $handle->tell());
2020-10-08 16:23:44 +02:00
$handle->end("foo");
2019-08-23 20:59:26 +02:00
$this->expectException(ClosedException::class);
2020-10-08 16:23:44 +02:00
$handle->write("bar");
}
2020-10-08 16:23:44 +02:00
public function testWriteInAppendMode()
2019-03-01 17:38:04 +01:00
{
2019-08-23 20:59:26 +02:00
$path = Fixture::path() . "/write";
2020-10-08 16:23:44 +02:00
$handle = $this->driver->openFile($path, "a+");
2019-08-23 20:59:26 +02:00
$this->assertSame(0, $handle->tell());
2020-10-08 16:23:44 +02:00
$handle->write("bar");
$handle->write("foo");
$handle->write("baz");
2019-08-23 20:59:26 +02:00
$this->assertSame(9, $handle->tell());
2020-10-08 16:23:44 +02:00
$handle->seek(0);
2019-08-23 20:59:26 +02:00
$this->assertSame(0, $handle->tell());
2020-10-08 16:23:44 +02:00
$this->assertSame("barfoobaz", $handle->read());
2019-03-01 17:38:04 +01:00
}
2020-10-08 16:23:44 +02:00
public function testReadingToEof()
{
2020-10-08 16:23:44 +02:00
$handle = $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-10-08 16:23:44 +02:00
$stat = $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()) {
2020-10-08 16:23:44 +02:00
$chunk = $handle->read($chunkSize);
2019-08-23 20:59:26 +02:00
$contents .= $chunk;
$position += \strlen($chunk);
$this->assertSame($position, $handle->tell());
}
2015-08-13 01:02:41 +02:00
2020-10-08 16:23:44 +02:00
$this->assertNull($handle->read());
$this->assertSame($this->driver->read(__FILE__), $contents);
2020-10-08 16:23:44 +02:00
$handle->close();
2015-08-13 01:02:41 +02:00
}
2020-10-08 16:23:44 +02:00
public function testSequentialReads()
{
2020-10-08 16:23:44 +02:00
$handle = $this->driver->openFile(__FILE__, "r");
2015-08-13 01:02:41 +02:00
2019-08-23 20:59:26 +02:00
$contents = "";
2020-10-08 16:23:44 +02:00
$contents .= $handle->read(10);
$contents .= $handle->read(10);
2015-08-13 01:02:41 +02:00
2020-10-08 16:23:44 +02:00
$expected = \substr($this->driver->read(__FILE__), 0, 20);
2019-08-23 20:59:26 +02:00
$this->assertSame($expected, $contents);
2020-10-08 16:23:44 +02:00
$handle->close();
2015-08-13 01:02:41 +02:00
}
2020-10-08 16:23:44 +02:00
public function testReadingFromOffset()
{
2020-10-08 16:23:44 +02:00
$handle = $this->driver->openFile(__FILE__, "r");
2019-08-23 20:59:26 +02:00
$this->assertSame(0, $handle->tell());
2020-10-08 16:23:44 +02:00
$handle->seek(10);
2019-08-23 20:59:26 +02:00
$this->assertSame(10, $handle->tell());
2020-10-08 16:23:44 +02:00
$chunk = $handle->read(90);
2019-08-23 20:59:26 +02:00
$this->assertSame(100, $handle->tell());
2020-10-08 16:23:44 +02:00
$expected = \substr($this->driver->read(__FILE__), 10, 90);
2019-08-23 20:59:26 +02:00
$this->assertSame($expected, $chunk);
2020-10-08 16:23:44 +02:00
$handle->close();
2015-08-13 01:02:41 +02:00
}
2020-10-08 16:23:44 +02:00
public function testSeekThrowsOnInvalidWhence()
{
2019-08-23 20:23:33 +02:00
$this->expectException(\Error::class);
2019-08-23 20:59:26 +02:00
try {
2020-10-08 16:23:44 +02:00
$handle = $this->driver->openFile(__FILE__, "r");
$handle->seek(0, 99999);
2019-08-23 20:59:26 +02:00
} finally {
2020-10-08 16:23:44 +02:00
$handle->close();
2019-08-23 20:59:26 +02:00
}
2015-08-13 01:02:41 +02:00
}
2020-10-08 16:23:44 +02:00
public function testSeekSetCur()
{
2020-10-08 16:23:44 +02:00
$handle = $this->driver->openFile(__FILE__, "r");
2019-08-23 20:59:26 +02:00
$this->assertSame(0, $handle->tell());
2020-10-08 16:23:44 +02:00
$handle->seek(10);
2019-08-23 20:59:26 +02:00
$this->assertSame(10, $handle->tell());
2020-10-08 16:23:44 +02:00
$handle->seek(-10, \SEEK_CUR);
2019-08-23 20:59:26 +02:00
$this->assertSame(0, $handle->tell());
2020-10-08 16:23:44 +02:00
$handle->close();
2015-08-13 01:02:41 +02:00
}
2020-10-08 16:23:44 +02:00
public function testSeekSetEnd()
{
2020-07-01 07:23:23 +02:00
$size = \filesize(__FILE__);
2020-10-08 16:23:44 +02:00
$handle = $this->driver->openFile(__FILE__, "r");
2019-08-23 20:59:26 +02:00
$this->assertSame(0, $handle->tell());
2020-10-08 16:23:44 +02:00
$handle->seek(-10, \SEEK_END);
2019-08-23 20:59:26 +02:00
$this->assertSame($size - 10, $handle->tell());
2020-10-08 16:23:44 +02:00
$handle->close();
2015-08-13 01:02:41 +02:00
}
2020-10-08 16:23:44 +02:00
public function testPath()
{
2020-10-08 16:23:44 +02:00
$handle = $this->driver->openFile(__FILE__, "r");
2020-06-30 21:45:09 +02:00
$this->assertSame(__FILE__, $handle->getPath());
2020-10-08 16:23:44 +02:00
$handle->close();
2015-08-13 01:02:41 +02:00
}
2020-10-08 16:23:44 +02:00
public function testMode()
{
2020-10-08 16:23:44 +02:00
$handle = $this->driver->openFile(__FILE__, "r");
2020-06-30 21:45:09 +02:00
$this->assertSame("r", $handle->getMode());
2020-10-08 16:23:44 +02:00
$handle->close();
2019-08-23 20:59:26 +02:00
}
2020-10-08 16:23:44 +02:00
public function testClose()
2019-08-23 20:59:26 +02:00
{
2020-10-08 16:23:44 +02:00
$handle = $this->driver->openFile(__FILE__, "r");
$handle->close();
2019-08-23 20:59:26 +02:00
$this->expectException(ClosedException::class);
2020-10-08 16:23:44 +02:00
$handle->read();
2015-08-13 01:02:41 +02:00
}
/**
* @depends testWrite
*/
2020-10-08 16:23:44 +02:00
public function testTruncateToSmallerSize()
{
2019-08-23 20:59:26 +02:00
$path = Fixture::path() . "/write";
2020-10-08 16:23:44 +02:00
$handle = $this->driver->openFile($path, "c+");
2019-08-23 20:59:26 +02:00
$handle->write("foo");
2020-10-08 16:23:44 +02:00
$handle->write("bar");
$handle->truncate(4);
$handle->seek(0);
$contents = $handle->read();
2019-08-23 20:59:26 +02:00
$this->assertTrue($handle->eof());
$this->assertSame("foob", $contents);
2020-10-08 16:23:44 +02:00
$handle->write("bar");
2019-08-23 20:59:26 +02:00
$this->assertSame(7, $handle->tell());
2020-10-08 16:23:44 +02:00
$handle->seek(0);
$contents = $handle->read();
2019-08-23 20:59:26 +02:00
$this->assertSame("foobbar", $contents);
2020-10-08 16:23:44 +02:00
$handle->close();
}
/**
* @depends testWrite
*/
2020-10-08 16:23:44 +02:00
public function testTruncateToLargerSize()
2019-08-23 20:59:26 +02:00
{
$path = Fixture::path() . "/write";
2020-10-08 16:23:44 +02:00
$handle = $this->driver->openFile($path, "c+");
2019-08-23 20:59:26 +02:00
2020-10-08 16:23:44 +02:00
$handle->write("foo");
$handle->truncate(6);
2019-08-23 20:59:26 +02:00
$this->assertSame(3, $handle->tell());
2020-10-08 16:23:44 +02:00
$handle->seek(0);
$contents = $handle->read();
2019-08-23 20:59:26 +02:00
$this->assertTrue($handle->eof());
$this->assertSame("foo\0\0\0", $contents);
2020-10-08 16:23:44 +02:00
$handle->write("bar");
2019-08-23 20:59:26 +02:00
$this->assertSame(9, $handle->tell());
2020-10-08 16:23:44 +02:00
$handle->seek(0);
$contents = $handle->read();
2019-08-23 20:59:26 +02:00
$this->assertSame("foo\0\0\0bar", $contents);
2020-10-08 16:23:44 +02:00
$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
}