Merge pull request #18 from wellrestedphp/fix-tests

Fix broken test for Stream
This commit is contained in:
PJ Dietz 2020-08-16 12:11:56 -04:00 committed by GitHub
commit 66ff6d2fc1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 3 deletions

35
test/src/StreamHelper.php Normal file
View File

@ -0,0 +1,35 @@
<?php
namespace WellRESTed\Message;
// Declare functions in this namespace so the class under test will use these
// instead of the internal global functions during testing.
class StreamHelper
{
public static $fail = false;
}
function fseek($resource, $offset, $whence = SEEK_SET)
{
if (StreamHelper::$fail) {
return -1;
}
return \fseek($resource, $offset, $whence);
}
function ftell($resource)
{
if (StreamHelper::$fail) {
return false;
}
return \ftell($resource);
}
function rewind($resource)
{
if (StreamHelper::$fail) {
return false;
}
return \rewind($resource);
}

View File

@ -5,8 +5,11 @@ namespace WellRESTed\Test\Unit\Message;
use InvalidArgumentException; use InvalidArgumentException;
use RuntimeException; use RuntimeException;
use WellRESTed\Message\Stream; use WellRESTed\Message\Stream;
use WellRESTed\Message\StreamHelper;
use WellRESTed\Test\TestCase; use WellRESTed\Test\TestCase;
require_once __DIR__ . "/../../../src/StreamHelper.php";
class StreamTest extends TestCase class StreamTest extends TestCase
{ {
private $resource; private $resource;
@ -18,6 +21,7 @@ class StreamTest extends TestCase
$this->resource = fopen("php://memory", "w+"); $this->resource = fopen("php://memory", "w+");
$this->resourceDevNull = fopen("/dev/null", "r"); $this->resourceDevNull = fopen("/dev/null", "r");
fwrite($this->resource, $this->content); fwrite($this->resource, $this->content);
StreamHelper::$fail = false;
} }
public function tearDown(): void public function tearDown(): void
@ -106,7 +110,8 @@ class StreamTest extends TestCase
public function testTellThrowsRuntimeExceptionWhenUnableToReadStreamPosition() public function testTellThrowsRuntimeExceptionWhenUnableToReadStreamPosition()
{ {
$stream = new Stream($this->resourceDevNull); StreamHelper::$fail = true;
$stream = new Stream($this->resource);
$this->expectException(RuntimeException::class); $this->expectException(RuntimeException::class);
$stream->tell(); $stream->tell();
} }
@ -136,7 +141,8 @@ class StreamTest extends TestCase
public function testSeekThrowsRuntimeExceptionWhenUnableToSeek() public function testSeekThrowsRuntimeExceptionWhenUnableToSeek()
{ {
$stream = new Stream($this->resourceDevNull); StreamHelper::$fail = true;
$stream = new Stream($this->resource);
$this->expectException(RuntimeException::class); $this->expectException(RuntimeException::class);
$stream->seek(10); $stream->seek(10);
} }
@ -151,7 +157,8 @@ class StreamTest extends TestCase
public function testRewindThrowsRuntimeExceptionWhenUnableToRewind() public function testRewindThrowsRuntimeExceptionWhenUnableToRewind()
{ {
$stream = new Stream($this->resourceDevNull); StreamHelper::$fail = true;
$stream = new Stream($this->resource);
$this->expectException(RuntimeException::class); $this->expectException(RuntimeException::class);
$stream->rewind(); $stream->rewind();
} }