Skip to content

Commit dccf20a

Browse files
committed
Merge pull request #324
2 parents 391334a + fcdc117 commit dccf20a

File tree

4 files changed

+168
-2
lines changed

4 files changed

+168
-2
lines changed

src/GridFS/StreamWrapper.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
namespace MongoDB\GridFS;
1919

20+
use MongoDB\BSON\UTCDateTime;
2021
use Exception;
2122

2223
/**
@@ -145,9 +146,23 @@ public function stream_stat()
145146
{
146147
$stat = $this->getStatTemplate();
147148

148-
$stat[2] = $stat['mode'] = $this->mode;
149+
$stat[2] = $stat['mode'] = $this->stream instanceof ReadableStream
150+
? 0100444 // S_IFREG & S_IRUSR & S_IRGRP & S_IROTH
151+
: 0100222; // S_IFREG & S_IWUSR & S_IWGRP & S_IWOTH
149152
$stat[7] = $stat['size'] = $this->stream->getSize();
150153

154+
$file = $this->stream->getFile();
155+
156+
if (isset($file->uploadDate) && $file->uploadDate instanceof UTCDateTime) {
157+
$timestamp = $file->uploadDate->toDateTime()->getTimestamp();
158+
$stat[9] = $stat['mtime'] = $timestamp;
159+
$stat[10] = $stat['ctime'] = $timestamp;
160+
}
161+
162+
if (isset($file->chunkSize) && is_integer($file->chunkSize)) {
163+
$stat[11] = $stat['blksize'] = $file->chunkSize;
164+
}
165+
151166
return $stat;
152167
}
153168

src/GridFS/WritableStream.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public function getFile()
158158
*/
159159
public function getSize()
160160
{
161-
return $this->length;
161+
return $this->length + strlen($this->buffer);
162162
}
163163

164164
/**
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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+
}

tests/GridFS/WritableStreamFunctionalTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,22 @@ public function testConstructorShouldRequireChunkSizeBytesOptionToBePositive()
6161
new WritableStream($this->collectionWrapper, 'filename', ['chunkSizeBytes' => 0]);
6262
}
6363

64+
public function testWriteBytesAlwaysUpdatesFileSize()
65+
{
66+
$stream = new WritableStream($this->collectionWrapper, 'filename', ['chunkSizeBytes' => 1024]);
67+
68+
$this->assertSame(0, $stream->getSize());
69+
$this->assertSame(512, $stream->writeBytes(str_repeat('a', 512)));
70+
$this->assertSame(512, $stream->getSize());
71+
$this->assertSame(512, $stream->writeBytes(str_repeat('a', 512)));
72+
$this->assertSame(1024, $stream->getSize());
73+
$this->assertSame(512, $stream->writeBytes(str_repeat('a', 512)));
74+
$this->assertSame(1536, $stream->getSize());
75+
76+
$stream->close();
77+
$this->assertSame(1536, $stream->getSize());
78+
}
79+
6480
/**
6581
* @dataProvider provideInputDataAndExpectedMD5
6682
*/

0 commit comments

Comments
 (0)