Debian Codegraph @

tests/phpunit/unit/includes/libs/ParamValidator/Util/UploadedFileStreamTest.php

raw symbols SCIP
1 <?php
2
3 namespace Wikimedia\Tests\ParamValidator\Util;
4
5 require_once __DIR__ . '/UploadedFileTestBase.php';
6
7 use Error;
8 use PHPUnit\Framework\AssertionFailedError;
9 use RuntimeException;
10 use TypeError;
11 use Wikimedia\ParamValidator\Util\UploadedFileStream;
12 use Wikimedia\TestingAccessWrapper;
13
14 /**
15 * @covers \Wikimedia\ParamValidator\Util\UploadedFileStream
16 */
17 class UploadedFileStreamTest extends UploadedFileTestBase {
18
19 public function testConstruct_doesNotExist() {
20 $filename = $this->makeTemp( __FUNCTION__ );
21 unlink( $filename );
22
23 $this->assertFileDoesNotExist( $filename, 'Non existence check' );
24 $this->expectException( RuntimeException::class );
25 $this->expectExceptionMessage( "Failed to open file:" );
26 $stream = new UploadedFileStream( $filename );
27 }
28
29 public function testConstruct_notReadable() {
30 if ( wfIsWindows() ) {
31 $this->markTestSkipped( "Skip test, since chmod does not work on windows" );
32 }
33
34 $filename = $this->makeTemp( __FUNCTION__ );
35
36 chmod( $filename, 0000 );
37 $this->expectException( RuntimeException::class );
38 $this->expectExceptionMessage( "Failed to open file:" );
39 $stream = new UploadedFileStream( $filename );
40 }
41
42 public function testCloseOnDestruct() {
43 $filename = $this->makeTemp( __FUNCTION__ );
44 $stream = new UploadedFileStream( $filename );
45 $fp = TestingAccessWrapper::newFromObject( $stream )->fp;
46 $this->assertSame( 'f', fread( $fp, 1 ), 'read check' );
47 unset( $stream );
48 try {
49 // PHP 7 raises warnings
50 $this->assertFalse( @fread( $fp, 1 ) );
51 } catch ( TypeError $ex ) {
52 // PHP 8 throws
53 }
54 }
55
56 public function testToString() {
57 $filename = $this->makeTemp( __FUNCTION__ );
58 $stream = new UploadedFileStream( $filename );
59
60 // Always starts at the start of the stream
61 $stream->seek( 3 );
62 $this->assertSame( 'foobar', (string)$stream );
63
64 // No exception when closed
65 $stream->close();
66 $this->assertSame( '', (string)$stream );
67 }
68
69 public function testToString_Error() {
70 if ( !class_exists( Error::class ) ) {
71 $this->markTestSkipped( 'No PHP Error class' );
72 }
73
74 // ... Yeah
75 $filename = $this->makeTemp( __FUNCTION__ );
76 $stream = $this->getMockBuilder( UploadedFileStream::class )
77 ->setConstructorArgs( [ $filename ] )
78 ->onlyMethods( [ 'getContents' ] )
79 ->getMock();
80 $stream->method( 'getContents' )->willReturnCallback( static function () {
81 throw new Error( 'Bogus' );
82 } );
83 $this->assertSame( '', (string)$stream );
84 }
85
86 public function testClose() {
87 $filename = $this->makeTemp( __FUNCTION__ );
88 $stream = new UploadedFileStream( $filename );
89
90 $stream->close();
91
92 // Second call doesn't error
93 $stream->close();
94 }
95
96 public function testDetach() {
97 $filename = $this->makeTemp( __FUNCTION__ );
98 $stream = new UploadedFileStream( $filename );
99
100 // We got the file descriptor
101 $fp = $stream->detach();
102 $this->assertNotNull( $fp );
103 $this->assertSame( 'f', fread( $fp, 1 ) );
104
105 // Stream operations now fail.
106 try {
107 $stream->seek( 0 );
108 } catch ( AssertionFailedError $ex ) {
109 throw $ex;
110 } catch ( RuntimeException $ex ) {
111 }
112
113 // Stream close doesn't affect the file descriptor
114 $stream->close();
115 $this->assertSame( 'o', fread( $fp, 1 ) );
116
117 // Stream destruction doesn't affect the file descriptor
118 unset( $stream );
119 $this->assertSame( 'o', fread( $fp, 1 ) );
120
121 // On a closed stream, we don't get a file descriptor
122 $stream = new UploadedFileStream( $filename );
123 $stream->close();
124 $this->assertNull( $stream->detach() );
125 }
126
127 public function testGetSize() {
128 $filename = $this->makeTemp( __FUNCTION__ );
129 $stream = new UploadedFileStream( $filename );
130 file_put_contents( $filename, 'foobarbaz' );
131 $this->assertSame( 9, $stream->getSize() );
132
133 // Cached
134 file_put_contents( $filename, 'baz' );
135 clearstatcache();
136 $this->assertSame( 3, stat( $filename )['size'], 'size check' );
137 $this->assertSame( 9, $stream->getSize() );
138
139 // No error if closed
140 $stream = new UploadedFileStream( $filename );
141 $stream->close();
142 $this->assertSame( null, $stream->getSize() );
143
144 // No error even if the fd goes bad
145 $stream = new UploadedFileStream( $filename );
146 fclose( TestingAccessWrapper::newFromObject( $stream )->fp );
147 $this->assertSame( null, $stream->getSize() );
148 }
149
150 public function testSeekTell() {
151 $filename = $this->makeTemp( __FUNCTION__ );
152 $stream = new UploadedFileStream( $filename );
153
154 $stream->seek( 2 );
155 $this->assertSame( 2, $stream->tell() );
156 $stream->seek( 2, SEEK_CUR );
157 $this->assertSame( 4, $stream->tell() );
158 $stream->seek( -5, SEEK_END );
159 $this->assertSame( 1, $stream->tell() );
160 $stream->read( 2 );
161 $this->assertSame( 3, $stream->tell() );
162
163 $stream->close();
164 try {
165 $stream->seek( 0 );
166 } catch ( AssertionFailedError $ex ) {
167 throw $ex;
168 } catch ( RuntimeException $ex ) {
169 }
170 try {
171 $stream->tell();
172 } catch ( AssertionFailedError $ex ) {
173 throw $ex;
174 } catch ( RuntimeException $ex ) {
175 }
176 }
177
178 public function testEof() {
179 $filename = $this->makeTemp( __FUNCTION__ );
180 $stream = new UploadedFileStream( $filename );
181
182 $this->assertFalse( $stream->eof() );
183 $stream->getContents();
184 $this->assertTrue( $stream->eof() );
185 $stream->seek( -1, SEEK_END );
186 $this->assertFalse( $stream->eof() );
187
188 // No error if closed
189 $stream = new UploadedFileStream( $filename );
190 $stream->close();
191 $this->assertTrue( $stream->eof() );
192
193 // No error even if the fd goes bad
194 $stream = new UploadedFileStream( $filename );
195 fclose( TestingAccessWrapper::newFromObject( $stream )->fp );
196 $this->assertIsBool( $stream->eof() );
197 }
198
199 public function testIsFuncs() {
200 $filename = $this->makeTemp( __FUNCTION__ );
201 $stream = new UploadedFileStream( $filename );
202 $this->assertTrue( $stream->isSeekable() );
203 $this->assertTrue( $stream->isReadable() );
204 $this->assertFalse( $stream->isWritable() );
205
206 $stream->close();
207 $this->assertFalse( $stream->isSeekable() );
208 $this->assertFalse( $stream->isReadable() );
209 $this->assertFalse( $stream->isWritable() );
210 }
211
212 public function testRewind() {
213 $filename = $this->makeTemp( __FUNCTION__ );
214 $stream = new UploadedFileStream( $filename );
215
216 $stream->seek( 2 );
217 $this->assertSame( 2, $stream->tell() );
218 $stream->rewind();
219 $this->assertSame( 0, $stream->tell() );
220
221 $stream->close();
222 try {
223 $stream->rewind();
224 } catch ( AssertionFailedError $ex ) {
225 throw $ex;
226 } catch ( RuntimeException $ex ) {
227 }
228 }
229
230 public function testWrite() {
231 $filename = $this->makeTemp( __FUNCTION__ );
232 $stream = new UploadedFileStream( $filename );
233
234 try {
235 $stream->write( 'foo' );
236 } catch ( AssertionFailedError $ex ) {
237 throw $ex;
238 } catch ( RuntimeException $ex ) {
239 }
240 }
241
242 public function testRead() {
243 $filename = $this->makeTemp( __FUNCTION__ );
244 $stream = new UploadedFileStream( $filename );
245
246 $this->assertSame( 'foo', $stream->read( 3 ) );
247 $this->assertSame( 'bar', $stream->read( 10 ) );
248 $this->assertSame( '', $stream->read( 10 ) );
249 $stream->rewind();
250 $this->assertSame( 'foobar', $stream->read( 10 ) );
251
252 $stream->close();
253 try {
254 $stream->read( 1 );
255 } catch ( AssertionFailedError $ex ) {
256 throw $ex;
257 } catch ( RuntimeException $ex ) {
258 }
259 }
260
261 public function testGetContents() {
262 $filename = $this->makeTemp( __FUNCTION__ );
263 $stream = new UploadedFileStream( $filename );
264
265 $this->assertSame( 'foobar', $stream->getContents() );
266 $this->assertSame( '', $stream->getContents() );
267 $stream->seek( 3 );
268 $this->assertSame( 'bar', $stream->getContents() );
269
270 $stream->close();
271 try {
272 $stream->getContents();
273 } catch ( AssertionFailedError $ex ) {
274 throw $ex;
275 } catch ( RuntimeException $ex ) {
276 }
277 }
278
279 public function testGetMetadata() {
280 // Whatever
281 $filename = $this->makeTemp( __FUNCTION__ );
282 $fp = fopen( $filename, 'r' );
283 $expect = stream_get_meta_data( $fp );
284 fclose( $fp );
285
286 $stream = new UploadedFileStream( $filename );
287 $this->assertSame( $expect, $stream->getMetadata() );
288 foreach ( $expect as $k => $v ) {
289 $this->assertSame( $v, $stream->getMetadata( $k ) );
290 }
291 $this->assertNull( $stream->getMetadata( 'bogus' ) );
292
293 $stream->close();
294 try {
295 $stream->getMetadata();
296 } catch ( AssertionFailedError $ex ) {
297 throw $ex;
298 } catch ( RuntimeException $ex ) {
299 }
300 }
301
302 }