From b4519e592d44fc5f7b9f01fb3a3f630267303600 Mon Sep 17 00:00:00 2001 From: Vincent Petry Date: Tue, 12 Apr 2022 14:42:50 +0200 Subject: [PATCH] Unit tests for ZIP getAllFilesStat() Signed-off-by: Vincent Petry --- lib/private/Archive/ZIP.php | 4 ++-- tests/lib/Archive/ZIPTest.php | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/lib/private/Archive/ZIP.php b/lib/private/Archive/ZIP.php index 743d313f951b0..a34917b3077f2 100644 --- a/lib/private/Archive/ZIP.php +++ b/lib/private/Archive/ZIP.php @@ -133,7 +133,7 @@ public function getFolder(string $path): array { */ public function getAllFilesStat() { $fileCount = $this->zip->numFiles; - for ($i = 0;$i < $fileCount;$i++) { + for ($i = 0; $i < $fileCount; $i++) { yield $this->zip->statIndex($i); } } @@ -158,7 +158,7 @@ public function getStat(string $path): ?array { public function getFiles(): array { $fileCount = $this->zip->numFiles; $files = []; - for ($i = 0;$i < $fileCount;$i++) { + for ($i = 0; $i < $fileCount; $i++) { $files[] = $this->zip->getNameIndex($i); } return $files; diff --git a/tests/lib/Archive/ZIPTest.php b/tests/lib/Archive/ZIPTest.php index 14443471da1ba..1d47d99dac5c0 100644 --- a/tests/lib/Archive/ZIPTest.php +++ b/tests/lib/Archive/ZIPTest.php @@ -19,4 +19,27 @@ protected function getExisting() { protected function getNew() { return new ZIP(\OC::$server->getTempManager()->getTempBaseDir().'/newArchive.zip'); } + + public function testGetAllFilesStat() { + $this->instance = $this->getExisting(); + $allStatsIterator = $this->instance->getAllFilesStat(); + $allStats = []; + foreach ($allStatsIterator as $stat) { + $allStats[$stat['name']] = $stat; + } + $expected = [ + 'lorem.txt' => ['mtime' => 1329528294], + 'logo-wide.png' => ['mtime' => 1329528294], + 'dir/' => ['mtime' => 1330699236], + 'dir/lorem.txt' => ['mtime' => 1329528294], + ]; + $count = 0; + foreach ($expected as $path => $expectedStat) { + $this->assertTrue(isset($allStats[$path])); + $stat = $allStats[$path]; + $this->assertEquals($expectedStat['mtime'], $stat['mtime']); + $this->assertTrue($this->instance->fileExists($stat['name']), 'file ' . $stat['name'] . ' does not exist in archive'); + } + $this->assertEquals(4, count($allStats), 'only found ' . count($allStats) . ' out of 4 expected files'); + } }