|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace MongoDB\Tests\GridFS; |
| 4 | + |
| 5 | +use MongoDB\BSON\Binary; |
| 6 | +use MongoDB\BSON\UTCDateTime; |
| 7 | + |
| 8 | +/** |
| 9 | + * Functional tests for the internal StreamWrapper class. |
| 10 | + */ |
| 11 | +class StreamWrapperFunctionalTest extends FunctionalTestCase |
| 12 | +{ |
| 13 | + public function setUp() |
| 14 | + { |
| 15 | + parent::setUp(); |
| 16 | + |
| 17 | + $this->filesCollection->insertMany([ |
| 18 | + ['_id' => 'length-10', 'length' => 10, 'chunkSize' => 4, 'uploadDate' => new UTCDateTime('1484202200000')], |
| 19 | + ]); |
| 20 | + |
| 21 | + $this->chunksCollection->insertMany([ |
| 22 | + ['_id' => 1, 'files_id' => 'length-10', 'n' => 0, 'data' => new Binary('abcd', Binary::TYPE_GENERIC)], |
| 23 | + ['_id' => 2, 'files_id' => 'length-10', 'n' => 1, 'data' => new Binary('efgh', Binary::TYPE_GENERIC)], |
| 24 | + ['_id' => 3, 'files_id' => 'length-10', 'n' => 2, 'data' => new Binary('ij', Binary::TYPE_GENERIC)], |
| 25 | + ]); |
| 26 | + } |
| 27 | + |
| 28 | + public function testReadableStreamClose() |
| 29 | + { |
| 30 | + $stream = $this->bucket->openDownloadStream('length-10'); |
| 31 | + |
| 32 | + $this->assertTrue(fclose($stream)); |
| 33 | + } |
| 34 | + |
| 35 | + public function testReadableStreamEof() |
| 36 | + { |
| 37 | + $stream = $this->bucket->openDownloadStream('length-10'); |
| 38 | + |
| 39 | + $this->assertFalse(feof($stream)); |
| 40 | + $this->assertStreamContents('abcdefghij', $stream); |
| 41 | + $this->assertTrue(feof($stream)); |
| 42 | + } |
| 43 | + |
| 44 | + public function testReadableStreamRead() |
| 45 | + { |
| 46 | + $stream = $this->bucket->openDownloadStream('length-10'); |
| 47 | + |
| 48 | + $this->assertSame('abc', fread($stream, 3)); |
| 49 | + $this->assertSame('defghij', fread($stream, 10)); |
| 50 | + $this->assertSame('', fread($stream, 3)); |
| 51 | + } |
| 52 | + |
| 53 | + public function testReadableStreamStat() |
| 54 | + { |
| 55 | + $stream = $this->bucket->openDownloadStream('length-10'); |
| 56 | + |
| 57 | + $stat = fstat($stream); |
| 58 | + $this->assertSame(0100444, $stat[2]); |
| 59 | + $this->assertSame(0100444, $stat['mode']); |
| 60 | + $this->assertSame(10, $stat[7]); |
| 61 | + $this->assertSame(10, $stat['size']); |
| 62 | + $this->assertSame(1484202200, $stat[9]); |
| 63 | + $this->assertSame(1484202200, $stat['mtime']); |
| 64 | + $this->assertSame(1484202200, $stat[10]); |
| 65 | + $this->assertSame(1484202200, $stat['ctime']); |
| 66 | + $this->assertSame(4, $stat[11]); |
| 67 | + $this->assertSame(4, $stat['blksize']); |
| 68 | + } |
| 69 | + |
| 70 | + public function testReadableStreamWrite() |
| 71 | + { |
| 72 | + $stream = $this->bucket->openDownloadStream('length-10'); |
| 73 | + |
| 74 | + $this->assertSame(0, fwrite($stream, 'foobar')); |
| 75 | + } |
| 76 | + |
| 77 | + public function testWritableStreamClose() |
| 78 | + { |
| 79 | + $stream = $this->bucket->openUploadStream('filename'); |
| 80 | + |
| 81 | + $this->assertSame(6, fwrite($stream, 'foobar')); |
| 82 | + $this->assertTrue(fclose($stream)); |
| 83 | + |
| 84 | + $this->assertStreamContents('foobar', $this->bucket->openDownloadStreamByName('filename')); |
| 85 | + } |
| 86 | + |
| 87 | + public function testWritableStreamEof() |
| 88 | + { |
| 89 | + $stream = $this->bucket->openUploadStream('filename'); |
| 90 | + |
| 91 | + $this->assertFalse(feof($stream)); |
| 92 | + $this->assertSame(6, fwrite($stream, 'foobar')); |
| 93 | + $this->assertFalse(feof($stream)); |
| 94 | + } |
| 95 | + |
| 96 | + public function testWritableStreamRead() |
| 97 | + { |
| 98 | + $stream = $this->bucket->openUploadStream('filename'); |
| 99 | + |
| 100 | + $this->assertSame('', fread($stream, 8192)); |
| 101 | + $this->assertSame(6, fwrite($stream, 'foobar')); |
| 102 | + $this->assertSame('', fread($stream, 8192)); |
| 103 | + } |
| 104 | + |
| 105 | + public function testWritableStreamStat() |
| 106 | + { |
| 107 | + $currentTimestamp = time(); |
| 108 | + $stream = $this->bucket->openUploadStream('filename', ['chunkSizeBytes' => 1024]); |
| 109 | + |
| 110 | + $stat = fstat($stream); |
| 111 | + $this->assertSame(0100222, $stat[2]); |
| 112 | + $this->assertSame(0100222, $stat['mode']); |
| 113 | + $this->assertSame(0, $stat[7]); |
| 114 | + $this->assertSame(0, $stat['size']); |
| 115 | + $this->assertGreaterThanOrEqual($currentTimestamp, $stat[9]); |
| 116 | + $this->assertGreaterThanOrEqual($currentTimestamp, $stat['mtime']); |
| 117 | + $this->assertGreaterThanOrEqual($currentTimestamp, $stat[10]); |
| 118 | + $this->assertGreaterThanOrEqual($currentTimestamp, $stat['ctime']); |
| 119 | + $this->assertSame(1024, $stat[11]); |
| 120 | + $this->assertSame(1024, $stat['blksize']); |
| 121 | + |
| 122 | + $this->assertSame(6, fwrite($stream, 'foobar')); |
| 123 | + |
| 124 | + $stat = fstat($stream); |
| 125 | + $this->assertSame(6, $stat[7]); |
| 126 | + $this->assertSame(6, $stat['size']); |
| 127 | + } |
| 128 | + |
| 129 | + public function testWritableStreamWrite() |
| 130 | + { |
| 131 | + $stream = $this->bucket->openUploadStream('filename'); |
| 132 | + |
| 133 | + $this->assertSame(6, fwrite($stream, 'foobar')); |
| 134 | + } |
| 135 | +} |
0 commit comments