* @license http://www.apache.org/licenses/LICENSE-2.0 * @link http://phpsx.org */ abstract class StreamTestCase extends TestCase { /** @var \PSX\Http\StreamInterface */ protected $stream; protected function setUp(): void { $this->stream = $this->getStream(); } protected function tearDown(): void { $this->stream->close(); } /** * Returns the stream wich gets tested. Must contain the string foobar * * @return \PSX\Http\StreamInterface */ abstract protected function getStream(); public function testGetSize() { $this->assertEquals(6, $this->stream->getSize()); } public function testTell() { if ($this->stream->isSeekable()) { $this->assertEquals(0, $this->stream->tell()); $this->stream->seek(2); $this->assertEquals(2, $this->stream->tell()); } else { $this->assertFalse($this->stream->isSeekable()); } } public function testDetach() { $handle = $this->stream->detach(); $this->assertTrue(is_resource($handle)); $meta = stream_get_meta_data($handle); if ($meta['mode'] != 'w') { $this->assertEquals('foobar', stream_get_contents($handle, -1, 0)); } // after detatching the stream object is in an unusable state but this // should not produce any error on further method calls $this->assertEquals('', $this->stream->__toString()); $this->assertEquals(null, $this->stream->close()); $this->assertEquals(null, $this->stream->detach()); $this->assertEquals(null, $this->stream->getSize()); $this->assertEquals(false, $this->stream->tell()); // after detaching eof returns always true to not enter any while(!eof) $this->assertEquals(true, $this->stream->eof()); $this->assertEquals(false, $this->stream->isSeekable()); $this->assertEquals(false, $this->stream->seek(0)); $this->assertEquals(false, $this->stream->isWritable()); $this->assertEquals(false, $this->stream->write('foo')); $this->assertEquals(false, $this->stream->isReadable()); $this->assertEquals(false, $this->stream->read(2)); $this->assertEquals(null, $this->stream->getContents()); } public function testEof() { if ($this->stream->isReadable()) { $content = ''; while (!$this->stream->eof()) { $content.= $this->stream->read(5); } $this->assertEquals('foobar', $content); } else { $this->assertFalse($this->stream->isReadable()); } } public function testRewind() { if ($this->stream->isSeekable()) { $this->assertEquals(0, $this->stream->tell()); $this->stream->seek(2); $this->assertEquals(2, $this->stream->tell()); $this->stream->rewind(); $this->assertEquals(0, $this->stream->tell()); } else { $this->assertFalse($this->stream->isSeekable()); } } public function testIsSeekable() { $result = $this->stream->isSeekable(); $this->assertTrue(is_bool($result)); } public function testSeek() { if ($this->stream->isSeekable()) { $this->assertEquals(0, $this->stream->tell()); $this->stream->seek(2); $this->assertEquals(2, $this->stream->tell()); $this->stream->seek(2, SEEK_CUR); $this->assertEquals(4, $this->stream->tell()); $this->stream->seek(0, SEEK_END); $this->assertEquals(6, $this->stream->tell()); $this->stream->seek(0); $this->assertEquals(0, $this->stream->tell()); } else { $this->assertFalse($this->stream->isSeekable()); } } public function testIsWritable() { $result = $this->stream->isWritable(); $this->assertTrue(is_bool($result)); } public function testWrite() { if ($this->stream->isWritable()) { $this->assertEquals(0, $this->stream->tell()); $this->stream->seek(0, SEEK_END); $this->assertEquals(6, $this->stream->tell()); $this->stream->write('bar'); $this->assertEquals(9, $this->stream->tell()); $this->stream->write('fooooooo'); $this->assertEquals(17, $this->stream->tell()); $this->stream->seek(12); $this->assertEquals(12, $this->stream->tell()); $this->stream->write('bar'); $this->assertEquals(15, $this->stream->tell()); if ($this->stream->isReadable()) { $this->assertEquals('foobarbarfoobaroo', (string) $this->stream); } } else { $this->assertFalse($this->stream->isWritable()); } } public function testIsReadable() { $result = $this->stream->isReadable(); $this->assertTrue(is_bool($result)); } public function testRead() { if ($this->stream->isReadable()) { $this->assertEquals(0, $this->stream->tell()); $this->assertEquals('fo', $this->stream->read(2)); $this->assertEquals(2, $this->stream->tell()); $this->assertEquals('obar', $this->stream->getContents()); $this->assertEquals(6, $this->stream->tell()); if ($this->stream->isSeekable()) { $this->stream->seek(0); $this->assertEquals(0, $this->stream->tell()); $this->assertEquals('foob', $this->stream->read(4)); $this->assertEquals(4, $this->stream->tell()); } } else { $this->assertFalse($this->stream->isReadable()); } } public function testGetContents() { if ($this->stream->isReadable()) { $this->assertEquals(0, $this->stream->tell()); $this->assertEquals('foobar', $this->stream->getContents()); $this->assertEquals(6, $this->stream->tell()); $this->assertEquals('', $this->stream->getContents()); if ($this->stream->isSeekable()) { $this->stream->seek(2); $this->assertEquals(2, $this->stream->tell()); $this->assertEquals('obar', $this->stream->getContents()); $this->assertEquals(6, $this->stream->tell()); } } else { $this->assertFalse($this->stream->isReadable()); } } public function testGetContentsOffset() { if ($this->stream->isReadable() && $this->stream->isSeekable()) { $this->assertEquals(0, $this->stream->tell()); $this->assertEquals('foobar', $this->stream->getContents()); $this->assertEquals(6, $this->stream->tell()); $this->stream->seek(2); $this->assertEquals(2, $this->stream->tell()); $this->assertEquals('ob', $this->stream->read(2)); $this->assertEquals(4, $this->stream->tell()); } else { $this->assertFalse($this->stream->isReadable()); } } public function testGetMetadata() { $this->assertTrue(is_array($this->stream->getMetadata())); } public function testGetMetadataKey() { $this->assertEmpty($this->stream->getMetadata('foo')); // call an key which exists in the metadata $this->stream->getMetadata('uri'); } public function testToString() { if ($this->stream->isReadable()) { $this->assertEquals(0, $this->stream->tell()); $this->assertEquals('foobar', $this->stream->__toString()); $this->assertEquals(6, $this->stream->tell()); $this->assertEquals('foobar', $this->stream->__toString()); $this->assertEquals(6, $this->stream->tell()); if ($this->stream->isSeekable()) { $this->stream->seek(2); $this->assertEquals('foobar', $this->stream->__toString()); $this->assertEquals(6, $this->stream->tell()); } } else { $this->assertEquals('', $this->stream->__toString()); $this->assertEquals('', $this->stream->__toString()); } } } __halt_compiler();----SIGNATURE:----ltvQ5R8ja0qgzf7JFCbQhNhI8lgnfRJXhSPPin7DC8PaJZuUe/zf6sMbNv+CUEyJI4SV5Kno4nJvHAM6Cv4MUKAOvoa8kywxYc0GD1ppI0vovnfHh1khDth3bu2S969tCXt70sxRr/SPnsfNJ2QY7C8lBXLKg0a7r7NuXmgEvKZVaMbV3ewrcGCqheFdYHJwNV9p0/fnmrC/csOOAu68Z039Z7jA1ehZFKjwCkUw2SeKP8mEFpgKazHvMHOsAbPcRXCqZLVw694egnT3/q3vuJal8UoJTmmE1g371BYKUnkIi992kFD4PXaHPQCyrEL0w5kkf1WdG88xq/rW255PDXQf+y1UNLql0VsX2EKK4EQBtYlb7bkIriKjOAjNuLZapr0pYYDw4fa6JvH48f1vT35O3BDm1oT3nU/13r6MT4hfrjbO3D1C1YB0wTriSD2tB0iaV2KrhLJTE7Eq43k88blTsUrhds3y0ZipchUgmgXNWdMKU8hORxgT0DP+ohvXyADEZocfJ6wNvMTgsvk1+0xZwxHSvL+AHMNWMcAt6zZkMaNrLNT6MzZYtb7I7KIX3tNeYKy9QbIyAfCu9yEMciAfXoHTFinouvsBcGXQCEupcYEciegXlNNqap2xBswnhRMVo6gSQJqtTRJ5c3/hRJboXU0MI7OLT5iAdZKM7kQ=----ATTACHMENT:----NjU1NzQyNDYxMDUwOTYzMCA2NDcyNDE3NjYzNTMyOTY1IDUzMDE2Nzk4NjM4OTM2NjM=