mirror of
https://github.com/filegator/filegator.git
synced 2025-08-24 19:52:51 +02:00
initial commit
This commit is contained in:
132
tests/backend/Unit/ArchiverTest.php
Normal file
132
tests/backend/Unit/ArchiverTest.php
Normal file
@@ -0,0 +1,132 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the FileGator package.
|
||||
*
|
||||
* (c) Milos Stojanovic <alcalbg@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE file
|
||||
*/
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use Filegator\Services\Archiver\Adapters\ZipArchiver;
|
||||
use Filegator\Services\Storage\Filesystem;
|
||||
use Filegator\Services\Tmpfs\Adapters\Tmpfs;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class ArchiverTest extends TestCase
|
||||
{
|
||||
protected $archiver;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
$tmpfs = new Tmpfs();
|
||||
$tmpfs->init([
|
||||
'path' => TEST_TMP_PATH,
|
||||
'gc_probability_perc' => 10,
|
||||
'gc_older_than' => 60 * 60 * 24 * 2, // 2 days
|
||||
]);
|
||||
|
||||
$this->archiver = new ZipArchiver($tmpfs);
|
||||
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
public function testCreatingArchive()
|
||||
{
|
||||
$storage = new Filesystem();
|
||||
$storage->init([
|
||||
'separator' => '/',
|
||||
'filesystem_adapter' => 'nulladapter',
|
||||
'adapters' => [
|
||||
'nulladapter' => function () {
|
||||
return new \League\Flysystem\Adapter\NullAdapter();
|
||||
},
|
||||
],
|
||||
]);
|
||||
|
||||
$uniqid = $this->archiver->createArchive($storage);
|
||||
|
||||
$this->assertNotNull($uniqid);
|
||||
$this->assertFileExists(TEST_TMP_PATH.$uniqid);
|
||||
}
|
||||
|
||||
public function testAddingDirectoryWithFilesAndSubdir()
|
||||
{
|
||||
$storage = new Filesystem();
|
||||
$storage->init([
|
||||
'separator' => '/',
|
||||
'filesystem_adapter' => 'memoryadapter',
|
||||
'adapters' => [
|
||||
'memoryadapter' => function () {
|
||||
return new \League\Flysystem\Memory\MemoryAdapter();
|
||||
},
|
||||
],
|
||||
]);
|
||||
|
||||
$storage->createDir('/', 'test');
|
||||
$storage->createDir('/test', 'sub');
|
||||
$storage->createFile('/test', 'file1.txt');
|
||||
$storage->createFile('/test', 'file2.txt');
|
||||
|
||||
$name = $this->archiver->createArchive($storage);
|
||||
$this->archiver->addDirectoryFromStorage('/test');
|
||||
$this->archiver->closeArchive();
|
||||
|
||||
$this->assertGreaterThan(0, filesize(TEST_TMP_PATH.$name));
|
||||
}
|
||||
|
||||
public function testUploadingArchiveToStorage()
|
||||
{
|
||||
$storage = new Filesystem();
|
||||
$storage->init([
|
||||
'separator' => '/',
|
||||
'filesystem_adapter' => 'memoryadapter',
|
||||
'adapters' => [
|
||||
'memoryadapter' => function () {
|
||||
return new \League\Flysystem\Memory\MemoryAdapter();
|
||||
},
|
||||
],
|
||||
]);
|
||||
|
||||
$storage->createDir('/', 'test');
|
||||
$storage->createDir('/test', 'sub');
|
||||
$storage->createFile('/test', 'file1.txt');
|
||||
$storage->createFile('/test', 'file2.txt');
|
||||
|
||||
$name = $this->archiver->createArchive($storage);
|
||||
$this->archiver->addDirectoryFromStorage('/test');
|
||||
$this->archiver->storeArchive('/destination', 'myarchive.zip');
|
||||
|
||||
$this->assertFileNotExists(TEST_TMP_PATH.$name);
|
||||
}
|
||||
|
||||
public function testUncompressingArchiveFromStorage()
|
||||
{
|
||||
$storage = new Filesystem();
|
||||
$storage->init([
|
||||
'separator' => '/',
|
||||
'filesystem_adapter' => 'memoryadapter',
|
||||
'adapters' => [
|
||||
'memoryadapter' => function () {
|
||||
return new \League\Flysystem\Memory\MemoryAdapter();
|
||||
},
|
||||
],
|
||||
]);
|
||||
|
||||
$stream = fopen(TEST_ARCHIVE, 'r');
|
||||
$storage->store('/', 'testarchive.zip', $stream);
|
||||
fclose($stream);
|
||||
|
||||
$storage->createDir('/', 'result');
|
||||
|
||||
$this->archiver->uncompress('/testarchive.zip', '/result', $storage);
|
||||
|
||||
$this->assertStringContainsString('testarchive', (json_encode($storage->getDirectoryCollection('/'))));
|
||||
$this->assertStringContainsString('onetwo', (json_encode($storage->getDirectoryCollection('/result'))));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user