-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OXDEV-8730 Created a ZipArchiveFactory used in ZipCreatorService
Signed-off-by: Anton Fedurtsya <anton@fedurtsya.com>
- Loading branch information
1 parent
6984adf
commit fb5cf61
Showing
7 changed files
with
126 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters