2019-04-22 19:52:21 +00:00
|
|
|
<?php
|
2019-10-03 19:24:12 +00:00
|
|
|
|
2019-04-22 19:52:21 +00:00
|
|
|
/**
|
|
|
|
* StreamTest class.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Alltube\Test;
|
|
|
|
|
2020-10-17 20:07:07 +00:00
|
|
|
use Alltube\Config;
|
|
|
|
use Alltube\Exception\ConfigException;
|
|
|
|
use Alltube\Library\Downloader;
|
2020-05-13 20:28:05 +00:00
|
|
|
use Psr\Http\Message\StreamInterface;
|
2019-04-22 19:52:21 +00:00
|
|
|
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
|
2019-04-22 19:52:21 +00:00
|
|
|
*/
|
|
|
|
protected $stream;
|
|
|
|
|
2020-10-17 20:07:07 +00:00
|
|
|
/**
|
|
|
|
* 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();
|
|
|
|
}
|
|
|
|
|
2019-04-22 19:52:21 +00:00
|
|
|
/**
|
|
|
|
* Clean variables used in tests.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2019-11-30 13:08:18 +00:00
|
|
|
protected function tearDown(): void
|
2019-04-22 19:52:21 +00:00
|
|
|
{
|
|
|
|
$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'));
|
2019-04-22 19:52:21 +00:00
|
|
|
} 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());
|
2019-04-22 19:52:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
2019-04-22 19:52:21 +00:00
|
|
|
$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());
|
2019-04-22 19:52:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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());
|
2019-04-22 19:52:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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());
|
2019-04-22 19:52:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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());
|
2019-04-22 19:52:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the getMetadata() function.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testGetMetadata()
|
|
|
|
{
|
2019-11-30 13:08:18 +00:00
|
|
|
$this->assertIsArray($this->stream->getMetadata());
|
2019-04-22 19:52:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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'));
|
2019-04-22 19:52:21 +00:00
|
|
|
$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());
|
2019-04-22 19:52:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
2019-04-22 19:52:21 +00:00
|
|
|
}
|
|
|
|
}
|