f4746438797d2dde406a07ae0cb92a4026d3f4d5
[lhc/web/wiklou.git] / tests / phpunit / includes / Rest / StringStreamTest.php
1 <?php
2
3 namespace MediaWiki\Tests\Rest;
4
5 use MediaWiki\Rest\StringStream;
6 use MediaWikiTestCase;
7
8 /** @covers \MediaWiki\Rest\StringStream */
9 class StringStreamTest extends MediaWikiTestCase {
10 public static function provideSeekGetContents() {
11 return [
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, '' ],
17 ];
18 }
19
20 /** @dataProvider provideSeekGetContents */
21 public function testCopyToStream( $input, $offset, $whence, $expected ) {
22 $ss = new StringStream;
23 $ss->write( $input );
24 $ss->seek( 1 );
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 );
31 }
32
33 public function testGetSize() {
34 $ss = new StringStream;
35 $this->assertSame( 0, $ss->getSize() );
36 $ss->write( "hello" );
37 $this->assertSame( 5, $ss->getSize() );
38 $ss->rewind();
39 $this->assertSame( 5, $ss->getSize() );
40 }
41
42 public function testTell() {
43 $ss = new StringStream;
44 $this->assertSame( $ss->tell(), 0 );
45 $ss->write( "abc" );
46 $this->assertSame( $ss->tell(), 3 );
47 $ss->seek( 0 );
48 $ss->read( 1 );
49 $this->assertSame( $ss->tell(), 1 );
50 }
51
52 public function testEof() {
53 $ss = new StringStream( 'abc' );
54 $this->assertFalse( $ss->eof() );
55 $ss->read( 1 );
56 $this->assertFalse( $ss->eof() );
57 $ss->read( 1 );
58 $this->assertFalse( $ss->eof() );
59 $ss->read( 1 );
60 $this->assertTrue( $ss->eof() );
61 $ss->rewind();
62 $this->assertFalse( $ss->eof() );
63 }
64
65 public function testIsSeekable() {
66 $ss = new StringStream;
67 $this->assertTrue( $ss->isSeekable() );
68 }
69
70 public function testIsReadable() {
71 $ss = new StringStream;
72 $this->assertTrue( $ss->isReadable() );
73 }
74
75 public function testIsWritable() {
76 $ss = new StringStream;
77 $this->assertTrue( $ss->isWritable() );
78 }
79
80 public function testSeekWrite() {
81 $ss = new StringStream;
82 $this->assertSame( '', (string)$ss );
83 $ss->write( 'a' );
84 $this->assertSame( 'a', (string)$ss );
85 $ss->write( 'b' );
86 $this->assertSame( 'ab', (string)$ss );
87 $ss->seek( 1 );
88 $ss->write( 'c' );
89 $this->assertSame( 'ac', (string)$ss );
90 }
91
92 /** @dataProvider provideSeekGetContents */
93 public function testSeekGetContents( $input, $offset, $whence, $expected ) {
94 $ss = new StringStream( $input );
95 $ss->seek( 1 );
96 $ss->seek( $offset, $whence );
97 $this->assertSame( $expected, $ss->getContents() );
98 }
99
100 public static function provideSeekRead() {
101 return [
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' ],
109 ];
110 }
111
112 /** @dataProvider provideSeekRead */
113 public function testSeekRead( $input, $offset, $whence, $length, $expected ) {
114 $ss = new StringStream( $input );
115 $ss->seek( 1 );
116 $ss->seek( $offset, $whence );
117 $this->assertSame( $expected, $ss->read( $length ) );
118 }
119
120 /** @expectedException \InvalidArgumentException */
121 public function testReadBeyondEnd() {
122 $ss = new StringStream( 'abc' );
123 $ss->seek( 1, SEEK_END );
124 }
125
126 /** @expectedException \InvalidArgumentException */
127 public function testReadBeforeStart() {
128 $ss = new StringStream( 'abc' );
129 $ss->seek( -1 );
130 }
131 }