*/ use oat\generis\test\TestCase; class StreamRangeTest extends TestCase { /** * @dataProvider rangesProvider */ public function testConstruct($stream, $rangeValue, $first, $last) { $range = new StreamRange($stream, $rangeValue); $this->assertEquals($first, $range->getFirstPos()); $this->assertEquals($last, $range->getLastPos()); } /** * @dataProvider wrongRangesProvider * @param $stream * @param $rangeValue * @throws StreamRangeException */ public function testConstructExcept($stream, $rangeValue) { $this->expectException(StreamRangeException::class); $range = new StreamRange($stream, $rangeValue); } public function wrongRangesProvider() { return [ [ 'stream' => $this->getStream('0123456789'), 'range' => '0-10', //last byte more than length ], [ 'stream' => $this->getStream('0123456789'), 'range' => '-11', //offset from the end more than length ], [ 'stream' => $this->getStream('0123456789'), 'range' => '10-', //first pos more than length ], [ 'stream' => $this->getStream('0123456789'), 'range' => '10-11', //first pos more than length ], [ 'stream' => $this->getStream('0123456789'), 'range' => '10-1', //first pos less than last ], ]; } /** * @return array */ public function rangesProvider() { return [ [ 'stream' => $this->getStream('0123456789'), 'range' => '0-9', 'first' => 0, 'last' => 9 ], [ 'stream' => $this->getStream('0123456789'), 'range' => '0-5', 'first' => 0, 'last' => 5 ], [ 'stream' => $this->getStream('0123456789'), 'range' => '5-9', 'first' => 5, 'last' => 9 ], [ 'stream' => $this->getStream('0123456789'), 'range' => '3-', 'first' => 3, 'last' => 9 ], [ 'stream' => $this->getStream('0123456789'), 'range' => '-3', 'first' => 7, 'last' => 9 ], [ 'stream' => $this->getStream('0123456789'), 'range' => '0-0', 'first' => 0, 'last' => 0 ], ]; } /** * @return Stream */ private function getStream($string) { $resource = fopen('php://memory', 'r+'); fwrite($resource, $string); rewind($resource); return new Stream($resource); } }