diff --git a/classes/streams/PlaylistArchiveStream.php b/classes/streams/PlaylistArchiveStream.php index 4dc5e8d..7173c0b 100644 --- a/classes/streams/PlaylistArchiveStream.php +++ b/classes/streams/PlaylistArchiveStream.php @@ -191,6 +191,8 @@ class PlaylistArchiveStream extends ZipArchive implements StreamInterface */ public function __toString() { + $this->rewind(); + return $this->getContents(); } diff --git a/tests/ControllerTest.php b/tests/ControllerTest.php index bad16a5..d4a94f9 100644 --- a/tests/ControllerTest.php +++ b/tests/ControllerTest.php @@ -5,7 +5,6 @@ namespace Alltube\Test; -use Alltube\Controller\BaseController; use Alltube\Controller\DownloadController; use Alltube\Controller\FrontController; use Alltube\LocaleManager; @@ -43,8 +42,6 @@ abstract class ControllerTest extends BaseTest /** * Controller instance used in tests. - * - * @var BaseController */ protected $controller; diff --git a/tests/ConvertedPlaylistArchiveStreamTest.php b/tests/ConvertedPlaylistArchiveStreamTest.php new file mode 100644 index 0000000..48a7d9e --- /dev/null +++ b/tests/ConvertedPlaylistArchiveStreamTest.php @@ -0,0 +1,27 @@ +stream = new ConvertedPlaylistArchiveStream($video); + } +} diff --git a/tests/FrontControllerTest.php b/tests/FrontControllerTest.php index 2c95290..1dabf40 100644 --- a/tests/FrontControllerTest.php +++ b/tests/FrontControllerTest.php @@ -16,13 +16,6 @@ use Slim\Http\Request; */ class FrontControllerTest extends ControllerTest { - /** - * Controller instance used in tests. - * - * @var FrontController - */ - protected $controller; - /** * Prepare tests. */ diff --git a/tests/PlaylistArchiveStreamTest.php b/tests/PlaylistArchiveStreamTest.php index 6d1eb6d..dfb2af7 100644 --- a/tests/PlaylistArchiveStreamTest.php +++ b/tests/PlaylistArchiveStreamTest.php @@ -9,17 +9,10 @@ use Alltube\Stream\PlaylistArchiveStream; use Alltube\Video; /** - * Unit tests for the ViewFactory class. + * Unit tests for the PlaylistArchiveStream class. */ -class PlaylistArchiveStreamTest extends BaseTest +class PlaylistArchiveStreamTest extends StreamTest { - /** - * PlaylistArchiveStream instance. - * - * @var PlaylistArchiveStream - */ - private $stream; - /** * Prepare tests. */ @@ -31,160 +24,4 @@ class PlaylistArchiveStreamTest extends BaseTest $this->stream = new PlaylistArchiveStream($video); } - - /** - * Clean variables used in tests. - * - * @return void - */ - protected function tearDown() - { - $this->stream->close(); - } - - /** - * Test the write() function. - * - * @return void - */ - public function testWrite() - { - $this->assertNull($this->stream->write('foo')); - } - - /** - * Test the tell() function. - * - * @return void - */ - public function testTell() - { - $this->assertInternalType('int', $this->stream->tell()); - } - - /** - * Test the seek() function. - * - * @return void - */ - public function testSeek() - { - $this->stream->write('foobar'); - $this->stream->seek(3); - $this->assertEquals(3, $this->stream->tell()); - } - - /** - * Test the read() function. - * - * @return void - */ - public function testRead() - { - $result = $this->stream->read(8192); - $this->assertInternalType('string', $result); - $this->assertLessThanOrEqual(8192, strlen($result)); - } - - /** - * Test the eof() function. - * - * @return void - */ - public function testEof() - { - $this->assertFalse($this->stream->eof()); - } - - /** - * Test the getSize() function. - * - * @return void - */ - public function testGetSize() - { - $this->assertNull($this->stream->getSize()); - } - - /** - * Test the isSeekable() function. - * - * @return void - */ - public function testIsSeekable() - { - $this->assertTrue($this->stream->isSeekable()); - } - - /** - * Test the rewind() function. - * - * @return void - */ - public function testRewind() - { - $this->stream->rewind(); - $this->assertEquals(0, $this->stream->tell()); - } - - /** - * Test the isWritable() function. - * - * @return void - */ - public function testIsWritable() - { - $this->assertTrue($this->stream->isWritable()); - } - - /** - * Test the isReadable() function. - * - * @return void - */ - public function testIsReadable() - { - $this->assertTrue($this->stream->isReadable()); - } - - /** - * Test the getContents() function. - * - * @return void - */ - public function testGetContents() - { - $this->assertInternalType('string', $this->stream->getContents()); - } - - /** - * Test the getMetadata() function. - * - * @return void - */ - public function testGetMetadata() - { - $this->assertInternalType('array', $this->stream->getMetadata()); - } - - /** - * Test the detach() function. - * - * @return void - */ - public function testDetach() - { - $this->assertInternalType('resource', $this->stream->detach()); - } - - /** - * Test the __toString() function. - * - * @return void - */ - public function testToString() - { - $this->assertInternalType('string', $this->stream->__toString()); - $this->assertInternalType('string', (string) $this->stream); - } } diff --git a/tests/StreamTest.php b/tests/StreamTest.php new file mode 100644 index 0000000..d74af82 --- /dev/null +++ b/tests/StreamTest.php @@ -0,0 +1,222 @@ +stream->close(); + } + + /** + * Test the write() function. + * + * @return void + */ + public function testWrite() + { + if ($this->stream->isWritable()) { + $this->assertNull($this->stream->write('foo')); + } else { + $this->expectException(RuntimeException::class); + $this->stream->write('foo'); + } + } + + /** + * Test the tell() function. + * + * @return void + */ + public function testTell() + { + $this->assertInternalType('int', $this->stream->tell()); + } + + /** + * Test the seek() function. + * + * @return void + */ + public function testSeek() + { + if ($this->stream->isSeekable()) { + if ($this->stream->isWritable()) { + // We might need some data. + $this->stream->write('foobar'); + } + + $this->stream->seek(3); + $this->assertEquals(3, $this->stream->tell()); + } else { + $this->expectException(RuntimeException::class); + $this->stream->seek(3); + } + } + + /** + * Test the read() function. + * + * @return void + */ + public function testRead() + { + $result = $this->stream->read(8192); + $this->assertInternalType('string', $result); + $this->assertLessThanOrEqual(8192, strlen($result)); + } + + /** + * Test the read() function. + * + * @return void + */ + public function testReadEntireStream() + { + $this->markTestIncomplete('Can we test the whole logic without reading the whole stream?'); + } + + /** + * Test the eof() function. + * + * @return void + */ + public function testEof() + { + $this->assertFalse($this->stream->eof()); + } + + /** + * Test the getSize() function. + * + * @return void + */ + public function testGetSize() + { + $this->assertNull($this->stream->getSize()); + } + + /** + * Test the isSeekable() function. + * + * @return void + */ + public function testIsSeekable() + { + $this->assertInternalType('boolean', $this->stream->isSeekable()); + } + + /** + * Test the rewind() function. + * + * @return void + */ + public function testRewind() + { + if ($this->stream->isSeekable()) { + if ($this->stream->isWritable()) { + // We might need some data. + $this->stream->write('foobar'); + } + + $this->stream->rewind(); + $this->assertEquals(0, $this->stream->tell()); + } else { + $this->expectException(RuntimeException::class); + $this->stream->rewind(); + } + } + + /** + * Test the isWritable() function. + * + * @return void + */ + public function testIsWritable() + { + $this->assertInternalType('boolean', $this->stream->isWritable()); + } + + /** + * Test the isReadable() function. + * + * @return void + */ + public function testIsReadable() + { + $this->assertTrue($this->stream->isReadable()); + } + + /** + * Test the getContents() function. + * + * @return void + */ + public function testGetContents() + { + $this->assertInternalType('string', $this->stream->getContents()); + } + + /** + * Test the getMetadata() function. + * + * @return void + */ + public function testGetMetadata() + { + $this->assertInternalType('array', $this->stream->getMetadata()); + } + + /** + * Test the getMetadata() function. + * + * @return void + */ + public function testGetMetadataWithKey() + { + $this->assertInternalType('string', $this->stream->getMetadata('stream_type')); + $this->assertInternalType('string', $this->stream->getMetadata('mode')); + $this->assertInternalType('boolean', $this->stream->getMetadata('seekable')); + $this->assertNull($this->stream->getMetadata('foo')); + } + + /** + * Test the detach() function. + * + * @return void + */ + public function testDetach() + { + $this->assertInternalType('resource', $this->stream->detach()); + } + + /** + * Test the __toString() function. + * + * @return void + */ + public function testToString() + { + $this->assertInternalType('string', $this->stream->__toString()); + $this->assertInternalType('string', (string) $this->stream); + } +} diff --git a/tests/YoutubeChunkStreamTest.php b/tests/YoutubeChunkStreamTest.php new file mode 100644 index 0000000..e386bd2 --- /dev/null +++ b/tests/YoutubeChunkStreamTest.php @@ -0,0 +1,27 @@ +stream = new YoutubeChunkStream($video->getHttpResponse()); + } +} diff --git a/tests/YoutubeStreamTest.php b/tests/YoutubeStreamTest.php new file mode 100644 index 0000000..3907389 --- /dev/null +++ b/tests/YoutubeStreamTest.php @@ -0,0 +1,27 @@ +stream = new YoutubeStream($video); + } +}