From 3f61d1807cc23aedf56e4c81ff1677a1a283a027 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Sun, 18 May 2014 00:40:29 +0700 Subject: [PATCH] Test fixes --- src/PhpWord/Shared/ZipArchive.php | 14 ++-- src/PhpWord/Writer/AbstractWriter.php | 5 +- tests/PhpWord/Tests/Shared/ZipArchiveTest.php | 74 +++++++++---------- 3 files changed, 44 insertions(+), 49 deletions(-) diff --git a/src/PhpWord/Shared/ZipArchive.php b/src/PhpWord/Shared/ZipArchive.php index 04c3e1fe..52bf0bce 100644 --- a/src/PhpWord/Shared/ZipArchive.php +++ b/src/PhpWord/Shared/ZipArchive.php @@ -31,9 +31,9 @@ use PhpOffice\PhpWord\Settings; * @method bool addFromString(string $localname, string $contents) * @method bool close() * @method bool extractTo(string $destination, mixed $entries = null) - * @method bool getFromName(string $name) - * @method bool getNameIndex(int $index) - * @method bool locateName (string $name) + * @method string getFromName(string $name) + * @method string getNameIndex(int $index) + * @method int locateName(string $name) * @method bool open(string $filename, int $flags = null) * @since 0.10.0 */ @@ -142,7 +142,7 @@ class ZipArchive * * @param string $destination * @param string|array $entries - * @return boolean + * @return bool * @since 0.10.0 */ public function extractTo($destination, $entries = null) @@ -266,7 +266,7 @@ class ZipArchive * * @param string $destination * @param string|array $entries - * @return boolean + * @return bool * @since 0.10.0 */ public function pclzipExtractTo($destination, $entries = null) @@ -320,7 +320,7 @@ class ZipArchive /** * Returns the name of an entry using its index (emulate \ZipArchive) * - * @param integer $index + * @param int $index * @return string|false * @since 0.10.0 */ @@ -338,7 +338,7 @@ class ZipArchive * Returns the index of the entry in the archive (emulate \ZipArchive) * * @param string $filename Filename for the file in zip archive - * @return integer|false + * @return int|false */ public function pclzipLocateName($filename) { diff --git a/src/PhpWord/Writer/AbstractWriter.php b/src/PhpWord/Writer/AbstractWriter.php index 30c133cd..52a1a28c 100644 --- a/src/PhpWord/Writer/AbstractWriter.php +++ b/src/PhpWord/Writer/AbstractWriter.php @@ -281,11 +281,10 @@ abstract class AbstractWriter implements WriterInterface /** * Add files to package * - * @param $zip + * @param \PhpOffice\PhpWord\Shared\ZipArchive $zip * @param mixed $elements - * @return \PhpOffice\PhpWord\Shared\ZipArchive $zip */ - protected function addFilesToPackage($zip, $elements) + protected function addFilesToPackage(ZipArchive $zip, $elements) { foreach ($elements as $element) { $type = $element['type']; // image|object|link diff --git a/tests/PhpWord/Tests/Shared/ZipArchiveTest.php b/tests/PhpWord/Tests/Shared/ZipArchiveTest.php index d4dad71b..5efe18d9 100644 --- a/tests/PhpWord/Tests/Shared/ZipArchiveTest.php +++ b/tests/PhpWord/Tests/Shared/ZipArchiveTest.php @@ -28,12 +28,43 @@ use PhpOffice\PhpWord\Shared\ZipArchive; */ class ZipArchiveTest extends \PHPUnit_Framework_TestCase { + + /** + * Test close method exception + * + * @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 + $fp = fopen($zipFile, "w"); + flock($fp, LOCK_EX); + + // Closing the file should throws an exception + $object->close(); + + // Unlock the file + flock($fp, LOCK_UN); + fclose($fp); + + @unlink($zipFile); + } + /** * Test all methods * + * @param string $zipClass * @covers :: */ - public function testZipArchive() + public function testZipArchive($zipClass = 'ZipArchive') { // Preparation $existingFile = __DIR__ . "/../_files/documents/sheet.xls"; @@ -43,7 +74,7 @@ class ZipArchiveTest extends \PHPUnit_Framework_TestCase @mkdir($destination1); @mkdir($destination2); - Settings::setZipClass('PhpOffice\PhpWord\Shared\ZipArchive'); + Settings::setZipClass($zipClass); $object = new ZipArchive(); $object->open($zipFile, ZipArchive::CREATE); @@ -74,48 +105,13 @@ class ZipArchiveTest extends \PHPUnit_Framework_TestCase } /** - * Test all methods + * Test PclZip * * @covers :: */ public function testPCLZip() { - // 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); - - Settings::setZipClass('ZipArchive'); - - $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 - $this->assertEquals(0, $object->locateName('xls/new.xls')); - $this->assertFalse($object->locateName('blablabla')); - - $this->assertEquals('Test', $object->getFromName('content/string.txt')); - $this->assertEquals('Test', $object->getFromName('/content/string.txt')); - - $this->assertFalse($object->getNameIndex(-1)); - $this->assertEquals('content/string.txt', $object->getNameIndex(1)); - - $this->assertFalse($object->extractTo('blablabla')); - $this->assertTrue($object->extractTo($destination1)); - $this->assertTrue($object->extractTo($destination2, 'xls/new.xls')); - $this->assertFalse($object->extractTo($destination2, 'blablabla')); - - // Cleanup - $this->deleteDir($destination1); - $this->deleteDir($destination2); - @unlink($zipFile); + $this->testZipArchive('PhpOffice\PhpWord\Shared\ZipArchive'); } /**