Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for Issue #222: handle EOF and offset correctly on read #224

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/OpenedFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ public function getGroup(): int

private function restorePosition(): void
{
$this->base->getContentObject()->seek($this->position, SEEK_SET);
$this->base->getContentObject()->seek($this->position, SEEK_SET, false);
}

private function savePosition(): void
Expand Down
2 changes: 1 addition & 1 deletion src/content/FileContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function read(int $count): string;
/**
* seeks to the given offset
*/
public function seek(int $offset, int $whence): bool;
public function seek(int $offset, int $whence, bool $resetEof = true): bool;

/**
* checks whether pointer is at end of file
Expand Down
25 changes: 21 additions & 4 deletions src/content/SeekableFileContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,26 @@ abstract class SeekableFileContent implements FileContent
*/
private $offset = 0;

/**
/**
* has the stream reached EOF; this happens on the first read *after* seeking to the end of the file
*
* @var bool
*/
private $eof=false;

/**
* reads the given amount of bytes from content
*/
public function read(int $count): string
{
// If offset is already at or past end of file, set EOF flag
if ( $this->offset >= $this->size() ) {
$this->eof = true;
return '';
}

$data = $this->doRead($this->offset, $count);
$this->offset += $count;
$this->offset += strlen($data);

return $data;
}
Expand All @@ -51,7 +64,7 @@ abstract protected function doRead(int $offset, int $count): string;
/**
* seeks to the given offset
*/
public function seek(int $offset, int $whence): bool
public function seek(int $offset, int $whence, bool $resetEof = true): bool
{
$newOffset = $this->offset;
switch ($whence) {
Expand All @@ -76,6 +89,10 @@ public function seek(int $offset, int $whence): bool
}

$this->offset = $newOffset;
// EOF is always false after a manual seek
if ( $resetEof ) {
$this->eof = false;
}

return true;
}
Expand All @@ -85,7 +102,7 @@ public function seek(int $offset, int $whence): bool
*/
public function eof(): bool
{
return $this->size() <= $this->offset;
return $this->eof;
}

/**
Expand Down
24 changes: 22 additions & 2 deletions tests/phpunit/content/StringBasedFileContentTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,18 +125,38 @@ public function readLessThenSizeDoesNotReachEof(): void
/**
* @test
*/
public function readSizeReachesEof(): void
public function readSizeReachesDoesNotReachEof(): void
{
$this->stringBasedFileContent->read(9);
assertFalse($this->stringBasedFileContent->eof());
}

/**
* @test
*/
public function readSizeReachesEofOnNextRead(): void
{
$this->stringBasedFileContent->read(9);
$this->stringBasedFileContent->read(1);
assertTrue($this->stringBasedFileContent->eof());
}

/**
* @test
*/
public function readMoreThanSizeReachesEof(): void
public function readMoreThanSizeDoesNotReachEof(): void
{
$this->stringBasedFileContent->read(10);
assertFalse($this->stringBasedFileContent->eof());
}

/**
* @test
*/
public function readMoreThanSizeReachesEofOnNextRead(): void
{
$this->stringBasedFileContent->read(10);
$this->stringBasedFileContent->read(1);
assertTrue($this->stringBasedFileContent->eof());
}

Expand Down
21 changes: 8 additions & 13 deletions tests/phpunit/vfsStreamFileTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,9 @@ public function contentCanBeChanged(): void
/**
* @test
*/
public function isAtEofWhenEmpty(): void
public function isNotAtEofWhenEmpty(): void
{
assertTrue($this->file->eof());
assertFalse($this->file->eof());
}

/**
Expand All @@ -169,19 +169,10 @@ public function readFromEmptyFileReturnsEmptyString(): void
/**
* @test
*/
public function readFromEmptyFileMovesPointer(): void
public function readFromEmptyFileDoesNotMovePointer(): void
{
$this->file->read(5);
assertThat($this->file->getBytesRead(), equals(5));
}

/**
* @test
*/
public function reportsAmountOfBytesReadEvenWhenEmpty(): void
{
$this->file->read(5);
assertThat($this->file->getBytesRead(), equals(5));
assertThat($this->file->getBytesRead(), equals(0));
}

/**
Expand Down Expand Up @@ -228,6 +219,10 @@ public function partialReads(): void

assertThat($this->file->read(3), equals('baz'));
assertThat($this->file->getBytesRead(), equals(9));
assertFalse($this->file->eof());

assertThat($this->file->read(1), equals(''));
assertThat($this->file->getBytesRead(), equals(9));
assertTrue($this->file->eof());
}

Expand Down
1 change: 1 addition & 0 deletions tests/phpunit/vfsStreamWrapperFileTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ public function recognizesEof(): void
{
$fp = fopen($this->fileInSubdir->url(), 'r');
fseek($fp, 1, SEEK_END);
fread($fp, 1);
assertTrue(feof($fp));
fclose($fp);
}
Expand Down
94 changes: 94 additions & 0 deletions tests/phpunit/vfsStreamWrapperTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -904,4 +904,98 @@ public function multipleWritesOnSameFileHaveDifferentPointers(): void

assertThat(file_get_contents($url), equals($contentB . $contentA));
}

/**
* @test
*/
public function readsAndWritesOnSameFileHaveDifferentPointers(): void
{
$contentA = uniqid('a');
$contentB = uniqid('b');
$url = $this->fileInSubdir->url();

$fp1 = fopen($url, 'wb');
$fp2 = fopen($url, 'rb');

fwrite($fp1, $contentA);
$contentBeforeWrite = fread($fp2, strlen($contentA));

fwrite($fp1, $contentB);
$contentAfterWrite = fread($fp2, strlen($contentB));

fclose($fp1);
fclose($fp2);

assertThat($contentBeforeWrite, equals($contentA));
assertThat($contentAfterWrite, equals($contentB));
}

/**
* @test
*/
public function feofIsFalseWhenEmptyFileOpened(): void
{
$this->fileInSubdir->setContent('');

$stream = fopen($this->fileInSubdir->url(), 'r');

assertFalse(feof($stream));
}

/**
* @test
*/
public function feofIsTrueAfterEmptyFileRead(): void
{
$this->fileInSubdir->setContent('');

$stream = fopen($this->fileInSubdir->url(), 'r');

fgets($stream);

assertTrue(feof($stream));
}

/**
* @test
*/
public function feofIsFalseWhenEmptyStreamRewound(): void
{
$this->fileInSubdir->setContent('');

$stream = fopen($this->fileInSubdir->url(), 'r');

fgets($stream);
rewind($stream);
assertFalse(feof($stream));
}

/**
* @test
*/
public function feofIsFalseAfterReadingLastLine(): void
{
$this->fileInSubdir->setContent("Line 1\n");

$stream = fopen($this->fileInSubdir->url(), 'r');

fgets($stream);

assertFalse(feof($stream));
}

/**
* @test
*/
public function feofIsTrueAfterReadingBeyondLastLine(): void
{
$this->fileInSubdir->setContent("Line 1\n");

$stream = fopen($this->fileInSubdir->url(), 'r');

fgets($stream);
fgets($stream);

assertTrue(feof($stream));
}
}