alltube/tests/StreamTest.php

248 lines
5.1 KiB
PHP
Raw Normal View History

<?php
/**
* StreamTest class.
*/
namespace Alltube\Test;
use Alltube\Config;
use Alltube\Exception\ConfigException;
use Alltube\Library\Downloader;
2020-05-13 20:28:05 +00:00
use Psr\Http\Message\StreamInterface;
use RuntimeException;
/**
* Abstract class used by the stream tests.
*/
abstract class StreamTest extends BaseTest
{
/**
* Stream instance.
2020-05-13 20:28:05 +00:00
* @var StreamInterface
*/
protected $stream;
/**
* Downloader class instance.
* @var Downloader
*/
protected $downloader;
/**
* Prepare tests.
* @throws ConfigException
*/
protected function setUp(): void
{
parent::setUp();
// So ffmpeg does not spam the output with broken pipe errors.
$config = new Config(['ffmpegVerbosity' => 'fatal']);
$this->downloader = $config->getDownloader();
}
/**
* Clean variables used in tests.
*
* @return void
*/
2019-11-30 13:08:18 +00:00
protected function tearDown(): void
{
$this->stream->close();
}
/**
* Test the write() function.
*
* @return void
*/
public function testWrite()
{
if ($this->stream->isWritable()) {
2020-05-14 10:02:13 +00:00
$this->assertIsInt($this->stream->write('foo'));
} else {
$this->expectException(RuntimeException::class);
$this->stream->write('foo');
}
}
/**
* Test the tell() function.
*
* @return void
*/
public function testTell()
{
2019-11-30 13:08:18 +00:00
$this->assertIsInt($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);
2019-11-30 13:08:18 +00:00
$this->assertIsString($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()
{
2020-05-14 10:02:13 +00:00
$this->assertIsBool($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()
{
2019-11-30 13:08:18 +00:00
$this->assertIsBool($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()
{
2019-11-30 13:08:18 +00:00
$this->assertIsBool($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()
{
2019-11-30 13:08:18 +00:00
$this->assertIsString($this->stream->getContents());
}
/**
* Test the getMetadata() function.
*
* @return void
*/
public function testGetMetadata()
{
2019-11-30 13:08:18 +00:00
$this->assertIsArray($this->stream->getMetadata());
}
/**
* Test the getMetadata() function.
*
* @return void
*/
public function testGetMetadataWithKey()
{
2019-11-30 13:08:18 +00:00
$this->assertIsString($this->stream->getMetadata('stream_type'));
$this->assertIsString($this->stream->getMetadata('mode'));
$this->assertIsBool($this->stream->getMetadata('seekable'));
$this->assertNull($this->stream->getMetadata('foo'));
}
/**
* Test the detach() function.
*
* @return void
*/
public function testDetach()
{
2019-11-30 13:08:18 +00:00
$this->assertIsResource($this->stream->detach());
}
/**
* Test the __toString() function.
*
* @return void
*/
public function testToString()
{
2019-11-30 13:08:18 +00:00
$this->assertIsString($this->stream->__toString());
$this->assertIsString((string) $this->stream);
}
}