Skip to content

Commit cddeec6

Browse files
Stephan Wentzpl-github
authored andcommitted
feat: Add assertZipHasFileWithCrc() assertion for zip files
1 parent bd7129f commit cddeec6

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

src/ZipContents/ZipContentsTrait.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,17 @@ final protected static function assertZipHasFileWithSize(
5555

5656
Assert::assertSame($expectedSize, $file->getSize(), $message);
5757
}
58+
59+
final protected static function assertZipHasFileWithCrc(
60+
string $expectedPath,
61+
string $expectedCrc,
62+
ZipInfo $zip,
63+
string $message = '',
64+
): void {
65+
self::assertZipHasFile($expectedPath, $zip, $message);
66+
67+
$file = $zip->getFile($expectedPath);
68+
69+
Assert::assertSame($expectedCrc, $file->getCrcAsHex(), $message);
70+
}
5871
}

tests/ZipContents/ZipContentsTraitTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,31 @@ public function testAssertZipHasFileWithSizeForZipStream(): void
125125

126126
self::assertZipHasFileWithSize('my-file.txt', 7, $zip);
127127
}
128+
129+
public function testAssertZipHasFileWithCrcForZipFileFails(): void
130+
{
131+
$zip = self::readZipFile(self::FILE);
132+
133+
try {
134+
self::assertZipHasFileWithCrc('foo.txt', 'b22c9747', $zip, 'assertZipHasFileWithCrc');
135+
136+
self::fail('ExpectationFailedException was not thrown.');
137+
} catch (ExpectationFailedException $e) {
138+
self::assertStringContainsString('assertZipHasFileWithCrc', $e->getMessage());
139+
}
140+
}
141+
142+
public function testAssertZipHasFileWithCrcForZipFile(): void
143+
{
144+
$zip = self::readZipFile(self::FILE);
145+
146+
self::assertZipHasFileWithCrc('my-file.txt', 'b22c9747', $zip);
147+
}
148+
149+
public function testAssertZipHasFileWithCrcForZipStream(): void
150+
{
151+
$zip = self::readZipStream(fopen(self::FILE, 'rb'), filesize(self::FILE));
152+
153+
self::assertZipHasFileWithCrc('my-file.txt', 'b22c9747', $zip);
154+
}
128155
}

0 commit comments

Comments
 (0)