Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for Zip Helper #48

Merged
merged 3 commits into from
Oct 31, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions tests/Helpers/Support/Zip/Destination/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*
*/
!.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
## Subfolder test
This is a text file inside a sub folder.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
## Subfolder test
This is a text file in the root folder.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions tests/Helpers/Support/Zip/Source/MultipleFiles/1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
## File One (File 1)
This is a test for the Multiple File Zip Test
2 changes: 2 additions & 0 deletions tests/Helpers/Support/Zip/Source/MultipleFiles/2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
## File Two (File 2)
This is a test for the Multiple File Zip Test
2 changes: 2 additions & 0 deletions tests/Helpers/Support/Zip/Source/MultipleFiles/3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
## File Three (File 3)
This is a test for the Multiple File Zip Test
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
136 changes: 136 additions & 0 deletions tests/Helpers/ZipTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?php

namespace Sammyjo20\Lasso\Tests\Helpers;

use Illuminate\Support\Facades\File;
use Sammyjo20\Lasso\Helpers\Zip;
use Sammyjo20\Lasso\Tests\TestCase;
use ZipArchive;

class ZipTest extends TestCase
{
private $sourceDirectory;
private $destinationDirectory;

protected function setUp(): void
{
parent::setUp();

$this->sourceDirectory = __DIR__ . '/Support/Zip/Source';
$this->destinationDirectory = __DIR__ . '/Support/Zip/Destination';

$this->cleanUpPreviousArtifacts();
}

/** @test */
public function it_can_add_a_single_file_from_a_source_directory_to_a_zip_file(): void
{
$sourceFile = ['SingleFile/logo.png'];
$zipFile = $this->destinationDirectory . '/SingleFile.zip';

$this->assertFileDoesNotExist($zipFile);

(new Zip($zipFile))
->addFilesFromDirectory($this->sourceDirectory .'/SingleFile')
->closeZip();

$this->assertFileExists($zipFile);

$this->assertZipFileContains($sourceFile, $zipFile);
}

/** @test */
public function it_adds_all_files_within_a_source_directory_to_a_zip_file(): void
{
$sourceFiles = [
'MultipleFiles/1.txt',
'MultipleFiles/2.txt',
'MultipleFiles/3.txt',
];

$zipFile = $this->destinationDirectory . '/MultiFile.zip';

$this->assertFileDoesNotExist($zipFile);

(new Zip($zipFile))
->addFilesFromDirectory($this->sourceDirectory .'/MultipleFiles')
->closeZip();

$this->assertFileExists($zipFile);

$this->assertZipFileContains($sourceFiles, $zipFile);
}

/** @test */
public function it_adds_all_files_within_a_source_directory_including_sub_folders_to_a_zip_file(): void
{
$sourceFiles = [
'FilesWithSubFolder/SubFolder/in_sub_folder.txt',
'FilesWithSubFolder/SubFolder/logo_in_sub_folder.png',
'FilesWithSubFolder/logo_in_root_folder.png',
'FilesWithSubFolder/in_root_folder.txt',
];

$zipFile = $this->destinationDirectory . '/WithSubFolder.zip';

$this->assertFileDoesNotExist($zipFile);

(new Zip($zipFile))
->addFilesFromDirectory($this->sourceDirectory .'/FilesWithSubFolder')
->closeZip();

$this->assertFileExists($zipFile);

$this->assertZipFileContains($sourceFiles, $zipFile);
}

/** @test */
public function it_can_unzip_an_existing_zip_file_correctly(): void
{
dd(hash_file('md5', $this->sourceDirectory));
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test appears to be incomplete


private function assertZipFileContains(array $sourceFiles, string $destinationZipFile): void
{
$inspectZipFile = new ZipArchive();
$inspectZipFile->open($destinationZipFile);

collect($sourceFiles)->each(function (string $filePath) use ($inspectZipFile) {
$relativePath = $this->getRelativePath($filePath);

$this->assertSame(
file_get_contents($this->sourceDirectory . '/' . $filePath),
$inspectZipFile->getFromName($relativePath)
);
});
}

/**
* Clean working directory at the start by purging files generated in previous tests,
* to be able to inspect the files manually if needed after a specific test has run.
*/
private function cleanUpPreviousArtifacts(): void
{
File::cleanDirectory($this->destinationDirectory);
}

/**
* @param string $filePath
* @return false|string
*
* Returns the relative path of source files, to be located in the zip file.
*/
private function getRelativePath(string $filePath): string
{
// Find the root directory (first directory listed)
$rootDirectory = explode('/', $filePath)[0];

// Remove the root directory from the given path
$normalizedPath = str_replace($rootDirectory, "", $filePath);

// Remove preliminary forward slash "/Dir" -> "Dir"
$relativePath = substr($normalizedPath, 1);

return $relativePath;
}
}