Skip to content

Commit

Permalink
streamize updates
Browse files Browse the repository at this point in the history
Added a bunch of tests for streamize  and updated docs because the
whole file or dir question is moot
  • Loading branch information
nozavroni committed Sep 16, 2016
1 parent f0dc0ea commit 325f783
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
3 changes: 1 addition & 2 deletions docs/users_guide/streams.rst
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ Although CSVelte cannot work with the ``SplFileObject`` class directly, it *can*
.. warning::

The :php:class:`SplFileObject` class does not have any way to access its underlying stream resource, so although :php:meth:`IO\\Stream::streamize()` can accept an :php:class:`SplFileObject`, it's pretty limited in that it will always open the file in ``r+b`` (binary-safe read + write) mode, regardless of what mode was used to open the :php:class:`SplFileObject`. As a result, the internal file pointer will be moved to the beginning of the stream. You must also make sure that the :php:class:`SplFileObject` represents a file rather than a directory [#]_, or an InvalidArgumentException will be thrown.
The :php:class:`SplFileObject` class does not have any way to access its underlying stream resource, so although :php:meth:`IO\\Stream::streamize()` can accept an :php:class:`SplFileObject`, it's pretty limited in that it will always open the file in ``r+b`` (binary-safe read + write) mode, regardless of what mode was used to open the :php:class:`SplFileObject`. As a result, the internal file pointer will be moved to the beginning of the stream.

Create a stream from a standard PHP string
------------------------------------------
Expand Down Expand Up @@ -151,4 +151,3 @@ If you already have a stream resource that you've opened using :php:func:`fopen`
.. [#] PHP defines stream wrappers as "additional code which tells the stream how to handle specific protocols/encodings". See `PHP streams documentation`_ for a more complete description.
.. [#] File access mode strings are a short (typically 1-3 characters) string containing very concise instructions about how a file or stream should be opened. See `fopen file modes`_ for a more detailed explanation.
.. [#] Standard input and standard output are preconnected I/O channels, input typically being a data stream going into a program from the user and output being the stream where a program writes its output. See `standard streams`_ Wikipedia page for more on stdin/stdout.
.. [#] See :php:meth:`SplFileObject::getType()` for how to determine whether :php:class:`SplFileObject` represents a file or a directory -- http://php.net/manual/en/splfileinfo.gettype.php
2 changes: 1 addition & 1 deletion src/CSVelte/IO/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class Stream implements Readable, Writable, Seekable
*/
public static function streamize($resource = '')
{
if ($resource instanceof SplFileObject && $resource->getType() == 'file') {
if ($resource instanceof SplFileObject) {
return new self($resource->getPathName());
}

Expand Down
37 changes: 37 additions & 0 deletions tests/CSVelte/IO/StreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -505,4 +505,41 @@ public function testDetachedStreamThrowsExceptionOnSeek()
$stream->seek(10);
}

/**
* @expectedException \InvalidArgumentException
*/
public function testStreamizeWithIntegerThrowsInvalidArgumentException()
{
$intval = 1;
Stream::streamize($intval);
}

/**
* @expectedException \InvalidArgumentException
*/
public function testStreamizeWithNonStringObjectThrowsInvalidArgumentException()
{
$obj = new \stdClass;
Stream::streamize($obj);
}

public function testStreamizeWithNoArguments()
{
$stream = Stream::streamize();
$this->assertTrue($stream->isReadable());
$this->assertTrue($stream->isWritable());
$this->assertTrue($stream->isSeekable());
$this->assertEquals("", $stream->read(10));
$this->assertTrue($stream->eof());
$this->assertEquals(10, $stream->write("helloworld"));
$this->assertEquals("helloworld", (string) $stream);
}

public function testStreamizeStream()
{
$stream = Stream::streamize("helloworld");
$streamcopy = Stream::streamize($stream);
$this->assertEquals((string) $stream, (string) $streamcopy);
}

}

0 comments on commit 325f783

Please sign in to comment.