Skip to content

Commit

Permalink
no message
Browse files Browse the repository at this point in the history
  • Loading branch information
Mubashar Ahmad committed Aug 22, 2022
1 parent 098fc10 commit 89c68fa
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 1 deletion.
12 changes: 11 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,25 @@
"merge"
],
"require": {
"php": ">=8.0",
"php": "^7.4 || ^8.0",
"illuminate/support": "^6.0|^7.0|^8.0|^9.0",
"ext-zip": "*"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"scrutinizer/ocular": "^1.9",
"orchestra/testbench": "^6.24"
},
"autoload": {
"psr-4": {
"ZanySoft\\Zip\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"ZanySoft\\Zip\\": "tests"
}
},
"extra": {
"laravel": {
"providers": [
Expand Down
16 changes: 16 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
1 change: 1 addition & 0 deletions tests/TestFiles/file1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Test File 1 Content
1 change: 1 addition & 0 deletions tests/TestFiles/file2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Test File 2 Content
Binary file added tests/TestFiles/password.zip
Binary file not shown.
128 changes: 128 additions & 0 deletions tests/ZipTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php


namespace ZanySoft\Zip\Tests;


use Orchestra\Testbench\TestCase;
use ZanySoft\Zip\Zip;

class ZipTest extends TestCase
{
private $testFilePath;

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

$this->testFilePath = __DIR__ . '/TestFiles';
}

public function tearDown(): void
{
parent::tearDown();

// array_map('unlink', glob($this->filesPath('Zips/*')) ?: []);
$this->deleteTestFiles();
}

public function testCreateZipFileWithoutAddingFiles()
{
$zip = Zip::create($this->filesPath('Zips/TestZip.zip'), true);
$zip->close();

$this->assertFalse(file_exists($this->filesPath('Zips/TestZip.zip')));
}

public function testCreateZipWithAddingFiles()
{
$zip = Zip::create($this->filesPath('Zips/TestZip.zip'));
$zip->add($this->filesPath('file1.txt'));
$zip->close();

$this->assertTrue(file_exists($this->filesPath('Zips/TestZip.zip')));
}

public function testSetSkipThrowsExceptionOnInvalidMode()
{
$this->expectException(\Exception::class);

$zip = Zip::create($this->filesPath('Zips/TestZip.zip'));
$zip->setSkipped('doesntexist');
}

public function testSetValidSkipMode()
{
$zip = Zip::create($this->filesPath('Zips/TestZip.zip'));
$zip->setSkipped('hidden');
$zip->close();

$this->assertEquals('HIDDEN', $zip->getSkipped());
}

public function testListFiles()
{
$zip = Zip::create($this->filesPath('Zips/TestZip.zip'));
$zip->add($this->filesPath('file1.txt'));
$zip->add($this->filesPath('file2.txt'));

$this->assertEquals(2, count($zip->listFiles()));
$this->assertEquals('file1.txt', $zip->listFiles()[0]);
$this->assertEquals('file2.txt', $zip->listFiles()[1]);

$zip->close();
}

public function testHasFile()
{
$zip = Zip::create($this->filesPath('Zips/TestZip.zip'));
$zip->add($this->filesPath('file1.txt'));

$this->assertTrue($zip->has('file1.txt'));

$zip->close();
}

public function testHasFileDoesNotExist()
{
$zip = Zip::create($this->filesPath('Zips/TestZip.zip'));
$zip->add($this->filesPath('file1.txt'));

$this->assertFalse($zip->has('file2.txt'));

$zip->close();
}

public function testExtract()
{
$zip = Zip::open($this->filesPath('password.zip'));
$zip->setPassword('password');
$zip->extract($this->filesPath('Zips'));

$this->assertTrue(file_exists($this->filesPath('Zips/file1.txt')));

$zip->close();
}

public function deleteTestFiles()
{
$directoryPath = $this->filesPath('Zips');

$files = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($directoryPath, \RecursiveDirectoryIterator::SKIP_DOTS),
\RecursiveIteratorIterator::CHILD_FIRST
);

foreach ($files as $fileinfo) {
$removeFunction = ($fileinfo->isDir() ? 'rmdir' : 'unlink');
$removeFunction($fileinfo->getRealPath());
}

return true;
}

private function filesPath($file)
{
return $this->testFilePath . '/' . $file;
}
}

0 comments on commit 89c68fa

Please sign in to comment.