This changeset resumes work on T89432 and related tickets by porting an initial set of tests to the new unit test suite separated out in I69b92db3e70093570e05cc0a64c7780a278b321a. The tests were only ported if they worked immediately without requiring any changes other than changing the test case class to MediaWikiUnitTestCase and moving the test to the new suite. If a test failed for any reason (even trivial misconfiguration), it was NOT ported. With this change, the unit tests suite now consits of a total of 455 tests. As before, you can run these tests via the following command: $ composer phpunit:unit Bug: T84948 Bug: T89432 Bug: T87781 Change-Id: Ibb8175981092d7f41864e641cc3c118af70a5c76
130 lines
3.4 KiB
PHP
130 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace MediaWiki\Tests\Rest;
|
|
|
|
use MediaWiki\Rest\StringStream;
|
|
|
|
/** @covers \MediaWiki\Rest\StringStream */
|
|
class StringStreamTest extends \MediaWikiUnitTestCase {
|
|
public static function provideSeekGetContents() {
|
|
return [
|
|
[ 'abcde', 0, SEEK_SET, 'abcde' ],
|
|
[ 'abcde', 1, SEEK_SET, 'bcde' ],
|
|
[ 'abcde', 5, SEEK_SET, '' ],
|
|
[ 'abcde', 1, SEEK_CUR, 'cde' ],
|
|
[ 'abcde', 0, SEEK_END, '' ],
|
|
];
|
|
}
|
|
|
|
/** @dataProvider provideSeekGetContents */
|
|
public function testCopyToStream( $input, $offset, $whence, $expected ) {
|
|
$ss = new StringStream;
|
|
$ss->write( $input );
|
|
$ss->seek( 1 );
|
|
$ss->seek( $offset, $whence );
|
|
$destStream = fopen( 'php://memory', 'w+' );
|
|
$ss->copyToStream( $destStream );
|
|
fseek( $destStream, 0 );
|
|
$result = stream_get_contents( $destStream );
|
|
$this->assertSame( $expected, $result );
|
|
}
|
|
|
|
public function testGetSize() {
|
|
$ss = new StringStream;
|
|
$this->assertSame( 0, $ss->getSize() );
|
|
$ss->write( "hello" );
|
|
$this->assertSame( 5, $ss->getSize() );
|
|
$ss->rewind();
|
|
$this->assertSame( 5, $ss->getSize() );
|
|
}
|
|
|
|
public function testTell() {
|
|
$ss = new StringStream;
|
|
$this->assertSame( $ss->tell(), 0 );
|
|
$ss->write( "abc" );
|
|
$this->assertSame( $ss->tell(), 3 );
|
|
$ss->seek( 0 );
|
|
$ss->read( 1 );
|
|
$this->assertSame( $ss->tell(), 1 );
|
|
}
|
|
|
|
public function testEof() {
|
|
$ss = new StringStream( 'abc' );
|
|
$this->assertFalse( $ss->eof() );
|
|
$ss->read( 1 );
|
|
$this->assertFalse( $ss->eof() );
|
|
$ss->read( 1 );
|
|
$this->assertFalse( $ss->eof() );
|
|
$ss->read( 1 );
|
|
$this->assertTrue( $ss->eof() );
|
|
$ss->rewind();
|
|
$this->assertFalse( $ss->eof() );
|
|
}
|
|
|
|
public function testIsSeekable() {
|
|
$ss = new StringStream;
|
|
$this->assertTrue( $ss->isSeekable() );
|
|
}
|
|
|
|
public function testIsReadable() {
|
|
$ss = new StringStream;
|
|
$this->assertTrue( $ss->isReadable() );
|
|
}
|
|
|
|
public function testIsWritable() {
|
|
$ss = new StringStream;
|
|
$this->assertTrue( $ss->isWritable() );
|
|
}
|
|
|
|
public function testSeekWrite() {
|
|
$ss = new StringStream;
|
|
$this->assertSame( '', (string)$ss );
|
|
$ss->write( 'a' );
|
|
$this->assertSame( 'a', (string)$ss );
|
|
$ss->write( 'b' );
|
|
$this->assertSame( 'ab', (string)$ss );
|
|
$ss->seek( 1 );
|
|
$ss->write( 'c' );
|
|
$this->assertSame( 'ac', (string)$ss );
|
|
}
|
|
|
|
/** @dataProvider provideSeekGetContents */
|
|
public function testSeekGetContents( $input, $offset, $whence, $expected ) {
|
|
$ss = new StringStream( $input );
|
|
$ss->seek( 1 );
|
|
$ss->seek( $offset, $whence );
|
|
$this->assertSame( $expected, $ss->getContents() );
|
|
}
|
|
|
|
public static function provideSeekRead() {
|
|
return [
|
|
[ 'abcde', 0, SEEK_SET, 1, 'a' ],
|
|
[ 'abcde', 0, SEEK_SET, 2, 'ab' ],
|
|
[ 'abcde', 4, SEEK_SET, 2, 'e' ],
|
|
[ 'abcde', 5, SEEK_SET, 1, '' ],
|
|
[ 'abcde', 1, SEEK_CUR, 1, 'c' ],
|
|
[ 'abcde', 0, SEEK_END, 1, '' ],
|
|
[ 'abcde', -1, SEEK_END, 1, 'e' ],
|
|
];
|
|
}
|
|
|
|
/** @dataProvider provideSeekRead */
|
|
public function testSeekRead( $input, $offset, $whence, $length, $expected ) {
|
|
$ss = new StringStream( $input );
|
|
$ss->seek( 1 );
|
|
$ss->seek( $offset, $whence );
|
|
$this->assertSame( $expected, $ss->read( $length ) );
|
|
}
|
|
|
|
/** @expectedException \InvalidArgumentException */
|
|
public function testReadBeyondEnd() {
|
|
$ss = new StringStream( 'abc' );
|
|
$ss->seek( 1, SEEK_END );
|
|
}
|
|
|
|
/** @expectedException \InvalidArgumentException */
|
|
public function testReadBeforeStart() {
|
|
$ss = new StringStream( 'abc' );
|
|
$ss->seek( -1 );
|
|
}
|
|
}
|