Add seekable wrapper to make non seekable streams seekable

This commit is contained in:
Robin Appelman 2015-10-16 15:16:24 +02:00
commit d31237bc40
2 changed files with 144 additions and 0 deletions

52
tests/SeekableWrapper.php Normal file
View file

@ -0,0 +1,52 @@
<?php
/**
* Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
* This file is licensed under the Licensed under the MIT license:
* http://opensource.org/licenses/MIT
*/
namespace Icewind\Streams\Tests;
class SeekableWrapper extends \PHPUnit_Framework_TestCase {
/**
* @param resource $source
* @return resource
*/
protected function wrapSource($source) {
return \Icewind\Streams\SeekableWrapper::wrap($source);
}
protected function getSource() {
$source = fopen('php://temp', 'w+');
fwrite($source, 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.');
fseek($source, 0);
return $source;
}
public function testCantWrapDir() {
$source = opendir(__DIR__);
$this->assertFalse(@$this->wrapSource($source));
}
public function testSourceNotSeeked() {
$source = $this->getSource();
$wrapped = $this->wrapSource($source);
fseek($wrapped, 6);
$this->assertEquals(6, ftell($source));
$this->assertEquals(6, ftell($wrapped));
$this->assertEquals('ipsum', fread($wrapped, '5'));
fseek($wrapped, 6);
$this->assertEquals(6, ftell($wrapped));
$this->assertGreaterThan(6, ftell($source));
}
public function testSeekRelative() {
$source = $this->getSource();
$wrapped = $this->wrapSource($source);
fseek($wrapped, 6);
fseek($wrapped, 6, SEEK_CUR);
$this->assertEquals(12, ftell($source));
$this->assertEquals(12, ftell($wrapped));
$this->assertEquals('dolor', fread($wrapped, '5'));
}
}