Debian Codegraph @

tests/phpunit/unit/includes/Rest/StringStreamTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Rest;
4
5 use InvalidArgumentException;
6 use MediaWiki\Rest\StringStream;
7 use MediaWikiUnitTestCase;
8
9 /** @covers \MediaWiki\Rest\StringStream */
10 class StringStreamTest extends MediaWikiUnitTestCase {
11 public static function provideSeekGetContents() {
12 return [
13 [ 'abcde', 0, SEEK_SET, 'abcde' ],
14 [ 'abcde', 1, SEEK_SET, 'bcde' ],
15 [ 'abcde', 5, SEEK_SET, '' ],
16 [ 'abcde', 1, SEEK_CUR, 'cde' ],
17 [ 'abcde', 0, SEEK_END, '' ],
18 ];
19 }
20
21 /** @dataProvider provideSeekGetContents */
22 public function testCopyToStream( $input, $offset, $whence, $expected ) {
23 $ss = new StringStream;
24 $ss->write( $input );
25 $ss->seek( 1 );
26 $ss->seek( $offset, $whence );
27 $destStream = fopen( 'php://memory', 'w+' );
28 $ss->copyToStream( $destStream );
29 fseek( $destStream, 0 );
30 $result = stream_get_contents( $destStream );
31 $this->assertSame( $expected, $result );
32 }
33
34 public function testGetSize() {
35 $ss = new StringStream;
36 $this->assertSame( 0, $ss->getSize() );
37 $ss->write( "hello" );
38 $this->assertSame( 5, $ss->getSize() );
39 $ss->rewind();
40 $this->assertSame( 5, $ss->getSize() );
41 }
42
43 public function testTell() {
44 $ss = new StringStream;
45 $this->assertSame( 0, $ss->tell() );
46 $ss->write( "abc" );
47 $this->assertSame( 3, $ss->tell() );
48 $ss->seek( 0 );
49 $ss->read( 1 );
50 $this->assertSame( 1, $ss->tell() );
51 }
52
53 public function testEof() {
54 $ss = new StringStream( 'abc' );
55 $this->assertFalse( $ss->eof() );
56 $ss->read( 1 );
57 $this->assertFalse( $ss->eof() );
58 $ss->read( 1 );
59 $this->assertFalse( $ss->eof() );
60 $ss->read( 1 );
61 $this->assertTrue( $ss->eof() );
62 $ss->rewind();
63 $this->assertFalse( $ss->eof() );
64 }
65
66 public function testIsSeekable() {
67 $ss = new StringStream;
68 $this->assertTrue( $ss->isSeekable() );
69 }
70
71 public function testIsReadable() {
72 $ss = new StringStream;
73 $this->assertTrue( $ss->isReadable() );
74 }
75
76 public function testIsWritable() {
77 $ss = new StringStream;
78 $this->assertTrue( $ss->isWritable() );
79 }
80
81 public function testSeekWrite() {
82 $ss = new StringStream;
83 $this->assertSame( '', (string)$ss );
84 $ss->write( 'a' );
85 $this->assertSame( 'a', (string)$ss );
86 $ss->write( 'b' );
87 $this->assertSame( 'ab', (string)$ss );
88 $ss->seek( 1 );
89 $ss->write( 'c' );
90 $this->assertSame( 'ac', (string)$ss );
91 }
92
93 /** @dataProvider provideSeekGetContents */
94 public function testSeekGetContents( $input, $offset, $whence, $expected ) {
95 $ss = new StringStream( $input );
96 $ss->seek( 1 );
97 $ss->seek( $offset, $whence );
98 $this->assertSame( $expected, $ss->getContents() );
99 }
100
101 public static function provideSeekRead() {
102 return [
103 [ 'abcde', 0, SEEK_SET, 1, 'a' ],
104 [ 'abcde', 0, SEEK_SET, 2, 'ab' ],
105 [ 'abcde', 4, SEEK_SET, 2, 'e' ],
106 [ 'abcde', 5, SEEK_SET, 1, '' ],
107 [ 'abcde', 1, SEEK_CUR, 1, 'c' ],
108 [ 'abcde', 0, SEEK_END, 1, '' ],
109 [ 'abcde', -1, SEEK_END, 1, 'e' ],
110 ];
111 }
112
113 /** @dataProvider provideSeekRead */
114 public function testSeekRead( $input, $offset, $whence, $length, $expected ) {
115 $ss = new StringStream( $input );
116 $ss->seek( 1 );
117 $ss->seek( $offset, $whence );
118 $this->assertSame( $expected, $ss->read( $length ) );
119 }
120
121 public function testReadBeyondEnd() {
122 $ss = new StringStream( 'abc' );
123 $this->expectException( InvalidArgumentException::class );
124 $ss->seek( 1, SEEK_END );
125 }
126
127 public function testReadBeforeStart() {
128 $ss = new StringStream( 'abc' );
129 $this->expectException( InvalidArgumentException::class );
130 $ss->seek( -1 );
131 }
132 }