Skip to content

Commit

Permalink
Fix Strict Simulation for empty files (#301)
Browse files Browse the repository at this point in the history
  • Loading branch information
maennchen committed Apr 22, 2024
1 parent e0ff7b8 commit 3d2f6e7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,11 @@ public function process(): string

$this->addFileHeader();

$detectedSize = $forecastSize ?? $this->compressedSize;
$detectedSize = $forecastSize ?? ($this->compressedSize > 0 ? $this->compressedSize : null);

if (
$this->isSimulation() &&
$detectedSize > 0
$detectedSize !== null
) {
($this->recordSentBytes)($detectedSize);
} else {
Expand Down
13 changes: 11 additions & 2 deletions test/ZipStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1104,7 +1104,8 @@ public function testAddFileCallbackLax(): void
public function testExecuteSimulation(): void
{
$zip = new ZipStream(
operationMode: OperationMode::SIMULATE_LAX,
operationMode: OperationMode::SIMULATE_STRICT,
defaultCompressionMethod: CompressionMethod::STORE,
defaultEnableZeroHeader: false,
sendHttpHeaders: false,
outputStream: $this->tempfileStream,
Expand All @@ -1118,6 +1119,14 @@ public function testExecuteSimulation(): void
}
);

$zip->addFileFromCallback(
'.gitkeep',
exactSize: 0,
callback: function () {
return '';
}
);

$size = $zip->finish();

$this->assertEquals(filesize($this->tempfile), 0);
Expand All @@ -1131,7 +1140,7 @@ public function testExecuteSimulation(): void
$tmpDir = $this->validateAndExtractZip($this->tempfile);

$files = $this->getRecursiveFileList($tmpDir);
$this->assertSame(['sample.txt'], $files);
$this->assertSame(['.gitkeep', 'sample.txt'], $files);
}

public function testExecuteSimulationBeforeFinish(): void
Expand Down

0 comments on commit 3d2f6e7

Please sign in to comment.