Skip to content

Commit 92e8288

Browse files
test(files/stream): add and expand quota stream tests for seeks and overwrites
Signed-off-by: Josh <josh.t.richards@gmail.com>
1 parent 4de2d4d commit 92e8288

File tree

1 file changed

+31
-14
lines changed

1 file changed

+31
-14
lines changed

tests/lib/Files/Stream/QuotaTest.php

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,40 @@ public function testWriteEnoughSpace(): void {
2828
$this->assertEquals('foobar', fread($stream, 100));
2929
}
3030

31-
public function testQuotaNotReducedByReadBeforeWrite() {
32-
$stream = $this->getStream('w+', 6); // Initial quota: 6
33-
// Read 3 bytes before writing, quota should remain 6
34-
$this->assertEquals('', fread($stream, 3));
35-
// Should allow writing 6 bytes, as reads do NOT affect write quota
36-
$this->assertEquals(6, fwrite($stream, 'foobar'));
31+
// Quota is not reduced by reads or seeks
32+
public function testReadsAndSeeksDoNotAffectQuota() {
33+
$stream = $this->getStream('w+', 6);
34+
$this->assertEquals('', fread($stream, 3)); // Read before write
35+
$this->assertEquals(6, fwrite($stream, 'foobar')); // Write to fill quota
36+
rewind($stream);
37+
$this->assertEquals('foo', fread($stream, 3));
38+
fseek($stream, 1, SEEK_CUR); // Seek should not affect quota
39+
$this->assertEquals(0, fwrite($stream, 'baz')); // No quota left for growth
40+
rewind($stream);
41+
$this->assertEquals('foobar', fread($stream, 100));
3742
}
3843

39-
public function testQuotaWriteFollowedByReadDoesNotAffectWriteQuota() {
40-
$stream = $this->getStream('w+', 6); // Initial quota: 6
41-
// Write exactly up to quota
42-
$this->assertEquals(6, fwrite($stream, 'foobar'));
43-
// Read from stream - should NOT affect quota or prevent further (failing) writes
44+
// Overwriting after quota is exhausted should succeed
45+
public function testOverwriteAfterQuotaExhausted() {
46+
$stream = $this->getStream('w+', 3);
47+
fwrite($stream, 'abc'); // Fill quota
48+
$this->assertEquals(0, fwrite($stream, 'd')); // No quota for growth
4449
rewind($stream);
45-
$this->assertEquals('foobar', fread($stream, 6));
46-
// Try another write - should fail (quota exhausted)
47-
$this->assertEquals(0, fwrite($stream, 'abc'));
50+
$this->assertEquals(3, fwrite($stream, 'xyz')); // Overwrite: should succeed
51+
rewind($stream);
52+
$this->assertEquals('xyz', fread($stream, 100));
53+
}
54+
55+
// Quota is only used for file growth
56+
public function testQuotaOnlyUsedForGrowth() {
57+
$stream = $this->getStream('w+', 5);
58+
$this->assertEquals(5, fwrite($stream, '12345')); // Fill quota
59+
rewind($stream);
60+
$this->assertEquals(3, fwrite($stream, 'abc')); // Overwrite allowed
61+
fseek($stream, 5, SEEK_SET);
62+
$this->assertEquals(0, fwrite($stream, 'xx')); // No quota for extension
63+
rewind($stream);
64+
$this->assertEquals('abc45', fread($stream, 100));
4865
}
4966

5067
public function testWriteNotEnoughSpace(): void {

0 commit comments

Comments
 (0)