3 namespace MediaWiki\Tests\Rest
;
5 use MediaWiki\Rest\StringStream
;
8 /** @covers \MediaWiki\Rest\StringStream */
9 class StringStreamTest
extends MediaWikiTestCase
{
10 public static function provideSeekGetContents() {
12 [ 'abcde', 0, SEEK_SET
, 'abcde' ],
13 [ 'abcde', 1, SEEK_SET
, 'bcde' ],
14 [ 'abcde', 5, SEEK_SET
, '' ],
15 [ 'abcde', 1, SEEK_CUR
, 'cde' ],
16 [ 'abcde', 0, SEEK_END
, '' ],
20 /** @dataProvider provideSeekGetContents */
21 public function testCopyToStream( $input, $offset, $whence, $expected ) {
22 $ss = new StringStream
;
25 $ss->seek( $offset, $whence );
26 $destStream = fopen( 'php://memory', 'w+' );
27 $ss->copyToStream( $destStream );
28 fseek( $destStream, 0 );
29 $result = stream_get_contents( $destStream );
30 $this->assertSame( $expected, $result );
33 public function testGetSize() {
34 $ss = new StringStream
;
35 $this->assertSame( 0, $ss->getSize() );
36 $ss->write( "hello" );
37 $this->assertSame( 5, $ss->getSize() );
39 $this->assertSame( 5, $ss->getSize() );
42 public function testTell() {
43 $ss = new StringStream
;
44 $this->assertSame( $ss->tell(), 0 );
46 $this->assertSame( $ss->tell(), 3 );
49 $this->assertSame( $ss->tell(), 1 );
52 public function testEof() {
53 $ss = new StringStream( 'abc' );
54 $this->assertFalse( $ss->eof() );
56 $this->assertFalse( $ss->eof() );
58 $this->assertFalse( $ss->eof() );
60 $this->assertTrue( $ss->eof() );
62 $this->assertFalse( $ss->eof() );
65 public function testIsSeekable() {
66 $ss = new StringStream
;
67 $this->assertTrue( $ss->isSeekable() );
70 public function testIsReadable() {
71 $ss = new StringStream
;
72 $this->assertTrue( $ss->isReadable() );
75 public function testIsWritable() {
76 $ss = new StringStream
;
77 $this->assertTrue( $ss->isWritable() );
80 public function testSeekWrite() {
81 $ss = new StringStream
;
82 $this->assertSame( '', (string)$ss );
84 $this->assertSame( 'a', (string)$ss );
86 $this->assertSame( 'ab', (string)$ss );
89 $this->assertSame( 'ac', (string)$ss );
92 /** @dataProvider provideSeekGetContents */
93 public function testSeekGetContents( $input, $offset, $whence, $expected ) {
94 $ss = new StringStream( $input );
96 $ss->seek( $offset, $whence );
97 $this->assertSame( $expected, $ss->getContents() );
100 public static function provideSeekRead() {
102 [ 'abcde', 0, SEEK_SET
, 1, 'a' ],
103 [ 'abcde', 0, SEEK_SET
, 2, 'ab' ],
104 [ 'abcde', 4, SEEK_SET
, 2, 'e' ],
105 [ 'abcde', 5, SEEK_SET
, 1, '' ],
106 [ 'abcde', 1, SEEK_CUR
, 1, 'c' ],
107 [ 'abcde', 0, SEEK_END
, 1, '' ],
108 [ 'abcde', -1, SEEK_END
, 1, 'e' ],
112 /** @dataProvider provideSeekRead */
113 public function testSeekRead( $input, $offset, $whence, $length, $expected ) {
114 $ss = new StringStream( $input );
116 $ss->seek( $offset, $whence );
117 $this->assertSame( $expected, $ss->read( $length ) );
120 /** @expectedException \InvalidArgumentException */
121 public function testReadBeyondEnd() {
122 $ss = new StringStream( 'abc' );
123 $ss->seek( 1, SEEK_END
);
126 /** @expectedException \InvalidArgumentException */
127 public function testReadBeforeStart() {
128 $ss = new StringStream( 'abc' );