TYPO3 CMS  TYPO3_7-6
StreamTest.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
18 
25 {
29  protected $stream;
30 
31  protected function setUp()
32  {
33  $this->stream = new Stream('php://memory', 'wb+');
34  }
35 
40  {
41  $this->assertInstanceOf(Stream::class, $this->stream);
42  }
43 
48  {
49  $resource = fopen('php://memory', 'wb+');
50  $stream = new Stream($resource);
51  $this->assertInstanceOf(Stream::class, $stream);
52  }
53 
58  {
59  $fileName = PATH_site . 'typo3temp/' . $this->getUniqueId('test_');
60  touch($fileName);
61  $this->testFilesToDelete[] = $fileName;
62  $stream = new Stream($fileName, 'w');
63  $this->assertFalse($stream->isReadable());
64  }
65 
70  {
71  $stream = new Stream('php://memory', 'r');
72  $this->assertFalse($stream->isWritable());
73  }
74 
79  {
80  $message = 'foo bar';
81  $this->stream->write($message);
82  $this->assertEquals($message, (string) $this->stream);
83  }
84 
88  public function detachReturnsResource()
89  {
90  $resource = fopen('php://memory', 'wb+');
91  $stream = new Stream($resource);
92  $this->assertSame($resource, $stream->detach());
93  }
94 
99  {
100  $this->setExpectedException('InvalidArgumentException');
101  $stream = new Stream([' THIS WILL NOT WORK ']);
102  }
103 
108  {
109  $fileName = PATH_site . 'typo3temp/' . $this->getUniqueId('test_');
110  touch($fileName);
111  $this->testFilesToDelete[] = $fileName;
112  file_put_contents($fileName, 'FOO BAR');
113  $stream = new Stream($fileName, 'w');
114 
115  $this->assertEquals('', $stream->__toString());
116  }
117 
121  public function closeClosesResource()
122  {
123  $fileName = PATH_site . 'typo3temp/' . $this->getUniqueId('test_');
124  touch($fileName);
125  $this->testFilesToDelete[] = $fileName;
126  $resource = fopen($fileName, 'wb+');
127  $stream = new Stream($resource);
128  $stream->close();
129  $this->assertFalse(is_resource($resource));
130  }
131 
135  public function closeUnsetsResource()
136  {
137  $fileName = PATH_site . 'typo3temp/' . $this->getUniqueId('test_');
138  touch($fileName);
139  $this->testFilesToDelete[] = $fileName;
140  $resource = fopen($fileName, 'wb+');
141  $stream = new Stream($resource);
142  $stream->close();
143 
144  $this->assertNull($stream->detach());
145  }
146 
150  public function closeDoesNothingAfterDetach()
151  {
152  $fileName = PATH_site . 'typo3temp/' . $this->getUniqueId('test_');
153  touch($fileName);
154  $this->testFilesToDelete[] = $fileName;
155  $resource = fopen($fileName, 'wb+');
156  $stream = new Stream($resource);
157  $detached = $stream->detach();
158 
159  $stream->close();
160  $this->assertTrue(is_resource($detached));
161  $this->assertSame($resource, $detached);
162  }
163 
168  {
169  $this->stream->detach();
170  $this->assertNull($this->stream->getSize());
171  }
172 
177  {
178  $fileName = PATH_site . 'typo3temp/' . $this->getUniqueId('test_');
179  $this->testFilesToDelete[] = $fileName;
180  file_put_contents($fileName, 'FOO BAR');
181  $resource = fopen($fileName, 'wb+');
182  $stream = new Stream($resource);
183 
184  fseek($resource, 2);
185 
186  $this->assertEquals(2, $stream->tell());
187  }
188 
193  {
194  $fileName = PATH_site . 'typo3temp/' . $this->getUniqueId('test_');
195  $this->testFilesToDelete[] = $fileName;
196  file_put_contents($fileName, 'FOO BAR');
197  $resource = fopen($fileName, 'wb+');
198  $stream = new Stream($resource);
199 
200  fseek($resource, 2);
201  $stream->detach();
202  $this->setExpectedException('RuntimeException', 'No resource');
203  $stream->tell();
204  }
205 
210  {
211  $fileName = PATH_site . 'typo3temp/' . $this->getUniqueId('test_');
212  $this->testFilesToDelete[] = $fileName;
213  file_put_contents($fileName, 'FOO BAR');
214  $resource = fopen($fileName, 'wb+');
215  $stream = new Stream($resource);
216 
217  fseek($resource, 2);
218  $this->assertFalse($stream->eof());
219  }
220 
225  {
226  $fileName = PATH_site . 'typo3temp/' . $this->getUniqueId('test_');
227  $this->testFilesToDelete[] = $fileName;
228  file_put_contents($fileName, 'FOO BAR');
229  $resource = fopen($fileName, 'wb+');
230  $stream = new Stream($resource);
231 
232  while (!feof($resource)) {
233  fread($resource, 4096);
234  }
235  $this->assertTrue($stream->eof());
236  }
237 
242  {
243  $fileName = PATH_site . 'typo3temp/' . $this->getUniqueId('test_');
244  $this->testFilesToDelete[] = $fileName;
245  file_put_contents($fileName, 'FOO BAR');
246  $resource = fopen($fileName, 'wb+');
247  $stream = new Stream($resource);
248 
249  fseek($resource, 2);
250  $stream->detach();
251  $this->assertTrue($stream->eof());
252  }
253 
258  {
259  $fileName = PATH_site . 'typo3temp/' . $this->getUniqueId('test_');
260  $this->testFilesToDelete[] = $fileName;
261  file_put_contents($fileName, 'FOO BAR');
262  $resource = fopen($fileName, 'wb+');
263  $stream = new Stream($resource);
264  $this->assertTrue($stream->isSeekable());
265  }
266 
271  {
272  $fileName = PATH_site . 'typo3temp/' . $this->getUniqueId('test_');
273  $this->testFilesToDelete[] = $fileName;
274  file_put_contents($fileName, 'FOO BAR');
275  $resource = fopen($fileName, 'wb+');
276  $stream = new Stream($resource);
277  $stream->detach();
278  $this->assertFalse($stream->isSeekable());
279  }
280 
285  {
286  $fileName = PATH_site . 'typo3temp/' . $this->getUniqueId('test_');
287  $this->testFilesToDelete[] = $fileName;
288  file_put_contents($fileName, 'FOO BAR');
289  $resource = fopen($fileName, 'wb+');
290  $stream = new Stream($resource);
291  $stream->seek(2);
292  $this->assertEquals(2, $stream->tell());
293  }
294 
298  public function rewindResetsToStartOfStream()
299  {
300  $fileName = PATH_site . 'typo3temp/' . $this->getUniqueId('test_');
301  $this->testFilesToDelete[] = $fileName;
302  file_put_contents($fileName, 'FOO BAR');
303  $resource = fopen($fileName, 'wb+');
304  $stream = new Stream($resource);
305  $stream->seek(2);
306  $stream->rewind();
307  $this->assertEquals(0, $stream->tell());
308  }
309 
314  {
315  $fileName = PATH_site . 'typo3temp/' . $this->getUniqueId('test_');
316  $this->testFilesToDelete[] = $fileName;
317  file_put_contents($fileName, 'FOO BAR');
318  $resource = fopen($fileName, 'wb+');
319  $stream = new Stream($resource);
320  $stream->detach();
321  $this->setExpectedException('RuntimeException', 'No resource');
322  $stream->seek(2);
323  }
324 
329  {
330  $fileName = PATH_site . 'typo3temp/' . $this->getUniqueId('test_');
331  $this->testFilesToDelete[] = $fileName;
332  file_put_contents($fileName, 'FOO BAR');
333  $resource = fopen($fileName, 'wb+');
334  $stream = new Stream($resource);
335  $stream->detach();
336  $this->assertFalse($stream->isWritable());
337  }
338 
343  {
344  $fileName = PATH_site . 'typo3temp/' . $this->getUniqueId('test_');
345  $this->testFilesToDelete[] = $fileName;
346  file_put_contents($fileName, 'FOO BAR');
347  $resource = fopen($fileName, 'wb+');
348  $stream = new Stream($resource);
349  $stream->detach();
350  $this->setExpectedException('RuntimeException', 'No resource');
351  $stream->write('bar');
352  }
353 
358  {
359  $fileName = PATH_site . 'typo3temp/' . $this->getUniqueId('test_');
360  $this->testFilesToDelete[] = $fileName;
361  file_put_contents($fileName, 'FOO BAR');
362  $resource = fopen($fileName, 'wb+');
363  $stream = new Stream($resource);
364  $stream->detach();
365  $this->assertFalse($stream->isReadable());
366  }
367 
372  {
373  $fileName = PATH_site . 'typo3temp/' . $this->getUniqueId('test_');
374  $this->testFilesToDelete[] = $fileName;
375  file_put_contents($fileName, 'FOO BAR');
376  $resource = fopen($fileName, 'r');
377  $stream = new Stream($resource);
378  $stream->detach();
379  $this->setExpectedException('RuntimeException', 'No resource');
380  $stream->read(4096);
381  }
382 
387  {
388  $fileName = PATH_site . 'typo3temp/' . $this->getUniqueId('test_');
389  $this->testFilesToDelete[] = $fileName;
390  file_put_contents($fileName, 'FOO BAR');
391  $resource = fopen($fileName, 'r');
392  $stream = new Stream($resource);
393  while (!feof($resource)) {
394  fread($resource, 4096);
395  }
396  $this->assertEquals('', $stream->read(4096));
397  }
398 
403  {
404  $fileName = PATH_site . 'typo3temp/' . $this->getUniqueId('test_');
405  $this->testFilesToDelete[] = $fileName;
406  file_put_contents($fileName, 'FOO BAR');
407  $resource = fopen($fileName, 'w');
408  $stream = new Stream($resource);
409  $this->assertEquals('', $stream->getContents());
410  }
411 
416  {
417  $fileName = tempnam(sys_get_temp_dir(), 'PHLY');
418  $this->testFilesToDelete[] = $fileName;
419 
420  return [
421  'null' => [null],
422  'false' => [false],
423  'true' => [true],
424  'int' => [1],
425  'float' => [1.1],
426  'string-non-resource' => ['foo-bar-baz'],
427  'array' => [[fopen($fileName, 'r+')]],
428  'object' => [(object) ['resource' => fopen($fileName, 'r+')]],
429  ];
430  }
431 
437  {
438  $this->setExpectedException('InvalidArgumentException', 'Invalid stream');
439  $this->stream->attach($resource);
440  }
441 
446  {
447  $fileName = PATH_site . 'typo3temp/' . $this->getUniqueId('test_');
448  touch($fileName);
449  $this->testFilesToDelete[] = $fileName;
450  $resource = fopen($fileName, 'r+');
451  $this->stream->attach($resource);
452 
453  $r = new \ReflectionProperty($this->stream, 'resource');
454  $r->setAccessible(true);
455  $test = $r->getValue($this->stream);
456  $this->assertSame($resource, $test);
457  }
458 
463  {
464  $fileName = PATH_site . 'typo3temp/' . $this->getUniqueId('test_');
465  touch($fileName);
466  $this->testFilesToDelete[] = $fileName;
467  $this->stream->attach($fileName);
468 
469  $resource = fopen($fileName, 'r+');
470  fwrite($resource, 'FooBar');
471 
472  $this->stream->rewind();
473  $test = (string) $this->stream;
474  $this->assertEquals('FooBar', $test);
475  }
476 
481  {
482  $fileName = PATH_site . 'typo3temp/' . $this->getUniqueId('test_');
483  touch($fileName);
484  $this->testFilesToDelete[] = $fileName;
485  $resource = fopen($fileName, 'r+');
486  $this->stream->attach($resource);
487 
488  fwrite($resource, 'FooBar');
489 
490  // rewind, because current pointer is at end of stream!
491  $this->stream->rewind();
492  $test = $this->stream->getContents();
493  $this->assertEquals('FooBar', $test);
494  }
495 
500  {
501  $fileName = PATH_site . 'typo3temp/' . $this->getUniqueId('test_');
502  touch($fileName);
503  $this->testFilesToDelete[] = $fileName;
504  $resource = fopen($fileName, 'r+');
505  $this->stream->attach($resource);
506 
507  fwrite($resource, 'FooBar');
508 
509  // seek to position 3
510  $this->stream->seek(3);
511  $test = $this->stream->getContents();
512  $this->assertEquals('Bar', $test);
513  }
514 
519  {
520  $fileName = PATH_site . 'typo3temp/' . $this->getUniqueId('test_');
521  touch($fileName);
522  $this->testFilesToDelete[] = $fileName;
523  $resource = fopen($fileName, 'r+');
524  $this->stream->attach($resource);
525 
526  $expected = stream_get_meta_data($resource);
527  $test = $this->stream->getMetadata();
528 
529  $this->assertEquals($expected, $test);
530  }
531 
536  {
537  $fileName = PATH_site . 'typo3temp/' . $this->getUniqueId('test_');
538  touch($fileName);
539  $this->testFilesToDelete[] = $fileName;
540  $resource = fopen($fileName, 'r+');
541  $this->stream->attach($resource);
542 
543  $metadata = stream_get_meta_data($resource);
544  $expected = $metadata['uri'];
545 
546  $test = $this->stream->getMetadata('uri');
547 
548  $this->assertEquals($expected, $test);
549  }
550 
555  {
556  $fileName = PATH_site . 'typo3temp/' . $this->getUniqueId('test_');
557  touch($fileName);
558  $this->testFilesToDelete[] = $fileName;
559  $resource = fopen($fileName, 'r+');
560  $this->stream->attach($resource);
561 
562  $this->assertNull($this->stream->getMetadata('TOTALLY_MADE_UP'));
563  }
564 
568  public function getSizeReturnsStreamSize()
569  {
570  $resource = fopen(__FILE__, 'r');
571  $expected = fstat($resource);
572  $stream = new Stream($resource);
573  $this->assertEquals($expected['size'], $stream->getSize());
574  }
575 }