Skip to content

Commit

Permalink
Initial working set of functional tests based on real streams
Browse files Browse the repository at this point in the history
The idea is to build up a library of tests which describe the behaviour
of real file streams under PHP. Regardless of the internal code
structure, this can then be used to check that the functionality of the
virtual file system is as close as possible.
  • Loading branch information
IMSoP committed Feb 27, 2020
1 parent 751b3df commit 1e8367b
Show file tree
Hide file tree
Showing 6 changed files with 634 additions and 0 deletions.
69 changes: 69 additions & 0 deletions tests/phpunit/functional/BaseFunctionalTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php


namespace bovigo\vfs\tests;

use bovigo\vfs\vfsStream;
use bovigo\vfs\vfsStreamFile;
use PHPUnit\Framework\TestCase;

/**
* Base class for setting up and configuring fixtures for stream wrapper functional tests.
* See README.md for an explanation of the two implementations of each method
*/
abstract class BaseFunctionalTestCase extends TestCase
{
/**
* @var vfsStreamFile
*/
protected $vfsFile;
/**
* @var string
*/
private $realFileName;

/**
* Create real or virtual files for use by all tests
*/
protected function setUp(): void
{
if ( isset($_ENV['TEST_THE_TESTS_WITH_REAL_FILES']) ) {
$this->realFileName = tempnam(sys_get_temp_dir(), 'vfsstream_test_');
}
else {
$root = vfsStream::setup();
$this->vfsFile = vfsStream::newFile('test');
$this->vfsFile->at($root);
}
}

protected function getMockFileName(): string
{
if ( isset($_ENV['TEST_THE_TESTS_WITH_REAL_FILES']) ) {
return $this->realFileName;
}
else {
return $this->vfsFile->url();
}
}

protected function setMockFileContent(string $content): void
{
if ( isset($_ENV['TEST_THE_TESTS_WITH_REAL_FILES']) ) {
file_put_contents($this->realFileName, $content);
}
else {
$this->vfsFile->setContent($content);
}
}

/**
* The virtual file system will clean itself up, but real files need real deletion
*/
protected function tearDown(): void
{
if ( isset($_ENV['TEST_THE_TESTS_WITH_REAL_FILES']) ) {
unlink($this->realFileName);
}
}
}
221 changes: 221 additions & 0 deletions tests/phpunit/functional/FeofTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
<?php

declare(strict_types=1);

/**
* This file is part of vfsStream.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace bovigo\vfs\tests;

use PHPUnit\Framework\Error\Warning;
use function bovigo\assert\assertFalse;
use function bovigo\assert\assertThat;
use function bovigo\assert\assertTrue;
use function bovigo\assert\predicate\equals;
use function fopen;

/**
* Test for behaviour of feof(), which should match that of real files
*/
class FeofTestCase extends BaseFunctionalTestCase
{
/**
* @test
*/
public function feofIsFalseWhenFileOpened(): void
{
$this->setMockFileContent("Line 1\n");

$stream = fopen($this->getMockFileName(), 'rb');

assertFalse(feof($stream));
}

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

$stream = fopen($this->getMockFileName(), 'rb');

assertFalse(feof($stream));
}

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

$stream = fopen($this->getMockFileName(), 'rb');

fgets($stream);

assertTrue(feof($stream));
}

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

$stream = fopen($this->getMockFileName(), 'rb');

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

/**
* @test
*/
public function feofIsFalseWhenSeekingToStart(): void
{
$fp = fopen($this->getMockFileName(), 'rb');
fseek($fp, 0, SEEK_SET);
assertFalse(feof($fp));
fclose($fp);
}

/**
* @test
*/
public function feofIsFalseWhenSeekingToEnd(): void
{
$fp = fopen($this->getMockFileName(), 'rb');
fseek($fp, 0, SEEK_END);
assertFalse(feof($fp));
fclose($fp);
}

/**
* @test
*/
public function feofIsFalseWhenSeekingPastEnd(): void
{
$fp = fopen($this->getMockFileName(), 'rb');
fseek($fp, 10, SEEK_END);
assertFalse(feof($fp));
fclose($fp);
}

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

$stream = fopen($this->getMockFileName(), 'rb');

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

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

$stream = fopen($this->getMockFileName(), 'rb');

assertThat(fgets($stream), equals("Line 1\n"));
assertFalse(feof($stream));
}

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

$stream = fopen($this->getMockFileName(), 'rb');

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

assertTrue(feof($stream));
}

/**
* @test
*/
public function readLessThanSizeDoesNotReachEof(): void
{
$this->setMockFileContent('123456789');
$stream = fopen($this->getMockFileName(), 'rb');
fread($stream, 3);
assertFalse(feof($stream));
}

/**
* @test
*/
public function readSizeDoesNotReachEof(): void
{
$this->setMockFileContent('123456789');
$stream = fopen($this->getMockFileName(), 'rb');
fread($stream, 9);
assertFalse(feof($stream));
}

/**
* @test
*/
public function readSizeReachesEofOnNextRead(): void
{
$this->setMockFileContent('123456789');
$stream = fopen($this->getMockFileName(), 'rb');
fread($stream, 9);
fread($stream, 1);
assertTrue(feof($stream));
}

/**
* @test
*/
public function readMoreThanSizeReachesEof(): void
{
$this->setMockFileContent('123456789');
$stream = fopen($this->getMockFileName(), 'rb');
fread($stream, 10);
assertTrue(feof($stream));
}

/**
* @test
*/
public function readMoreThanSizeReachesEofOnNextRead(): void
{
$this->setMockFileContent('123456789');
$stream = fopen($this->getMockFileName(), 'rb');
fread($stream, 10);
fread($stream, 1);
assertTrue(feof($stream));
}

/**
* @test
*/
public function streamIsNotEofAfterTruncate(): void
{
$this->setMockFileContent('test');
$stream = fopen($this->getMockFileName(), 'rb+');
fread($stream, 4);
ftruncate($stream, 0);
assertFalse(feof($stream));
}
}
Loading

0 comments on commit 1e8367b

Please sign in to comment.