1
0
mirror of https://github.com/php/pie.git synced 2026-03-23 23:12:17 +01:00
Files
archived-pie/test/unit/Downloading/ExtractZipTest.php
2024-04-24 09:24:24 +01:00

49 lines
1.5 KiB
PHP

<?php
declare(strict_types=1);
namespace Php\PieUnitTest\Downloading;
use Php\Pie\Downloading\ExtractZip;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use ZipArchive;
use function file_get_contents;
use function mkdir;
use function sprintf;
use function sys_get_temp_dir;
use function uniqid;
use const DIRECTORY_SEPARATOR;
/** @covers \Php\Pie\Downloading\ExtractZip */
#[CoversClass(ExtractZip::class)]
final class ExtractZipTest extends TestCase
{
public function testZipFileCanBeExtracted(): void
{
$localPath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid('pie_test_', true);
mkdir($localPath, 0777, true);
$extr = new ExtractZip();
$extractedPath = $extr->to(__DIR__ . '/../../assets/test-zip.zip', $localPath);
// The test-zip.zip should contain a deterministic file content for this:
self::assertSame('Hello there! Test UUID b925c59b-3e6f-4e45-8029-19431df18de4', file_get_contents($extractedPath . DIRECTORY_SEPARATOR . 'test-file.txt'));
}
public function testFailureToExtractZipWithInvalidZip(): void
{
$localPath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid('pie_test_DO_NOT_EXIST_', true);
$extr = new ExtractZip();
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage(sprintf('Could not open ZIP [%d]: %s', ZipArchive::ER_NOZIP, __FILE__));
$extr->to(__FILE__, $localPath);
}
}