Skip to content

Commit 5638731

Browse files
pl-githubtemp
authored andcommitted
feat: Support 7z files
1 parent 80b45f0 commit 5638731

13 files changed

+492
-0
lines changed

.github/workflows/build.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ jobs:
3131
php-version: "${{ matrix.php-version }}"
3232
extensions: mbstring
3333

34+
- name: "Install unix packages"
35+
run: "sudo apt-get update && sudo apt-get install -y 7zip"
36+
3437
- name: "Cache dependencies"
3538
uses: "actions/cache@v4"
3639
with:

composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"dama/doctrine-test-bundle": "^8.0",
1919
"doctrine/dbal": "^3.4",
2020
"ergebnis/phpstan-rules": "^2.2.0",
21+
"gemorroj/archive7z": "^5.3",
2122
"jangregor/phpstan-prophecy": "^1.0",
2223
"mikey179/vfsstream": "^1.6.11",
2324
"monolog/monolog": "^2.3|^3.0",
@@ -52,6 +53,7 @@
5253
"doctrine/dbal": "For schema trait",
5354
"doctrine/event-manager": "For schema trait",
5455
"dama/doctrine-test-bundle": "For schema trait, when using DAMA Static Driver",
56+
"gemorroj/archive7z": "For 7z file support",
5557
"monolog/monolog": "For http client mock trait",
5658
"riverline/multipart-parser": "For multipart file uploads",
5759
"symfony/browser-kit": "For request trait",

phpstan.neon.dist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ parameters:
1616
- '#Method Brainbits\\FunctionalTestHelpers\\Tests\\.*Test::setUpSnapshot\(\) is protected, but since the containing class is final, it can be private.#'
1717
- '#Method Brainbits\\FunctionalTestHelpers\\Tests\\Request\\RequestTraitTest::.*\(\) is protected, but since the containing class is final, it can be private#'
1818
- '#Method Brainbits\\FunctionalTestHelpers\\Tests\\Schema\\SchemaTraitTest::.*\(\) is protected, but since the containing class is final, it can be private#'
19+
- '#Method Brainbits\\FunctionalTestHelpers\\Tests\\SevenZipContents\\SevenZipContentsTraitTest::.*\(\) is protected, but since the containing class is final, it can be private#'
1920
- '#Method Brainbits\\FunctionalTestHelpers\\Tests\\Uuid\\UuidTraitTest::.*\(\) is protected, but since the containing class is final, it can be private#'
2021
- '#Method Brainbits\\FunctionalTestHelpers\\Tests\\ZipContents\\ZipContentsTraitTest::.*\(\) is protected, but since the containing class is final, it can be private#'
2122
- '#Safe\\DateTimeImmutable#'
@@ -29,6 +30,7 @@ parameters:
2930
enabled: false
3031
noExtends:
3132
classesAllowedToBeExtended:
33+
- Archive7z\Archive7z
3234
- Monolog\Handler\AbstractProcessingHandler
3335
- PHPUnit\Framework\Constraint\Constraint
3436
- RuntimeException

phpunit.xml.dist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
<coverage/>
55
<php>
66
<ini name="error_reporting" value="-1"/>
7+
<ini name="date.timezone" value="UTC"/>
8+
<env name="TZ" value="UTC"/>
79
</php>
810
<testsuites>
911
<testsuite name="Project Test Suite">
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Brainbits\FunctionalTestHelpers\SevenZipContents;
6+
7+
use RuntimeException;
8+
9+
use function sprintf;
10+
11+
final class InvalidArchive extends RuntimeException
12+
{
13+
public static function notAFile(mixed $path): self
14+
{
15+
return new self(sprintf('Path %s is not valid', $path));
16+
}
17+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Brainbits\FunctionalTestHelpers\SevenZipContents;
6+
7+
use Archive7z\Archive7z;
8+
9+
use function escapeshellarg;
10+
use function exec;
11+
use function file_exists;
12+
use function is_string;
13+
14+
final class SevenZipArchive extends Archive7z
15+
{
16+
private const array EXECUTABLES = ['7z', '7zz', '7za'];
17+
18+
private static string|null $binary7z = null;
19+
20+
public function __construct(string $filename, float|null $timeout = 60.0)
21+
{
22+
parent::__construct($filename, self::getBinary7zFromPath(), $timeout);
23+
}
24+
25+
private static function getBinary7zFromPath(): string
26+
{
27+
if (self::$binary7z) {
28+
return self::$binary7z;
29+
}
30+
31+
$binary7z = null;
32+
foreach (self::EXECUTABLES as $executable) {
33+
$resultCode = 0;
34+
$binary7z = exec('which ' . escapeshellarg($executable), result_code: $resultCode); // @phpstan-ignore-line
35+
36+
if ($resultCode === 0 && is_string($binary7z) && $binary7z !== '' && file_exists($binary7z)) {
37+
break;
38+
}
39+
}
40+
41+
self::$binary7z = self::makeBinary7z($binary7z);
42+
43+
return self::$binary7z;
44+
}
45+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Brainbits\FunctionalTestHelpers\SevenZipContents;
6+
7+
use function is_file;
8+
9+
final class SevenZipContents
10+
{
11+
public function readFile(string $file): SevenZipInfo
12+
{
13+
if (!is_file($file)) {
14+
throw InvalidArchive::notAFile($file);
15+
}
16+
17+
$archive = new SevenZipArchive($file);
18+
$info = $archive->getInfo();
19+
20+
$fileInfos = [];
21+
foreach ($archive->getEntries() as $entry) {
22+
$path = $entry->getPath();
23+
24+
$size = (int) $entry->getSize();
25+
$packedSize = (int) $entry->getPackedSize();
26+
$compression = $size && $packedSize
27+
? (int) ($packedSize * 100 / $size)
28+
: 0;
29+
30+
$fileInfos[$path] = new SevenZipFileInfo(
31+
$path,
32+
$size,
33+
$packedSize,
34+
$compression,
35+
$entry->getModified(),
36+
$entry->getCrc(),
37+
$entry->isDirectory(),
38+
);
39+
}
40+
41+
return new SevenZipInfo($info->getPhysicalSize(), $fileInfos);
42+
}
43+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Brainbits\FunctionalTestHelpers\SevenZipContents;
6+
7+
use PHPUnit\Framework\Assert;
8+
use PHPUnit\Framework\TestCase;
9+
10+
/** @mixin TestCase */
11+
trait SevenZipContentsTrait
12+
{
13+
final protected static function read7zFile(string $path): SevenZipInfo
14+
{
15+
$zipContents = new SevenZipContents();
16+
17+
return $zipContents->readFile($path);
18+
}
19+
20+
final protected static function assert7zHasSize(int $expectedSize, SevenZipInfo $zip, string $message = ''): void
21+
{
22+
Assert::assertSame($expectedSize, $zip->getSize(), $message);
23+
}
24+
25+
final protected static function assert7zHasNumberOfFiles(
26+
int $expectedNumberOfFiles,
27+
SevenZipInfo $zip,
28+
string $message = '',
29+
): void {
30+
Assert::assertCount($expectedNumberOfFiles, $zip, $message);
31+
}
32+
33+
final protected static function assert7zHasFile(string $expectedPath, SevenZipInfo $zip, string $message = ''): void
34+
{
35+
Assert::assertTrue($zip->hasFile($expectedPath), $message);
36+
}
37+
38+
final protected static function assert7zHasFileWithSize(
39+
string $expectedPath,
40+
int $expectedSize,
41+
SevenZipInfo $zip,
42+
string $message = '',
43+
): void {
44+
self::assert7zHasFile($expectedPath, $zip, $message);
45+
46+
$file = $zip->getFile($expectedPath);
47+
48+
Assert::assertSame($expectedSize, $file?->getSize(), $message);
49+
}
50+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Brainbits\FunctionalTestHelpers\SevenZipContents;
6+
7+
use DateTimeImmutable;
8+
9+
use function array_pop;
10+
use function array_push;
11+
use function explode;
12+
use function implode;
13+
use function str_replace;
14+
use function substr;
15+
use function trim;
16+
17+
final readonly class SevenZipFileInfo
18+
{
19+
private string $path;
20+
private DateTimeImmutable|null $lastModified;
21+
22+
public function __construct(
23+
string $path,
24+
private int $size,
25+
private int $compressedSize,
26+
private int $compression,
27+
string|null $lastModified,
28+
private string|null $crc,
29+
private bool $isDir,
30+
) {
31+
$this->path = $this->cleanPath($path);
32+
33+
$this->lastModified = $lastModified
34+
? DateTimeImmutable::createFromFormat('Y-m-d H:i:s', substr($lastModified, 0, 19))
35+
: null;
36+
}
37+
38+
public function getPath(): string
39+
{
40+
return $this->path;
41+
}
42+
43+
public function getSize(): int
44+
{
45+
if ($this->isDir) {
46+
return 0;
47+
}
48+
49+
return $this->size;
50+
}
51+
52+
public function getCompressedSize(): int
53+
{
54+
return $this->compressedSize;
55+
}
56+
57+
public function getCompression(): int
58+
{
59+
return $this->compression;
60+
}
61+
62+
public function getCrc(): string|null
63+
{
64+
return $this->crc;
65+
}
66+
67+
public function isDir(): bool
68+
{
69+
return $this->isDir;
70+
}
71+
72+
public function getLastModified(): DateTimeImmutable|null
73+
{
74+
return $this->lastModified;
75+
}
76+
77+
/**
78+
* Cleans up a path and removes relative parts, also strips leading slashes
79+
*/
80+
private function cleanPath(string $path): string
81+
{
82+
$path = str_replace('\\', '/', $path);
83+
$path = explode('/', $path);
84+
$newpath = [];
85+
foreach ($path as $p) {
86+
if ($p === '' || $p === '.') {
87+
continue;
88+
}
89+
90+
if ($p === '..') {
91+
array_pop($newpath);
92+
continue;
93+
}
94+
95+
array_push($newpath, $p);
96+
}
97+
98+
return trim(implode('/', $newpath), '/');
99+
}
100+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Brainbits\FunctionalTestHelpers\SevenZipContents;
6+
7+
use Countable;
8+
use Generator;
9+
use IteratorAggregate;
10+
11+
use function array_key_exists;
12+
use function array_values;
13+
use function count;
14+
15+
/** @implements IteratorAggregate<string, SevenZipFileInfo> */
16+
final class SevenZipInfo implements Countable, IteratorAggregate
17+
{
18+
/** @param SevenZipFileInfo[] $files */
19+
public function __construct(private int $size, private array $files)
20+
{
21+
}
22+
23+
public function getSize(): int
24+
{
25+
return $this->size;
26+
}
27+
28+
/** @return list<SevenZipFileInfo> */
29+
public function getFiles(): array
30+
{
31+
return array_values($this->files);
32+
}
33+
34+
public function hasFile(string $path): bool
35+
{
36+
return array_key_exists($path, $this->files);
37+
}
38+
39+
public function getFile(string $path): SevenZipFileInfo|null
40+
{
41+
if (!$this->hasFile($path)) {
42+
return null;
43+
}
44+
45+
return $this->files[$path];
46+
}
47+
48+
public function getIterator(): Generator
49+
{
50+
yield from $this->files;
51+
}
52+
53+
public function count(): int
54+
{
55+
return count($this->files);
56+
}
57+
}

0 commit comments

Comments
 (0)