Test fixes

This commit is contained in:
Ivan Lanin 2014-05-18 00:40:29 +07:00
parent 5519131f3e
commit 3f61d1807c
3 changed files with 44 additions and 49 deletions

View File

@ -31,9 +31,9 @@ use PhpOffice\PhpWord\Settings;
* @method bool addFromString(string $localname, string $contents) * @method bool addFromString(string $localname, string $contents)
* @method bool close() * @method bool close()
* @method bool extractTo(string $destination, mixed $entries = null) * @method bool extractTo(string $destination, mixed $entries = null)
* @method bool getFromName(string $name) * @method string getFromName(string $name)
* @method bool getNameIndex(int $index) * @method string getNameIndex(int $index)
* @method bool locateName (string $name) * @method int locateName(string $name)
* @method bool open(string $filename, int $flags = null) * @method bool open(string $filename, int $flags = null)
* @since 0.10.0 * @since 0.10.0
*/ */
@ -142,7 +142,7 @@ class ZipArchive
* *
* @param string $destination * @param string $destination
* @param string|array $entries * @param string|array $entries
* @return boolean * @return bool
* @since 0.10.0 * @since 0.10.0
*/ */
public function extractTo($destination, $entries = null) public function extractTo($destination, $entries = null)
@ -266,7 +266,7 @@ class ZipArchive
* *
* @param string $destination * @param string $destination
* @param string|array $entries * @param string|array $entries
* @return boolean * @return bool
* @since 0.10.0 * @since 0.10.0
*/ */
public function pclzipExtractTo($destination, $entries = null) public function pclzipExtractTo($destination, $entries = null)
@ -320,7 +320,7 @@ class ZipArchive
/** /**
* Returns the name of an entry using its index (emulate \ZipArchive) * Returns the name of an entry using its index (emulate \ZipArchive)
* *
* @param integer $index * @param int $index
* @return string|false * @return string|false
* @since 0.10.0 * @since 0.10.0
*/ */
@ -338,7 +338,7 @@ class ZipArchive
* Returns the index of the entry in the archive (emulate \ZipArchive) * Returns the index of the entry in the archive (emulate \ZipArchive)
* *
* @param string $filename Filename for the file in zip archive * @param string $filename Filename for the file in zip archive
* @return integer|false * @return int|false
*/ */
public function pclzipLocateName($filename) public function pclzipLocateName($filename)
{ {

View File

@ -281,11 +281,10 @@ abstract class AbstractWriter implements WriterInterface
/** /**
* Add files to package * Add files to package
* *
* @param $zip * @param \PhpOffice\PhpWord\Shared\ZipArchive $zip
* @param mixed $elements * @param mixed $elements
* @return \PhpOffice\PhpWord\Shared\ZipArchive $zip
*/ */
protected function addFilesToPackage($zip, $elements) protected function addFilesToPackage(ZipArchive $zip, $elements)
{ {
foreach ($elements as $element) { foreach ($elements as $element) {
$type = $element['type']; // image|object|link $type = $element['type']; // image|object|link

View File

@ -28,12 +28,43 @@ use PhpOffice\PhpWord\Shared\ZipArchive;
*/ */
class ZipArchiveTest extends \PHPUnit_Framework_TestCase 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 * Test all methods
* *
* @param string $zipClass
* @covers ::<public> * @covers ::<public>
*/ */
public function testZipArchive() public function testZipArchive($zipClass = 'ZipArchive')
{ {
// Preparation // Preparation
$existingFile = __DIR__ . "/../_files/documents/sheet.xls"; $existingFile = __DIR__ . "/../_files/documents/sheet.xls";
@ -43,7 +74,7 @@ class ZipArchiveTest extends \PHPUnit_Framework_TestCase
@mkdir($destination1); @mkdir($destination1);
@mkdir($destination2); @mkdir($destination2);
Settings::setZipClass('PhpOffice\PhpWord\Shared\ZipArchive'); Settings::setZipClass($zipClass);
$object = new ZipArchive(); $object = new ZipArchive();
$object->open($zipFile, ZipArchive::CREATE); $object->open($zipFile, ZipArchive::CREATE);
@ -74,48 +105,13 @@ class ZipArchiveTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* Test all methods * Test PclZip
* *
* @covers ::<public> * @covers ::<public>
*/ */
public function testPCLZip() public function testPCLZip()
{ {
// Preparation $this->testZipArchive('PhpOffice\PhpWord\Shared\ZipArchive');
$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);
} }
/** /**