-
-
Notifications
You must be signed in to change notification settings - Fork 17
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
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
* | ||
*/ | ||
!.gitignore |
2 changes: 2 additions & 0 deletions
2
tests/Helpers/Support/Zip/Source/FilesWithSubFolder/SubFolder/in_sub_folder.txt
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,2 @@ | ||
## Subfolder test | ||
This is a text file inside a sub folder. |
Binary file added
BIN
+73.3 KB
.../Helpers/Support/Zip/Source/FilesWithSubFolder/SubFolder/logo_in_sub_folder.png
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
2
tests/Helpers/Support/Zip/Source/FilesWithSubFolder/in_root_folder.txt
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,2 @@ | ||
## Subfolder test | ||
This is a text file in the root folder. |
Binary file added
BIN
+73.3 KB
tests/Helpers/Support/Zip/Source/FilesWithSubFolder/logo_in_root_folder.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,2 @@ | ||
## File One (File 1) | ||
This is a test for the Multiple File Zip Test |
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,2 @@ | ||
## File Two (File 2) | ||
This is a test for the Multiple File Zip Test |
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,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.
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,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)); | ||
} | ||
|
||
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; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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