Skip to content

Commit 3f61d18

Browse files
committed
Test fixes
1 parent 5519131 commit 3f61d18

File tree

3 files changed

+37
-42
lines changed

3 files changed

+37
-42
lines changed

src/PhpWord/Shared/ZipArchive.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@
3131
* @method bool addFromString(string $localname, string $contents)
3232
* @method bool close()
3333
* @method bool extractTo(string $destination, mixed $entries = null)
34-
* @method bool getFromName(string $name)
35-
* @method bool getNameIndex(int $index)
36-
* @method bool locateName (string $name)
34+
* @method string getFromName(string $name)
35+
* @method string getNameIndex(int $index)
36+
* @method int locateName(string $name)
3737
* @method bool open(string $filename, int $flags = null)
3838
* @since 0.10.0
3939
*/
@@ -142,7 +142,7 @@ public function close()
142142
*
143143
* @param string $destination
144144
* @param string|array $entries
145-
* @return boolean
145+
* @return bool
146146
* @since 0.10.0
147147
*/
148148
public function extractTo($destination, $entries = null)
@@ -266,7 +266,7 @@ public function pclzipAddFromString($localname, $contents)
266266
*
267267
* @param string $destination
268268
* @param string|array $entries
269-
* @return boolean
269+
* @return bool
270270
* @since 0.10.0
271271
*/
272272
public function pclzipExtractTo($destination, $entries = null)
@@ -320,7 +320,7 @@ public function pclzipGetFromName($filename)
320320
/**
321321
* Returns the name of an entry using its index (emulate \ZipArchive)
322322
*
323-
* @param integer $index
323+
* @param int $index
324324
* @return string|false
325325
* @since 0.10.0
326326
*/
@@ -338,7 +338,7 @@ public function pclzipGetNameIndex($index)
338338
* Returns the index of the entry in the archive (emulate \ZipArchive)
339339
*
340340
* @param string $filename Filename for the file in zip archive
341-
* @return integer|false
341+
* @return int|false
342342
*/
343343
public function pclzipLocateName($filename)
344344
{

src/PhpWord/Writer/AbstractWriter.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,11 +281,10 @@ protected function getZipArchive($filename)
281281
/**
282282
* Add files to package
283283
*
284-
* @param $zip
284+
* @param \PhpOffice\PhpWord\Shared\ZipArchive $zip
285285
* @param mixed $elements
286-
* @return \PhpOffice\PhpWord\Shared\ZipArchive $zip
287286
*/
288-
protected function addFilesToPackage($zip, $elements)
287+
protected function addFilesToPackage(ZipArchive $zip, $elements)
289288
{
290289
foreach ($elements as $element) {
291290
$type = $element['type']; // image|object|link

tests/PhpWord/Tests/Shared/ZipArchiveTest.php

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -28,57 +28,43 @@
2828
*/
2929
class ZipArchiveTest extends \PHPUnit_Framework_TestCase
3030
{
31+
3132
/**
32-
* Test all methods
33+
* Test close method exception
3334
*
34-
* @covers ::<public>
35+
* @expectedException \PhpOffice\PhpWord\Exception\Exception
36+
* @expectedExceptionMessage Could not close zip file
37+
* @covers ::close
3538
*/
36-
public function testZipArchive()
39+
public function testCloseException()
3740
{
38-
// Preparation
39-
$existingFile = __DIR__ . "/../_files/documents/sheet.xls";
40-
$zipFile = __DIR__ . "/../_files/documents/ziptest.zip";
41-
$destination1 = __DIR__ . "/../_files/documents/extract1";
42-
$destination2 = __DIR__ . "/../_files/documents/extract2";
43-
@mkdir($destination1);
44-
@mkdir($destination2);
45-
46-
Settings::setZipClass('PhpOffice\PhpWord\Shared\ZipArchive');
41+
$zipFile = __DIR__ . "/../_files/documents/ziptest.zip";
4742

4843
$object = new ZipArchive();
4944
$object->open($zipFile, ZipArchive::CREATE);
50-
$object->addFile($existingFile, 'xls/new.xls');
5145
$object->addFromString('content/string.txt', 'Test');
52-
$object->close();
53-
$object->open($zipFile);
54-
55-
// Run tests
56-
$this->assertEquals(0, $object->locateName('xls/new.xls'));
57-
$this->assertFalse($object->locateName('blablabla'));
5846

59-
$this->assertEquals('Test', $object->getFromName('content/string.txt'));
60-
$this->assertEquals('Test', $object->getFromName('/content/string.txt'));
47+
// Lock the file
48+
$fp = fopen($zipFile, "w");
49+
flock($fp, LOCK_EX);
6150

62-
$this->assertFalse($object->getNameIndex(-1));
63-
$this->assertEquals('content/string.txt', $object->getNameIndex(1));
51+
// Closing the file should throws an exception
52+
$object->close();
6453

65-
$this->assertFalse($object->extractTo('blablabla'));
66-
$this->assertTrue($object->extractTo($destination1));
67-
$this->assertTrue($object->extractTo($destination2, 'xls/new.xls'));
68-
$this->assertFalse($object->extractTo($destination2, 'blablabla'));
54+
// Unlock the file
55+
flock($fp, LOCK_UN);
56+
fclose($fp);
6957

70-
// Cleanup
71-
$this->deleteDir($destination1);
72-
$this->deleteDir($destination2);
7358
@unlink($zipFile);
7459
}
7560

7661
/**
7762
* Test all methods
7863
*
64+
* @param string $zipClass
7965
* @covers ::<public>
8066
*/
81-
public function testPCLZip()
67+
public function testZipArchive($zipClass = 'ZipArchive')
8268
{
8369
// Preparation
8470
$existingFile = __DIR__ . "/../_files/documents/sheet.xls";
@@ -88,7 +74,7 @@ public function testPCLZip()
8874
@mkdir($destination1);
8975
@mkdir($destination2);
9076

91-
Settings::setZipClass('ZipArchive');
77+
Settings::setZipClass($zipClass);
9278

9379
$object = new ZipArchive();
9480
$object->open($zipFile, ZipArchive::CREATE);
@@ -118,6 +104,16 @@ public function testPCLZip()
118104
@unlink($zipFile);
119105
}
120106

107+
/**
108+
* Test PclZip
109+
*
110+
* @covers ::<public>
111+
*/
112+
public function testPCLZip()
113+
{
114+
$this->testZipArchive('PhpOffice\PhpWord\Shared\ZipArchive');
115+
}
116+
121117
/**
122118
* Delete directory
123119
*

0 commit comments

Comments
 (0)