PHPWord/tests/PhpWordTests/Shared/ZipArchiveTest.php

138 lines
4.1 KiB
PHP
Raw Normal View History

<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @see https://github.com/PHPOffice/PHPWord
2022-09-16 11:45:45 +02:00
*
2014-05-04 21:03:28 +04:00
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
2022-09-16 14:09:17 +02:00
namespace PhpOffice\PhpWordTests\Shared;
use PhpOffice\PhpWord\Settings;
2022-09-16 14:09:17 +02:00
use PhpOffice\PhpWord\Shared\ZipArchive;
/**
2022-09-16 11:45:45 +02:00
* Test class for PhpOffice\PhpWord\Shared\ZipArchive.
*
* @coversDefaultClass \PhpOffice\PhpWord\Shared\ZipArchive
2022-09-16 11:45:45 +02:00
*
* @runTestsInSeparateProcesses
*/
class ZipArchiveTest extends \PHPUnit\Framework\TestCase
{
// /**
// * Test close method exception: Working in local, not working in Travis
// *
// * expectedException \PhpOffice\PhpWord\Exception\Exception
// * expectedExceptionMessage Could not close zip file
// * covers ::close
// */
// public function testCloseException()
// {
// $zipFile = __DIR__ . "/../_files/documents/ziptest.zip";
// $object = new ZipArchive();
// $object->open($zipFile, ZipArchive::CREATE);
// $object->addFromString('content/string.txt', 'Test');
// // Lock the file
// $resource = fopen($zipFile, "w");
// flock($resource, LOCK_EX);
// // Closing the file should throws an exception
// $object->close();
// // Unlock the file
// flock($resource, LOCK_UN);
// fclose($resource);
// @unlink($zipFile);
// }
/**
2022-09-16 11:45:45 +02:00
* Test all methods.
*
2014-05-18 00:40:29 +07:00
* @param string $zipClass
2022-09-16 11:45:45 +02:00
*
* @covers ::<public>
*/
2022-09-16 11:45:45 +02:00
public function testZipArchive($zipClass = 'ZipArchive'): void
{
// Preparation
$existingFile = __DIR__ . '/../_files/documents/sheet.xls';
$zipFile = __DIR__ . '/../_files/documents/ziptest.zip';
$destination1 = __DIR__ . '/../_files/documents/extract1';
$destination2 = __DIR__ . '/../_files/documents/extract2';
@mkdir($destination1);
@mkdir($destination2);
2014-05-18 00:40:29 +07:00
Settings::setZipClass($zipClass);
$object = new ZipArchive();
$object->open($zipFile, ZipArchive::CREATE);
$object->addFile($existingFile, 'xls/new.xls');
$object->addFromString('content/string.txt', 'Test');
$object->close();
$object->open($zipFile);
// Run tests
2022-09-16 11:45:45 +02:00
self::assertEquals(0, $object->locateName('xls/new.xls'));
self::assertFalse($object->locateName('blablabla'));
2022-09-16 11:45:45 +02:00
self::assertEquals('Test', $object->getFromName('content/string.txt'));
self::assertEquals('Test', $object->getFromName('/content/string.txt'));
2022-09-16 11:45:45 +02:00
self::assertFalse($object->getNameIndex(-1));
self::assertEquals('content/string.txt', $object->getNameIndex(1));
2022-09-16 11:45:45 +02:00
self::assertFalse($object->extractTo('blablabla'));
self::assertTrue($object->extractTo($destination1));
self::assertTrue($object->extractTo($destination2, 'xls/new.xls'));
self::assertFalse($object->extractTo($destination2, 'blablabla'));
// Cleanup
$this->deleteDir($destination1);
$this->deleteDir($destination2);
@unlink($zipFile);
}
2014-05-18 00:40:29 +07:00
/**
2022-09-16 11:45:45 +02:00
* Test PclZip.
2014-05-18 00:40:29 +07:00
*
* @covers ::<public>
*/
2022-09-16 11:45:45 +02:00
public function testPCLZip(): void
2014-05-18 00:40:29 +07:00
{
$this->testZipArchive('PhpOffice\PhpWord\Shared\ZipArchive');
}
2014-04-12 10:23:31 +07:00
/**
2022-09-16 11:45:45 +02:00
* Delete directory.
*
* @param string $dir
2014-04-12 10:23:31 +07:00
*/
2022-09-16 11:45:45 +02:00
private function deleteDir($dir): void
{
foreach (scandir($dir) as $file) {
if ('.' === $file || '..' === $file) {
continue;
} elseif (is_file($dir . '/' . $file)) {
unlink($dir . '/' . $file);
} elseif (is_dir($dir . '/' . $file)) {
$this->deleteDir($dir . '/' . $file);
}
}
rmdir($dir);
}
}