Skip to content

Commit ddd2c3e

Browse files
committed
PHPLIB-251: WriteableStream::getSize() should include buffer length
1 parent bc98d9c commit ddd2c3e

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

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
/**

tests/GridFS/StreamWrapperFunctionalTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ public function testReadableStreamStat()
5656
$stat = fstat($stream);
5757
$this->assertSame(0100444, $stat[2]);
5858
$this->assertSame(0100444, $stat['mode']);
59+
$this->assertSame(10, $stat[7]);
60+
$this->assertSame(10, $stat['size']);
5961
}
6062

6163
public function testReadableStreamWrite()
@@ -100,6 +102,14 @@ public function testWritableStreamStat()
100102
$stat = fstat($stream);
101103
$this->assertSame(0100222, $stat[2]);
102104
$this->assertSame(0100222, $stat['mode']);
105+
$this->assertSame(0, $stat[7]);
106+
$this->assertSame(0, $stat['size']);
107+
108+
$this->assertSame(6, fwrite($stream, 'foobar'));
109+
110+
$stat = fstat($stream);
111+
$this->assertSame(6, $stat[7]);
112+
$this->assertSame(6, $stat['size']);
103113
}
104114

105115
public function testWritableStreamWrite()

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)