3 namespace MediaWiki\Tests\Rest
;
5 use MediaWiki\Rest\StringStream
;
7 /** @covers \MediaWiki\Rest\StringStream */
8 class StringStreamTest
extends \MediaWikiUnitTestCase
{
9 public static function provideSeekGetContents() {
11 [ 'abcde', 0, SEEK_SET
, 'abcde' ],
12 [ 'abcde', 1, SEEK_SET
, 'bcde' ],
13 [ 'abcde', 5, SEEK_SET
, '' ],
14 [ 'abcde', 1, SEEK_CUR
, 'cde' ],
15 [ 'abcde', 0, SEEK_END
, '' ],
19 /** @dataProvider provideSeekGetContents */
20 public function testCopyToStream( $input, $offset, $whence, $expected ) {
21 $ss = new StringStream
;
24 $ss->seek( $offset, $whence );
25 $destStream = fopen( 'php://memory', 'w+' );
26 $ss->copyToStream( $destStream );
27 fseek( $destStream, 0 );
28 $result = stream_get_contents( $destStream );
29 $this->assertSame( $expected, $result );
32 public function testGetSize() {
33 $ss = new StringStream
;
34 $this->assertSame( 0, $ss->getSize() );
35 $ss->write( "hello" );
36 $this->assertSame( 5, $ss->getSize() );
38 $this->assertSame( 5, $ss->getSize() );
41 public function testTell() {
42 $ss = new StringStream
;
43 $this->assertSame( $ss->tell(), 0 );
45 $this->assertSame( $ss->tell(), 3 );
48 $this->assertSame( $ss->tell(), 1 );
51 public function testEof() {
52 $ss = new StringStream( 'abc' );
53 $this->assertFalse( $ss->eof() );
55 $this->assertFalse( $ss->eof() );
57 $this->assertFalse( $ss->eof() );
59 $this->assertTrue( $ss->eof() );
61 $this->assertFalse( $ss->eof() );
64 public function testIsSeekable() {
65 $ss = new StringStream
;
66 $this->assertTrue( $ss->isSeekable() );
69 public function testIsReadable() {
70 $ss = new StringStream
;
71 $this->assertTrue( $ss->isReadable() );
74 public function testIsWritable() {
75 $ss = new StringStream
;
76 $this->assertTrue( $ss->isWritable() );
79 public function testSeekWrite() {
80 $ss = new StringStream
;
81 $this->assertSame( '', (string)$ss );
83 $this->assertSame( 'a', (string)$ss );
85 $this->assertSame( 'ab', (string)$ss );
88 $this->assertSame( 'ac', (string)$ss );
91 /** @dataProvider provideSeekGetContents */
92 public function testSeekGetContents( $input, $offset, $whence, $expected ) {
93 $ss = new StringStream( $input );
95 $ss->seek( $offset, $whence );
96 $this->assertSame( $expected, $ss->getContents() );
99 public static function provideSeekRead() {
101 [ 'abcde', 0, SEEK_SET
, 1, 'a' ],
102 [ 'abcde', 0, SEEK_SET
, 2, 'ab' ],
103 [ 'abcde', 4, SEEK_SET
, 2, 'e' ],
104 [ 'abcde', 5, SEEK_SET
, 1, '' ],
105 [ 'abcde', 1, SEEK_CUR
, 1, 'c' ],
106 [ 'abcde', 0, SEEK_END
, 1, '' ],
107 [ 'abcde', -1, SEEK_END
, 1, 'e' ],
111 /** @dataProvider provideSeekRead */
112 public function testSeekRead( $input, $offset, $whence, $length, $expected ) {
113 $ss = new StringStream( $input );
115 $ss->seek( $offset, $whence );
116 $this->assertSame( $expected, $ss->read( $length ) );
119 /** @expectedException \InvalidArgumentException */
120 public function testReadBeyondEnd() {
121 $ss = new StringStream( 'abc' );
122 $ss->seek( 1, SEEK_END
);
125 /** @expectedException \InvalidArgumentException */
126 public function testReadBeforeStart() {
127 $ss = new StringStream( 'abc' );