Skip to content

Commit

Permalink
OXDEV-8730 Created a ZipArchiveFactory used in ZipCreatorService
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Fedurtsya <anton@fedurtsya.com>
  • Loading branch information
Sieg authored and RahatHameed committed Sep 26, 2024
1 parent 6984adf commit fb5cf61
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 53 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"prefer-dist": true,
"require": {
"php": ">=8.2",
"symfony/filesystem": "*"
"symfony/filesystem": "*",
"ext-zip": "*"
},
"require-dev": {
"phpstan/phpstan": "^1.8.11",
Expand Down
22 changes: 22 additions & 0 deletions src/UserData/Service/ZipArchiveFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace OxidEsales\GdprOptinModule\UserData\Service;

use OxidEsales\GdprOptinModule\UserData\Exception\ZipCreationException;
use ZipArchive;

class ZipArchiveFactory implements ZipArchiveFactoryInterface
{
/**
* @inheritDoc
*/
public function create(string $filePath): ZipArchive
{
$zipArchive = new ZipArchive();
if ($zipArchive->open($filePath, ZipArchive::CREATE | ZipArchive::OVERWRITE) !== true) {
throw new ZipCreationException("Unable to open zip file at {$filePath}");
}

return $zipArchive;
}
}
19 changes: 19 additions & 0 deletions src/UserData/Service/ZipArchiveFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/**
* Copyright © OXID eSales AG. All rights reserved.
* See LICENSE file for license details.
*/

namespace OxidEsales\GdprOptinModule\UserData\Service;

use OxidEsales\GdprOptinModule\UserData\Exception\ZipCreationException;
use ZipArchive;

interface ZipArchiveFactoryInterface
{
/**
* @throws ZipCreationException
*/
public function create(string $filePath): ZipArchive;
}
15 changes: 4 additions & 11 deletions src/UserData/Service/ZipCreatorService.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@

namespace OxidEsales\GdprOptinModule\UserData\Service;

use OxidEsales\GdprOptinModule\UserData\Exception\ZipCreationException;
use ZipArchive;

class ZipCreatorService implements ZipCreatorServiceInterface
{
public function __construct(
private ZipArchive $zipArchive
private ZipArchiveFactoryInterface $zipArchiveFactory
) {
}

Expand All @@ -24,16 +21,12 @@ public function __construct(
*/
public function createZip(array $files, string $outputFilePath): void
{
if ($this->zipArchive->open($outputFilePath, ZipArchive::CREATE) !== true) {
throw new ZipCreationException("Unable to open zip file at {$outputFilePath}");
}
$zipArchive = $this->zipArchiveFactory->create($outputFilePath);

foreach ($files as $file) {
$fileName = $file->getFileName();
$fileContent = $file->getContent();
$this->zipArchive->addFromString($fileName, $fileContent);
$zipArchive->addFromString($file->getFileName(), $file->getContent());
}

$this->zipArchive->close();
$zipArchive->close();
}
}
6 changes: 2 additions & 4 deletions src/UserData/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,8 @@ services:
OxidEsales\GdprOptinModule\UserData\Service\UserDataExportServiceInterface:
class: OxidEsales\GdprOptinModule\UserData\Service\UserDataExportService

OxidEsales\GdprOptinModule\UserData\Service\ZipArchive:
class: ZipArchive
OxidEsales\GdprOptinModule\UserData\Service\ZipArchiveFactoryInterface:
class: OxidEsales\GdprOptinModule\UserData\Service\ZipArchiveFactory

OxidEsales\GdprOptinModule\UserData\Service\ZipCreatorServiceInterface:
class: OxidEsales\GdprOptinModule\UserData\Service\ZipCreatorService
arguments:
- '@OxidEsales\GdprOptinModule\UserData\Service\ZipArchive'
57 changes: 57 additions & 0 deletions tests/Unit/UserData/Service/ZipArchiveFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace OxidEsales\GdprOptinModule\Tests\Unit\UserData\Service;

use org\bovigo\vfs\vfsStream;
use OxidEsales\GdprOptinModule\UserData\Exception\ZipCreationException;
use OxidEsales\GdprOptinModule\UserData\Service\ZipArchiveFactory;
use PHPUnit\Framework\TestCase;
use ZipArchive;

class ZipArchiveFactoryTest extends TestCase
{
private string $tempDir;

protected function setUp(): void
{
$this->createTempDirectory();
}

public function testCreateZipArchive(): void
{
vfsStream::setup('outputDir');
$filePath = vfsStream::url('outputDir/test.zip');

$sut = new ZipArchiveFactory();
$zipArchive1 = $sut->create(filePath: $filePath);
$zipArchive2 = $sut->create(filePath: $filePath);

$this->assertInstanceOf(ZipArchive::class, $zipArchive1);

$this->assertNotSame($zipArchive1, $zipArchive2);
}

public function testCreateZipArchiveThrowsAnException()
{
$this->expectException(ZipCreationException::class);
$this->expectExceptionMessage("Unable to open zip file at {$this->tempDir}");

$sut = new ZipArchiveFactory();
$sut->create(filePath: $this->tempDir);
}

private function createTempDirectory(): void
{
$this->tempDir = sys_get_temp_dir() . '/testDir';

mkdir($this->tempDir);
chmod($this->tempDir, 0444);
}

protected function tearDown(): void
{
if (file_exists($this->tempDir)) {
rmdir($this->tempDir);
}
}
}
57 changes: 20 additions & 37 deletions tests/Unit/UserData/Service/ZipCreatorServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@

namespace OxidEsales\GdprOptinModule\Tests\Unit\UserData\Service;

use org\bovigo\vfs\vfsStream;
use OxidEsales\GdprOptinModule\UserData\DataType\ResultFileInterface;
use OxidEsales\GdprOptinModule\UserData\Exception\ZipCreationException;
use OxidEsales\GdprOptinModule\UserData\Service\ZipArchiveFactoryInterface;
use OxidEsales\GdprOptinModule\UserData\Service\ZipCreatorService;
use PHPUnit\Framework\TestCase;
use ZipArchive;
Expand All @@ -20,49 +19,33 @@ class ZipCreatorServiceTest extends TestCase
{
public function testZipCreation(): void
{
vfsStream::setup('outputDir');
$outputZipFilePath = vfsStream::url('outputDir/test_zip_file.zip');
$fileName = uniqid();
$fileContent = uniqid();

$resultFileInterStub = $this->createConfiguredMock(ResultFileInterface::class, [
'getFileName' => $fileName,
'getContent' => $fileContent,
]);

$zipArchiveSpy = $this->createMock(ZipArchive::class);
$zipArchiveSpy
->method('open')
->with($outputZipFilePath, ZipArchive::CREATE)
->willReturn(true);

$zipArchiveSpy
$zipArchiveMock = $this->createMock(ZipArchive::class);
$zipArchiveMock
->method('addFromString')
->with($fileName, $fileContent);

$sut = new ZipCreatorService(zipArchive: $zipArchiveSpy);
$sut->createZip(files: [$resultFileInterStub], outputFilePath: $outputZipFilePath);

$zipArchiveSpy
->method('close');
}
->with($fileName, $fileContent)
->willReturn(true);

public function testCreateZipThrowsExceptionWhenUnableToOpen(): void
{
vfsStream::setup('outputDir');
$outputZipFilePath = vfsStream::url('outputDir/output.zip');
$zipArchiveMock
->method('close')
->willReturn(true);

$this->expectException(ZipCreationException::class);
$this->expectExceptionMessage("Unable to open zip file at {$outputZipFilePath}");
$zipFileName = 'output/test.zip';
$zipArchiveFactoryMock = $this->createMock(ZipArchiveFactoryInterface::class);
$zipArchiveFactoryMock
->method('create')
->with($zipFileName)
->willReturn($zipArchiveMock);

$zipArchiveMock = $this->createMock(ZipArchive::class);
$zipArchiveMock
->method('open')
->with($outputZipFilePath, ZipArchive::CREATE)
->willReturn(false);
$sut = new ZipCreatorService(zipArchiveFactory: $zipArchiveFactoryMock);

$resultFileMock = $this->createConfiguredMock(ResultFileInterface::class, [
'getFileName' => $fileName,
'getContent' => $fileContent,
]);

$sut = new ZipCreatorService(zipArchive: $zipArchiveMock);
$sut->createZip([], $outputZipFilePath);
$sut->createZip(files: [$resultFileMock], outputFilePath: $zipFileName);
}
}

0 comments on commit fb5cf61

Please sign in to comment.