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