|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace MongoDB\Tests\GridFS; |
| 4 | + |
| 5 | +use MongoDB\BSON\Binary; |
| 6 | + |
| 7 | +/** |
| 8 | + * Functional tests for the internal StreamWrapper class. |
| 9 | + */ |
| 10 | +class StreamWrapperFunctionalTest extends FunctionalTestCase |
| 11 | +{ |
| 12 | + public function setUp() |
| 13 | + { |
| 14 | + parent::setUp(); |
| 15 | + |
| 16 | + $this->filesCollection->insertMany([ |
| 17 | + ['_id' => 'length-10', 'length' => 10, 'chunkSize' => 4], |
| 18 | + ]); |
| 19 | + |
| 20 | + $this->chunksCollection->insertMany([ |
| 21 | + ['_id' => 1, 'files_id' => 'length-10', 'n' => 0, 'data' => new Binary('abcd', Binary::TYPE_GENERIC)], |
| 22 | + ['_id' => 2, 'files_id' => 'length-10', 'n' => 1, 'data' => new Binary('efgh', Binary::TYPE_GENERIC)], |
| 23 | + ['_id' => 3, 'files_id' => 'length-10', 'n' => 2, 'data' => new Binary('ij', Binary::TYPE_GENERIC)], |
| 24 | + ]); |
| 25 | + } |
| 26 | + |
| 27 | + public function testReadableStreamClose() |
| 28 | + { |
| 29 | + $stream = $this->bucket->openDownloadStream('length-10'); |
| 30 | + |
| 31 | + $this->assertTrue(fclose($stream)); |
| 32 | + } |
| 33 | + |
| 34 | + public function testReadableStreamEof() |
| 35 | + { |
| 36 | + $stream = $this->bucket->openDownloadStream('length-10'); |
| 37 | + |
| 38 | + $this->assertFalse(feof($stream)); |
| 39 | + $this->assertStreamContents('abcdefghij', $stream); |
| 40 | + $this->assertTrue(feof($stream)); |
| 41 | + } |
| 42 | + |
| 43 | + public function testReadableStreamRead() |
| 44 | + { |
| 45 | + $stream = $this->bucket->openDownloadStream('length-10'); |
| 46 | + |
| 47 | + $this->assertSame('abc', fread($stream, 3)); |
| 48 | + $this->assertSame('defghij', fread($stream, 10)); |
| 49 | + $this->assertSame('', fread($stream, 3)); |
| 50 | + } |
| 51 | + |
| 52 | + public function testReadableStreamWrite() |
| 53 | + { |
| 54 | + $stream = $this->bucket->openDownloadStream('length-10'); |
| 55 | + |
| 56 | + $this->assertSame(0, fwrite($stream, 'foobar')); |
| 57 | + } |
| 58 | + |
| 59 | + public function testWritableStreamClose() |
| 60 | + { |
| 61 | + $stream = $this->bucket->openUploadStream('filename'); |
| 62 | + |
| 63 | + $this->assertSame(6, fwrite($stream, 'foobar')); |
| 64 | + $this->assertTrue(fclose($stream)); |
| 65 | + |
| 66 | + $this->assertStreamContents('foobar', $this->bucket->openDownloadStreamByName('filename')); |
| 67 | + } |
| 68 | + |
| 69 | + public function testWritableStreamEof() |
| 70 | + { |
| 71 | + $stream = $this->bucket->openUploadStream('filename'); |
| 72 | + |
| 73 | + $this->assertFalse(feof($stream)); |
| 74 | + $this->assertSame(6, fwrite($stream, 'foobar')); |
| 75 | + $this->assertFalse(feof($stream)); |
| 76 | + } |
| 77 | + |
| 78 | + public function testWritableStreamRead() |
| 79 | + { |
| 80 | + $stream = $this->bucket->openUploadStream('filename'); |
| 81 | + |
| 82 | + $this->assertSame('', fread($stream, 8192)); |
| 83 | + $this->assertSame(6, fwrite($stream, 'foobar')); |
| 84 | + $this->assertSame('', fread($stream, 8192)); |
| 85 | + } |
| 86 | + |
| 87 | + public function testWritableStreamWrite() |
| 88 | + { |
| 89 | + $stream = $this->bucket->openUploadStream('filename'); |
| 90 | + |
| 91 | + $this->assertSame(6, fwrite($stream, 'foobar')); |
| 92 | + } |
| 93 | +} |
0 commit comments