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

Initial working set of functional tests based on real streams #225

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
68 changes: 68 additions & 0 deletions tests/phpunit/functional/BaseFunctionalTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

namespace bovigo\vfs\tests;

use bovigo\vfs\vfsStream;
use bovigo\vfs\vfsStreamFile;
use PHPUnit\Framework\TestCase;
use function file_put_contents;
use function sys_get_temp_dir;
use function tempnam;
use function unlink;

/**
* 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);
}
}
}
229 changes: 229 additions & 0 deletions tests/phpunit/functional/FeofTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
<?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 const SEEK_END;
use const SEEK_SET;
use function bovigo\assert\assertFalse;
use function bovigo\assert\assertThat;
use function bovigo\assert\assertTrue;
use function bovigo\assert\predicate\equals;
use function fclose;
use function feof;
use function fgets;
use function fopen;
use function fread;
use function fseek;
use function ftruncate;
use function rewind;

/**
* 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