Add NullStream
This commit is contained in:
parent
cbeadbda53
commit
dea577fdb4
|
|
@ -0,0 +1,189 @@
|
|||
<?php
|
||||
|
||||
namespace WellRESTed\Stream;
|
||||
|
||||
use Psr\Http\Message\StreamableInterface;
|
||||
|
||||
class NullStream implements StreamableInterface
|
||||
{
|
||||
/**
|
||||
* Reads all data from the stream into a string, from the beginning to end.
|
||||
*
|
||||
* This method MUST attempt to seek to the beginning of the stream before
|
||||
* reading data and read the stream until the end is reached.
|
||||
*
|
||||
* Warning: This could attempt to load a large amount of data into memory.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes the stream and any underlying resources.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function close()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Separates any underlying resources from the stream.
|
||||
*
|
||||
* After the stream has been detached, the stream is in an unusable state.
|
||||
*
|
||||
* @return resource|null Underlying PHP stream, if any
|
||||
*/
|
||||
public function detach()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the size of the stream if known
|
||||
*
|
||||
* @return int|null Returns the size in bytes if known, or null if unknown.
|
||||
*/
|
||||
public function getSize()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current position of the file read/write pointer
|
||||
*
|
||||
* @return int|bool Position of the file pointer or false on error.
|
||||
*/
|
||||
public function tell()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the stream is at the end of the stream.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function eof()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether or not the stream is seekable.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isSeekable()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Seek to a position in the stream.
|
||||
*
|
||||
* @link http://www.php.net/manual/en/function.fseek.php
|
||||
* @param int $offset Stream offset
|
||||
* @param int $whence Specifies how the cursor position will be calculated
|
||||
* based on the seek offset. Valid values are identical to the built-in
|
||||
* PHP $whence values for `fseek()`. SEEK_SET: Set position equal to
|
||||
* offset bytes SEEK_CUR: Set position to current location plus offset
|
||||
* SEEK_END: Set position to end-of-stream plus offset.
|
||||
* @return bool Returns TRUE on success or FALSE on failure.
|
||||
*/
|
||||
public function seek($offset, $whence = SEEK_SET)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Seek to the beginning of the stream.
|
||||
*
|
||||
* If the stream is not seekable, this method will return FALSE, indicating
|
||||
* failure; otherwise, it will perform a seek(0), and return the status of
|
||||
* that operation.
|
||||
*
|
||||
* @see seek()
|
||||
* @link http://www.php.net/manual/en/function.fseek.php
|
||||
* @return bool Returns TRUE on success or FALSE on failure.
|
||||
*/
|
||||
public function rewind()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether or not the stream is writable.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isWritable()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write data to the stream.
|
||||
*
|
||||
* @param string $string The string that is to be written.
|
||||
* @return int|bool Returns the number of bytes written to the stream on
|
||||
* success or FALSE on failure.
|
||||
*/
|
||||
public function write($string)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether or not the stream is readable.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isReadable()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read data from the stream.
|
||||
*
|
||||
* @param int $length Read up to $length bytes from the object and return
|
||||
* them. Fewer than $length bytes may be returned if underlying stream
|
||||
* call returns fewer bytes.
|
||||
* @return string|false Returns the data read from the stream, false if
|
||||
* unable to read or if an error occurs.
|
||||
*/
|
||||
public function read($length)
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the remaining contents in a string
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getContents()
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* Get stream metadata as an associative array or retrieve a specific key.
|
||||
*
|
||||
* The keys returned are identical to the keys returned from PHP's
|
||||
* stream_get_meta_data() function.
|
||||
*
|
||||
* @link http://php.net/manual/en/function.stream-get-meta-data.php
|
||||
* @param string $key Specific metadata to retrieve.
|
||||
* @return array|mixed|null Returns an associative array if no key is
|
||||
* provided. Returns a specific key value if a key is provided and the
|
||||
* value is found, or null if the key is not found.
|
||||
*/
|
||||
public function getMetadata($key = null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,151 @@
|
|||
<?php
|
||||
|
||||
namespace WellRESTed\Test\Stream;
|
||||
|
||||
use WellRESTed\Stream\NullStream;
|
||||
|
||||
class NullStreamTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @covers WellRESTed\Stream\NullStream::__toString()
|
||||
*/
|
||||
public function testCastsToString()
|
||||
{
|
||||
$stream = new NullStream();
|
||||
$this->assertEquals("", (string) $stream);
|
||||
}
|
||||
|
||||
public function testCloseDoesNothing()
|
||||
{
|
||||
$stream = new NullStream();
|
||||
$this->assertNull($stream->close());
|
||||
}
|
||||
/**
|
||||
* @covers WellRESTed\Stream\NullStream::detach()
|
||||
* @uses WellRESTed\Stream\Stream
|
||||
*/
|
||||
public function testDetachReturnsNull()
|
||||
{
|
||||
$stream = new NullStream();
|
||||
$this->assertNull($stream->detach());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers WellRESTed\Stream\NullStream::getSize
|
||||
* @uses WellRESTed\Stream\Stream
|
||||
*/
|
||||
public function testSizeReturnsZero()
|
||||
{
|
||||
$stream = new NullStream();
|
||||
$this->assertEquals(0, $stream->getSize());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers WellRESTed\Stream\NullStream::tell
|
||||
*/
|
||||
public function testTellReturnsFalse()
|
||||
{
|
||||
$stream = new NullStream();
|
||||
$this->assertFalse($stream->tell());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers WellRESTed\Stream\NullStream::eof
|
||||
*/
|
||||
public function testEofReturnsReturnsTrue()
|
||||
{
|
||||
$stream = new NullStream();
|
||||
$this->assertTrue($stream->eof());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers WellRESTed\Stream\NullStream::isSeekable
|
||||
* @uses WellRESTed\Stream\Stream
|
||||
*/
|
||||
public function testIsSeekableReturnsFalse()
|
||||
{
|
||||
$stream = new NullStream();
|
||||
$this->assertFalse($stream->isSeekable());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers WellRESTed\Stream\NullStream::seek
|
||||
*/
|
||||
public function testSeekReturnsFalse()
|
||||
{
|
||||
$stream = new NullStream();
|
||||
$this->assertFalse($stream->seek(10));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers WellRESTed\Stream\NullStream::rewind
|
||||
*/
|
||||
public function testRewindReturnsFalse()
|
||||
{
|
||||
$stream = new NullStream();
|
||||
$this->assertFalse($stream->rewind());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers WellRESTed\Stream\NullStream::isWritable
|
||||
*/
|
||||
public function testIsWritableReturnsFalse()
|
||||
{
|
||||
$stream = new NullStream();
|
||||
$this->assertFalse($stream->isWritable());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers WellRESTed\Stream\NullStream::write
|
||||
*/
|
||||
public function testWriteReturnsFalse()
|
||||
{
|
||||
$stream = new NullStream();
|
||||
$this->assertFalse($stream->write(""));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers WellRESTed\Stream\NullStream::isReadable
|
||||
*/
|
||||
public function testIsReableReturnsTrue()
|
||||
{
|
||||
$stream = new NullStream();
|
||||
$this->assertTrue($stream->isReadable());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers WellRESTed\Stream\NullStream::read
|
||||
*/
|
||||
public function testReadReturnsEmptyString()
|
||||
{
|
||||
$stream = new NullStream();
|
||||
$this->assertEquals("", $stream->read(100));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers WellRESTed\Stream\NullStream::getContents
|
||||
*/
|
||||
public function testGetContentsReturnsEmptyString()
|
||||
{
|
||||
$stream = new NullStream();
|
||||
$this->assertEquals("", $stream->getContents());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers WellRESTed\Stream\NullStream::getMetadata
|
||||
*/
|
||||
public function testGetMetadataReturnsNull()
|
||||
{
|
||||
$stream = new NullStream();
|
||||
$this->assertNull($stream->getMetadata());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers WellRESTed\Stream\NullStream::getMetadata
|
||||
*/
|
||||
public function testGetMetadataReturnsNullWithKey()
|
||||
{
|
||||
$stream = new NullStream();
|
||||
$this->assertNull($stream->getMetadata("size"));
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue