diff --git a/composer.json b/composer.json index 16b02fd..7a56479 100644 --- a/composer.json +++ b/composer.json @@ -41,7 +41,8 @@ "suggest": { "ext-openssl": "Needed to support encrypt zip entries or use ext-mcrypt", "ext-mcrypt": "Needed to support encrypt zip entries or use ext-openssl", - "ext-bz2": "Needed to support BZIP2 compression" + "ext-bz2": "Needed to support BZIP2 compression", + "ext-fileinfo": "Needed to get mime-type file" }, "minimum-stability": "stable" } diff --git a/src/PhpZip/Crypto/TraditionalPkwareEncryptionEngine.php b/src/PhpZip/Crypto/TraditionalPkwareEncryptionEngine.php index 961ac1f..45881e0 100644 --- a/src/PhpZip/Crypto/TraditionalPkwareEncryptionEngine.php +++ b/src/PhpZip/Crypto/TraditionalPkwareEncryptionEngine.php @@ -176,6 +176,7 @@ class TraditionalPkwareEncryptionEngine implements ZipEncryptionEngine * * @param string $data * @return string + * @throws ZipCryptoException */ public function encrypt($data) { @@ -202,7 +203,7 @@ class TraditionalPkwareEncryptionEngine implements ZipEncryptionEngine */ private function encryptData($content) { - if (null === $content) { + if ($content === null) { throw new ZipCryptoException('content is null'); } $buff = ''; diff --git a/src/PhpZip/Crypto/WinZipAesEngine.php b/src/PhpZip/Crypto/WinZipAesEngine.php index 04fd808..e070bd5 100644 --- a/src/PhpZip/Crypto/WinZipAesEngine.php +++ b/src/PhpZip/Crypto/WinZipAesEngine.php @@ -5,6 +5,7 @@ namespace PhpZip\Crypto; use PhpZip\Exception\RuntimeException; use PhpZip\Exception\ZipAuthenticationException; use PhpZip\Exception\ZipCryptoException; +use PhpZip\Exception\ZipException; use PhpZip\Extra\Fields\WinZipAesEntryExtraField; use PhpZip\Model\ZipEntry; use PhpZip\Util\CryptoUtil; @@ -49,6 +50,7 @@ class WinZipAesEngine implements ZipEncryptionEngine * @return string * @throws ZipAuthenticationException * @throws ZipCryptoException + * @throws \PhpZip\Exception\ZipException */ public function decrypt($content) { @@ -129,26 +131,26 @@ class WinZipAesEngine implements ZipEncryptionEngine " (authenticated WinZip AES entry content has been tampered with)"); } - return self::aesCtrSegmentIntegerCounter(false, $content, $key, $iv); + return self::aesCtrSegmentIntegerCounter($content, $key, $iv, false); } /** * Decryption or encryption AES-CTR with Segment Integer Count (SIC). * - * @param bool $encrypted If true encryption else decryption * @param string $str Data * @param string $key Key * @param string $iv IV + * @param bool $encrypted If true encryption else decryption * @return string */ - private static function aesCtrSegmentIntegerCounter($encrypted = true, $str, $key, $iv) + private static function aesCtrSegmentIntegerCounter($str, $key, $iv, $encrypted = true) { $numOfBlocks = ceil(strlen($str) / 16); $ctrStr = ''; for ($i = 0; $i < $numOfBlocks; ++$i) { for ($j = 0; $j < 16; ++$j) { $n = ord($iv[$j]); - if (0x100 === ++$n) { + if (++$n === 0x100) { // overflow, set this one to 0, increment next $iv[$j] = chr(0); } else { @@ -172,7 +174,6 @@ class WinZipAesEngine implements ZipEncryptionEngine * @param string $key Aes key * @param string $iv Aes IV * @return string Encrypted data - * @throws RuntimeException */ private static function encryptCtr($data, $key, $iv) { @@ -180,6 +181,7 @@ class WinZipAesEngine implements ZipEncryptionEngine $numBits = strlen($key) * 8; return openssl_encrypt($data, 'AES-' . $numBits . '-CTR', $key, OPENSSL_RAW_DATA, $iv); } elseif (extension_loaded("mcrypt")) { + /** @noinspection PhpDeprecationInspection */ return mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $data, "ctr", $iv); } else { throw new RuntimeException('Extension openssl or mcrypt not loaded'); @@ -193,7 +195,6 @@ class WinZipAesEngine implements ZipEncryptionEngine * @param string $key Aes key * @param string $iv Aes IV * @return string Raw data - * @throws RuntimeException */ private static function decryptCtr($data, $key, $iv) { @@ -201,6 +202,7 @@ class WinZipAesEngine implements ZipEncryptionEngine $numBits = strlen($key) * 8; return openssl_decrypt($data, 'AES-' . $numBits . '-CTR', $key, OPENSSL_RAW_DATA, $iv); } elseif (extension_loaded("mcrypt")) { + /** @noinspection PhpDeprecationInspection */ return mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $data, "ctr", $iv); } else { throw new RuntimeException('Extension openssl or mcrypt not loaded'); @@ -212,12 +214,15 @@ class WinZipAesEngine implements ZipEncryptionEngine * * @param string $content * @return string + * @throws \PhpZip\Exception\ZipException */ public function encrypt($content) { // Init key strength. $password = $this->entry->getPassword(); - assert($password !== null); + if ($password === null){ + throw new ZipException('No password was set for the entry "'.$this->entry->getName().'"'); + } // WinZip 99-character limit // @see https://sourceforge.net/p/p7zip/discussion/383044/thread/c859a2f0/ @@ -226,8 +231,6 @@ class WinZipAesEngine implements ZipEncryptionEngine $keyStrengthBits = WinZipAesEntryExtraField::getKeyStrangeFromEncryptionMethod($this->entry->getEncryptionMethod()); $keyStrengthBytes = $keyStrengthBits / 8; - assert(self::AES_BLOCK_SIZE_BITS <= $keyStrengthBits); - $salt = CryptoUtil::randomBytes($keyStrengthBytes / 2); $keyParam = hash_pbkdf2("sha1", $password, $salt, self::ITERATION_COUNT, (2 * $keyStrengthBits + self::PWD_VERIFIER_BITS) / 8, true); @@ -239,7 +242,7 @@ class WinZipAesEngine implements ZipEncryptionEngine $key = substr($keyParam, 0, $keyStrengthBytes); - $content = self::aesCtrSegmentIntegerCounter(true, $content, $key, $iv); + $content = self::aesCtrSegmentIntegerCounter($content, $key, $iv, true); $mac = hash_hmac('sha1', $content, $sha1HMacParam, true); diff --git a/src/PhpZip/Exception/Crc32Exception.php b/src/PhpZip/Exception/Crc32Exception.php index f82b233..b0ec2d0 100644 --- a/src/PhpZip/Exception/Crc32Exception.php +++ b/src/PhpZip/Exception/Crc32Exception.php @@ -37,8 +37,14 @@ class Crc32Exception extends ZipException */ public function __construct($name, $expected, $actual) { - parent::__construct($name . " (expected CRC32 value 0x" . - dechex($expected) . ", but is actually 0x" . dechex($actual) . ")"); + parent::__construct( + sprintf( + "%s (expected CRC32 value 0x%x, but is actually 0x%x)", + $name, + $expected, + $actual + ) + ); assert($expected != $actual); $this->expectedCrc = $expected; $this->actualCrc = $actual; diff --git a/src/PhpZip/Exception/RuntimeException.php b/src/PhpZip/Exception/RuntimeException.php index ddc13d7..f745b9a 100644 --- a/src/PhpZip/Exception/RuntimeException.php +++ b/src/PhpZip/Exception/RuntimeException.php @@ -4,10 +4,11 @@ namespace PhpZip\Exception; /** * Runtime exception. + * Exception thrown if an error which can only be found on runtime occurs. * * @author Ne-Lexa alexey@nelexa.ru * @license MIT */ -class RuntimeException extends ZipException +class RuntimeException extends \RuntimeException { } diff --git a/src/PhpZip/Exception/ZipEntryNotFoundException.php b/src/PhpZip/Exception/ZipEntryNotFoundException.php new file mode 100644 index 0000000..e8cdbae --- /dev/null +++ b/src/PhpZip/Exception/ZipEntryNotFoundException.php @@ -0,0 +1,38 @@ +entryName = $entryName; + } + + /** + * @return string + */ + public function getEntryName() + { + return $this->entryName; + } +} diff --git a/src/PhpZip/Exception/ZipNotFoundEntry.php b/src/PhpZip/Exception/ZipNotFoundEntry.php index dfa07a7..ffc3064 100644 --- a/src/PhpZip/Exception/ZipNotFoundEntry.php +++ b/src/PhpZip/Exception/ZipNotFoundEntry.php @@ -7,8 +7,8 @@ namespace PhpZip\Exception; * * @author Ne-Lexa alexey@nelexa.ru * @license MIT - * @see \Exception + * @deprecated Rename class exception, using ZipEntryNotFoundException */ -class ZipNotFoundEntry extends ZipException +class ZipNotFoundEntry extends ZipEntryNotFoundException { } diff --git a/src/PhpZip/Exception/ZipUnsupportMethod.php b/src/PhpZip/Exception/ZipUnsupportMethod.php index 05198cd..98cf6d9 100644 --- a/src/PhpZip/Exception/ZipUnsupportMethod.php +++ b/src/PhpZip/Exception/ZipUnsupportMethod.php @@ -7,8 +7,8 @@ namespace PhpZip\Exception; * * @author Ne-Lexa alexey@nelexa.ru * @license MIT - * @see \Exception + * @deprecated Rename exception class, using ZipUnsupportMethodException */ -class ZipUnsupportMethod extends ZipException +class ZipUnsupportMethod extends ZipUnsupportMethodException { } diff --git a/src/PhpZip/Exception/ZipUnsupportMethodException.php b/src/PhpZip/Exception/ZipUnsupportMethodException.php new file mode 100644 index 0000000..dd66cce --- /dev/null +++ b/src/PhpZip/Exception/ZipUnsupportMethodException.php @@ -0,0 +1,7 @@ + * @return mixed Can return all value types. * @since 5.0.0 + * @throws ZipException */ public function offsetGet($offset) { @@ -145,13 +146,15 @@ class ExtraFieldsCollection implements \Countable, \ArrayAccess, \Iterator * The value to set. *

* @return void - * @throws InvalidArgumentException + * @throws ZipException * @since 5.0.0 */ public function offsetSet($offset, $value) { if ($value instanceof ExtraField) { - assert($offset == $value::getHeaderId()); + if ($offset !== $value::getHeaderId()){ + throw new InvalidArgumentException("Value header id !== array access key"); + } $this->add($value); } else { throw new InvalidArgumentException('value is not instanceof ' . ExtraField::class); @@ -166,6 +169,7 @@ class ExtraFieldsCollection implements \Countable, \ArrayAccess, \Iterator *

* @return void * @since 5.0.0 + * @throws ZipException */ public function offsetUnset($offset) { diff --git a/src/PhpZip/Extra/ExtraFieldsFactory.php b/src/PhpZip/Extra/ExtraFieldsFactory.php index 37faa4f..4d6e3c9 100644 --- a/src/PhpZip/Extra/ExtraFieldsFactory.php +++ b/src/PhpZip/Extra/ExtraFieldsFactory.php @@ -38,7 +38,7 @@ class ExtraFieldsFactory public static function createExtraFieldCollections($extra, ZipEntry $entry = null) { $extraFieldsCollection = new ExtraFieldsCollection(); - if (null !== $extra) { + if ($extra !== null) { $extraLength = strlen($extra); if ($extraLength > 0xffff) { throw new ZipException("Extra Fields too large: " . $extraLength); @@ -63,6 +63,11 @@ class ExtraFieldsFactory return $extraFieldsCollection; } + /** + * @param ExtraFieldsCollection $extraFieldsCollection + * @return string + * @throws ZipException + */ public static function createSerializedData(ExtraFieldsCollection $extraFieldsCollection) { $extraData = ''; @@ -102,7 +107,7 @@ class ExtraFieldsFactory if (isset(self::getRegistry()[$headerId])) { $extraClassName = self::getRegistry()[$headerId]; $extraField = new $extraClassName; - if ($extraField::getHeaderId() !== $headerId) { + if ($headerId !== $extraField::getHeaderId()) { throw new ZipException('Runtime error support headerId ' . $headerId); } } else { @@ -118,7 +123,7 @@ class ExtraFieldsFactory */ protected static function getRegistry() { - if (null === self::$registry) { + if (self::$registry === null) { self::$registry[WinZipAesEntryExtraField::getHeaderId()] = WinZipAesEntryExtraField::class; self::$registry[NtfsExtraField::getHeaderId()] = NtfsExtraField::class; self::$registry[Zip64ExtraField::getHeaderId()] = Zip64ExtraField::class; diff --git a/src/PhpZip/Extra/Fields/ApkAlignmentExtraField.php b/src/PhpZip/Extra/Fields/ApkAlignmentExtraField.php index 0280c44..a9b9031 100644 --- a/src/PhpZip/Extra/Fields/ApkAlignmentExtraField.php +++ b/src/PhpZip/Extra/Fields/ApkAlignmentExtraField.php @@ -61,7 +61,6 @@ class ApkAlignmentExtraField implements ExtraField /** * Initializes this Extra Field by deserializing a Data Block. * @param string $data - * @throws InvalidArgumentException */ public function deserialize($data) { diff --git a/src/PhpZip/Extra/Fields/JarMarkerExtraField.php b/src/PhpZip/Extra/Fields/JarMarkerExtraField.php index 4558408..01c66b4 100644 --- a/src/PhpZip/Extra/Fields/JarMarkerExtraField.php +++ b/src/PhpZip/Extra/Fields/JarMarkerExtraField.php @@ -44,7 +44,7 @@ class JarMarkerExtraField implements ExtraField */ public function deserialize($data) { - if (0 !== strlen($data)) { + if (strlen($data) !== 0) { throw new ZipException("JarMarker doesn't expect any data"); } } diff --git a/src/PhpZip/Extra/Fields/NtfsExtraField.php b/src/PhpZip/Extra/Fields/NtfsExtraField.php index 45efb36..02f761c 100644 --- a/src/PhpZip/Extra/Fields/NtfsExtraField.php +++ b/src/PhpZip/Extra/Fields/NtfsExtraField.php @@ -55,7 +55,7 @@ class NtfsExtraField implements ExtraField public function deserialize($data) { $unpack = unpack('vtag/vsizeAttr', substr($data, 0, 4)); - if (24 === $unpack['sizeAttr']) { + if ($unpack['sizeAttr'] === 24) { $tagData = substr($data, 4, $unpack['sizeAttr']); $this->mtime = PackUtil::unpackLongLE(substr($tagData, 0, 8)) / 10000000 - 11644473600; $this->atime = PackUtil::unpackLongLE(substr($tagData, 8, 8)) / 10000000 - 11644473600; @@ -70,7 +70,7 @@ class NtfsExtraField implements ExtraField public function serialize() { $serialize = ''; - if (null !== $this->mtime && null !== $this->atime && null !== $this->ctime) { + if ($this->mtime !== null && $this->atime !== null && $this->ctime !== null) { $mtimeLong = ($this->mtime + 11644473600) * 10000000; $atimeLong = ($this->atime + 11644473600) * 10000000; $ctimeLong = ($this->ctime + 11644473600) * 10000000; diff --git a/src/PhpZip/Extra/Fields/WinZipAesEntryExtraField.php b/src/PhpZip/Extra/Fields/WinZipAesEntryExtraField.php index 83b5e97..7f640bf 100644 --- a/src/PhpZip/Extra/Fields/WinZipAesEntryExtraField.php +++ b/src/PhpZip/Extra/Fields/WinZipAesEntryExtraField.php @@ -119,6 +119,7 @@ class WinZipAesEntryExtraField implements ExtraField /** * @return bool|int + * @throws ZipException */ public function getKeyStrength() { @@ -153,6 +154,7 @@ class WinZipAesEntryExtraField implements ExtraField * Internal encryption method. * * @return int + * @throws ZipException */ public function getEncryptionMethod() { diff --git a/src/PhpZip/Extra/Fields/Zip64ExtraField.php b/src/PhpZip/Extra/Fields/Zip64ExtraField.php index 52c3e38..c39dff0 100644 --- a/src/PhpZip/Extra/Fields/Zip64ExtraField.php +++ b/src/PhpZip/Extra/Fields/Zip64ExtraField.php @@ -29,7 +29,7 @@ class Zip64ExtraField implements ExtraField */ public function __construct(ZipEntry $entry = null) { - if (null !== $entry) { + if ($entry !== null) { $this->setEntry($entry); } } @@ -57,11 +57,10 @@ class Zip64ExtraField implements ExtraField /** * Serializes a Data Block. * @return string - * @throws RuntimeException */ public function serialize() { - if (null === $this->entry) { + if ($this->entry === null) { throw new RuntimeException("entry is null"); } $data = ''; @@ -86,11 +85,11 @@ class Zip64ExtraField implements ExtraField /** * Initializes this Extra Field by deserializing a Data Block. * @param string $data - * @throws RuntimeException + * @throws \PhpZip\Exception\ZipException */ public function deserialize($data) { - if (null === $this->entry) { + if ($this->entry === null) { throw new RuntimeException("entry is null"); } $off = 0; diff --git a/src/PhpZip/Model/Entry/ZipAbstractEntry.php b/src/PhpZip/Model/Entry/ZipAbstractEntry.php index 72d1722..b8cdb5e 100644 --- a/src/PhpZip/Model/Entry/ZipAbstractEntry.php +++ b/src/PhpZip/Model/Entry/ZipAbstractEntry.php @@ -109,6 +109,7 @@ abstract class ZipAbstractEntry implements ZipEntry /** * @param ZipEntry $entry + * @throws ZipException */ public function setEntry(ZipEntry $entry) { @@ -212,7 +213,7 @@ abstract class ZipAbstractEntry implements ZipEntry */ protected function isInit($mask) { - return 0 !== ($this->init & $mask); + return ($this->init & $mask) !== 0; } /** @@ -274,7 +275,6 @@ abstract class ZipAbstractEntry implements ZipEntry * * @param int $compressedSize The Compressed Size. * @return ZipEntry - * @throws ZipException */ public function setCompressedSize($compressedSize) { @@ -297,7 +297,6 @@ abstract class ZipAbstractEntry implements ZipEntry * * @param int $size The (Uncompressed) Size. * @return ZipEntry - * @throws ZipException */ public function setSize($size) { @@ -318,7 +317,6 @@ abstract class ZipAbstractEntry implements ZipEntry /** * @param int $offset * @return ZipEntry - * @throws ZipException */ public function setOffset($offset) { @@ -382,7 +380,7 @@ abstract class ZipAbstractEntry implements ZipEntry */ public function getGeneralPurposeBitFlag($mask) { - return 0 !== ($this->general & $mask); + return ($this->general & $mask) !== 0; } /** @@ -390,6 +388,7 @@ abstract class ZipAbstractEntry implements ZipEntry * encryption artifacts. * * @return ZipEntry + * @throws ZipException */ public function disableEncryption() { @@ -401,7 +400,7 @@ abstract class ZipAbstractEntry implements ZipEntry */ $field = $this->extraFieldsCollection[$headerId]; if (self::METHOD_WINZIP_AES === $this->getMethod()) { - $this->setMethod(null === $field ? self::UNKNOWN : $field->getMethod()); + $this->setMethod($field === null ? self::UNKNOWN : $field->getMethod()); } unset($this->extraFieldsCollection[$headerId]); } @@ -444,7 +443,7 @@ abstract class ZipAbstractEntry implements ZipEntry */ public function setMethod($method) { - if (self::UNKNOWN === $method) { + if ($method === self::UNKNOWN) { $this->method = $method; $this->setInit(self::BIT_METHOD, false); return $this; @@ -510,6 +509,7 @@ abstract class ZipAbstractEntry implements ZipEntry * * @param int $unixTimestamp * @return ZipEntry + * @throws ZipException */ public function setTime($unixTimestamp) { @@ -541,7 +541,6 @@ abstract class ZipAbstractEntry implements ZipEntry * * @param int $externalAttributes the external file attributes. * @return ZipEntry - * @throws ZipException */ public function setExternalAttributes($externalAttributes) { @@ -619,7 +618,7 @@ abstract class ZipAbstractEntry implements ZipEntry */ public function setComment($comment) { - if (null !== $comment) { + if ($comment !== null) { $commentLength = strlen($comment); if (0x0000 > $commentLength || $commentLength > 0xffff) { throw new ZipException("Comment too long"); @@ -635,7 +634,7 @@ abstract class ZipAbstractEntry implements ZipEntry */ public function isDataDescriptorRequired() { - return self::UNKNOWN == ($this->getCrc() | $this->getCompressedSize() | $this->getSize()); + return ($this->getCrc() | $this->getCompressedSize() | $this->getSize()) == self::UNKNOWN; } /** @@ -653,7 +652,6 @@ abstract class ZipAbstractEntry implements ZipEntry * * @param int $crc * @return ZipEntry - * @throws ZipException */ public function setCrc($crc) { @@ -676,11 +674,12 @@ abstract class ZipAbstractEntry implements ZipEntry * @param string $password * @param null|int $encryptionMethod * @return ZipEntry + * @throws ZipException */ public function setPassword($password, $encryptionMethod = null) { $this->password = $password; - if (null !== $encryptionMethod) { + if ($encryptionMethod !== null) { $this->setEncryptionMethod($encryptionMethod); } if (!empty($this->password)) { @@ -715,10 +714,10 @@ abstract class ZipAbstractEntry implements ZipEntry { if (null !== $encryptionMethod) { if ( - ZipFileInterface::ENCRYPTION_METHOD_TRADITIONAL !== $encryptionMethod - && ZipFileInterface::ENCRYPTION_METHOD_WINZIP_AES_128 !== $encryptionMethod - && ZipFileInterface::ENCRYPTION_METHOD_WINZIP_AES_192 !== $encryptionMethod - && ZipFileInterface::ENCRYPTION_METHOD_WINZIP_AES_256 !== $encryptionMethod + $encryptionMethod !== ZipFileInterface::ENCRYPTION_METHOD_TRADITIONAL + && $encryptionMethod !== ZipFileInterface::ENCRYPTION_METHOD_WINZIP_AES_128 + && $encryptionMethod !== ZipFileInterface::ENCRYPTION_METHOD_WINZIP_AES_192 + && $encryptionMethod !== ZipFileInterface::ENCRYPTION_METHOD_WINZIP_AES_256 ) { throw new ZipException('Invalid encryption method'); } @@ -738,7 +737,6 @@ abstract class ZipAbstractEntry implements ZipEntry /** * @param int $compressionLevel * @return ZipEntry - * @throws InvalidArgumentException */ public function setCompressionLevel($compressionLevel = ZipFileInterface::LEVEL_DEFAULT_COMPRESSION) { diff --git a/src/PhpZip/Model/Entry/ZipChangesEntry.php b/src/PhpZip/Model/Entry/ZipChangesEntry.php index 205a793..100512c 100644 --- a/src/PhpZip/Model/Entry/ZipChangesEntry.php +++ b/src/PhpZip/Model/Entry/ZipChangesEntry.php @@ -20,6 +20,8 @@ class ZipChangesEntry extends ZipAbstractEntry /** * ZipChangesEntry constructor. * @param ZipSourceEntry $entry + * @throws ZipException + * @throws \PhpZip\Exception\InvalidArgumentException */ public function __construct(ZipSourceEntry $entry) { @@ -46,7 +48,6 @@ class ZipChangesEntry extends ZipAbstractEntry * Returns an string content of the given entry. * * @return null|string - * @throws ZipException */ public function getEntryContent() { diff --git a/src/PhpZip/Model/Entry/ZipNewEntry.php b/src/PhpZip/Model/Entry/ZipNewEntry.php index d10a32c..ef56b66 100644 --- a/src/PhpZip/Model/Entry/ZipNewEntry.php +++ b/src/PhpZip/Model/Entry/ZipNewEntry.php @@ -3,7 +3,6 @@ namespace PhpZip\Model\Entry; use PhpZip\Exception\InvalidArgumentException; -use PhpZip\Exception\ZipException; use PhpZip\ZipFileInterface; /** @@ -27,7 +26,6 @@ class ZipNewEntry extends ZipAbstractEntry /** * ZipNewEntry constructor. * @param string|resource|null $content - * @throws InvalidArgumentException */ public function __construct($content = null) { @@ -42,12 +40,12 @@ class ZipNewEntry extends ZipAbstractEntry * Returns an string content of the given entry. * * @return null|string - * @throws ZipException */ public function getEntryContent() { if (is_resource($this->content)) { - return stream_get_contents($this->content, -1, 0); + rewind($this->content); + return stream_get_contents($this->content); } return $this->content; } @@ -81,7 +79,7 @@ class ZipNewEntry extends ZipAbstractEntry public function __destruct() { - if (!$this->clone && null !== $this->content && is_resource($this->content)) { + if (!$this->clone && $this->content !== null && is_resource($this->content)) { fclose($this->content); $this->content = null; } diff --git a/src/PhpZip/Model/Entry/ZipSourceEntry.php b/src/PhpZip/Model/Entry/ZipSourceEntry.php index f7acc91..f443fd4 100644 --- a/src/PhpZip/Model/Entry/ZipSourceEntry.php +++ b/src/PhpZip/Model/Entry/ZipSourceEntry.php @@ -61,7 +61,8 @@ class ZipSourceEntry extends ZipAbstractEntry */ public function getEntryContent() { - if (null === $this->entryContent) { + if ($this->entryContent === null) { + // In order not to unpack again, we cache the content in memory or on disk $content = $this->inputStream->readEntryContent($this); if ($this->getSize() < self::MAX_SIZE_CACHED_CONTENT_IN_MEMORY) { $this->entryContent = $content; @@ -89,7 +90,7 @@ class ZipSourceEntry extends ZipAbstractEntry public function __destruct() { - if (!$this->clone && null !== $this->entryContent && is_resource($this->entryContent)) { + if (!$this->clone && $this->entryContent !== null && is_resource($this->entryContent)) { fclose($this->entryContent); } } diff --git a/src/PhpZip/Model/ZipEntryMatcher.php b/src/PhpZip/Model/ZipEntryMatcher.php index 7ce6406..0c99138 100644 --- a/src/PhpZip/Model/ZipEntryMatcher.php +++ b/src/PhpZip/Model/ZipEntryMatcher.php @@ -145,7 +145,7 @@ class ZipEntryMatcher implements \Countable $entry = $this->zipModel->getEntry($entry); if (!$entry->isDirectory()) { $entry = $this->zipModel->getEntryForChanges($entry); - $entry->clearEncryption(); + $entry->disableEncryption(); } }); } diff --git a/src/PhpZip/Model/ZipInfo.php b/src/PhpZip/Model/ZipInfo.php index 6434703..c9de660 100644 --- a/src/PhpZip/Model/ZipInfo.php +++ b/src/PhpZip/Model/ZipInfo.php @@ -186,6 +186,7 @@ class ZipInfo * ZipInfo constructor. * * @param ZipEntry $entry + * @throws \PhpZip\Exception\ZipException */ public function __construct(ZipEntry $entry) { @@ -323,6 +324,7 @@ class ZipInfo /** * @param ZipEntry $entry * @return int + * @throws \PhpZip\Exception\ZipException */ private static function getMethodId(ZipEntry $entry) { @@ -344,6 +346,7 @@ class ZipInfo /** * @param ZipEntry $entry * @return string + * @throws \PhpZip\Exception\ZipException */ private static function getEntryMethodName(ZipEntry $entry) { diff --git a/src/PhpZip/Model/ZipModel.php b/src/PhpZip/Model/ZipModel.php index ac62b17..49d2d0c 100644 --- a/src/PhpZip/Model/ZipModel.php +++ b/src/PhpZip/Model/ZipModel.php @@ -3,8 +3,8 @@ namespace PhpZip\Model; use PhpZip\Exception\InvalidArgumentException; +use PhpZip\Exception\ZipEntryNotFoundException; use PhpZip\Exception\ZipException; -use PhpZip\Exception\ZipNotFoundEntry; use PhpZip\Model\Entry\ZipChangesEntry; use PhpZip\Model\Entry\ZipSourceEntry; use PhpZip\ZipFileInterface; @@ -74,11 +74,10 @@ class ZipModel implements \Countable /** * @param string $comment - * @throws InvalidArgumentException */ public function setArchiveComment($comment) { - if (null !== $comment && strlen($comment) !== 0) { + if ($comment !== null && strlen($comment) !== 0) { $comment = (string)$comment; $length = strlen($comment); if (0x0000 > $length || $length > 0xffff) { @@ -97,6 +96,7 @@ class ZipModel implements \Countable * Specify a password for extracting files. * * @param null|string $password + * @throws ZipException */ public function setReadPassword($password) { @@ -110,12 +110,13 @@ class ZipModel implements \Countable /** * @param string $entryName * @param string $password - * @throws ZipNotFoundEntry + * @throws ZipEntryNotFoundException + * @throws ZipException */ public function setReadPasswordEntry($entryName, $password) { if (!isset($this->inputEntries[$entryName])) { - throw new ZipNotFoundEntry('Not found entry ' . $entryName); + throw new ZipEntryNotFoundException($entryName); } if ($this->inputEntries[$entryName]->isEncrypted()) { $this->inputEntries[$entryName]->setPassword($password); @@ -181,8 +182,7 @@ class ZipModel implements \Countable /** * @param string|ZipEntry $old * @param string|ZipEntry $new - * @throws InvalidArgumentException - * @throws ZipNotFoundEntry + * @throws ZipException */ public function renameEntry($old, $new) { @@ -202,6 +202,8 @@ class ZipModel implements \Countable /** * @param string|ZipEntry $entry * @return ZipChangesEntry|ZipEntry + * @throws ZipException + * @throws ZipEntryNotFoundException */ public function getEntryForChanges($entry) { @@ -216,7 +218,7 @@ class ZipModel implements \Countable /** * @param string|ZipEntry $entryName * @return ZipEntry - * @throws ZipNotFoundEntry + * @throws ZipEntryNotFoundException */ public function getEntry($entryName) { @@ -224,7 +226,7 @@ class ZipModel implements \Countable if (isset($this->outEntries[$entryName])) { return $this->outEntries[$entryName]; } - throw new ZipNotFoundEntry('Zip entry "' . $entryName . '" not found'); + throw new ZipEntryNotFoundException($entryName); } /** @@ -327,7 +329,6 @@ class ZipModel implements \Countable /** * @param int $encryptionMethod - * @throws ZipException */ public function setEncryptionMethod($encryptionMethod = ZipFileInterface::ENCRYPTION_METHOD_WINZIP_AES_256) { diff --git a/src/PhpZip/Stream/ZipInputStream.php b/src/PhpZip/Stream/ZipInputStream.php index 5284d22..8ea5e24 100644 --- a/src/PhpZip/Stream/ZipInputStream.php +++ b/src/PhpZip/Stream/ZipInputStream.php @@ -9,7 +9,7 @@ use PhpZip\Exception\InvalidArgumentException; use PhpZip\Exception\RuntimeException; use PhpZip\Exception\ZipCryptoException; use PhpZip\Exception\ZipException; -use PhpZip\Exception\ZipUnsupportMethod; +use PhpZip\Exception\ZipUnsupportMethodException; use PhpZip\Extra\ExtraFieldsCollection; use PhpZip\Extra\ExtraFieldsFactory; use PhpZip\Extra\Fields\ApkAlignmentExtraField; @@ -56,7 +56,6 @@ class ZipInputStream implements ZipInputStreamInterface /** * ZipInputStream constructor. * @param resource $in - * @throws RuntimeException */ public function __construct($in) { @@ -69,6 +68,7 @@ class ZipInputStream implements ZipInputStreamInterface /** * @return ZipModel + * @throws ZipException */ public function readZip() { @@ -165,7 +165,7 @@ class ZipInputStream implements ZipInputStreamInterface $offset = $endOfCentralDirRecordPos - $data['cdSize']; fseek($this->in, $offset, SEEK_SET); $offset -= $data['cdPos']; - if (0 !== $offset) { + if ($offset !== 0) { $this->mapper = new OffsetPositionMapper($offset); } $entryCount = $data['cdEntries']; @@ -262,14 +262,13 @@ class ZipInputStream implements ZipInputStreamInterface $entries[$entry->getName()] = $entry; } - if (0 !== $numEntries % 0x10000) { + if (($numEntries % 0x10000) !== 0) { throw new ZipException("Expected " . abs($numEntries) . ($numEntries > 0 ? " more" : " less") . " entries in the Central Directory!"); } if ($this->preamble + $this->postamble >= fstat($this->in)['size']) { - assert(0 === $numEntries); $this->checkZipFileSignature(); } @@ -278,13 +277,13 @@ class ZipInputStream implements ZipInputStreamInterface /** * @return ZipEntry - * @throws InvalidArgumentException + * @throws ZipException */ public function readEntry() { // central file header signature 4 bytes (0x02014b50) $fileHeaderSig = unpack('V', fread($this->in, 4))[1]; - if (ZipOutputStreamInterface::CENTRAL_FILE_HEADER_SIG !== $fileHeaderSig) { + if ($fileHeaderSig !== ZipOutputStreamInterface::CENTRAL_FILE_HEADER_SIG) { throw new InvalidArgumentException("Corrupt zip file. Can not read zip entry."); } @@ -329,10 +328,10 @@ class ZipInputStream implements ZipInputStreamInterface $entry->setSize($data['rawSize']); $entry->setExternalAttributes($data['rawExternalAttributes']); $entry->setOffset($data['lfhOff']); // must be unmapped! - if (0 < $data['extraLength']) { + if ($data['extraLength'] > 0) { $entry->setExtra(fread($this->in, $data['extraLength'])); } - if (0 < $data['commentLength']) { + if ($data['commentLength'] > 0) { $entry->setComment(fread($this->in, $data['commentLength'])); } return $entry; @@ -352,19 +351,20 @@ class ZipInputStream implements ZipInputStreamInterface throw new InvalidArgumentException('entry must be ' . ZipSourceEntry::class); } $isEncrypted = $entry->isEncrypted(); - if ($isEncrypted && null === $entry->getPassword()) { + if ($isEncrypted && $entry->getPassword() === null) { throw new ZipException("Can not password from entry " . $entry->getName()); } $pos = $entry->getOffset(); - assert(ZipEntry::UNKNOWN !== $pos); - $pos = PHP_INT_SIZE === 4 ? sprintf('%u', $pos) : $pos; + $pos = PHP_INT_SIZE === 4 + ? sprintf('%u', $pos) // PHP 32-Bit + : $pos; // PHP 64-Bit $startPos = $pos = $this->mapper->map($pos); fseek($this->in, $startPos); // local file header signature 4 bytes (0x04034b50) - if (ZipEntry::LOCAL_FILE_HEADER_SIG !== unpack('V', fread($this->in, 4))[1]) { + if (unpack('V', fread($this->in, 4))[1] !== ZipEntry::LOCAL_FILE_HEADER_SIG) { throw new ZipException($entry->getName() . " (expected Local File Header)"); } fseek($this->in, $pos + ZipEntry::LOCAL_FILE_HEADER_FILE_NAME_LENGTH_POS); @@ -390,7 +390,7 @@ class ZipInputStream implements ZipInputStreamInterface $skipCheckCrc = false; if ($isEncrypted) { - if (ZipEntry::METHOD_WINZIP_AES === $method) { + if ($method === ZipEntry::METHOD_WINZIP_AES) { // Strong Encryption Specification - WinZip AES $winZipAesEngine = new WinZipAesEngine($entry); $content = $winZipAesEngine->decrypt($content); @@ -418,7 +418,7 @@ class ZipInputStream implements ZipInputStreamInterface // but older apps might not. fseek($this->in, $pos + $compressedSize); $localCrc = unpack('V', fread($this->in, 4))[1]; - if (ZipEntry::DATA_DESCRIPTOR_SIG === $localCrc) { + if ($localCrc === ZipEntry::DATA_DESCRIPTOR_SIG) { $localCrc = unpack('V', fread($this->in, 4))[1]; } } else { @@ -449,7 +449,7 @@ class ZipInputStream implements ZipInputStreamInterface $content = bzdecompress($content); break; default: - throw new ZipUnsupportMethod($entry->getName() . + throw new ZipUnsupportMethodException($entry->getName() . " (compression method " . $method . " is not supported)"); } if (!$skipCheckCrc) { @@ -480,6 +480,7 @@ class ZipInputStream implements ZipInputStreamInterface * * @param ZipEntry $entry * @param ZipOutputStreamInterface $out + * @throws ZipException */ public function copyEntry(ZipEntry $entry, ZipOutputStreamInterface $out) { @@ -551,8 +552,7 @@ class ZipInputStream implements ZipInputStreamInterface // skip source extraLength from input stream fseek($this->in, $sourceExtraLength, SEEK_CUR); } else { - $copyInToOutLength += ZipEntry::LOCAL_FILE_HEADER_MIN_LEN + $sourceExtraLength + $nameLength; - ; + $copyInToOutLength += ZipEntry::LOCAL_FILE_HEADER_MIN_LEN + $sourceExtraLength + $nameLength;; } if ($entry->getGeneralPurposeBitFlag(ZipEntry::GPBF_DATA_DESCRIPTOR)) { // crc-32 4 bytes @@ -595,7 +595,7 @@ class ZipInputStream implements ZipInputStreamInterface public function close() { - if ($this->in != null) { + if ($this->in !== null) { fclose($this->in); $this->in = null; } diff --git a/src/PhpZip/Stream/ZipOutputStream.php b/src/PhpZip/Stream/ZipOutputStream.php index c1c875d..3afe057 100644 --- a/src/PhpZip/Stream/ZipOutputStream.php +++ b/src/PhpZip/Stream/ZipOutputStream.php @@ -22,8 +22,7 @@ use PhpZip\Util\StringUtil; use PhpZip\ZipFileInterface; /** - * Write - * ip file + * Write zip file * * @author Ne-Lexa alexey@nelexa.ru * @license MIT @@ -43,7 +42,6 @@ class ZipOutputStream implements ZipOutputStreamInterface * ZipOutputStream constructor. * @param resource $out * @param ZipModel $zipModel - * @throws InvalidArgumentException */ public function __construct($out, ZipModel $zipModel) { @@ -54,6 +52,9 @@ class ZipOutputStream implements ZipOutputStreamInterface $this->zipModel = $zipModel; } + /** + * @throws ZipException + */ public function writeZip() { $entries = $this->zipModel->getEntries(); @@ -123,7 +124,7 @@ class ZipOutputStream implements ZipOutputStreamInterface } $size = $nameLength + $extraLength; - if (0xffff < $size) { + if ($size > 0xffff) { throw new ZipException( $entry->getName() . " (the total size of " . $size . " bytes for the name, extra fields and comment " . @@ -168,7 +169,7 @@ class ZipOutputStream implements ZipOutputStreamInterface if ($entry instanceof ZipChangesEntry && !$entry->isChangedContent()) { $entry->getSourceEntry()->getInputStream()->copyEntryData($entry->getSourceEntry(), $this); - } elseif (null !== $entryContent) { + } elseif ($entryContent !== null) { fwrite($this->out, $entryContent); } @@ -186,7 +187,7 @@ class ZipOutputStream implements ZipOutputStreamInterface } else { fwrite($this->out, pack('VV', $entry->getCompressedSize(), $entry->getSize())); } - } elseif ($entry->getCompressedSize() != $compressedSize) { + } elseif ($compressedSize != $entry->getCompressedSize()) { throw new ZipException( $entry->getName() . " (expected compressed entry size of " . $entry->getCompressedSize() . " bytes, " . @@ -202,10 +203,10 @@ class ZipOutputStream implements ZipOutputStreamInterface */ protected function entryCommitChangesAndReturnContent(ZipEntry $entry) { - if (ZipEntry::UNKNOWN === $entry->getPlatform()) { + if ($entry->getPlatform() === ZipEntry::UNKNOWN) { $entry->setPlatform(ZipEntry::PLATFORM_UNIX); } - if (ZipEntry::UNKNOWN === $entry->getTime()) { + if ($entry->getTime() === ZipEntry::UNKNOWN) { $entry->setTime(time()); } $method = $entry->getMethod(); @@ -214,7 +215,7 @@ class ZipOutputStream implements ZipOutputStreamInterface // See appendix D of PKWARE's ZIP File Format Specification. $utf8 = true; - if ($encrypted && null === $entry->getPassword()) { + if ($encrypted && $entry->getPassword() === null) { throw new ZipException("Can not password from entry " . $entry->getName()); } @@ -232,12 +233,12 @@ class ZipOutputStream implements ZipOutputStreamInterface $entry->setSize(strlen($entryContent)); $entry->setCrc(crc32($entryContent)); - if ($encrypted && ZipEntry::METHOD_WINZIP_AES === $method) { + if ($encrypted && $method === ZipEntry::METHOD_WINZIP_AES) { /** * @var WinZipAesEntryExtraField $field */ $field = $extraFieldsCollection->get(WinZipAesEntryExtraField::getHeaderId()); - if (null !== $field) { + if ($field !== null) { $method = $field->getMethod(); } } @@ -269,7 +270,7 @@ class ZipOutputStream implements ZipOutputStreamInterface throw new ZipException($entry->getName() . " (unsupported compression method " . $method . ")"); } - if (ZipFileInterface::METHOD_DEFLATED === $method) { + if ($method === ZipFileInterface::METHOD_DEFLATED) { $bit1 = false; $bit2 = false; switch ($entry->getCompressionLevel()) { @@ -302,7 +303,7 @@ class ZipOutputStream implements ZipOutputStreamInterface $field->setKeyStrength($keyStrength); $field->setMethod($method); $size = $entry->getSize(); - if (20 <= $size && ZipFileInterface::METHOD_BZIP2 !== $method) { + if ($size >= 20 && $method !== ZipFileInterface::METHOD_BZIP2) { $field->setVendorVersion(WinZipAesEntryExtraField::VV_AE_1); } else { $field->setVendorVersion(WinZipAesEntryExtraField::VV_AE_2); @@ -343,7 +344,7 @@ class ZipOutputStream implements ZipOutputStreamInterface */ protected function determineBestCompressionMethod(ZipEntry $entry, $content) { - if (null !== $content) { + if ($content !== null) { $entryContent = gzdeflate($content, $entry->getCompressionLevel()); if (strlen($entryContent) < strlen($content)) { $entry->setMethod(ZipFileInterface::METHOD_DEFLATED); @@ -358,8 +359,6 @@ class ZipOutputStream implements ZipOutputStreamInterface * Writes a Central File Header record. * * @param OutputOffsetEntry $outEntry - * @throws RuntimeException - * @internal param OutPosEntry $entry */ protected function writeCentralDirectoryHeader(OutputOffsetEntry $outEntry) { @@ -368,7 +367,7 @@ class ZipOutputStream implements ZipOutputStreamInterface $size = $entry->getSize(); // This test MUST NOT include the CRC-32 because VV_AE_2 sets it to // UNKNOWN! - if (ZipEntry::UNKNOWN === ($compressedSize | $size)) { + if (($compressedSize | $size) === ZipEntry::UNKNOWN) { throw new RuntimeException("invalid entry"); } $extra = $entry->getExtra(); @@ -415,11 +414,11 @@ class ZipOutputStream implements ZipOutputStreamInterface ); // file name (variable size) fwrite($this->out, $entry->getName()); - if (0 < $extraSize) { + if ($extraSize > 0) { // extra field (variable size) fwrite($this->out, $extra); } - if (0 < $commentLength) { + if ($commentLength > 0) { // file comment (variable size) fwrite($this->out, $entry->getComment()); } diff --git a/src/PhpZip/Util/CryptoUtil.php b/src/PhpZip/Util/CryptoUtil.php index 7403c93..8108f7f 100644 --- a/src/PhpZip/Util/CryptoUtil.php +++ b/src/PhpZip/Util/CryptoUtil.php @@ -15,16 +15,20 @@ class CryptoUtil * * @param int $length * @return string - * @throws RuntimeException */ final public static function randomBytes($length) { $length = (int)$length; if (function_exists('random_bytes')) { - return random_bytes($length); + try { + return random_bytes($length); + } catch (\Exception $e) { + throw new \RuntimeException("Could not generate a random string."); + } } elseif (function_exists('openssl_random_pseudo_bytes')) { return openssl_random_pseudo_bytes($length); } elseif (function_exists('mcrypt_create_iv')) { + /** @noinspection PhpDeprecationInspection */ return mcrypt_create_iv($length); } else { throw new RuntimeException('Extension openssl or mcrypt not loaded'); diff --git a/src/PhpZip/Util/Iterator/IgnoreFilesRecursiveFilterIterator.php b/src/PhpZip/Util/Iterator/IgnoreFilesRecursiveFilterIterator.php index 7781576..da6670f 100644 --- a/src/PhpZip/Util/Iterator/IgnoreFilesRecursiveFilterIterator.php +++ b/src/PhpZip/Util/Iterator/IgnoreFilesRecursiveFilterIterator.php @@ -12,7 +12,6 @@ use PhpZip\Util\StringUtil; */ class IgnoreFilesRecursiveFilterIterator extends \RecursiveFilterIterator { - /** * Ignore list files * @@ -65,6 +64,7 @@ class IgnoreFilesRecursiveFilterIterator extends \RecursiveFilterIterator */ public function getChildren() { + /** @noinspection PhpUndefinedMethodInspection */ return new self($this->getInnerIterator()->getChildren(), $this->ignoreFiles); } } diff --git a/src/PhpZip/Util/PackUtil.php b/src/PhpZip/Util/PackUtil.php index c622360..5b3be4e 100644 --- a/src/PhpZip/Util/PackUtil.php +++ b/src/PhpZip/Util/PackUtil.php @@ -2,8 +2,6 @@ namespace PhpZip\Util; -use PhpZip\Exception\ZipException; - /** * Pack util * @@ -35,7 +33,6 @@ class PackUtil /** * @param string|int $value * @return int - * @throws ZipException */ public static function unpackLongLE($value) { diff --git a/src/PhpZip/ZipFile.php b/src/PhpZip/ZipFile.php index 5806242..50a23e4 100644 --- a/src/PhpZip/ZipFile.php +++ b/src/PhpZip/ZipFile.php @@ -3,9 +3,9 @@ namespace PhpZip; use PhpZip\Exception\InvalidArgumentException; +use PhpZip\Exception\ZipEntryNotFoundException; use PhpZip\Exception\ZipException; -use PhpZip\Exception\ZipNotFoundEntry; -use PhpZip\Exception\ZipUnsupportMethod; +use PhpZip\Exception\ZipUnsupportMethodException; use PhpZip\Model\Entry\ZipNewEntry; use PhpZip\Model\ZipEntry; use PhpZip\Model\ZipEntryMatcher; @@ -34,8 +34,7 @@ use Psr\Http\Message\ResponseInterface; class ZipFile implements ZipFileInterface { /** - * Allow compression methods. - * @var int[] + * @var int[] Allow compression methods. */ private static $allowCompressionMethods = [ self::METHOD_STORED, @@ -45,8 +44,7 @@ class ZipFile implements ZipFileInterface ]; /** - * Allow encryption methods. - * @var int[] + * @var int[] Allow encryption methods. */ private static $allowEncryptionMethods = [ self::ENCRYPTION_METHOD_TRADITIONAL, @@ -56,9 +54,7 @@ class ZipFile implements ZipFileInterface ]; /** - * Default mime types. - * - * @var array + * @var array Default mime types. */ private static $defaultMimeTypes = [ 'zip' => 'application/zip', @@ -69,11 +65,10 @@ class ZipFile implements ZipFileInterface ]; /** - * Input seekable input stream. - * - * @var ZipInputStreamInterface + * @var ZipInputStreamInterface Input seekable input stream. */ protected $inputStream; + /** * @var ZipModel */ @@ -92,13 +87,12 @@ class ZipFile implements ZipFileInterface * * @param string $filename * @return ZipFileInterface - * @throws InvalidArgumentException if file doesn't exists. - * @throws ZipException if can't open file. + * @throws ZipException if can't open file. */ public function openFile($filename) { if (!file_exists($filename)) { - throw new InvalidArgumentException("File $filename can't exists."); + throw new ZipException("File $filename can't exists."); } if (!($handle = @fopen($filename, 'rb'))) { throw new ZipException("File $filename can't open."); @@ -112,13 +106,12 @@ class ZipFile implements ZipFileInterface * * @param string $data * @return ZipFileInterface - * @throws InvalidArgumentException if data not available. - * @throws ZipException if can't open temp stream. + * @throws ZipException if can't open temp stream. */ public function openFromString($data) { - if (null === $data || strlen($data) === 0) { - throw new InvalidArgumentException("Data not available"); + if ($data === null || strlen($data) === 0) { + throw new InvalidArgumentException("Empty string passed"); } if (!($handle = fopen('php://temp', 'r+b'))) { throw new ZipException("Can't open temp stream."); @@ -134,8 +127,7 @@ class ZipFile implements ZipFileInterface * * @param resource $handle * @return ZipFileInterface - * @throws InvalidArgumentException Invalid stream resource - * or resource cannot seekable stream + * @throws ZipException */ public function openFromStream($handle) { @@ -143,11 +135,11 @@ class ZipFile implements ZipFileInterface throw new InvalidArgumentException("Invalid stream resource."); } $type = get_resource_type($handle); - if ('stream' !== $type) { + if ($type !== 'stream') { throw new InvalidArgumentException("Invalid resource type - $type."); } $meta = stream_get_meta_data($handle); - if ('dir' === $meta['stream_type']) { + if ($meta['stream_type'] === 'dir') { throw new InvalidArgumentException("Invalid stream type - {$meta['stream_type']}."); } if (!$meta['seekable']) { @@ -189,7 +181,6 @@ class ZipFile implements ZipFileInterface * * @param null|string $comment * @return ZipFileInterface - * @throws InvalidArgumentException Length comment out of range */ public function setArchiveComment($comment = null) { @@ -204,7 +195,7 @@ class ZipFile implements ZipFileInterface * * @param string $entryName * @return bool - * @throws ZipNotFoundEntry + * @throws ZipEntryNotFoundException */ public function isDirectory($entryName) { @@ -216,7 +207,7 @@ class ZipFile implements ZipFileInterface * * @param string $entryName * @return string - * @throws ZipNotFoundEntry + * @throws ZipEntryNotFoundException */ public function getEntryComment($entryName) { @@ -229,7 +220,8 @@ class ZipFile implements ZipFileInterface * @param string $entryName * @param string|null $comment * @return ZipFileInterface - * @throws ZipNotFoundEntry + * @throws ZipException + * @throws ZipEntryNotFoundException */ public function setEntryComment($entryName, $comment = null) { @@ -242,6 +234,7 @@ class ZipFile implements ZipFileInterface * * @param string $entryName * @return string + * @throws ZipException */ public function getEntryContents($entryName) { @@ -264,7 +257,8 @@ class ZipFile implements ZipFileInterface * * @param string|ZipEntry $entryName * @return ZipInfo - * @throws ZipNotFoundEntry + * @throws ZipEntryNotFoundException + * @throws ZipException */ public function getEntryInfo($entryName) { @@ -347,7 +341,7 @@ class ZipFile implements ZipFileInterface chmod($dir, 0755); touch($dir, $entry->getTime()); } - if (false === file_put_contents($file, $entry->getEntryContent())) { + if (file_put_contents($file, $entry->getEntryContent()) === false) { throw new ZipException('Can not extract file ' . $entry->getName()); } touch($file, $entry->getTime()); @@ -361,34 +355,36 @@ class ZipFile implements ZipFileInterface * @param string $localName Zip entry name. * @param string $contents String contents. * @param int|null $compressionMethod Compression method. - * Use ZipFile::METHOD_STORED, ZipFile::METHOD_DEFLATED or ZipFile::METHOD_BZIP2. - * If null, then auto choosing method. + * Use {@see ZipFile::METHOD_STORED}, {@see ZipFile::METHOD_DEFLATED} or {@see ZipFile::METHOD_BZIP2}. + * If null, then auto choosing method. * @return ZipFileInterface - * @throws InvalidArgumentException If incorrect data or entry name. - * @throws ZipUnsupportMethod + * @throws ZipException * @see ZipFileInterface::METHOD_STORED * @see ZipFileInterface::METHOD_DEFLATED * @see ZipFileInterface::METHOD_BZIP2 */ public function addFromString($localName, $contents, $compressionMethod = null) { - if (null === $contents) { + if ($contents === null) { throw new InvalidArgumentException("Contents is null"); } - $localName = (string)$localName; - if (null === $localName || 0 === strlen($localName)) { - throw new InvalidArgumentException("Incorrect entry name " . $localName); + if ($localName === null) { + throw new InvalidArgumentException("Entry name is null"); + } + $localName = ltrim((string)$localName, "\\/"); + if (strlen($localName) === 0) { + throw new InvalidArgumentException("Empty entry name"); } $contents = (string)$contents; $length = strlen($contents); - if (null === $compressionMethod) { + if ($compressionMethod === null) { if ($length >= 512) { $compressionMethod = ZipEntry::UNKNOWN; } else { - $compressionMethod = ZipFileInterface::METHOD_STORED; + $compressionMethod = self::METHOD_STORED; } } elseif (!in_array($compressionMethod, self::$allowCompressionMethods, true)) { - throw new ZipUnsupportMethod('Unsupported compression method ' . $compressionMethod); + throw new ZipUnsupportMethodException('Unsupported compression method ' . $compressionMethod); } $externalAttributes = 0100644 << 16; @@ -411,45 +407,44 @@ class ZipFile implements ZipFileInterface * Use ZipFile::METHOD_STORED, ZipFile::METHOD_DEFLATED or ZipFile::METHOD_BZIP2. * If null, then auto choosing method. * @return ZipFileInterface - * @throws InvalidArgumentException - * @throws ZipUnsupportMethod + * @throws ZipException * @see ZipFileInterface::METHOD_STORED * @see ZipFileInterface::METHOD_DEFLATED * @see ZipFileInterface::METHOD_BZIP2 */ public function addFile($filename, $localName = null, $compressionMethod = null) { - if (null === $filename) { + if ($filename === null) { throw new InvalidArgumentException("Filename is null"); } if (!is_file($filename)) { - throw new InvalidArgumentException("File $filename is not exists"); + throw new ZipException("File $filename is not exists"); } - if (null === $compressionMethod) { + if ($compressionMethod === null) { if (function_exists('mime_content_type')) { $mimeType = @mime_content_type($filename); $type = strtok($mimeType, '/'); - if ('image' === $type) { - $compressionMethod = ZipFileInterface::METHOD_STORED; - } elseif ('text' === $type && filesize($filename) < 150) { - $compressionMethod = ZipFileInterface::METHOD_STORED; + if ($type === 'image') { + $compressionMethod = self::METHOD_STORED; + } elseif ($type === 'text' && filesize($filename) < 150) { + $compressionMethod = self::METHOD_STORED; } else { $compressionMethod = ZipEntry::UNKNOWN; } - } elseif (@filesize($filename) >= 512) { + } elseif (filesize($filename) >= 512) { $compressionMethod = ZipEntry::UNKNOWN; } else { - $compressionMethod = ZipFileInterface::METHOD_STORED; + $compressionMethod = self::METHOD_STORED; } } elseif (!in_array($compressionMethod, self::$allowCompressionMethods, true)) { - throw new ZipUnsupportMethod('Unsupported method ' . $compressionMethod); + throw new ZipUnsupportMethodException('Unsupported compression method ' . $compressionMethod); } if (!($handle = @fopen($filename, 'rb'))) { - throw new InvalidArgumentException('File ' . $filename . ' can not open.'); + throw new ZipException('File ' . $filename . ' can not open.'); } - if (null === $localName) { + if ($localName === null) { $localName = basename($filename); } $this->addFromStream($handle, $localName, $compressionMethod); @@ -466,8 +461,7 @@ class ZipFile implements ZipFileInterface * Use ZipFile::METHOD_STORED, ZipFile::METHOD_DEFLATED or ZipFile::METHOD_BZIP2. * If null, then auto choosing method. * @return ZipFileInterface - * @throws InvalidArgumentException - * @throws ZipUnsupportMethod + * @throws ZipException * @see ZipFileInterface::METHOD_STORED * @see ZipFileInterface::METHOD_DEFLATED * @see ZipFileInterface::METHOD_BZIP2 @@ -475,22 +469,25 @@ class ZipFile implements ZipFileInterface public function addFromStream($stream, $localName, $compressionMethod = null) { if (!is_resource($stream)) { - throw new InvalidArgumentException("stream is not resource"); + throw new InvalidArgumentException("Stream is not resource"); } - $localName = (string)$localName; - if (empty($localName)) { - throw new InvalidArgumentException("Incorrect entry name " . $localName); + if ($localName === null) { + throw new InvalidArgumentException("Entry name is null"); + } + $localName = ltrim((string)$localName, "\\/"); + if (strlen($localName) === 0) { + throw new InvalidArgumentException("Empty entry name"); } $fstat = fstat($stream); $length = $fstat['size']; - if (null === $compressionMethod) { + if ($compressionMethod === null) { if ($length >= 512) { $compressionMethod = ZipEntry::UNKNOWN; } else { - $compressionMethod = ZipFileInterface::METHOD_STORED; + $compressionMethod = self::METHOD_STORED; } } elseif (!in_array($compressionMethod, self::$allowCompressionMethods, true)) { - throw new ZipUnsupportMethod('Unsupported method ' . $compressionMethod); + throw new ZipUnsupportMethodException('Unsupported method ' . $compressionMethod); } $mode = sprintf('%o', $fstat['mode']); @@ -511,21 +508,24 @@ class ZipFile implements ZipFileInterface * * @param string $dirName * @return ZipFileInterface - * @throws InvalidArgumentException + * @throws ZipException */ public function addEmptyDir($dirName) { - $dirName = (string)$dirName; - if (strlen($dirName) === 0) { - throw new InvalidArgumentException("DirName empty"); + if ($dirName === null) { + throw new InvalidArgumentException("Dir name is null"); } - $dirName = rtrim($dirName, '/') . '/'; + $dirName = ltrim((string)$dirName, "\\/"); + if (strlen($dirName) === 0) { + throw new InvalidArgumentException("Empty dir name"); + } + $dirName = rtrim($dirName, '\\/') . '/'; $externalAttributes = 040755 << 16; $entry = new ZipNewEntry(); $entry->setName($dirName); $entry->setTime(time()); - $entry->setMethod(ZipFileInterface::METHOD_STORED); + $entry->setMethod(self::METHOD_STORED); $entry->setSize(0); $entry->setCompressedSize(0); $entry->setCrc(0); @@ -544,16 +544,19 @@ class ZipFile implements ZipFileInterface * Use ZipFile::METHOD_STORED, ZipFile::METHOD_DEFLATED or ZipFile::METHOD_BZIP2. * If null, then auto choosing method. * @return ZipFileInterface - * @throws InvalidArgumentException + * @throws ZipException */ public function addDir($inputDir, $localPath = "/", $compressionMethod = null) { + if ($inputDir === null) { + throw new InvalidArgumentException('Input dir is null'); + } $inputDir = (string)$inputDir; - if (null === $inputDir || strlen($inputDir) === 0) { + if (strlen($inputDir) === 0) { throw new InvalidArgumentException('Input dir empty'); } if (!is_dir($inputDir)) { - throw new InvalidArgumentException('Directory ' . $inputDir . ' can\'t exists'); + throw new ZipException('Directory ' . $inputDir . ' can\'t exists'); } $inputDir = rtrim($inputDir, '/\\') . DIRECTORY_SEPARATOR; @@ -570,8 +573,7 @@ class ZipFile implements ZipFileInterface * Use ZipFile::METHOD_STORED, ZipFile::METHOD_DEFLATED or ZipFile::METHOD_BZIP2. * If null, then auto choosing method. * @return ZipFileInterface - * @throws InvalidArgumentException - * @throws ZipUnsupportMethod + * @throws ZipException * @see ZipFileInterface::METHOD_STORED * @see ZipFileInterface::METHOD_DEFLATED * @see ZipFileInterface::METHOD_BZIP2 @@ -600,8 +602,7 @@ class ZipFile implements ZipFileInterface * Use ZipFile::METHOD_STORED, ZipFile::METHOD_DEFLATED or ZipFile::METHOD_BZIP2. * If null, then auto choosing method. * @return ZipFileInterface - * @throws InvalidArgumentException - * @throws ZipUnsupportMethod + * @throws ZipException * @see ZipFileInterface::METHOD_STORED * @see ZipFileInterface::METHOD_DEFLATED * @see ZipFileInterface::METHOD_BZIP2 @@ -613,8 +614,8 @@ class ZipFile implements ZipFileInterface ) { $localPath = (string)$localPath; - if (null !== $localPath && 0 !== strlen($localPath)) { - $localPath = rtrim($localPath, '/'); + if (strlen($localPath) !== 0) { + $localPath = trim($localPath, '\\/'); } else { $localPath = ""; } @@ -629,10 +630,10 @@ class ZipFile implements ZipFileInterface $files = []; foreach ($iterator as $file) { if ($file instanceof \SplFileInfo) { - if ('..' === $file->getBasename()) { + if ($file->getBasename() === '..') { continue; } - if ('.' === $file->getBasename()) { + if ($file->getBasename() === '.') { $files[] = dirname($file->getPathname()); } else { $files[] = $file->getPathname(); @@ -647,9 +648,9 @@ class ZipFile implements ZipFileInterface $path = array_shift($files); foreach ($files as $file) { $relativePath = str_replace($path, $localPath, $file); - $relativePath = ltrim($relativePath, '/'); - if (is_dir($file)) { - FilesUtil::isEmptyDir($file) && $this->addEmptyDir($relativePath); + $relativePath = ltrim($relativePath, '\\/'); + if (is_dir($file) && FilesUtil::isEmptyDir($file)) { + $this->addEmptyDir($relativePath); } elseif (is_file($file)) { $this->addFile($file, $relativePath, $compressionMethod); } @@ -667,7 +668,7 @@ class ZipFile implements ZipFileInterface * Use ZipFile::METHOD_STORED, ZipFile::METHOD_DEFLATED or ZipFile::METHOD_BZIP2. * If null, then auto choosing method. * @return ZipFileInterface - * @throws InvalidArgumentException + * @throws ZipException * @sse https://en.wikipedia.org/wiki/Glob_(programming) Glob pattern syntax */ public function addFilesFromGlob($inputDir, $globPattern, $localPath = '/', $compressionMethod = null) @@ -686,7 +687,7 @@ class ZipFile implements ZipFileInterface * Use ZipFile::METHOD_STORED, ZipFile::METHOD_DEFLATED or ZipFile::METHOD_BZIP2. * If null, then auto choosing method. * @return ZipFileInterface - * @throws InvalidArgumentException + * @throws ZipException * @sse https://en.wikipedia.org/wiki/Glob_(programming) Glob pattern syntax */ private function addGlob( @@ -698,11 +699,11 @@ class ZipFile implements ZipFileInterface ) { $inputDir = (string)$inputDir; - if (null === $inputDir || 0 === strlen($inputDir)) { + if (strlen($inputDir) === 0) { throw new InvalidArgumentException('Input dir empty'); } if (!is_dir($inputDir)) { - throw new InvalidArgumentException('Directory ' . $inputDir . ' can\'t exists'); + throw new ZipException('Directory ' . $inputDir . ' can\'t exists'); } $globPattern = (string)$globPattern; if (empty($globPattern)) { @@ -713,11 +714,11 @@ class ZipFile implements ZipFileInterface $globPattern = $inputDir . $globPattern; $filesFound = FilesUtil::globFileSearch($globPattern, GLOB_BRACE, $recursive); - if (false === $filesFound || empty($filesFound)) { + if ($filesFound === false || empty($filesFound)) { return $this; } if (!empty($localPath) && is_string($localPath)) { - $localPath = rtrim($localPath, '/') . '/'; + $localPath = trim($localPath, '/\\') . '/'; } else { $localPath = "/"; } @@ -727,9 +728,9 @@ class ZipFile implements ZipFileInterface */ foreach ($filesFound as $file) { $filename = str_replace($inputDir, $localPath, $file); - $filename = ltrim($filename, '/'); - if (is_dir($file)) { - FilesUtil::isEmptyDir($file) && $this->addEmptyDir($filename); + $filename = ltrim($filename, '\\/'); + if (is_dir($file) && FilesUtil::isEmptyDir($file)) { + $this->addEmptyDir($filename); } elseif (is_file($file)) { $this->addFile($file, $filename, $compressionMethod); } @@ -747,7 +748,7 @@ class ZipFile implements ZipFileInterface * Use ZipFile::METHOD_STORED, ZipFile::METHOD_DEFLATED or ZipFile::METHOD_BZIP2. * If null, then auto choosing method. * @return ZipFileInterface - * @throws InvalidArgumentException + * @throws ZipException * @sse https://en.wikipedia.org/wiki/Glob_(programming) Glob pattern syntax */ public function addFilesFromGlobRecursive($inputDir, $globPattern, $localPath = '/', $compressionMethod = null) @@ -765,6 +766,7 @@ class ZipFile implements ZipFileInterface * Use ZipFile::METHOD_STORED, ZipFile::METHOD_DEFLATED or ZipFile::METHOD_BZIP2. * If null, then auto choosing method. * @return ZipFileInterface + * @throws ZipException * @internal param bool $recursive Recursive search. */ public function addFilesFromRegex($inputDir, $regexPattern, $localPath = "/", $compressionMethod = null) @@ -783,7 +785,7 @@ class ZipFile implements ZipFileInterface * Use ZipFile::METHOD_STORED, ZipFile::METHOD_DEFLATED or ZipFile::METHOD_BZIP2. * If null, then auto choosing method. * @return ZipFileInterface - * @throws InvalidArgumentException + * @throws ZipException */ private function addRegex( $inputDir, @@ -798,7 +800,7 @@ class ZipFile implements ZipFileInterface throw new InvalidArgumentException("regex pattern empty"); } $inputDir = (string)$inputDir; - if (null === $inputDir || 0 === strlen($inputDir)) { + if (strlen($inputDir) === 0) { throw new InvalidArgumentException('Input dir empty'); } if (!is_dir($inputDir)) { @@ -807,11 +809,11 @@ class ZipFile implements ZipFileInterface $inputDir = rtrim($inputDir, '/\\') . DIRECTORY_SEPARATOR; $files = FilesUtil::regexFileSearch($inputDir, $regexPattern, $recursive); - if (false === $files || empty($files)) { + if (empty($files)) { return $this; } if (!empty($localPath) && is_string($localPath)) { - $localPath = rtrim($localPath, '/') . '/'; + $localPath = trim($localPath, '\\/') . '/'; } else { $localPath = "/"; } @@ -822,9 +824,9 @@ class ZipFile implements ZipFileInterface */ foreach ($files as $file) { $filename = str_replace($inputDir, $localPath, $file); - $filename = ltrim($filename, '/'); - if (is_dir($file)) { - FilesUtil::isEmptyDir($file) && $this->addEmptyDir($filename); + $filename = ltrim($filename, '\\/'); + if (is_dir($file) && FilesUtil::isEmptyDir($file)) { + $this->addEmptyDir($filename); } elseif (is_file($file)) { $this->addFile($file, $filename, $compressionMethod); } @@ -842,6 +844,7 @@ class ZipFile implements ZipFileInterface * Use ZipFile::METHOD_STORED, ZipFile::METHOD_DEFLATED or ZipFile::METHOD_BZIP2. * If null, then auto choosing method. * @return ZipFileInterface + * @throws ZipException * @internal param bool $recursive Recursive search. */ public function addFilesFromRegexRecursive($inputDir, $regexPattern, $localPath = "/", $compressionMethod = null) @@ -869,14 +872,16 @@ class ZipFile implements ZipFileInterface * @param string $oldName Old entry name. * @param string $newName New entry name. * @return ZipFileInterface - * @throws InvalidArgumentException - * @throws ZipNotFoundEntry + * + * @throws ZipException */ public function rename($oldName, $newName) { - if (null === $oldName || null === $newName) { + if ($oldName === null || $newName === null) { throw new InvalidArgumentException("name is null"); } + $oldName = ltrim((string)$oldName, '\\/'); + $newName = ltrim((string)$newName, '\\/'); if ($oldName !== $newName) { $this->zipModel->renameEntry($oldName, $newName); } @@ -888,13 +893,13 @@ class ZipFile implements ZipFileInterface * * @param string $entryName Zip Entry name. * @return ZipFileInterface - * @throws ZipNotFoundEntry If entry not found. + * @throws ZipEntryNotFoundException If entry not found. */ public function deleteFromName($entryName) { - $entryName = (string)$entryName; + $entryName = ltrim((string)$entryName, '\\/'); if (!$this->zipModel->deleteEntry($entryName)) { - throw new ZipNotFoundEntry("Entry " . $entryName . ' not found!'); + throw new ZipEntryNotFoundException($entryName); } return $this; } @@ -904,12 +909,11 @@ class ZipFile implements ZipFileInterface * * @param string $globPattern Glob pattern * @return ZipFileInterface - * @throws InvalidArgumentException * @sse https://en.wikipedia.org/wiki/Glob_(programming) Glob pattern syntax */ public function deleteFromGlob($globPattern) { - if (null === $globPattern || !is_string($globPattern) || empty($globPattern)) { + if ($globPattern === null || !is_string($globPattern) || empty($globPattern)) { throw new InvalidArgumentException("Glob pattern is empty"); } $globPattern = '~' . FilesUtil::convertGlobToRegEx($globPattern) . '~si'; @@ -922,11 +926,10 @@ class ZipFile implements ZipFileInterface * * @param string $regexPattern Regex pattern * @return ZipFileInterface - * @throws InvalidArgumentException */ public function deleteFromRegex($regexPattern) { - if (null === $regexPattern || !is_string($regexPattern) || empty($regexPattern)) { + if ($regexPattern === null || !is_string($regexPattern) || empty($regexPattern)) { throw new InvalidArgumentException("Regex pattern is empty."); } $this->matcher()->match($regexPattern)->delete(); @@ -948,19 +951,18 @@ class ZipFile implements ZipFileInterface * * @param int $compressionLevel * @return ZipFileInterface - * @throws InvalidArgumentException * @see ZipFileInterface::LEVEL_DEFAULT_COMPRESSION * @see ZipFileInterface::LEVEL_SUPER_FAST * @see ZipFileInterface::LEVEL_FAST * @see ZipFileInterface::LEVEL_BEST_COMPRESSION */ - public function setCompressionLevel($compressionLevel = ZipFileInterface::LEVEL_DEFAULT_COMPRESSION) + public function setCompressionLevel($compressionLevel = self::LEVEL_DEFAULT_COMPRESSION) { - if ($compressionLevel < ZipFileInterface::LEVEL_DEFAULT_COMPRESSION || - $compressionLevel > ZipFileInterface::LEVEL_BEST_COMPRESSION + if ($compressionLevel < self::LEVEL_DEFAULT_COMPRESSION || + $compressionLevel > self::LEVEL_BEST_COMPRESSION ) { throw new InvalidArgumentException('Invalid compression level. Minimum level ' . - ZipFileInterface::LEVEL_DEFAULT_COMPRESSION . '. Maximum level ' . ZipFileInterface::LEVEL_BEST_COMPRESSION); + self::LEVEL_DEFAULT_COMPRESSION . '. Maximum level ' . self::LEVEL_BEST_COMPRESSION); } $this->matcher()->all()->invoke(function ($entry) use ($compressionLevel) { $this->setCompressionLevelEntry($entry, $compressionLevel); @@ -980,15 +982,15 @@ class ZipFile implements ZipFileInterface */ public function setCompressionLevelEntry($entryName, $compressionLevel) { - if (null !== $compressionLevel) { + if ($compressionLevel !== null) { if ($compressionLevel < ZipFileInterface::LEVEL_DEFAULT_COMPRESSION || $compressionLevel > ZipFileInterface::LEVEL_BEST_COMPRESSION ) { throw new InvalidArgumentException('Invalid compression level. Minimum level ' . - ZipFileInterface::LEVEL_DEFAULT_COMPRESSION . '. Maximum level ' . ZipFileInterface::LEVEL_BEST_COMPRESSION); + self::LEVEL_DEFAULT_COMPRESSION . '. Maximum level ' . self::LEVEL_BEST_COMPRESSION); } $entry = $this->zipModel->getEntry($entryName); - if ($entry->getCompressionLevel() !== $compressionLevel) { + if ($compressionLevel !== $entry->getCompressionLevel()) { $entry = $this->zipModel->getEntryForChanges($entry); $entry->setCompressionLevel($compressionLevel); } @@ -1008,10 +1010,10 @@ class ZipFile implements ZipFileInterface public function setCompressionMethodEntry($entryName, $compressionMethod) { if (!in_array($compressionMethod, self::$allowCompressionMethods, true)) { - throw new ZipUnsupportMethod('Unsupported method ' . $compressionMethod); + throw new ZipUnsupportMethodException('Unsupported method ' . $compressionMethod); } $entry = $this->zipModel->getEntry($entryName); - if ($entry->getMethod() !== $compressionMethod) { + if ($compressionMethod !== $entry->getMethod()) { $this->zipModel ->getEntryForChanges($entry) ->setMethod($compressionMethod); @@ -1037,6 +1039,7 @@ class ZipFile implements ZipFileInterface * * @param string $password Password * @return ZipFileInterface + * @throws ZipException * @deprecated using ZipFileInterface::setReadPassword() */ public function withReadPassword($password) @@ -1049,6 +1052,7 @@ class ZipFile implements ZipFileInterface * * @param string $password Password * @return ZipFileInterface + * @throws ZipException */ public function setReadPassword($password) { @@ -1062,6 +1066,7 @@ class ZipFile implements ZipFileInterface * @param string $entryName * @param string $password Password * @return ZipFileInterface + * @throws ZipException */ public function setReadPasswordEntry($entryName, $password) { @@ -1076,6 +1081,7 @@ class ZipFile implements ZipFileInterface * @param int|null $encryptionMethod Encryption method * @return ZipFileInterface * @deprecated using ZipFileInterface::setPassword() + * @throws ZipException */ public function withNewPassword($password, $encryptionMethod = self::ENCRYPTION_METHOD_WINZIP_AES_256) { @@ -1093,9 +1099,9 @@ class ZipFile implements ZipFileInterface public function setPassword($password, $encryptionMethod = self::ENCRYPTION_METHOD_WINZIP_AES_256) { $this->zipModel->setWritePassword($password); - if (null !== $encryptionMethod) { - if (!in_array($encryptionMethod, self::$allowEncryptionMethods)) { - throw new ZipException('Invalid encryption method'); + if ($encryptionMethod !== null) { + if (!in_array($encryptionMethod, self::$allowEncryptionMethods, true)) { + throw new ZipException('Invalid encryption method "' . $encryptionMethod . '"'); } $this->zipModel->setEncryptionMethod($encryptionMethod); } @@ -1113,9 +1119,9 @@ class ZipFile implements ZipFileInterface */ public function setPasswordEntry($entryName, $password, $encryptionMethod = null) { - if (null !== $encryptionMethod) { - if (!in_array($encryptionMethod, self::$allowEncryptionMethods)) { - throw new ZipException('Invalid encryption method'); + if ($encryptionMethod !== null) { + if (!in_array($encryptionMethod, self::$allowEncryptionMethods, true)) { + throw new ZipException('Invalid encryption method "' . $encryptionMethod . '"'); } } $this->matcher()->add($entryName)->setPassword($password, $encryptionMethod); @@ -1190,7 +1196,6 @@ class ZipFile implements ZipFileInterface * * @param string $filename Output filename * @return ZipFileInterface - * @throws InvalidArgumentException * @throws ZipException */ public function saveAsFile($filename) @@ -1204,6 +1209,9 @@ class ZipFile implements ZipFileInterface $this->saveAsStream($handle); if (!@rename($tempFilename, $filename)) { + if (is_file($tempFilename)) { + unlink($tempFilename); + } throw new ZipException('Can not move ' . $tempFilename . ' to ' . $filename); } return $this; @@ -1234,7 +1242,7 @@ class ZipFile implements ZipFileInterface * @param string $outputFilename Output filename * @param string|null $mimeType Mime-Type * @param bool $attachment Http Header 'Content-Disposition' if true then attachment otherwise inline - * @throws InvalidArgumentException + * @throws ZipException */ public function outputAsAttachment($outputFilename, $mimeType = null, $attachment = true) { @@ -1273,7 +1281,7 @@ class ZipFile implements ZipFileInterface * @param string|null $mimeType Mime-Type * @param bool $attachment Http Header 'Content-Disposition' if true then attachment otherwise inline * @return ResponseInterface - * @throws InvalidArgumentException + * @throws ZipException */ public function outputAsResponse(ResponseInterface $response, $outputFilename, $mimeType = null, $attachment = true) { @@ -1310,7 +1318,8 @@ class ZipFile implements ZipFileInterface } /** - * @param $handle + * @param resource $handle + * @throws ZipException */ protected function writeZipToStream($handle) { @@ -1323,7 +1332,7 @@ class ZipFile implements ZipFileInterface /** * Returns the zip archive as a string. * @return string - * @throws InvalidArgumentException + * @throws ZipException */ public function outputAsString() { @@ -1349,7 +1358,7 @@ class ZipFile implements ZipFileInterface */ public function close() { - if (null !== $this->inputStream) { + if ($this->inputStream !== null) { $this->inputStream->close(); $this->inputStream = null; $this->zipModel = new ZipModel(); @@ -1363,22 +1372,22 @@ class ZipFile implements ZipFileInterface */ public function rewrite() { - if (null === $this->inputStream) { + if ($this->inputStream === null) { throw new ZipException('input stream is null'); } $meta = stream_get_meta_data($this->inputStream->getStream()); $content = $this->outputAsString(); $this->close(); - if ('plainfile' === $meta['wrapper_type']) { + if ($meta['wrapper_type'] === 'plainfile') { /** * @var resource $uri */ $uri = $meta['uri']; if (file_put_contents($uri, $content) === false) { - throw new ZipException("Can not overwrite the zip file in the {$uri} file."); + throw new ZipException("Can not overwrite the zip file in the $uri file."); } if (!($handle = @fopen($uri, 'rb'))) { - throw new ZipException("File {$uri} can't open."); + throw new ZipException("File $uri can't open."); } return $this->openFromStream($handle); } @@ -1398,7 +1407,7 @@ class ZipFile implements ZipFileInterface * @link http://php.net/manual/en/arrayaccess.offsetset.php * @param string $entryName The offset to assign the value to. * @param mixed $contents The value to set. - * @throws InvalidArgumentException + * @throws ZipException * @see ZipFile::addFromString * @see ZipFile::addEmptyDir * @see ZipFile::addFile @@ -1406,10 +1415,10 @@ class ZipFile implements ZipFileInterface */ public function offsetSet($entryName, $contents) { - if (null === $entryName) { + if ($entryName === null) { throw new InvalidArgumentException('entryName is null'); } - $entryName = (string)$entryName; + $entryName = ltrim((string)$entryName, "\\/"); if (strlen($entryName) === 0) { throw new InvalidArgumentException('entryName is empty'); } @@ -1426,8 +1435,7 @@ class ZipFile implements ZipFileInterface } elseif (is_resource($contents)) { $this->addFromStream($contents, $entryName); } else { - $contents = (string)$contents; - $this->addFromString($entryName, $contents); + $this->addFromString($entryName, (string)$contents); } } @@ -1435,7 +1443,7 @@ class ZipFile implements ZipFileInterface * Offset to unset * @link http://php.net/manual/en/arrayaccess.offsetunset.php * @param string $entryName The offset to unset. - * @throws ZipUnsupportMethod + * @throws ZipEntryNotFoundException */ public function offsetUnset($entryName) { @@ -1447,6 +1455,7 @@ class ZipFile implements ZipFileInterface * @link http://php.net/manual/en/iterator.current.php * @return mixed Can return any type. * @since 5.0.0 + * @throws ZipException */ public function current() { @@ -1458,7 +1467,7 @@ class ZipFile implements ZipFileInterface * @link http://php.net/manual/en/arrayaccess.offsetget.php * @param string $entryName The offset to retrieve. * @return string|null - * @throws ZipNotFoundEntry + * @throws ZipException */ public function offsetGet($entryName) { diff --git a/src/PhpZip/ZipFileInterface.php b/src/PhpZip/ZipFileInterface.php index 53ab761..2237283 100644 --- a/src/PhpZip/ZipFileInterface.php +++ b/src/PhpZip/ZipFileInterface.php @@ -2,10 +2,8 @@ namespace PhpZip; -use PhpZip\Exception\InvalidArgumentException; +use PhpZip\Exception\ZipEntryNotFoundException; use PhpZip\Exception\ZipException; -use PhpZip\Exception\ZipNotFoundEntry; -use PhpZip\Exception\ZipUnsupportMethod; use PhpZip\Model\ZipEntry; use PhpZip\Model\ZipEntryMatcher; use PhpZip\Model\ZipInfo; @@ -87,7 +85,6 @@ interface ZipFileInterface extends \Countable, \ArrayAccess, \Iterator * * @param string $filename * @return ZipFileInterface - * @throws InvalidArgumentException if file doesn't exists. * @throws ZipException if can't open file. */ public function openFile($filename); @@ -97,7 +94,6 @@ interface ZipFileInterface extends \Countable, \ArrayAccess, \Iterator * * @param string $data * @return ZipFileInterface - * @throws InvalidArgumentException if data not available. * @throws ZipException if can't open temp stream. */ public function openFromString($data); @@ -107,8 +103,6 @@ interface ZipFileInterface extends \Countable, \ArrayAccess, \Iterator * * @param resource $handle * @return ZipFileInterface - * @throws InvalidArgumentException Invalid stream resource - * or resource cannot seekable stream */ public function openFromStream($handle); @@ -129,7 +123,6 @@ interface ZipFileInterface extends \Countable, \ArrayAccess, \Iterator * * @param null|string $comment * @return ZipFileInterface - * @throws InvalidArgumentException Length comment out of range */ public function setArchiveComment($comment = null); @@ -140,7 +133,7 @@ interface ZipFileInterface extends \Countable, \ArrayAccess, \Iterator * * @param string $entryName * @return bool - * @throws ZipNotFoundEntry + * @throws ZipEntryNotFoundException */ public function isDirectory($entryName); @@ -149,7 +142,7 @@ interface ZipFileInterface extends \Countable, \ArrayAccess, \Iterator * * @param string $entryName * @return string - * @throws ZipNotFoundEntry + * @throws ZipEntryNotFoundException */ public function getEntryComment($entryName); @@ -159,7 +152,7 @@ interface ZipFileInterface extends \Countable, \ArrayAccess, \Iterator * @param string $entryName * @param string|null $comment * @return ZipFileInterface - * @throws ZipNotFoundEntry + * @throws ZipEntryNotFoundException */ public function setEntryComment($entryName, $comment = null); @@ -184,7 +177,7 @@ interface ZipFileInterface extends \Countable, \ArrayAccess, \Iterator * * @param string|ZipEntry $entryName * @return ZipInfo - * @throws ZipNotFoundEntry + * @throws ZipEntryNotFoundException */ public function getEntryInfo($entryName); @@ -222,8 +215,6 @@ interface ZipFileInterface extends \Countable, \ArrayAccess, \Iterator * Use ZipFile::METHOD_STORED, ZipFile::METHOD_DEFLATED or ZipFile::METHOD_BZIP2. * If null, then auto choosing method. * @return ZipFileInterface - * @throws InvalidArgumentException If incorrect data or entry name. - * @throws ZipUnsupportMethod * @see ZipFileInterface::METHOD_STORED * @see ZipFileInterface::METHOD_DEFLATED * @see ZipFileInterface::METHOD_BZIP2 @@ -239,8 +230,6 @@ interface ZipFileInterface extends \Countable, \ArrayAccess, \Iterator * Use ZipFile::METHOD_STORED, ZipFile::METHOD_DEFLATED or ZipFile::METHOD_BZIP2. * If null, then auto choosing method. * @return ZipFileInterface - * @throws InvalidArgumentException - * @throws ZipUnsupportMethod * @see ZipFileInterface::METHOD_STORED * @see ZipFileInterface::METHOD_DEFLATED * @see ZipFileInterface::METHOD_BZIP2 @@ -256,8 +245,6 @@ interface ZipFileInterface extends \Countable, \ArrayAccess, \Iterator * Use ZipFile::METHOD_STORED, ZipFile::METHOD_DEFLATED or ZipFile::METHOD_BZIP2. * If null, then auto choosing method. * @return ZipFileInterface - * @throws InvalidArgumentException - * @throws ZipUnsupportMethod * @see ZipFileInterface::METHOD_STORED * @see ZipFileInterface::METHOD_DEFLATED * @see ZipFileInterface::METHOD_BZIP2 @@ -269,7 +256,6 @@ interface ZipFileInterface extends \Countable, \ArrayAccess, \Iterator * * @param string $dirName * @return ZipFileInterface - * @throws InvalidArgumentException */ public function addEmptyDir($dirName); @@ -282,7 +268,6 @@ interface ZipFileInterface extends \Countable, \ArrayAccess, \Iterator * Use ZipFile::METHOD_STORED, ZipFile::METHOD_DEFLATED or ZipFile::METHOD_BZIP2. * If null, then auto choosing method. * @return ZipFileInterface - * @throws InvalidArgumentException */ public function addDir($inputDir, $localPath = "/", $compressionMethod = null); @@ -295,8 +280,6 @@ interface ZipFileInterface extends \Countable, \ArrayAccess, \Iterator * Use ZipFile::METHOD_STORED, ZipFile::METHOD_DEFLATED or ZipFile::METHOD_BZIP2. * If null, then auto choosing method. * @return ZipFileInterface - * @throws InvalidArgumentException - * @throws ZipUnsupportMethod * @see ZipFileInterface::METHOD_STORED * @see ZipFileInterface::METHOD_DEFLATED * @see ZipFileInterface::METHOD_BZIP2 @@ -312,8 +295,6 @@ interface ZipFileInterface extends \Countable, \ArrayAccess, \Iterator * Use ZipFile::METHOD_STORED, ZipFile::METHOD_DEFLATED or ZipFile::METHOD_BZIP2. * If null, then auto choosing method. * @return ZipFileInterface - * @throws InvalidArgumentException - * @throws ZipUnsupportMethod * @see ZipFileInterface::METHOD_STORED * @see ZipFileInterface::METHOD_DEFLATED * @see ZipFileInterface::METHOD_BZIP2 @@ -330,7 +311,6 @@ interface ZipFileInterface extends \Countable, \ArrayAccess, \Iterator * Use ZipFile::METHOD_STORED, ZipFile::METHOD_DEFLATED or ZipFile::METHOD_BZIP2. * If null, then auto choosing method. * @return ZipFileInterface - * @throws InvalidArgumentException * @sse https://en.wikipedia.org/wiki/Glob_(programming) Glob pattern syntax */ public function addFilesFromGlob($inputDir, $globPattern, $localPath = '/', $compressionMethod = null); @@ -345,7 +325,6 @@ interface ZipFileInterface extends \Countable, \ArrayAccess, \Iterator * Use ZipFile::METHOD_STORED, ZipFile::METHOD_DEFLATED or ZipFile::METHOD_BZIP2. * If null, then auto choosing method. * @return ZipFileInterface - * @throws InvalidArgumentException * @sse https://en.wikipedia.org/wiki/Glob_(programming) Glob pattern syntax */ public function addFilesFromGlobRecursive($inputDir, $globPattern, $localPath = '/', $compressionMethod = null); @@ -393,8 +372,7 @@ interface ZipFileInterface extends \Countable, \ArrayAccess, \Iterator * @param string $oldName Old entry name. * @param string $newName New entry name. * @return ZipFileInterface - * @throws InvalidArgumentException - * @throws ZipNotFoundEntry + * @throws ZipEntryNotFoundException */ public function rename($oldName, $newName); @@ -403,7 +381,7 @@ interface ZipFileInterface extends \Countable, \ArrayAccess, \Iterator * * @param string $entryName Zip Entry name. * @return ZipFileInterface - * @throws ZipNotFoundEntry If entry not found. + * @throws ZipEntryNotFoundException If entry not found. */ public function deleteFromName($entryName); @@ -412,7 +390,6 @@ interface ZipFileInterface extends \Countable, \ArrayAccess, \Iterator * * @param string $globPattern Glob pattern * @return ZipFileInterface - * @throws InvalidArgumentException * @sse https://en.wikipedia.org/wiki/Glob_(programming) Glob pattern syntax */ public function deleteFromGlob($globPattern); @@ -422,7 +399,6 @@ interface ZipFileInterface extends \Countable, \ArrayAccess, \Iterator * * @param string $regexPattern Regex pattern * @return ZipFileInterface - * @throws InvalidArgumentException */ public function deleteFromRegex($regexPattern); @@ -576,7 +552,6 @@ interface ZipFileInterface extends \Countable, \ArrayAccess, \Iterator * * @param string $filename Output filename * @return ZipFileInterface - * @throws InvalidArgumentException * @throws ZipException */ public function saveAsFile($filename); @@ -608,14 +583,12 @@ interface ZipFileInterface extends \Countable, \ArrayAccess, \Iterator * @param string|null $mimeType Mime-Type * @param bool $attachment Http Header 'Content-Disposition' if true then attachment otherwise inline * @return ResponseInterface - * @throws InvalidArgumentException */ public function outputAsResponse(ResponseInterface $response, $outputFilename, $mimeType = null, $attachment = true); /** * Returns the zip archive as a string. * @return string - * @throws InvalidArgumentException */ public function outputAsString(); diff --git a/tests/PhpZip/PhpZipExtResourceTest.php b/tests/PhpZip/PhpZipExtResourceTest.php index f1efb6f..595b1fb 100644 --- a/tests/PhpZip/PhpZipExtResourceTest.php +++ b/tests/PhpZip/PhpZipExtResourceTest.php @@ -2,7 +2,7 @@ namespace PhpZip; -use PhpZip\Exception\ZipAuthenticationException; +use PhpZip\Exception\ZipException; /** * Some tests from the official extension of php-zip. @@ -12,6 +12,7 @@ class PhpZipExtResourceTest extends ZipTestCase /** * Bug #7214 (zip_entry_read() binary safe) * @see https://github.com/php/php-src/blob/master/ext/zip/tests/bug7214.phpt + * @throws ZipException */ public function testBinaryNull() { @@ -21,16 +22,17 @@ class PhpZipExtResourceTest extends ZipTestCase $zipFile->openFile($filename); foreach ($zipFile as $name => $contents) { $info = $zipFile->getEntryInfo($name); - self::assertEquals(strlen($contents), $info->getSize()); + $this->assertEquals(strlen($contents), $info->getSize()); } $zipFile->close(); - self::assertCorrectZipArchive($filename); + $this->assertCorrectZipArchive($filename); } /** * Bug #8009 (cannot add again same entry to an archive) * @see https://github.com/php/php-src/blob/master/ext/zip/tests/bug8009.phpt + * @throws ZipException */ public function testBug8009() { @@ -42,13 +44,13 @@ class PhpZipExtResourceTest extends ZipTestCase $zipFile->saveAsFile($this->outputFilename); $zipFile->close(); - self::assertCorrectZipArchive($this->outputFilename); + $this->assertCorrectZipArchive($this->outputFilename); $zipFile->openFile($this->outputFilename); - self::assertCount(2, $zipFile); - self::assertTrue(isset($zipFile['1.txt'])); - self::assertTrue(isset($zipFile['2.txt'])); - self::assertEquals($zipFile['2.txt'], $zipFile['1.txt']); + $this->assertCount(2, $zipFile); + $this->assertTrue(isset($zipFile['1.txt'])); + $this->assertTrue(isset($zipFile['2.txt'])); + $this->assertEquals($zipFile['2.txt'], $zipFile['1.txt']); $zipFile->close(); } @@ -58,17 +60,18 @@ class PhpZipExtResourceTest extends ZipTestCase * @see https://github.com/php/php-src/blob/master/ext/zip/tests/bug40228-mb.phpt * @dataProvider provideBug40228 * @param string $filename + * @throws ZipException */ public function testBug40228($filename) { - self::assertTrue(mkdir($this->outputDirname, 0755, true)); + $this->assertTrue(mkdir($this->outputDirname, 0755, true)); $zipFile = new ZipFile(); $zipFile->openFile($filename); $zipFile->extractTo($this->outputDirname); $zipFile->close(); - self::assertTrue(is_dir($this->outputDirname . '/test/empty')); + $this->assertTrue(is_dir($this->outputDirname . '/test/empty')); } public function provideBug40228() @@ -83,6 +86,7 @@ class PhpZipExtResourceTest extends ZipTestCase * @see https://github.com/php/php-src/blob/master/ext/zip/tests/bug49072.phpt * @expectedException \PhpZip\Exception\Crc32Exception * @expectedExceptionMessage file1 + * @throws ZipException */ public function testBug49072() { @@ -98,21 +102,22 @@ class PhpZipExtResourceTest extends ZipTestCase * @see https://github.com/php/php-src/blob/master/ext/zip/tests/bug70752.phpt * @expectedException \PhpZip\Exception\ZipAuthenticationException * @expectedExceptionMessage Bad password for entry bug70752.txt + * @throws ZipException */ public function testBug70752() { $filename = __DIR__ . '/php-zip-ext-test-resources/bug70752.zip'; - self::assertTrue(mkdir($this->outputDirname, 0755, true)); + $this->assertTrue(mkdir($this->outputDirname, 0755, true)); $zipFile = new ZipFile(); try { $zipFile->openFile($filename); $zipFile->setReadPassword('bar'); $zipFile->extractTo($this->outputDirname); - self::markTestIncomplete('failed test'); - } catch (ZipAuthenticationException $exception) { - self::assertFalse(file_exists($this->outputDirname . '/bug70752.txt')); + $this->markTestIncomplete('failed test'); + } catch (ZipException $exception) { + $this->assertFalse(file_exists($this->outputDirname . '/bug70752.txt')); $zipFile->close(); throw $exception; } @@ -121,6 +126,7 @@ class PhpZipExtResourceTest extends ZipTestCase /** * Bug #12414 ( extracting files from damaged archives) * @see https://github.com/php/php-src/blob/master/ext/zip/tests/pecl12414.phpt + * @throws ZipException */ public function testPecl12414() { @@ -132,10 +138,10 @@ class PhpZipExtResourceTest extends ZipTestCase $zipFile->openFile($filename); $info = $zipFile->getEntryInfo($entryName); - self::assertTrue($info->getSize() > 0); + $this->assertTrue($info->getSize() > 0); $contents = $zipFile[$entryName]; - self::assertEquals(strlen($contents), $info->getSize()); + $this->assertEquals(strlen($contents), $info->getSize()); $zipFile->close(); } diff --git a/tests/PhpZip/ZipAlignTest.php b/tests/PhpZip/ZipAlignTest.php index bb98eab..36c273f 100644 --- a/tests/PhpZip/ZipAlignTest.php +++ b/tests/PhpZip/ZipAlignTest.php @@ -2,6 +2,7 @@ namespace PhpZip; +use PhpZip\Exception\ZipException; use PhpZip\Util\CryptoUtil; /** @@ -9,14 +10,17 @@ use PhpZip\Util\CryptoUtil; */ class ZipAlignTest extends ZipTestCase { + /** + * @throws ZipException + */ public function testApkAlignedAndSetZipAlignAndReSave() { $filename = __DIR__ . '/resources/test.apk'; - self::assertCorrectZipArchive($filename); - $result = self::doZipAlignVerify($filename); + $this->assertCorrectZipArchive($filename); + $result = $this->assertVerifyZipAlign($filename); if (null !== $result) { - self::assertTrue($result); + $this->assertTrue($result); } $zipFile = new ZipFile(); @@ -25,15 +29,16 @@ class ZipAlignTest extends ZipTestCase $zipFile->saveAsFile($this->outputFilename); $zipFile->close(); - self::assertCorrectZipArchive($this->outputFilename); - $result = self::doZipAlignVerify($this->outputFilename, true); + $this->assertCorrectZipArchive($this->outputFilename); + $result = $this->assertVerifyZipAlign($this->outputFilename, true); if (null !== $result) { - self::assertTrue($result); + $this->assertTrue($result); } } /** * Test zip alignment. + * @throws ZipException */ public function testZipAlignSourceZip() { @@ -48,30 +53,33 @@ class ZipAlignTest extends ZipTestCase $zipFile->saveAsFile($this->outputFilename); $zipFile->close(); - self::assertCorrectZipArchive($this->outputFilename); + $this->assertCorrectZipArchive($this->outputFilename); - $result = self::doZipAlignVerify($this->outputFilename); + $result = $this->assertVerifyZipAlign($this->outputFilename); if ($result === null) { return; } // zip align not installed // check not zip align - self::assertFalse($result); + $this->assertFalse($result); $zipFile->openFile($this->outputFilename); $zipFile->setZipAlign(4); $zipFile->saveAsFile($this->outputFilename); $zipFile->close(); - self::assertCorrectZipArchive($this->outputFilename); + $this->assertCorrectZipArchive($this->outputFilename); - $result = self::doZipAlignVerify($this->outputFilename, true); - self::assertNotNull($result); + $result = $this->assertVerifyZipAlign($this->outputFilename, true); + $this->assertNotNull($result); // check zip align - self::assertTrue($result); + $this->assertTrue($result); } + /** + * @throws ZipException + */ public function testZipAlignNewFiles() { $zipFile = new ZipFile(); @@ -86,16 +94,19 @@ class ZipAlignTest extends ZipTestCase $zipFile->saveAsFile($this->outputFilename); $zipFile->close(); - self::assertCorrectZipArchive($this->outputFilename); + $this->assertCorrectZipArchive($this->outputFilename); - $result = self::doZipAlignVerify($this->outputFilename); + $result = $this->assertVerifyZipAlign($this->outputFilename); if ($result === null) { return; } // zip align not installed // check not zip align - self::assertTrue($result); + $this->assertTrue($result); } + /** + * @throws ZipException + */ public function testZipAlignFromModifiedZipArchive() { $zipFile = new ZipFile(); @@ -109,15 +120,15 @@ class ZipAlignTest extends ZipTestCase $zipFile->saveAsFile($this->outputFilename); $zipFile->close(); - self::assertCorrectZipArchive($this->outputFilename); + $this->assertCorrectZipArchive($this->outputFilename); - $result = self::doZipAlignVerify($this->outputFilename); + $result = $this->assertVerifyZipAlign($this->outputFilename); if ($result === null) { return; } // zip align not installed // check not zip align - self::assertFalse($result); + $this->assertFalse($result); $zipFile->openFile($this->outputFilename); $zipFile->deleteFromRegex("~entry2[\d]+\.txt$~s"); @@ -136,12 +147,12 @@ class ZipAlignTest extends ZipTestCase $zipFile->saveAsFile($this->outputFilename); $zipFile->close(); - self::assertCorrectZipArchive($this->outputFilename); + $this->assertCorrectZipArchive($this->outputFilename); - $result = self::doZipAlignVerify($this->outputFilename, true); - self::assertNotNull($result); + $result = $this->assertVerifyZipAlign($this->outputFilename, true); + $this->assertNotNull($result); // check zip align - self::assertTrue($result); + $this->assertTrue($result); } } diff --git a/tests/PhpZip/ZipEventTest.php b/tests/PhpZip/ZipEventTest.php index f23f0b1..818f920 100644 --- a/tests/PhpZip/ZipEventTest.php +++ b/tests/PhpZip/ZipEventTest.php @@ -2,6 +2,8 @@ namespace PhpZip; +use PhpZip\Exception\ZipException; + class ZipFileExtended extends ZipFile { protected function onBeforeSave() @@ -14,29 +16,32 @@ class ZipFileExtended extends ZipFile class ZipEventTest extends ZipTestCase { + /** + * @throws ZipException + */ public function testBeforeSave() { $zipFile = new ZipFileExtended(); $zipFile->openFile(__DIR__ . '/resources/test.apk'); - self::assertTrue(isset($zipFile['META-INF/MANIFEST.MF'])); - self::assertTrue(isset($zipFile['META-INF/CERT.SF'])); - self::assertTrue(isset($zipFile['META-INF/CERT.RSA'])); + $this->assertTrue(isset($zipFile['META-INF/MANIFEST.MF'])); + $this->assertTrue(isset($zipFile['META-INF/CERT.SF'])); + $this->assertTrue(isset($zipFile['META-INF/CERT.RSA'])); $zipFile->saveAsFile($this->outputFilename); - self::assertFalse(isset($zipFile['META-INF/MANIFEST.MF'])); - self::assertFalse(isset($zipFile['META-INF/CERT.SF'])); - self::assertFalse(isset($zipFile['META-INF/CERT.RSA'])); + $this->assertFalse(isset($zipFile['META-INF/MANIFEST.MF'])); + $this->assertFalse(isset($zipFile['META-INF/CERT.SF'])); + $this->assertFalse(isset($zipFile['META-INF/CERT.RSA'])); $zipFile->close(); - self::assertCorrectZipArchive($this->outputFilename); - $result = self::doZipAlignVerify($this->outputFilename); + $this->assertCorrectZipArchive($this->outputFilename); + $result = $this->assertVerifyZipAlign($this->outputFilename); if (null !== $result) { - self::assertTrue($result); + $this->assertTrue($result); } $zipFile->openFile($this->outputFilename); - self::assertFalse(isset($zipFile['META-INF/MANIFEST.MF'])); - self::assertFalse(isset($zipFile['META-INF/CERT.SF'])); - self::assertFalse(isset($zipFile['META-INF/CERT.RSA'])); + $this->assertFalse(isset($zipFile['META-INF/MANIFEST.MF'])); + $this->assertFalse(isset($zipFile['META-INF/CERT.SF'])); + $this->assertFalse(isset($zipFile['META-INF/CERT.RSA'])); $zipFile->close(); } } diff --git a/tests/PhpZip/ZipFileAddDirTest.php b/tests/PhpZip/ZipFileAddDirTest.php index 039c1c3..3b13553 100644 --- a/tests/PhpZip/ZipFileAddDirTest.php +++ b/tests/PhpZip/ZipFileAddDirTest.php @@ -2,6 +2,7 @@ namespace PhpZip; +use PhpZip\Exception\ZipException; use PhpZip\Util\Iterator\IgnoreFilesFilterIterator; use PhpZip\Util\Iterator\IgnoreFilesRecursiveFilterIterator; @@ -72,6 +73,9 @@ class ZipFileAddDirTest extends ZipTestCase self::assertEmpty($actualResultFiles); } + /** + * @throws ZipException + */ public function testAddDirWithLocalPath() { $localPath = 'to/path'; @@ -81,10 +85,10 @@ class ZipFileAddDirTest extends ZipTestCase $zipFile->saveAsFile($this->outputFilename); $zipFile->close(); - self::assertCorrectZipArchive($this->outputFilename); + $this->assertCorrectZipArchive($this->outputFilename); $zipFile->openFile($this->outputFilename); - self::assertFilesResult($zipFile, [ + $this->assertFilesResult($zipFile, [ '.hidden', 'text file.txt', 'Текстовый документ.txt', @@ -93,6 +97,9 @@ class ZipFileAddDirTest extends ZipTestCase $zipFile->close(); } + /** + * @throws ZipException + */ public function testAddDirWithoutLocalPath() { $zipFile = new ZipFile(); @@ -100,10 +107,10 @@ class ZipFileAddDirTest extends ZipTestCase $zipFile->saveAsFile($this->outputFilename); $zipFile->close(); - self::assertCorrectZipArchive($this->outputFilename); + $this->assertCorrectZipArchive($this->outputFilename); $zipFile->openFile($this->outputFilename); - self::assertFilesResult($zipFile, [ + $this->assertFilesResult($zipFile, [ '.hidden', 'text file.txt', 'Текстовый документ.txt', @@ -112,6 +119,9 @@ class ZipFileAddDirTest extends ZipTestCase $zipFile->close(); } + /** + * @throws ZipException + */ public function testAddFilesFromIterator() { $localPath = 'to/project'; @@ -123,10 +133,10 @@ class ZipFileAddDirTest extends ZipTestCase $zipFile->saveAsFile($this->outputFilename); $zipFile->close(); - self::assertCorrectZipArchive($this->outputFilename); + $this->assertCorrectZipArchive($this->outputFilename); $zipFile->openFile($this->outputFilename); - self::assertFilesResult($zipFile, [ + $this->assertFilesResult($zipFile, [ '.hidden', 'text file.txt', 'Текстовый документ.txt', @@ -135,6 +145,9 @@ class ZipFileAddDirTest extends ZipTestCase $zipFile->close(); } + /** + * @throws ZipException + */ public function testAddFilesFromIteratorEmptyLocalPath() { $localPath = ''; @@ -146,10 +159,10 @@ class ZipFileAddDirTest extends ZipTestCase $zipFile->saveAsFile($this->outputFilename); $zipFile->close(); - self::assertCorrectZipArchive($this->outputFilename); + $this->assertCorrectZipArchive($this->outputFilename); $zipFile->openFile($this->outputFilename); - self::assertFilesResult($zipFile, [ + $this->assertFilesResult($zipFile, [ '.hidden', 'text file.txt', 'Текстовый документ.txt', @@ -158,6 +171,9 @@ class ZipFileAddDirTest extends ZipTestCase $zipFile->close(); } + /** + * @throws ZipException + */ public function testAddFilesFromRecursiveIterator() { $localPath = 'to/project'; @@ -169,13 +185,16 @@ class ZipFileAddDirTest extends ZipTestCase $zipFile->saveAsFile($this->outputFilename); $zipFile->close(); - self::assertCorrectZipArchive($this->outputFilename); + $this->assertCorrectZipArchive($this->outputFilename); $zipFile->openFile($this->outputFilename); - self::assertFilesResult($zipFile, array_keys(self::$files), $localPath); + $this->assertFilesResult($zipFile, array_keys(self::$files), $localPath); $zipFile->close(); } + /** + * @throws ZipException + */ public function testAddRecursiveDirWithLocalPath() { $localPath = 'to/path'; @@ -185,13 +204,16 @@ class ZipFileAddDirTest extends ZipTestCase $zipFile->saveAsFile($this->outputFilename); $zipFile->close(); - self::assertCorrectZipArchive($this->outputFilename); + $this->assertCorrectZipArchive($this->outputFilename); $zipFile->openFile($this->outputFilename); - self::assertFilesResult($zipFile, array_keys(self::$files), $localPath); + $this->assertFilesResult($zipFile, array_keys(self::$files), $localPath); $zipFile->close(); } + /** + * @throws ZipException + */ public function testAddRecursiveDirWithoutLocalPath() { $zipFile = new ZipFile(); @@ -199,13 +221,16 @@ class ZipFileAddDirTest extends ZipTestCase $zipFile->saveAsFile($this->outputFilename); $zipFile->close(); - self::assertCorrectZipArchive($this->outputFilename); + $this->assertCorrectZipArchive($this->outputFilename); $zipFile->openFile($this->outputFilename); - self::assertFilesResult($zipFile, array_keys(self::$files)); + $this->assertFilesResult($zipFile, array_keys(self::$files)); $zipFile->close(); } + /** + * @throws ZipException + */ public function testAddFilesFromIteratorWithIgnoreFiles() { $localPath = 'to/project'; @@ -222,16 +247,19 @@ class ZipFileAddDirTest extends ZipTestCase $zipFile->saveAsFile($this->outputFilename); $zipFile->close(); - self::assertCorrectZipArchive($this->outputFilename); + $this->assertCorrectZipArchive($this->outputFilename); $zipFile->openFile($this->outputFilename); - self::assertFilesResult($zipFile, [ + $this->assertFilesResult($zipFile, [ '.hidden', 'text file.txt', ], $localPath); $zipFile->close(); } + /** + * @throws ZipException + */ public function testAddFilesFromRecursiveIteratorWithIgnoreFiles() { $localPath = 'to/project'; @@ -250,10 +278,10 @@ class ZipFileAddDirTest extends ZipTestCase $zipFile->saveAsFile($this->outputFilename); $zipFile->close(); - self::assertCorrectZipArchive($this->outputFilename); + $this->assertCorrectZipArchive($this->outputFilename); $zipFile->openFile($this->outputFilename); - self::assertFilesResult($zipFile, [ + $this->assertFilesResult($zipFile, [ 'text file.txt', 'Текстовый документ.txt', 'empty dir/', @@ -268,6 +296,7 @@ class ZipFileAddDirTest extends ZipTestCase /** * Create archive and add files from glob pattern + * @throws ZipException */ public function testAddFilesFromGlob() { @@ -278,10 +307,10 @@ class ZipFileAddDirTest extends ZipTestCase $zipFile->saveAsFile($this->outputFilename); $zipFile->close(); - self::assertCorrectZipArchive($this->outputFilename); + $this->assertCorrectZipArchive($this->outputFilename); $zipFile->openFile($this->outputFilename); - self::assertFilesResult($zipFile, [ + $this->assertFilesResult($zipFile, [ 'text file.txt', 'Текстовый документ.txt', ], $localPath); @@ -290,6 +319,7 @@ class ZipFileAddDirTest extends ZipTestCase /** * Create archive and add recursively files from glob pattern + * @throws ZipException */ public function testAddFilesFromGlobRecursive() { @@ -300,10 +330,10 @@ class ZipFileAddDirTest extends ZipTestCase $zipFile->saveAsFile($this->outputFilename); $zipFile->close(); - self::assertCorrectZipArchive($this->outputFilename); + $this->assertCorrectZipArchive($this->outputFilename); $zipFile->openFile($this->outputFilename); - self::assertFilesResult($zipFile, [ + $this->assertFilesResult($zipFile, [ 'text file.txt', 'Текстовый документ.txt', 'category/list.txt', @@ -317,6 +347,7 @@ class ZipFileAddDirTest extends ZipTestCase /** * Create archive and add files from regex pattern + * @throws ZipException */ public function testAddFilesFromRegex() { @@ -327,10 +358,10 @@ class ZipFileAddDirTest extends ZipTestCase $zipFile->saveAsFile($this->outputFilename); $zipFile->close(); - self::assertCorrectZipArchive($this->outputFilename); + $this->assertCorrectZipArchive($this->outputFilename); $zipFile->openFile($this->outputFilename); - self::assertFilesResult($zipFile, [ + $this->assertFilesResult($zipFile, [ 'text file.txt', 'Текстовый документ.txt', ], $localPath); @@ -339,6 +370,7 @@ class ZipFileAddDirTest extends ZipTestCase /** * Create archive and add files recursively from regex pattern + * @throws ZipException */ public function testAddFilesFromRegexRecursive() { @@ -349,10 +381,10 @@ class ZipFileAddDirTest extends ZipTestCase $zipFile->saveAsFile($this->outputFilename); $zipFile->close(); - self::assertCorrectZipArchive($this->outputFilename); + $this->assertCorrectZipArchive($this->outputFilename); $zipFile->openFile($this->outputFilename); - self::assertFilesResult($zipFile, [ + $this->assertFilesResult($zipFile, [ 'text file.txt', 'Текстовый документ.txt', 'category/list.txt', @@ -364,6 +396,9 @@ class ZipFileAddDirTest extends ZipTestCase $zipFile->close(); } + /** + * @throws ZipException + */ public function testArrayAccessAddDir() { $localPath = 'path/to'; @@ -374,10 +409,10 @@ class ZipFileAddDirTest extends ZipTestCase $zipFile->saveAsFile($this->outputFilename); $zipFile->close(); - self::assertCorrectZipArchive($this->outputFilename); + $this->assertCorrectZipArchive($this->outputFilename); $zipFile->openFile($this->outputFilename); - self::assertFilesResult($zipFile, array_keys(self::$files), $localPath); + $this->assertFilesResult($zipFile, array_keys(self::$files), $localPath); $zipFile->close(); } } diff --git a/tests/PhpZip/ZipFileTest.php b/tests/PhpZip/ZipFileTest.php index 4cbc75e..35a7e4f 100644 --- a/tests/PhpZip/ZipFileTest.php +++ b/tests/PhpZip/ZipFileTest.php @@ -2,6 +2,8 @@ namespace PhpZip; +use PhpZip\Exception\ZipEntryNotFoundException; +use PhpZip\Exception\ZipException; use PhpZip\Model\ZipEntry; use PhpZip\Model\ZipInfo; use PhpZip\Util\CryptoUtil; @@ -16,7 +18,7 @@ class ZipFileTest extends ZipTestCase { /** - * @expectedException \PhpZip\Exception\InvalidArgumentException + * @expectedException \PhpZip\Exception\ZipException * @expectedExceptionMessage can't exists */ public function testOpenFileCantExists() @@ -35,8 +37,8 @@ class ZipFileTest extends ZipTestCase $this->markTestSkipped('Skip the test for a user with root privileges'); } - self::assertNotFalse(file_put_contents($this->outputFilename, 'content')); - self::assertTrue(chmod($this->outputFilename, 0222)); + $this->assertNotFalse(file_put_contents($this->outputFilename, 'content')); + $this->assertTrue(chmod($this->outputFilename, 0222)); $zipFile = new ZipFile(); $zipFile->openFile($this->outputFilename); @@ -48,7 +50,7 @@ class ZipFileTest extends ZipTestCase */ public function testOpenFileEmptyFile() { - self::assertNotFalse(touch($this->outputFilename)); + $this->assertNotFalse(touch($this->outputFilename)); $zipFile = new ZipFile(); $zipFile->openFile($this->outputFilename); } @@ -59,14 +61,15 @@ class ZipFileTest extends ZipTestCase */ public function testOpenFileInvalidZip() { - self::assertNotFalse(file_put_contents($this->outputFilename, CryptoUtil::randomBytes(255))); + $this->assertNotFalse(file_put_contents($this->outputFilename, CryptoUtil::randomBytes(255))); $zipFile = new ZipFile(); $zipFile->openFile($this->outputFilename); } /** * @expectedException \PhpZip\Exception\InvalidArgumentException - * @expectedExceptionMessage Data not available + * @expectedExceptionMessage Empty string passed + * @throws ZipException */ public function testOpenFromStringNullString() { @@ -76,7 +79,8 @@ class ZipFileTest extends ZipTestCase /** * @expectedException \PhpZip\Exception\InvalidArgumentException - * @expectedExceptionMessage Data not available + * @expectedExceptionMessage Empty string passed + * @throws ZipException */ public function testOpenFromStringEmptyString() { @@ -94,6 +98,9 @@ class ZipFileTest extends ZipTestCase $zipFile->openFromString(CryptoUtil::randomBytes(255)); } + /** + * @throws ZipException + */ public function testOpenFromString() { $zipFile = new ZipFile(); @@ -103,17 +110,18 @@ class ZipFileTest extends ZipTestCase $zipFile->close(); $zipFile->openFromString($zipContents); - self::assertEquals($zipFile->count(), 2); - self::assertTrue(isset($zipFile['file'])); - self::assertTrue(isset($zipFile['file2'])); - self::assertEquals($zipFile['file'], 'content'); - self::assertEquals($zipFile['file2'], 'content 2'); + $this->assertEquals($zipFile->count(), 2); + $this->assertTrue(isset($zipFile['file'])); + $this->assertTrue(isset($zipFile['file2'])); + $this->assertEquals($zipFile['file'], 'content'); + $this->assertEquals($zipFile['file2'], 'content 2'); $zipFile->close(); } /** * @expectedException \PhpZip\Exception\InvalidArgumentException * @expectedExceptionMessage Invalid stream resource + * @throws ZipException */ public function testOpenFromStreamNullStream() { @@ -124,6 +132,7 @@ class ZipFileTest extends ZipTestCase /** * @expectedException \PhpZip\Exception\InvalidArgumentException * @expectedExceptionMessage Invalid stream resource + * @throws ZipException */ public function testOpenFromStreamInvalidResourceType() { @@ -135,6 +144,7 @@ class ZipFileTest extends ZipTestCase /** * @expectedException \PhpZip\Exception\InvalidArgumentException * @expectedExceptionMessage Invalid resource type - gd. + * @throws ZipException */ public function testOpenFromStreamInvalidResourceType2() { @@ -148,6 +158,7 @@ class ZipFileTest extends ZipTestCase /** * @expectedException \PhpZip\Exception\InvalidArgumentException * @expectedExceptionMessage Invalid stream type - dir. + * @throws ZipException */ public function testOpenFromStreamInvalidResourceType3() { @@ -158,6 +169,7 @@ class ZipFileTest extends ZipTestCase /** * @expectedException \PhpZip\Exception\InvalidArgumentException * @expectedExceptionMessage Resource cannot seekable stream. + * @throws ZipException */ public function testOpenFromStreamNoSeekable() { @@ -195,6 +207,9 @@ class ZipFileTest extends ZipTestCase $zipFile->openFromStream($fp); } + /** + * @throws ZipException + */ public function testOpenFromStream() { $zipFile = new ZipFile(); @@ -205,14 +220,15 @@ class ZipFileTest extends ZipTestCase $handle = fopen($this->outputFilename, 'rb'); $zipFile->openFromStream($handle); - self::assertEquals($zipFile->count(), 1); - self::assertTrue(isset($zipFile['file'])); - self::assertEquals($zipFile['file'], 'content'); + $this->assertEquals($zipFile->count(), 1); + $this->assertTrue(isset($zipFile['file'])); + $this->assertEquals($zipFile['file'], 'content'); $zipFile->close(); } /** * Test create, open and extract empty archive. + * @throws ZipException */ public function testEmptyArchive() { @@ -221,26 +237,27 @@ class ZipFileTest extends ZipTestCase ->saveAsFile($this->outputFilename) ->close(); - self::assertCorrectEmptyZip($this->outputFilename); - self::assertTrue(mkdir($this->outputDirname, 0755, true)); + $this->assertCorrectEmptyZip($this->outputFilename); + $this->assertTrue(mkdir($this->outputDirname, 0755, true)); $zipFile->openFile($this->outputFilename); - self::assertEquals($zipFile->count(), 0); + $this->assertEquals($zipFile->count(), 0); $zipFile ->extractTo($this->outputDirname) ->close(); - self::assertTrue(FilesUtil::isEmptyDir($this->outputDirname)); + $this->assertTrue(FilesUtil::isEmptyDir($this->outputDirname)); } /** * No modified archive * * @see ZipOutputFile::create() + * @throws ZipException */ public function testNoModifiedArchive() { - self::assertTrue(mkdir($this->outputDirname, 0755, true)); + $this->assertTrue(mkdir($this->outputDirname, 0755, true)); $fileActual = $this->outputDirname . DIRECTORY_SEPARATOR . 'file_actual.zip'; $fileExpected = $this->outputDirname . DIRECTORY_SEPARATOR . 'file_expected.zip'; @@ -248,26 +265,26 @@ class ZipFileTest extends ZipTestCase $zipFile = new ZipFile(); $zipFile->addDirRecursive(__DIR__.'/../../src'); $sourceCount = $zipFile->count(); - self::assertTrue($sourceCount > 0); + $this->assertTrue($sourceCount > 0); $zipFile ->saveAsFile($fileActual) ->close(); - self::assertCorrectZipArchive($fileActual); + $this->assertCorrectZipArchive($fileActual); $zipFile ->openFile($fileActual) ->saveAsFile($fileExpected); - self::assertCorrectZipArchive($fileExpected); + $this->assertCorrectZipArchive($fileExpected); $zipFileExpected = new ZipFile(); $zipFileExpected->openFile($fileExpected); - self::assertEquals($zipFile->count(), $sourceCount); - self::assertEquals($zipFileExpected->count(), $zipFile->count()); - self::assertEquals($zipFileExpected->getListFiles(), $zipFile->getListFiles()); + $this->assertEquals($zipFile->count(), $sourceCount); + $this->assertEquals($zipFileExpected->count(), $zipFile->count()); + $this->assertEquals($zipFileExpected->getListFiles(), $zipFile->getListFiles()); foreach ($zipFile as $entryName => $content) { - self::assertEquals($zipFileExpected[$entryName], $content); + $this->assertEquals($zipFileExpected[$entryName], $content); } $zipFileExpected->close(); @@ -281,6 +298,7 @@ class ZipFileTest extends ZipTestCase * @see ZipOutputFile::addFromFile() * @see ZipOutputFile::addFromStream() * @see ZipFile::getEntryContents() + * @throws ZipException */ public function testCreateArchiveAndAddFiles() { @@ -312,39 +330,42 @@ class ZipFileTest extends ZipTestCase $zipFile[$filenameFromString2] = $outputFromString2; $zipFile[$emptyDirName2] = null; $zipFile[$emptyDirName3] = 'this content ignoring'; - self::assertEquals(count($zipFile), 7); + $this->assertEquals(count($zipFile), 7); $zipFile ->saveAsFile($this->outputFilename) ->close(); unlink($tempFile); - self::assertCorrectZipArchive($this->outputFilename); + $this->assertCorrectZipArchive($this->outputFilename); $zipFile->openFile($this->outputFilename); - self::assertEquals(count($zipFile), 7); - self::assertEquals($zipFile[$filenameFromString], $outputFromString); - self::assertEquals($zipFile[$filenameFromFile], $outputFromFile); - self::assertEquals($zipFile[$filenameFromStream], $outputFromStream); - self::assertEquals($zipFile[$filenameFromString2], $outputFromString2); - self::assertTrue(isset($zipFile[$emptyDirName])); - self::assertTrue(isset($zipFile[$emptyDirName2])); - self::assertTrue(isset($zipFile[$emptyDirName3])); - self::assertTrue($zipFile->isDirectory($emptyDirName)); - self::assertTrue($zipFile->isDirectory($emptyDirName2)); - self::assertTrue($zipFile->isDirectory($emptyDirName3)); + $this->assertEquals(count($zipFile), 7); + $this->assertEquals($zipFile[$filenameFromString], $outputFromString); + $this->assertEquals($zipFile[$filenameFromFile], $outputFromFile); + $this->assertEquals($zipFile[$filenameFromStream], $outputFromStream); + $this->assertEquals($zipFile[$filenameFromString2], $outputFromString2); + $this->assertTrue(isset($zipFile[$emptyDirName])); + $this->assertTrue(isset($zipFile[$emptyDirName2])); + $this->assertTrue(isset($zipFile[$emptyDirName3])); + $this->assertTrue($zipFile->isDirectory($emptyDirName)); + $this->assertTrue($zipFile->isDirectory($emptyDirName2)); + $this->assertTrue($zipFile->isDirectory($emptyDirName3)); $listFiles = $zipFile->getListFiles(); - self::assertEquals($listFiles[0], $filenameFromString); - self::assertEquals($listFiles[1], $filenameFromFile); - self::assertEquals($listFiles[2], $filenameFromStream); - self::assertEquals($listFiles[3], $emptyDirName); - self::assertEquals($listFiles[4], $filenameFromString2); - self::assertEquals($listFiles[5], $emptyDirName2); - self::assertEquals($listFiles[6], $emptyDirName3); + $this->assertEquals($listFiles[0], $filenameFromString); + $this->assertEquals($listFiles[1], $filenameFromFile); + $this->assertEquals($listFiles[2], $filenameFromStream); + $this->assertEquals($listFiles[3], $emptyDirName); + $this->assertEquals($listFiles[4], $filenameFromString2); + $this->assertEquals($listFiles[5], $emptyDirName2); + $this->assertEquals($listFiles[6], $emptyDirName3); $zipFile->close(); } + /** + * @throws ZipException + */ public function testEmptyContent() { $zipFile = new ZipFile(); @@ -353,12 +374,13 @@ class ZipFileTest extends ZipTestCase $zipFile->close(); $zipFile->openFile($this->outputFilename); - self::assertEquals($zipFile['file'], ''); + $this->assertEquals($zipFile['file'], ''); $zipFile->close(); } /** * Test compression method from image file. + * @throws ZipException */ public function testCompressionMethodFromImageMimeType() { @@ -367,7 +389,7 @@ class ZipFileTest extends ZipTestCase } $outputFilename = $this->outputFilename; $this->outputFilename .= '.gif'; - self::assertNotFalse( + $this->assertNotFalse( file_put_contents( $this->outputFilename, base64_decode('R0lGODlhAQABAJAAAP8AAAAAACH5BAUQAAAALAAAAAABAAEAAAICBAEAOw==') @@ -383,12 +405,13 @@ class ZipFileTest extends ZipTestCase $zipFile->openFile($this->outputFilename); $info = $zipFile->getEntryInfo($basename); - self::assertEquals($info->getMethodName(), 'No compression'); + $this->assertEquals($info->getMethodName(), 'No compression'); $zipFile->close(); } /** * Rename zip entry name. + * @throws ZipException */ public function testRename() { @@ -400,7 +423,7 @@ class ZipFileTest extends ZipTestCase $zipFile->saveAsFile($this->outputFilename); $zipFile->close(); - self::assertCorrectZipArchive($this->outputFilename); + $this->assertCorrectZipArchive($this->outputFilename); $zipFile->openFile($this->outputFilename); $zipFile->rename($oldName, $newName); @@ -413,23 +436,24 @@ class ZipFileTest extends ZipTestCase $zipFile->saveAsFile($this->outputFilename); $zipFile->close(); - self::assertCorrectZipArchive($this->outputFilename); + $this->assertCorrectZipArchive($this->outputFilename); $zipFile->openFile($this->outputFilename); - self::assertFalse(isset($zipFile[$oldName])); - self::assertTrue(isset($zipFile[$newName])); - self::assertFalse(isset($zipFile['file1.txt'])); - self::assertFalse(isset($zipFile['file2.txt'])); - self::assertFalse(isset($zipFile['file3.txt'])); - self::assertTrue(isset($zipFile['file_long_name.txt'])); - self::assertTrue(isset($zipFile['file4.txt'])); - self::assertTrue(isset($zipFile['fi.txt'])); + $this->assertFalse(isset($zipFile[$oldName])); + $this->assertTrue(isset($zipFile[$newName])); + $this->assertFalse(isset($zipFile['file1.txt'])); + $this->assertFalse(isset($zipFile['file2.txt'])); + $this->assertFalse(isset($zipFile['file3.txt'])); + $this->assertTrue(isset($zipFile['file_long_name.txt'])); + $this->assertTrue(isset($zipFile['file4.txt'])); + $this->assertTrue(isset($zipFile['fi.txt'])); $zipFile->close(); } /** * @expectedException \PhpZip\Exception\InvalidArgumentException * @expectedExceptionMessage name is null + * @throws ZipException */ public function testRenameEntryNull() { @@ -440,6 +464,7 @@ class ZipFileTest extends ZipTestCase /** * @expectedException \PhpZip\Exception\InvalidArgumentException * @expectedExceptionMessage name is null + * @throws ZipException */ public function testRenameEntryNull2() { @@ -450,6 +475,7 @@ class ZipFileTest extends ZipTestCase /** * @expectedException \PhpZip\Exception\InvalidArgumentException * @expectedExceptionMessage is exists + * @throws ZipException */ public function testRenameEntryNewEntyExists() { @@ -465,7 +491,8 @@ class ZipFileTest extends ZipTestCase } /** - * @expectedException \PhpZip\Exception\ZipNotFoundEntry + * @expectedException \PhpZip\Exception\ZipEntryNotFoundException + * @throws ZipException */ public function testRenameEntryNotFound() { @@ -482,6 +509,7 @@ class ZipFileTest extends ZipTestCase /** * Delete entry from name. + * @throws ZipException */ public function testDeleteFromName() { @@ -493,20 +521,24 @@ class ZipFileTest extends ZipTestCase $zipFile->saveAsFile($this->outputFilename); $zipFile->close(); - self::assertCorrectZipArchive($this->outputFilename); + $this->assertCorrectZipArchive($this->outputFilename); $zipFile->openFile($this->outputFilename); $zipFile->deleteFromName($deleteEntryName); $zipFile->saveAsFile($this->outputFilename); $zipFile->close(); - self::assertCorrectZipArchive($this->outputFilename); + $this->assertCorrectZipArchive($this->outputFilename); $zipFile->openFile($this->outputFilename); - self::assertFalse(isset($zipFile[$deleteEntryName])); + $this->assertFalse(isset($zipFile[$deleteEntryName])); $zipFile->close(); } + /** + * @throws Exception\ZipEntryNotFoundException + * @throws ZipException + */ public function testDeleteNewEntry() { $zipFile = new ZipFile(); @@ -517,14 +549,14 @@ class ZipFileTest extends ZipTestCase $zipFile->close(); $zipFile->openFile($this->outputFilename); - self::assertEquals(sizeof($zipFile), 1); - self::assertTrue(isset($zipFile['entry1'])); - self::assertFalse(isset($zipFile['entry2'])); + $this->assertEquals(sizeof($zipFile), 1); + $this->assertTrue(isset($zipFile['entry1'])); + $this->assertFalse(isset($zipFile['entry2'])); $zipFile->close(); } /** - * @expectedException \PhpZip\Exception\ZipNotFoundEntry + * @expectedException \PhpZip\Exception\ZipEntryNotFoundException */ public function testDeleteFromNameNotFoundEntry() { @@ -534,6 +566,7 @@ class ZipFileTest extends ZipTestCase /** * Delete zip entries from glob pattern + * @throws ZipException */ public function testDeleteFromGlob() { @@ -541,29 +574,29 @@ class ZipFileTest extends ZipTestCase $zipFile = new ZipFile(); $zipFile->addFilesFromGlobRecursive($inputDir, '**.{xml,json,md}', '/'); - self::assertTrue(isset($zipFile['composer.json'])); - self::assertTrue(isset($zipFile['phpunit.xml'])); + $this->assertTrue(isset($zipFile['composer.json'])); + $this->assertTrue(isset($zipFile['phpunit.xml'])); $zipFile->saveAsFile($this->outputFilename); $zipFile->close(); - self::assertCorrectZipArchive($this->outputFilename); + $this->assertCorrectZipArchive($this->outputFilename); $zipFile->openFile($this->outputFilename); - self::assertTrue(isset($zipFile['composer.json'])); - self::assertTrue(isset($zipFile['phpunit.xml'])); + $this->assertTrue(isset($zipFile['composer.json'])); + $this->assertTrue(isset($zipFile['phpunit.xml'])); $zipFile->deleteFromGlob('**.{xml,json}'); - self::assertFalse(isset($zipFile['composer.json'])); - self::assertFalse(isset($zipFile['phpunit.xml'])); + $this->assertFalse(isset($zipFile['composer.json'])); + $this->assertFalse(isset($zipFile['phpunit.xml'])); $zipFile->saveAsFile($this->outputFilename); $zipFile->close(); - self::assertCorrectZipArchive($this->outputFilename); + $this->assertCorrectZipArchive($this->outputFilename); $zipFile->openFile($this->outputFilename); - self::assertTrue($zipFile->count() > 0); + $this->assertTrue($zipFile->count() > 0); foreach ($zipFile->getListFiles() as $name) { - self::assertStringEndsWith('.md', $name); + $this->assertStringEndsWith('.md', $name); } $zipFile->close(); @@ -591,6 +624,7 @@ class ZipFileTest extends ZipTestCase /** * Delete entries from regex pattern + * @throws ZipException */ public function testDeleteFromRegex() { @@ -601,7 +635,7 @@ class ZipFileTest extends ZipTestCase $zipFile->saveAsFile($this->outputFilename); $zipFile->close(); - self::assertCorrectZipArchive($this->outputFilename); + $this->assertCorrectZipArchive($this->outputFilename); $zipFile->openFile($this->outputFilename); $zipFile->deleteFromRegex('~\.(json)$~i'); @@ -610,12 +644,12 @@ class ZipFileTest extends ZipTestCase $zipFile->saveAsFile($this->outputFilename); $zipFile->close(); - self::assertCorrectZipArchive($this->outputFilename); + $this->assertCorrectZipArchive($this->outputFilename); $zipFile->openFile($this->outputFilename); - self::assertFalse(isset($zipFile['Path/composer.json'])); - self::assertFalse(isset($zipFile['Path/test.txt'])); - self::assertTrue(isset($zipFile['Path/phpunit.xml'])); + $this->assertFalse(isset($zipFile['Path/composer.json'])); + $this->assertFalse(isset($zipFile['Path/test.txt'])); + $this->assertTrue(isset($zipFile['Path/phpunit.xml'])); $zipFile->close(); } @@ -641,32 +675,34 @@ class ZipFileTest extends ZipTestCase /** * Delete all entries + * @throws ZipException */ public function testDeleteAll() { $zipFile = new ZipFile(); $zipFile->addDirRecursive(dirname(dirname(__DIR__)) .DIRECTORY_SEPARATOR. 'src'); - self::assertTrue($zipFile->count() > 0); + $this->assertTrue($zipFile->count() > 0); $zipFile->saveAsFile($this->outputFilename); $zipFile->close(); - self::assertCorrectZipArchive($this->outputFilename); + $this->assertCorrectZipArchive($this->outputFilename); $zipFile->openFile($this->outputFilename); - self::assertTrue($zipFile->count() > 0); + $this->assertTrue($zipFile->count() > 0); $zipFile->deleteAll(); $zipFile->saveAsFile($this->outputFilename); $zipFile->close(); - self::assertCorrectEmptyZip($this->outputFilename); + $this->assertCorrectEmptyZip($this->outputFilename); $zipFile->openFile($this->outputFilename); - self::assertEquals($zipFile->count(), 0); + $this->assertEquals($zipFile->count(), 0); $zipFile->close(); } /** * Test zip archive comment. + * @throws ZipException */ public function testArchiveComment() { @@ -684,19 +720,19 @@ class ZipFileTest extends ZipTestCase $zipFile->saveAsFile($this->outputFilename); $zipFile->close(); - self::assertCorrectZipArchive($this->outputFilename); + $this->assertCorrectZipArchive($this->outputFilename); $zipFile->openFile($this->outputFilename); - self::assertEquals($zipFile->getArchiveComment(), $comment); + $this->assertEquals($zipFile->getArchiveComment(), $comment); $zipFile->setArchiveComment(null); // remove archive comment $zipFile->saveAsFile($this->outputFilename); $zipFile->close(); - self::assertCorrectZipArchive($this->outputFilename); + $this->assertCorrectZipArchive($this->outputFilename); // check empty comment $zipFile->openFile($this->outputFilename); - self::assertEquals($zipFile->getArchiveComment(), ""); + $this->assertEquals($zipFile->getArchiveComment(), ""); $zipFile->close(); } @@ -717,6 +753,7 @@ class ZipFileTest extends ZipTestCase /** * Test zip entry comment. + * @throws ZipException */ public function testEntryComment() { @@ -756,15 +793,15 @@ class ZipFileTest extends ZipTestCase $zipFile->saveAsFile($this->outputFilename); $zipFile->close(); - self::assertCorrectZipArchive($this->outputFilename); + $this->assertCorrectZipArchive($this->outputFilename); // check and modify comments $zipFile->openFile($this->outputFilename); foreach ($zipFile->getListFiles() as $entryName) { $entriesItem = $entries[$entryName]; - self::assertNotEmpty($entriesItem); - self::assertEquals($zipFile[$entryName], $entriesItem['data']); - self::assertEquals($zipFile->getEntryComment($entryName), (string)$entriesItem['comment']); + $this->assertNotEmpty($entriesItem); + $this->assertEquals($zipFile[$entryName], $entriesItem['data']); + $this->assertEquals($zipFile->getEntryComment($entryName), (string)$entriesItem['comment']); } // modify comment $entries['file5.txt']['comment'] = mt_rand(1, 100000000); @@ -772,14 +809,14 @@ class ZipFileTest extends ZipTestCase $zipFile->saveAsFile($this->outputFilename); $zipFile->close(); - self::assertCorrectZipArchive($this->outputFilename); + $this->assertCorrectZipArchive($this->outputFilename); // check modify comments $zipFile->openFile($this->outputFilename); foreach ($entries as $entryName => $entriesItem) { - self::assertTrue(isset($zipFile[$entryName])); - self::assertEquals($zipFile->getEntryComment($entryName), (string)$entriesItem['comment']); - self::assertEquals($zipFile[$entryName], $entriesItem['data']); + $this->assertTrue(isset($zipFile[$entryName])); + $this->assertEquals($zipFile->getEntryComment($entryName), (string)$entriesItem['comment']); + $this->assertEquals($zipFile[$entryName], $entriesItem['data']); } $zipFile->close(); } @@ -802,7 +839,8 @@ class ZipFileTest extends ZipTestCase } /** - * @expectedException \PhpZip\Exception\ZipNotFoundEntry + * @expectedException \PhpZip\Exception\ZipEntryNotFoundException + * @throws ZipException */ public function testSetEntryCommentNotFoundEntry() { @@ -812,6 +850,7 @@ class ZipFileTest extends ZipTestCase /** * Test all available support compression methods. + * @throws ZipException */ public function testCompressionMethod() { @@ -842,17 +881,17 @@ class ZipFileTest extends ZipTestCase $zipFile->saveAsFile($this->outputFilename); $zipFile->close(); - self::assertCorrectZipArchive($this->outputFilename); + $this->assertCorrectZipArchive($this->outputFilename); $zipFile->openFile($this->outputFilename); $zipFile->setCompressionLevel(ZipFileInterface::LEVEL_BEST_COMPRESSION); $zipAllInfo = $zipFile->getAllInfo(); foreach ($zipAllInfo as $entryName => $info) { - self::assertEquals($zipFile[$entryName], $entries[$entryName]['data']); - self::assertEquals($info->getMethodName(), $entries[$entryName]['expected']); + $this->assertEquals($zipFile[$entryName], $entries[$entryName]['data']); + $this->assertEquals($info->getMethodName(), $entries[$entryName]['expected']); $entryInfo = $zipFile->getEntryInfo($entryName); - self::assertEquals($entryInfo, $info); + $this->assertEquals($entryInfo, $info); } $zipFile->close(); } @@ -880,6 +919,7 @@ class ZipFileTest extends ZipTestCase /** * Test extract all files. + * @throws ZipException */ public function testExtract() { @@ -901,19 +941,19 @@ class ZipFileTest extends ZipTestCase $zipFile->saveAsFile($this->outputFilename); $zipFile->close(); - self::assertTrue(mkdir($this->outputDirname, 0755, true)); + $this->assertTrue(mkdir($this->outputDirname, 0755, true)); $zipFile->openFile($this->outputFilename); $zipFile->extractTo($this->outputDirname); foreach ($entries as $entryName => $value) { $fullExtractedFilename = $this->outputDirname . DIRECTORY_SEPARATOR . $entryName; if ($value === null) { - self::assertTrue(is_dir($fullExtractedFilename)); - self::assertTrue(FilesUtil::isEmptyDir($fullExtractedFilename)); + $this->assertTrue(is_dir($fullExtractedFilename)); + $this->assertTrue(FilesUtil::isEmptyDir($fullExtractedFilename)); } else { - self::assertTrue(is_file($fullExtractedFilename)); + $this->assertTrue(is_file($fullExtractedFilename)); $contents = file_get_contents($fullExtractedFilename); - self::assertEquals($contents, $value); + $this->assertEquals($contents, $value); } } $zipFile->close(); @@ -921,6 +961,7 @@ class ZipFileTest extends ZipTestCase /** * Test extract some files + * @throws ZipException */ public function testExtractSomeFiles() { @@ -944,7 +985,7 @@ class ZipFileTest extends ZipTestCase 'test empty/dir2/' ]; - self::assertTrue(mkdir($this->outputDirname, 0755, true)); + $this->assertTrue(mkdir($this->outputDirname, 0755, true)); $zipFile = new ZipFile(); $zipFile->addAll($entries); @@ -958,24 +999,24 @@ class ZipFileTest extends ZipTestCase $fullExtractFilename = $this->outputDirname . DIRECTORY_SEPARATOR . $entryName; if (in_array($entryName, $extractEntries)) { if ($value === null) { - self::assertTrue(is_dir($fullExtractFilename)); - self::assertTrue(FilesUtil::isEmptyDir($fullExtractFilename)); + $this->assertTrue(is_dir($fullExtractFilename)); + $this->assertTrue(FilesUtil::isEmptyDir($fullExtractFilename)); } else { - self::assertTrue(is_file($fullExtractFilename)); + $this->assertTrue(is_file($fullExtractFilename)); $contents = file_get_contents($fullExtractFilename); - self::assertEquals($contents, $value); + $this->assertEquals($contents, $value); } } else { if ($value === null) { - self::assertFalse(is_dir($fullExtractFilename)); + $this->assertFalse(is_dir($fullExtractFilename)); } else { - self::assertFalse(is_file($fullExtractFilename)); + $this->assertFalse(is_file($fullExtractFilename)); } } } - self::assertFalse(is_file($this->outputDirname . DIRECTORY_SEPARATOR . 'test/test/test.txt')); + $this->assertFalse(is_file($this->outputDirname . DIRECTORY_SEPARATOR . 'test/test/test.txt')); $zipFile->extractTo($this->outputDirname, 'test/test/test.txt'); - self::assertTrue(is_file($this->outputDirname . DIRECTORY_SEPARATOR . 'test/test/test.txt')); + $this->assertTrue(is_file($this->outputDirname . DIRECTORY_SEPARATOR . 'test/test/test.txt')); $zipFile->close(); } @@ -1025,8 +1066,8 @@ class ZipFileTest extends ZipTestCase $zipFile->saveAsFile($this->outputFilename); $zipFile->close(); - self::assertTrue(mkdir($this->outputDirname, 0444, true)); - self::assertTrue(chmod($this->outputDirname, 0444)); + $this->assertTrue(mkdir($this->outputDirname, 0444, true)); + $this->assertTrue(chmod($this->outputDirname, 0444)); $zipFile->openFile($this->outputFilename); $zipFile->extractTo($this->outputDirname); @@ -1055,6 +1096,7 @@ class ZipFileTest extends ZipTestCase /** * @expectedException \PhpZip\Exception\InvalidArgumentException * @expectedExceptionMessage Contents is null + * @throws ZipException */ public function testAddFromStringNullContents() { @@ -1064,7 +1106,8 @@ class ZipFileTest extends ZipTestCase /** * @expectedException \PhpZip\Exception\InvalidArgumentException - * @expectedExceptionMessage Incorrect entry name + * @expectedExceptionMessage Entry name is null + * @throws ZipException */ public function testAddFromStringNullEntryName() { @@ -1073,8 +1116,9 @@ class ZipFileTest extends ZipTestCase } /** - * @expectedException \PhpZip\Exception\ZipUnsupportMethod + * @expectedException \PhpZip\Exception\ZipUnsupportMethodException * @expectedExceptionMessage Unsupported compression method + * @throws ZipException */ public function testAddFromStringUnsupportedMethod() { @@ -1084,7 +1128,8 @@ class ZipFileTest extends ZipTestCase /** * @expectedException \PhpZip\Exception\InvalidArgumentException - * @expectedExceptionMessage Incorrect entry name + * @expectedExceptionMessage Empty entry name + * @throws ZipException */ public function testAddFromStringEmptyEntryName() { @@ -1094,14 +1139,15 @@ class ZipFileTest extends ZipTestCase /** * Test compression method from add string. + * @throws ZipException */ public function testAddFromStringCompressionMethod() { $fileStored = sys_get_temp_dir() . '/zip-stored.txt'; $fileDeflated = sys_get_temp_dir() . '/zip-deflated.txt'; - self::assertNotFalse(file_put_contents($fileStored, 'content')); - self::assertNotFalse(file_put_contents($fileDeflated, str_repeat('content', 200))); + $this->assertNotFalse(file_put_contents($fileStored, 'content')); + $this->assertNotFalse(file_put_contents($fileDeflated, str_repeat('content', 200))); $zipFile = new ZipFile(); $zipFile->addFromString(basename($fileStored), file_get_contents($fileStored)); @@ -1115,14 +1161,15 @@ class ZipFileTest extends ZipTestCase $zipFile->openFile($this->outputFilename); $infoStored = $zipFile->getEntryInfo(basename($fileStored)); $infoDeflated = $zipFile->getEntryInfo(basename($fileDeflated)); - self::assertEquals($infoStored->getMethodName(), 'No compression'); - self::assertEquals($infoDeflated->getMethodName(), 'Deflate'); + $this->assertEquals($infoStored->getMethodName(), 'No compression'); + $this->assertEquals($infoDeflated->getMethodName(), 'Deflate'); $zipFile->close(); } /** * @expectedException \PhpZip\Exception\InvalidArgumentException - * @expectedExceptionMessage stream is not resource + * @expectedExceptionMessage Stream is not resource + * @throws ZipException */ public function testAddFromStreamInvalidResource() { @@ -1133,7 +1180,8 @@ class ZipFileTest extends ZipTestCase /** * @expectedException \PhpZip\Exception\InvalidArgumentException - * @expectedExceptionMessage Incorrect entry name + * @expectedExceptionMessage Empty entry name + * @throws ZipException */ public function testAddFromStreamEmptyEntryName() { @@ -1144,8 +1192,9 @@ class ZipFileTest extends ZipTestCase } /** - * @expectedException \PhpZip\Exception\ZipUnsupportMethod + * @expectedException \PhpZip\Exception\ZipUnsupportMethodException * @expectedExceptionMessage Unsupported method + * @throws ZipException */ public function testAddFromStreamUnsupportedMethod() { @@ -1157,14 +1206,15 @@ class ZipFileTest extends ZipTestCase /** * Test compression method from add stream. + * @throws ZipException */ public function testAddFromStreamCompressionMethod() { $fileStored = sys_get_temp_dir() . '/zip-stored.txt'; $fileDeflated = sys_get_temp_dir() . '/zip-deflated.txt'; - self::assertNotFalse(file_put_contents($fileStored, 'content')); - self::assertNotFalse(file_put_contents($fileDeflated, str_repeat('content', 200))); + $this->assertNotFalse(file_put_contents($fileStored, 'content')); + $this->assertNotFalse(file_put_contents($fileDeflated, str_repeat('content', 200))); $fpStored = fopen($fileStored, 'rb'); $fpDeflated = fopen($fileDeflated, 'rb'); @@ -1181,14 +1231,15 @@ class ZipFileTest extends ZipTestCase $zipFile->openFile($this->outputFilename); $infoStored = $zipFile->getEntryInfo(basename($fileStored)); $infoDeflated = $zipFile->getEntryInfo(basename($fileDeflated)); - self::assertEquals($infoStored->getMethodName(), 'No compression'); - self::assertEquals($infoDeflated->getMethodName(), 'Deflate'); + $this->assertEquals($infoStored->getMethodName(), 'No compression'); + $this->assertEquals($infoDeflated->getMethodName(), 'Deflate'); $zipFile->close(); } /** * @expectedException \PhpZip\Exception\InvalidArgumentException * @expectedExceptionMessage Filename is null + * @throws ZipException */ public function testAddFileNullFileName() { @@ -1197,7 +1248,7 @@ class ZipFileTest extends ZipTestCase } /** - * @expectedException \PhpZip\Exception\InvalidArgumentException + * @expectedException \PhpZip\Exception\ZipException * @expectedExceptionMessage is not exists */ public function testAddFileCantExists() @@ -1207,8 +1258,9 @@ class ZipFileTest extends ZipTestCase } /** - * @expectedException \PhpZip\Exception\ZipUnsupportMethod - * @expectedExceptionMessage Unsupported method + * @expectedException \PhpZip\Exception\ZipUnsupportMethodException + * @expectedExceptionMessage Unsupported compression method 99 + * @throws ZipException */ public function testAddFileUnsupportedMethod() { @@ -1217,17 +1269,18 @@ class ZipFileTest extends ZipTestCase } /** - * @expectedException \PhpZip\Exception\InvalidArgumentException + * @expectedException \PhpZip\Exception\ZipException * @expectedExceptionMessage can not open + * @throws ZipException */ public function testAddFileCantOpen() { - if (0 === posix_getuid()) { + if (posix_getuid() === 0) { $this->markTestSkipped('Skip the test for a user with root privileges'); } - self::assertNotFalse(file_put_contents($this->outputFilename, '')); - self::assertTrue(chmod($this->outputFilename, 0244)); + $this->assertNotFalse(file_put_contents($this->outputFilename, '')); + $this->assertTrue(chmod($this->outputFilename, 0244)); $zipFile = new ZipFile(); $zipFile->addFile($this->outputFilename); @@ -1235,7 +1288,8 @@ class ZipFileTest extends ZipTestCase /** * @expectedException \PhpZip\Exception\InvalidArgumentException - * @expectedExceptionMessage Input dir empty + * @expectedExceptionMessage Input dir is null + * @throws ZipException */ public function testAddDirNullDirname() { @@ -1246,6 +1300,7 @@ class ZipFileTest extends ZipTestCase /** * @expectedException \PhpZip\Exception\InvalidArgumentException * @expectedExceptionMessage Input dir empty + * @throws ZipException */ public function testAddDirEmptyDirname() { @@ -1254,7 +1309,7 @@ class ZipFileTest extends ZipTestCase } /** - * @expectedException \PhpZip\Exception\InvalidArgumentException + * @expectedException \PhpZip\Exception\ZipException * @expectedExceptionMessage can't exists */ public function testAddDirCantExists() @@ -1266,6 +1321,7 @@ class ZipFileTest extends ZipTestCase /** * @expectedException \PhpZip\Exception\InvalidArgumentException * @expectedExceptionMessage Input dir empty + * @throws ZipException */ public function testAddDirRecursiveNullDirname() { @@ -1276,6 +1332,7 @@ class ZipFileTest extends ZipTestCase /** * @expectedException \PhpZip\Exception\InvalidArgumentException * @expectedExceptionMessage Input dir empty + * @throws ZipException */ public function testAddDirRecursiveEmptyDirname() { @@ -1286,6 +1343,7 @@ class ZipFileTest extends ZipTestCase /** * @expectedException \PhpZip\Exception\InvalidArgumentException * @expectedExceptionMessage can't exists + * @throws ZipException */ public function testAddDirRecursiveCantExists() { @@ -1296,6 +1354,7 @@ class ZipFileTest extends ZipTestCase /** * @expectedException \PhpZip\Exception\InvalidArgumentException * @expectedExceptionMessage Input dir empty + * @throws ZipException */ public function testAddFilesFromGlobNull() { @@ -1306,6 +1365,7 @@ class ZipFileTest extends ZipTestCase /** * @expectedException \PhpZip\Exception\InvalidArgumentException * @expectedExceptionMessage Input dir empty + * @throws ZipException */ public function testAddFilesFromGlobEmpty() { @@ -1314,7 +1374,7 @@ class ZipFileTest extends ZipTestCase } /** - * @expectedException \PhpZip\Exception\InvalidArgumentException + * @expectedException \PhpZip\Exception\ZipException * @expectedExceptionMessage can't exists */ public function testAddFilesFromGlobCantExists() @@ -1326,6 +1386,7 @@ class ZipFileTest extends ZipTestCase /** * @expectedException \PhpZip\Exception\InvalidArgumentException * @expectedExceptionMessage glob pattern empty + * @throws ZipException */ public function testAddFilesFromGlobNullPattern() { @@ -1336,6 +1397,7 @@ class ZipFileTest extends ZipTestCase /** * @expectedException \PhpZip\Exception\InvalidArgumentException * @expectedExceptionMessage glob pattern empty + * @throws ZipException */ public function testAddFilesFromGlobEmptyPattern() { @@ -1346,6 +1408,7 @@ class ZipFileTest extends ZipTestCase /** * @expectedException \PhpZip\Exception\InvalidArgumentException * @expectedExceptionMessage Input dir empty + * @throws ZipException */ public function testAddFilesFromGlobRecursiveNull() { @@ -1356,6 +1419,7 @@ class ZipFileTest extends ZipTestCase /** * @expectedException \PhpZip\Exception\InvalidArgumentException * @expectedExceptionMessage Input dir empty + * @throws ZipException */ public function testAddFilesFromGlobRecursiveEmpty() { @@ -1364,8 +1428,9 @@ class ZipFileTest extends ZipTestCase } /** - * @expectedException \PhpZip\Exception\InvalidArgumentException + * @expectedException \PhpZip\Exception\ZipException * @expectedExceptionMessage can't exists + * @throws ZipException */ public function testAddFilesFromGlobRecursiveCantExists() { @@ -1376,6 +1441,7 @@ class ZipFileTest extends ZipTestCase /** * @expectedException \PhpZip\Exception\InvalidArgumentException * @expectedExceptionMessage glob pattern empty + * @throws ZipException */ public function testAddFilesFromGlobRecursiveNullPattern() { @@ -1386,6 +1452,7 @@ class ZipFileTest extends ZipTestCase /** * @expectedException \PhpZip\Exception\InvalidArgumentException * @expectedExceptionMessage glob pattern empty + * @throws ZipException */ public function testAddFilesFromGlobRecursiveEmptyPattern() { @@ -1396,6 +1463,7 @@ class ZipFileTest extends ZipTestCase /** * @expectedException \PhpZip\Exception\InvalidArgumentException * @expectedExceptionMessage Input dir empty + * @throws ZipException */ public function testAddFilesFromRegexDirectoryNull() { @@ -1406,6 +1474,7 @@ class ZipFileTest extends ZipTestCase /** * @expectedException \PhpZip\Exception\InvalidArgumentException * @expectedExceptionMessage Input dir empty + * @throws ZipException */ public function testAddFilesFromRegexDirectoryEmpty() { @@ -1416,6 +1485,7 @@ class ZipFileTest extends ZipTestCase /** * @expectedException \PhpZip\Exception\InvalidArgumentException * @expectedExceptionMessage can't exists + * @throws ZipException */ public function testAddFilesFromRegexCantExists() { @@ -1426,6 +1496,7 @@ class ZipFileTest extends ZipTestCase /** * @expectedException \PhpZip\Exception\InvalidArgumentException * @expectedExceptionMessage regex pattern empty + * @throws ZipException */ public function testAddFilesFromRegexNullPattern() { @@ -1436,6 +1507,7 @@ class ZipFileTest extends ZipTestCase /** * @expectedException \PhpZip\Exception\InvalidArgumentException * @expectedExceptionMessage regex pattern empty + * @throws ZipException */ public function testAddFilesFromRegexEmptyPattern() { @@ -1446,6 +1518,7 @@ class ZipFileTest extends ZipTestCase /** * @expectedException \PhpZip\Exception\InvalidArgumentException * @expectedExceptionMessage Input dir empty + * @throws ZipException */ public function testAddFilesFromRegexRecursiveDirectoryNull() { @@ -1456,6 +1529,7 @@ class ZipFileTest extends ZipTestCase /** * @expectedException \PhpZip\Exception\InvalidArgumentException * @expectedExceptionMessage Input dir empty + * @throws ZipException */ public function testAddFilesFromRegexRecursiveEmpty() { @@ -1464,7 +1538,7 @@ class ZipFileTest extends ZipTestCase } /** - * @expectedException \PhpZip\Exception\InvalidArgumentException + * @expectedException \PhpZip\Exception\ZipException * @expectedExceptionMessage can't exists */ public function testAddFilesFromRegexRecursiveCantExists() @@ -1476,6 +1550,7 @@ class ZipFileTest extends ZipTestCase /** * @expectedException \PhpZip\Exception\InvalidArgumentException * @expectedExceptionMessage regex pattern empty + * @throws ZipException */ public function testAddFilesFromRegexRecursiveNullPattern() { @@ -1486,6 +1561,7 @@ class ZipFileTest extends ZipTestCase /** * @expectedException \PhpZip\Exception\InvalidArgumentException * @expectedExceptionMessage regex pattern empty + * @throws ZipException */ public function testAddFilesFromRegexRecursiveEmptyPattern() { @@ -1496,6 +1572,7 @@ class ZipFileTest extends ZipTestCase /** * @expectedException \PhpZip\Exception\InvalidArgumentException * @expectedExceptionMessage handle is not resource + * @throws ZipException */ public function testSaveAsStreamBadStream() { @@ -1507,6 +1584,7 @@ class ZipFileTest extends ZipTestCase /** * @expectedException \PhpZip\Exception\InvalidArgumentException * @expectedExceptionMessage can not open from write + * @throws ZipException */ public function testSaveAsFileNotWritable() { @@ -1514,8 +1592,8 @@ class ZipFileTest extends ZipTestCase $this->markTestSkipped('Skip the test for a user with root privileges'); } - self::assertTrue(mkdir($this->outputDirname, 0444, true)); - self::assertTrue(chmod($this->outputDirname, 0444)); + $this->assertTrue(mkdir($this->outputDirname, 0444, true)); + $this->assertTrue(chmod($this->outputDirname, 0444)); $this->outputFilename = $this->outputDirname . DIRECTORY_SEPARATOR . basename($this->outputFilename); @@ -1525,6 +1603,7 @@ class ZipFileTest extends ZipTestCase /** * Test `ZipFile` implemented \ArrayAccess, \Countable and |iterator. + * @throws ZipException */ public function testZipFileArrayAccessAndCountableAndIterator() { @@ -1547,19 +1626,19 @@ class ZipFileTest extends ZipTestCase $zipFile->saveAsFile($this->outputFilename); $zipFile->close(); - self::assertCorrectZipArchive($this->outputFilename); + $this->assertCorrectZipArchive($this->outputFilename); $zipFile->openFile($this->outputFilename); // Test \Countable - self::assertEquals($zipFile->count(), $numFiles); - self::assertEquals(count($zipFile), $numFiles); + $this->assertEquals($zipFile->count(), $numFiles); + $this->assertEquals(count($zipFile), $numFiles); // Test \ArrayAccess reset($files); foreach ($zipFile as $entryName => $content) { - self::assertEquals($entryName, key($files)); - self::assertEquals($content, current($files)); + $this->assertEquals($entryName, key($files)); + $this->assertEquals($content, current($files)); next($files); } @@ -1571,8 +1650,8 @@ class ZipFileTest extends ZipTestCase $key = $iterator->key(); $value = $iterator->current(); - self::assertEquals($key, key($files)); - self::assertEquals($value, current($files)); + $this->assertEquals($key, key($files)); + $this->assertEquals($value, current($files)); next($files); $iterator->next(); @@ -1586,28 +1665,31 @@ class ZipFileTest extends ZipTestCase $zipFile->saveAsFile($this->outputFilename); $zipFile->close(); - self::assertCorrectZipArchive($this->outputFilename); + $this->assertCorrectZipArchive($this->outputFilename); $zipFile->openFile($this->outputFilename); - self::assertTrue(isset($zipFile['file1.txt'])); - self::assertTrue(isset($zipFile['dir/file2.txt'])); - self::assertTrue(isset($zipFile['dir/empty dir/'])); - self::assertFalse(isset($zipFile['dir/empty dir/2/'])); + $this->assertTrue(isset($zipFile['file1.txt'])); + $this->assertTrue(isset($zipFile['dir/file2.txt'])); + $this->assertTrue(isset($zipFile['dir/empty dir/'])); + $this->assertFalse(isset($zipFile['dir/empty dir/2/'])); $zipFile['dir/empty dir/2/'] = null; unset($zipFile['dir/file2.txt'], $zipFile['dir/empty dir/']); $zipFile->saveAsFile($this->outputFilename); $zipFile->close(); - self::assertCorrectZipArchive($this->outputFilename); + $this->assertCorrectZipArchive($this->outputFilename); $zipFile->openFile($this->outputFilename); - self::assertTrue(isset($zipFile['file1.txt'])); - self::assertFalse(isset($zipFile['dir/file2.txt'])); - self::assertFalse(isset($zipFile['dir/empty dir/'])); - self::assertTrue(isset($zipFile['dir/empty dir/2/'])); + $this->assertTrue(isset($zipFile['file1.txt'])); + $this->assertFalse(isset($zipFile['dir/file2.txt'])); + $this->assertFalse(isset($zipFile['dir/empty dir/'])); + $this->assertTrue(isset($zipFile['dir/empty dir/2/'])); $zipFile->close(); } + /** + * @throws ZipException + */ public function testArrayAccessAddFile() { $entryName = 'path/to/file.dat'; @@ -1619,17 +1701,21 @@ class ZipFileTest extends ZipTestCase $zipFile->saveAsFile($this->outputFilename); $zipFile->close(); - self::assertCorrectZipArchive($this->outputFilename); + $this->assertCorrectZipArchive($this->outputFilename); $zipFile->openFile($this->outputFilename); - self::assertEquals(sizeof($zipFile), 2); - self::assertTrue(isset($zipFile[$entryName])); - self::assertTrue(isset($zipFile[$entryNameStream])); - self::assertEquals($zipFile[$entryName], file_get_contents(__FILE__)); - self::assertEquals($zipFile[$entryNameStream], file_get_contents(__FILE__)); + $this->assertEquals(sizeof($zipFile), 2); + $this->assertTrue(isset($zipFile[$entryName])); + $this->assertTrue(isset($zipFile[$entryNameStream])); + $this->assertEquals($zipFile[$entryName], file_get_contents(__FILE__)); + $this->assertEquals($zipFile[$entryNameStream], file_get_contents(__FILE__)); $zipFile->close(); } + /** + * @throws Exception\ZipEntryNotFoundException + * @throws ZipException + */ public function testUnknownCompressionMethod() { $zipFile = new ZipFile(); @@ -1637,23 +1723,24 @@ class ZipFileTest extends ZipTestCase $zipFile->addFromString('file', 'content', ZipEntry::UNKNOWN); $zipFile->addFromString('file2', base64_encode(CryptoUtil::randomBytes(512)), ZipEntry::UNKNOWN); - self::assertEquals($zipFile->getEntryInfo('file')->getMethodName(), 'Unknown'); - self::assertEquals($zipFile->getEntryInfo('file2')->getMethodName(), 'Unknown'); + $this->assertEquals($zipFile->getEntryInfo('file')->getMethodName(), 'Unknown'); + $this->assertEquals($zipFile->getEntryInfo('file2')->getMethodName(), 'Unknown'); $zipFile->saveAsFile($this->outputFilename); $zipFile->close(); $zipFile->openFile($this->outputFilename); - self::assertEquals($zipFile->getEntryInfo('file')->getMethodName(), 'No compression'); - self::assertEquals($zipFile->getEntryInfo('file2')->getMethodName(), 'Deflate'); + $this->assertEquals($zipFile->getEntryInfo('file')->getMethodName(), 'No compression'); + $this->assertEquals($zipFile->getEntryInfo('file2')->getMethodName(), 'Deflate'); $zipFile->close(); } /** * @expectedException \PhpZip\Exception\InvalidArgumentException - * @expectedExceptionMessage DirName empty + * @expectedExceptionMessage Dir name is null + * @throws ZipException */ public function testAddEmptyDirNullName() { @@ -1663,7 +1750,8 @@ class ZipFileTest extends ZipTestCase /** * @expectedException \PhpZip\Exception\InvalidArgumentException - * @expectedExceptionMessage DirName empty + * @expectedExceptionMessage Empty dir name + * @throws ZipException */ public function testAddEmptyDirEmptyName() { @@ -1672,8 +1760,8 @@ class ZipFileTest extends ZipTestCase } /** - * @expectedException \PhpZip\Exception\ZipNotFoundEntry - * @expectedExceptionMessage Zip entry "bad entry name" not found + * @expectedException \PhpZip\Exception\ZipEntryNotFoundException + * @expectedExceptionMessage "bad entry name" */ public function testNotFoundEntry() { @@ -1683,13 +1771,14 @@ class ZipFileTest extends ZipTestCase /** * Test rewrite input file. + * @throws ZipException */ public function testRewriteFile() { $zipFile = new ZipFile(); $zipFile['file'] = 'content'; $zipFile['file2'] = 'content2'; - self::assertEquals(count($zipFile), 2); + $this->assertEquals(count($zipFile), 2); $zipFile ->saveAsFile($this->outputFilename) ->close(); @@ -1697,23 +1786,24 @@ class ZipFileTest extends ZipTestCase $md5file = md5_file($this->outputFilename); $zipFile->openFile($this->outputFilename); - self::assertEquals(count($zipFile), 2); - self::assertTrue(isset($zipFile['file'])); - self::assertTrue(isset($zipFile['file2'])); + $this->assertEquals(count($zipFile), 2); + $this->assertTrue(isset($zipFile['file'])); + $this->assertTrue(isset($zipFile['file2'])); $zipFile['file3'] = 'content3'; - self::assertEquals(count($zipFile), 3); + $this->assertEquals(count($zipFile), 3); $zipFile = $zipFile->rewrite(); - self::assertEquals(count($zipFile), 3); - self::assertTrue(isset($zipFile['file'])); - self::assertTrue(isset($zipFile['file2'])); - self::assertTrue(isset($zipFile['file3'])); + $this->assertEquals(count($zipFile), 3); + $this->assertTrue(isset($zipFile['file'])); + $this->assertTrue(isset($zipFile['file2'])); + $this->assertTrue(isset($zipFile['file3'])); $zipFile->close(); - self::assertNotEquals(md5_file($this->outputFilename), $md5file); + $this->assertNotEquals(md5_file($this->outputFilename), $md5file); } /** * Test rewrite for string. + * @throws ZipException */ public function testRewriteString() { @@ -1724,15 +1814,15 @@ class ZipFileTest extends ZipTestCase $zipFile->close(); $zipFile->openFromString(file_get_contents($this->outputFilename)); - self::assertEquals(count($zipFile), 2); - self::assertTrue(isset($zipFile['file'])); - self::assertTrue(isset($zipFile['file2'])); + $this->assertEquals(count($zipFile), 2); + $this->assertTrue(isset($zipFile['file'])); + $this->assertTrue(isset($zipFile['file2'])); $zipFile['file3'] = 'content3'; $zipFile = $zipFile->rewrite(); - self::assertEquals(count($zipFile), 3); - self::assertTrue(isset($zipFile['file'])); - self::assertTrue(isset($zipFile['file2'])); - self::assertTrue(isset($zipFile['file3'])); + $this->assertEquals(count($zipFile), 3); + $this->assertTrue(isset($zipFile['file'])); + $this->assertTrue(isset($zipFile['file2'])); + $this->assertTrue(isset($zipFile['file3'])); $zipFile->close(); } @@ -1746,40 +1836,46 @@ class ZipFileTest extends ZipTestCase $zipFile->rewrite(); } + /** + * @throws ZipException + */ public function testFilename0() { $zipFile = new ZipFile(); $zipFile[0] = 0; - self::assertTrue(isset($zipFile[0])); - self::assertTrue(isset($zipFile['0'])); - self::assertCount(1, $zipFile); + $this->assertTrue(isset($zipFile[0])); + $this->assertTrue(isset($zipFile['0'])); + $this->assertCount(1, $zipFile); $zipFile ->saveAsFile($this->outputFilename) ->close(); - self::assertCorrectZipArchive($this->outputFilename); + $this->assertCorrectZipArchive($this->outputFilename); $zipFile->openFile($this->outputFilename); - self::assertTrue(isset($zipFile[0])); - self::assertTrue(isset($zipFile['0'])); - self::assertEquals($zipFile['0'], '0'); - self::assertCount(1, $zipFile); + $this->assertTrue(isset($zipFile[0])); + $this->assertTrue(isset($zipFile['0'])); + $this->assertEquals($zipFile['0'], '0'); + $this->assertCount(1, $zipFile); $zipFile->close(); - self::assertTrue(unlink($this->outputFilename)); + $this->assertTrue(unlink($this->outputFilename)); $zipFile = new ZipFile(); $zipFile->addFromString(0, 0); - self::assertTrue(isset($zipFile[0])); - self::assertTrue(isset($zipFile['0'])); - self::assertCount(1, $zipFile); + $this->assertTrue(isset($zipFile[0])); + $this->assertTrue(isset($zipFile['0'])); + $this->assertCount(1, $zipFile); $zipFile ->saveAsFile($this->outputFilename) ->close(); - self::assertCorrectZipArchive($this->outputFilename); + $this->assertCorrectZipArchive($this->outputFilename); } + /** + * @throws ZipException + */ public function testPsrResponse() { $zipFile = new ZipFile(); @@ -1793,6 +1889,10 @@ class ZipFileTest extends ZipTestCase $this->assertEquals('attachment; filename="file.jar"', $response->getHeaderLine('content-disposition')); } + /** + * @throws ZipEntryNotFoundException + * @throws ZipException + */ public function testCompressionLevel() { $zipFile = new ZipFile(); @@ -1808,16 +1908,16 @@ class ZipFileTest extends ZipTestCase ->saveAsFile($this->outputFilename) ->close(); - self::assertCorrectZipArchive($this->outputFilename); + $this->assertCorrectZipArchive($this->outputFilename); $zipFile->openFile($this->outputFilename); - self::assertEquals($zipFile->getEntryInfo('file') + $this->assertEquals($zipFile->getEntryInfo('file') ->getCompressionLevel(), ZipFileInterface::LEVEL_BEST_COMPRESSION); - self::assertEquals($zipFile->getEntryInfo('file2') + $this->assertEquals($zipFile->getEntryInfo('file2') ->getCompressionLevel(), ZipFileInterface::LEVEL_FAST); - self::assertEquals($zipFile->getEntryInfo('file3') + $this->assertEquals($zipFile->getEntryInfo('file3') ->getCompressionLevel(), ZipFileInterface::LEVEL_SUPER_FAST); - self::assertEquals($zipFile->getEntryInfo('file4') + $this->assertEquals($zipFile->getEntryInfo('file4') ->getCompressionLevel(), ZipFileInterface::LEVEL_DEFAULT_COMPRESSION); $zipFile->close(); } @@ -1825,6 +1925,7 @@ class ZipFileTest extends ZipTestCase /** * @expectedException \PhpZip\Exception\InvalidArgumentException * @expectedExceptionMessage Invalid compression level + * @throws ZipException */ public function testInvalidCompressionLevel() { @@ -1836,6 +1937,7 @@ class ZipFileTest extends ZipTestCase /** * @expectedException \PhpZip\Exception\InvalidArgumentException * @expectedExceptionMessage Invalid compression level + * @throws ZipException */ public function testInvalidCompressionLevelEntry() { @@ -1844,6 +1946,9 @@ class ZipFileTest extends ZipTestCase $zipFile->setCompressionLevelEntry('file', 15); } + /** + * @throws ZipException + */ public function testCompressionGlobal() { $zipFile = new ZipFile(); @@ -1855,16 +1960,20 @@ class ZipFileTest extends ZipTestCase ->saveAsFile($this->outputFilename) ->close(); - self::assertCorrectZipArchive($this->outputFilename); + $this->assertCorrectZipArchive($this->outputFilename); $zipFile->openFile($this->outputFilename); $infoList = $zipFile->getAllInfo(); array_walk($infoList, function (ZipInfo $zipInfo) { - self::assertEquals($zipInfo->getCompressionLevel(), ZipFileInterface::LEVEL_BEST_SPEED); + $this->assertEquals($zipInfo->getCompressionLevel(), ZipFileInterface::LEVEL_BEST_SPEED); }); $zipFile->close(); } + /** + * @throws ZipEntryNotFoundException + * @throws ZipException + */ public function testCompressionMethodEntry() { $zipFile = new ZipFile(); @@ -1873,17 +1982,18 @@ class ZipFileTest extends ZipTestCase $zipFile->close(); $zipFile->openFile($this->outputFilename); - self::assertEquals($zipFile->getEntryInfo('file')->getMethodName(), 'No compression'); + $this->assertEquals($zipFile->getEntryInfo('file')->getMethodName(), 'No compression'); $zipFile->setCompressionMethodEntry('file', ZipFileInterface::METHOD_DEFLATED); - self::assertEquals($zipFile->getEntryInfo('file')->getMethodName(), 'Deflate'); + $this->assertEquals($zipFile->getEntryInfo('file')->getMethodName(), 'Deflate'); $zipFile->rewrite(); - self::assertEquals($zipFile->getEntryInfo('file')->getMethodName(), 'Deflate'); + $this->assertEquals($zipFile->getEntryInfo('file')->getMethodName(), 'Deflate'); } /** - * @expectedException \PhpZip\Exception\ZipUnsupportMethod + * @expectedException \PhpZip\Exception\ZipUnsupportMethodException * @expectedExceptionMessage Unsupported method + * @throws ZipException */ public function testInvalidCompressionMethodEntry() { @@ -1892,6 +2002,9 @@ class ZipFileTest extends ZipTestCase $zipFile->setCompressionMethodEntry('file', 99); } + /** + * @throws ZipException + */ public function testUnchangeAll() { $zipFile = new ZipFile(); @@ -1899,32 +2012,35 @@ class ZipFileTest extends ZipTestCase $zipFile[$i] = $i; } $zipFile->setArchiveComment('comment'); - self::assertCount(10, $zipFile); - self::assertEquals($zipFile->getArchiveComment(), 'comment'); + $this->assertCount(10, $zipFile); + $this->assertEquals($zipFile->getArchiveComment(), 'comment'); $zipFile->saveAsFile($this->outputFilename); $zipFile->unchangeAll(); - self::assertCount(0, $zipFile); - self::assertEquals($zipFile->getArchiveComment(), null); + $this->assertCount(0, $zipFile); + $this->assertEquals($zipFile->getArchiveComment(), null); $zipFile->close(); $zipFile->openFile($this->outputFilename); - self::assertCount(10, $zipFile); - self::assertEquals($zipFile->getArchiveComment(), 'comment'); + $this->assertCount(10, $zipFile); + $this->assertEquals($zipFile->getArchiveComment(), 'comment'); for ($i = 10; $i < 100; $i++) { $zipFile[$i] = $i; } $zipFile->setArchiveComment('comment 2'); - self::assertCount(100, $zipFile); - self::assertEquals($zipFile->getArchiveComment(), 'comment 2'); + $this->assertCount(100, $zipFile); + $this->assertEquals($zipFile->getArchiveComment(), 'comment 2'); $zipFile->unchangeAll(); - self::assertCount(10, $zipFile); - self::assertEquals($zipFile->getArchiveComment(), 'comment'); + $this->assertCount(10, $zipFile); + $this->assertEquals($zipFile->getArchiveComment(), 'comment'); $zipFile->close(); } + /** + * @throws ZipException + */ public function testUnchangeArchiveComment() { $zipFile = new ZipFile(); @@ -1932,23 +2048,27 @@ class ZipFileTest extends ZipTestCase $zipFile[$i] = $i; } $zipFile->setArchiveComment('comment'); - self::assertEquals($zipFile->getArchiveComment(), 'comment'); + $this->assertEquals($zipFile->getArchiveComment(), 'comment'); $zipFile->saveAsFile($this->outputFilename); $zipFile->unchangeArchiveComment(); - self::assertEquals($zipFile->getArchiveComment(), null); + $this->assertEquals($zipFile->getArchiveComment(), null); $zipFile->close(); $zipFile->openFile($this->outputFilename); - self::assertEquals($zipFile->getArchiveComment(), 'comment'); + $this->assertEquals($zipFile->getArchiveComment(), 'comment'); $zipFile->setArchiveComment('comment 2'); - self::assertEquals($zipFile->getArchiveComment(), 'comment 2'); + $this->assertEquals($zipFile->getArchiveComment(), 'comment 2'); $zipFile->unchangeArchiveComment(); - self::assertEquals($zipFile->getArchiveComment(), 'comment'); + $this->assertEquals($zipFile->getArchiveComment(), 'comment'); $zipFile->close(); } + /** + * @throws ZipEntryNotFoundException + * @throws ZipException + */ public function testUnchangeEntry() { $zipFile = new ZipFile(); @@ -1963,25 +2083,26 @@ class ZipFileTest extends ZipTestCase $zipFile['file 1'] = 'modify content 1'; $zipFile->setPasswordEntry('file 1', 'password'); - self::assertEquals($zipFile['file 1'], 'modify content 1'); - self::assertTrue($zipFile->getEntryInfo('file 1')->isEncrypted()); + $this->assertEquals($zipFile['file 1'], 'modify content 1'); + $this->assertTrue($zipFile->getEntryInfo('file 1')->isEncrypted()); - self::assertEquals($zipFile['file 2'], 'content 2'); - self::assertFalse($zipFile->getEntryInfo('file 2')->isEncrypted()); + $this->assertEquals($zipFile['file 2'], 'content 2'); + $this->assertFalse($zipFile->getEntryInfo('file 2')->isEncrypted()); $zipFile->unchangeEntry('file 1'); - self::assertEquals($zipFile['file 1'], 'content 1'); - self::assertFalse($zipFile->getEntryInfo('file 1')->isEncrypted()); + $this->assertEquals($zipFile['file 1'], 'content 1'); + $this->assertFalse($zipFile->getEntryInfo('file 1')->isEncrypted()); - self::assertEquals($zipFile['file 2'], 'content 2'); - self::assertFalse($zipFile->getEntryInfo('file 2')->isEncrypted()); + $this->assertEquals($zipFile['file 2'], 'content 2'); + $this->assertFalse($zipFile->getEntryInfo('file 2')->isEncrypted()); $zipFile->close(); } /** * Test support ZIP64 ext (slow test - normal). * Create > 65535 files in archive and open and extract to /dev/null. + * @throws ZipException */ public function testCreateAndOpenZip64Ext() { @@ -1994,14 +2115,14 @@ class ZipFileTest extends ZipTestCase $zipFile->saveAsFile($this->outputFilename); $zipFile->close(); - self::assertCorrectZipArchive($this->outputFilename); + $this->assertCorrectZipArchive($this->outputFilename); $zipFile->openFile($this->outputFilename); - self::assertEquals($zipFile->count(), $countFiles); + $this->assertEquals($zipFile->count(), $countFiles); $i = 0; foreach ($zipFile as $entry => $content) { - self::assertEquals($entry, $i . '.txt'); - self::assertEquals($content, $i); + $this->assertEquals($entry, $i . '.txt'); + $this->assertEquals($content, $i); $i++; } $zipFile->close(); diff --git a/tests/PhpZip/ZipMatcherTest.php b/tests/PhpZip/ZipMatcherTest.php index c824765..32da1b4 100644 --- a/tests/PhpZip/ZipMatcherTest.php +++ b/tests/PhpZip/ZipMatcherTest.php @@ -16,7 +16,7 @@ class ZipMatcherTest extends \PHPUnit_Framework_TestCase } $matcher = $zipFile->matcher(); - self::assertInstanceOf(ZipEntryMatcher::class, $matcher); + $this->assertInstanceOf(ZipEntryMatcher::class, $matcher); $this->assertTrue(is_array($matcher->getMatches())); $this->assertCount(0, $matcher); @@ -40,7 +40,7 @@ class ZipMatcherTest extends \PHPUnit_Framework_TestCase $matcher->setPassword('qwerty'); $info = $zipFile->getAllInfo(); array_walk($info, function (ZipInfo $zipInfo) use ($actualMatches) { - self::assertEquals($zipInfo->isEncrypted(), in_array($zipInfo->getName(), $actualMatches)); + $this->assertEquals($zipInfo->isEncrypted(), in_array($zipInfo->getName(), $actualMatches)); }); $matcher->all(); @@ -86,12 +86,12 @@ class ZipMatcherTest extends \PHPUnit_Framework_TestCase ]; foreach ($renameEntriesArray as $name) { - self::assertTrue(isset($zipFile[$name])); + $this->assertTrue(isset($zipFile[$name])); } $matcher = $zipFile->matcher(); $matcher->match('~^file_(1|5)\d+~'); - self::assertEquals($matcher->getMatches(), $renameEntriesArray); + $this->assertEquals($matcher->getMatches(), $renameEntriesArray); $matcher->invoke(function ($entryName) use ($zipFile) { $newName = preg_replace('~\.(jpe?g)$~i', '.no_optimize.$1', $entryName); @@ -99,11 +99,11 @@ class ZipMatcherTest extends \PHPUnit_Framework_TestCase }); foreach ($renameEntriesArray as $name) { - self::assertFalse(isset($zipFile[$name])); + $this->assertFalse(isset($zipFile[$name])); $pathInfo = pathinfo($name); $newName = $pathInfo['filename'].'.no_optimize.'.$pathInfo['extension']; - self::assertTrue(isset($zipFile[$newName])); + $this->assertTrue(isset($zipFile[$newName])); } $zipFile->close(); diff --git a/tests/PhpZip/ZipPasswordTest.php b/tests/PhpZip/ZipPasswordTest.php index 1321530..09722f0 100644 --- a/tests/PhpZip/ZipPasswordTest.php +++ b/tests/PhpZip/ZipPasswordTest.php @@ -3,6 +3,8 @@ namespace PhpZip; use PhpZip\Exception\ZipAuthenticationException; +use PhpZip\Exception\ZipEntryNotFoundException; +use PhpZip\Exception\ZipException; use PhpZip\Model\ZipInfo; use PhpZip\Util\CryptoUtil; @@ -13,6 +15,7 @@ class ZipPasswordTest extends ZipFileAddDirTest { /** * Test archive password. + * @throws ZipException */ public function testSetPassword() { @@ -30,7 +33,7 @@ class ZipPasswordTest extends ZipFileAddDirTest $zipFile->saveAsFile($this->outputFilename); $zipFile->close(); - self::assertCorrectZipArchive($this->outputFilename, $password); + $this->assertCorrectZipArchive($this->outputFilename, $password); // check bad password for ZipCrypto $zipFile->openFile($this->outputFilename); @@ -38,20 +41,20 @@ class ZipPasswordTest extends ZipFileAddDirTest foreach ($zipFile->getListFiles() as $entryName) { try { $zipFile[$entryName]; - self::fail("Expected Exception has not been raised."); + $this->fail("Expected Exception has not been raised."); } catch (ZipAuthenticationException $ae) { - self::assertNotNull($ae); + $this->assertNotNull($ae); } } // check correct password for ZipCrypto $zipFile->setReadPassword($password); foreach ($zipFile->getAllInfo() as $info) { - self::assertTrue($info->isEncrypted()); - self::assertContains('ZipCrypto', $info->getMethodName()); + $this->assertTrue($info->isEncrypted()); + $this->assertContains('ZipCrypto', $info->getMethodName()); $decryptContent = $zipFile[$info->getName()]; - self::assertNotEmpty($decryptContent); - self::assertContains('assertNotEmpty($decryptContent); + $this->assertContains('saveAsFile($this->outputFilename); $zipFile->close(); - self::assertCorrectZipArchive($this->outputFilename, $password); + $this->assertCorrectZipArchive($this->outputFilename, $password); // check from WinZip AES encryption $zipFile->openFile($this->outputFilename); @@ -68,20 +71,20 @@ class ZipPasswordTest extends ZipFileAddDirTest foreach ($zipFile->getListFiles() as $entryName) { try { $zipFile[$entryName]; - self::fail("Expected Exception has not been raised."); + $this->fail("Expected Exception has not been raised."); } catch (ZipAuthenticationException $ae) { - self::assertNotNull($ae); + $this->assertNotNull($ae); } } // set correct password WinZip AES $zipFile->setReadPassword($password); foreach ($zipFile->getAllInfo() as $info) { - self::assertTrue($info->isEncrypted()); - self::assertContains('WinZip', $info->getMethodName()); + $this->assertTrue($info->isEncrypted()); + $this->assertContains('WinZip', $info->getMethodName()); $decryptContent = $zipFile[$info->getName()]; - self::assertNotEmpty($decryptContent); - self::assertContains('assertNotEmpty($decryptContent); + $this->assertContains('saveAsFile($this->outputFilename); $zipFile->close(); - self::assertCorrectZipArchive($this->outputFilename); + $this->assertCorrectZipArchive($this->outputFilename); // check remove password $zipFile->openFile($this->outputFilename); foreach ($zipFile->getAllInfo() as $info) { - self::assertFalse($info->isEncrypted()); + $this->assertFalse($info->isEncrypted()); } $zipFile->close(); } + /** + * @throws ZipException + */ public function testTraditionalEncryption() { if (PHP_INT_SIZE === 4) { @@ -115,15 +121,15 @@ class ZipPasswordTest extends ZipFileAddDirTest $zip->saveAsFile($this->outputFilename); $zip->close(); - self::assertCorrectZipArchive($this->outputFilename, $password); + $this->assertCorrectZipArchive($this->outputFilename, $password); $zip->openFile($this->outputFilename); $zip->setReadPassword($password); - self::assertFilesResult($zip, array_keys(self::$files)); + $this->assertFilesResult($zip, array_keys(self::$files)); foreach ($zip->getAllInfo() as $info) { if (!$info->isFolder()) { - self::assertTrue($info->isEncrypted()); - self::assertContains('ZipCrypto', $info->getMethodName()); + $this->assertTrue($info->isEncrypted()); + $this->assertContains('ZipCrypto', $info->getMethodName()); } } $zip->close(); @@ -133,6 +139,7 @@ class ZipPasswordTest extends ZipFileAddDirTest * @dataProvider winZipKeyStrengthProvider * @param int $encryptionMethod * @param int $bitSize + * @throws ZipException */ public function testWinZipAesEncryption($encryptionMethod, $bitSize) { @@ -144,16 +151,16 @@ class ZipPasswordTest extends ZipFileAddDirTest $zip->saveAsFile($this->outputFilename); $zip->close(); - self::assertCorrectZipArchive($this->outputFilename, $password); + $this->assertCorrectZipArchive($this->outputFilename, $password); $zip->openFile($this->outputFilename); $zip->setReadPassword($password); - self::assertFilesResult($zip, array_keys(self::$files)); + $this->assertFilesResult($zip, array_keys(self::$files)); foreach ($zip->getAllInfo() as $info) { if (!$info->isFolder()) { - self::assertTrue($info->isEncrypted()); - self::assertEquals($info->getEncryptionMethod(), $encryptionMethod); - self::assertContains('WinZip AES-' . $bitSize, $info->getMethodName()); + $this->assertTrue($info->isEncrypted()); + $this->assertEquals($info->getEncryptionMethod(), $encryptionMethod); + $this->assertContains('WinZip AES-' . $bitSize, $info->getMethodName()); } } $zip->close(); @@ -172,6 +179,10 @@ class ZipPasswordTest extends ZipFileAddDirTest ]; } + /** + * @throws Exception\ZipEntryNotFoundException + * @throws ZipException + */ public function testEncryptionEntries() { if (PHP_INT_SIZE === 4) { @@ -191,7 +202,7 @@ class ZipPasswordTest extends ZipFileAddDirTest $zip->openFile($this->outputFilename); $zip->setReadPasswordEntry('.hidden', $password1); $zip->setReadPasswordEntry('text file.txt', $password2); - self::assertFilesResult($zip, [ + $this->assertFilesResult($zip, [ '.hidden', 'text file.txt', 'Текстовый документ.txt', @@ -199,19 +210,23 @@ class ZipPasswordTest extends ZipFileAddDirTest ]); $info = $zip->getEntryInfo('.hidden'); - self::assertTrue($info->isEncrypted()); - self::assertContains('ZipCrypto', $info->getMethodName()); + $this->assertTrue($info->isEncrypted()); + $this->assertContains('ZipCrypto', $info->getMethodName()); $info = $zip->getEntryInfo('text file.txt'); - self::assertTrue($info->isEncrypted()); - self::assertContains('WinZip AES', $info->getMethodName()); + $this->assertTrue($info->isEncrypted()); + $this->assertContains('WinZip AES', $info->getMethodName()); - self::assertFalse($zip->getEntryInfo('Текстовый документ.txt')->isEncrypted()); - self::assertFalse($zip->getEntryInfo('empty dir/')->isEncrypted()); + $this->assertFalse($zip->getEntryInfo('Текстовый документ.txt')->isEncrypted()); + $this->assertFalse($zip->getEntryInfo('empty dir/')->isEncrypted()); $zip->close(); } + /** + * @throws Exception\ZipEntryNotFoundException + * @throws ZipException + */ public function testEncryptionEntriesWithDefaultPassword() { if (PHP_INT_SIZE === 4) { @@ -234,7 +249,7 @@ class ZipPasswordTest extends ZipFileAddDirTest $zip->setReadPassword($defaultPassword); $zip->setReadPasswordEntry('.hidden', $password1); $zip->setReadPasswordEntry('text file.txt', $password2); - self::assertFilesResult($zip, [ + $this->assertFilesResult($zip, [ '.hidden', 'text file.txt', 'Текстовый документ.txt', @@ -242,18 +257,18 @@ class ZipPasswordTest extends ZipFileAddDirTest ]); $info = $zip->getEntryInfo('.hidden'); - self::assertTrue($info->isEncrypted()); - self::assertContains('ZipCrypto', $info->getMethodName()); + $this->assertTrue($info->isEncrypted()); + $this->assertContains('ZipCrypto', $info->getMethodName()); $info = $zip->getEntryInfo('text file.txt'); - self::assertTrue($info->isEncrypted()); - self::assertContains('WinZip AES', $info->getMethodName()); + $this->assertTrue($info->isEncrypted()); + $this->assertContains('WinZip AES', $info->getMethodName()); $info = $zip->getEntryInfo('Текстовый документ.txt'); - self::assertTrue($info->isEncrypted()); - self::assertContains('WinZip AES', $info->getMethodName()); + $this->assertTrue($info->isEncrypted()); + $this->assertContains('WinZip AES', $info->getMethodName()); - self::assertFalse($zip->getEntryInfo('empty dir/')->isEncrypted()); + $this->assertFalse($zip->getEntryInfo('empty dir/')->isEncrypted()); $zip->close(); } @@ -271,28 +286,32 @@ class ZipPasswordTest extends ZipFileAddDirTest $zipFile->outputAsString(); } + /** + * @throws Exception\ZipEntryNotFoundException + * @throws ZipException + */ public function testEntryPassword() { $zipFile = new ZipFile(); $zipFile->setPassword('pass'); $zipFile['file'] = 'content'; - self::assertFalse($zipFile->getEntryInfo('file')->isEncrypted()); + $this->assertFalse($zipFile->getEntryInfo('file')->isEncrypted()); for ($i = 1; $i <= 10; $i++) { $zipFile['file' . $i] = 'content'; if ($i < 6) { $zipFile->setPasswordEntry('file' . $i, 'pass'); - self::assertTrue($zipFile->getEntryInfo('file' . $i)->isEncrypted()); + $this->assertTrue($zipFile->getEntryInfo('file' . $i)->isEncrypted()); } else { - self::assertFalse($zipFile->getEntryInfo('file' . $i)->isEncrypted()); + $this->assertFalse($zipFile->getEntryInfo('file' . $i)->isEncrypted()); } } $zipFile->disableEncryptionEntry('file3'); - self::assertFalse($zipFile->getEntryInfo('file3')->isEncrypted()); - self::asserttrue($zipFile->getEntryInfo('file2')->isEncrypted()); + $this->assertFalse($zipFile->getEntryInfo('file3')->isEncrypted()); + $this->asserttrue($zipFile->getEntryInfo('file2')->isEncrypted()); $zipFile->disableEncryption(); $infoList = $zipFile->getAllInfo(); array_walk($infoList, function (ZipInfo $zipInfo) { - self::assertFalse($zipInfo->isEncrypted()); + $this->assertFalse($zipInfo->isEncrypted()); }); $zipFile->close(); } @@ -308,6 +327,10 @@ class ZipPasswordTest extends ZipFileAddDirTest $zipFile->setPasswordEntry('file', 'pass', 99); } + /** + * @throws ZipEntryNotFoundException + * @throws ZipException + */ public function testArchivePasswordUpdateWithoutSetReadPassword() { $zipFile = new ZipFile(); @@ -318,37 +341,38 @@ class ZipPasswordTest extends ZipFileAddDirTest $zipFile->saveAsFile($this->outputFilename); $zipFile->close(); - self::assertCorrectZipArchive($this->outputFilename, 'password'); + $this->assertCorrectZipArchive($this->outputFilename, 'password'); $zipFile->openFile($this->outputFilename); - self::assertCount(3, $zipFile); + $this->assertCount(3, $zipFile); foreach ($zipFile->getAllInfo() as $info) { - self::assertTrue($info->isEncrypted()); + $this->assertTrue($info->isEncrypted()); } unset($zipFile['file3']); $zipFile['file4'] = 'content'; $zipFile->rewrite(); - self::assertCorrectZipArchive($this->outputFilename, 'password'); + $this->assertCorrectZipArchive($this->outputFilename, 'password'); - self::assertCount(3, $zipFile); - self::assertFalse(isset($zipFile['file3'])); - self::assertTrue(isset($zipFile['file4'])); - self::assertTrue($zipFile->getEntryInfo('file1')->isEncrypted()); - self::assertTrue($zipFile->getEntryInfo('file2')->isEncrypted()); - self::assertFalse($zipFile->getEntryInfo('file4')->isEncrypted()); - self::assertEquals($zipFile['file4'], 'content'); + $this->assertCount(3, $zipFile); + $this->assertFalse(isset($zipFile['file3'])); + $this->assertTrue(isset($zipFile['file4'])); + $this->assertTrue($zipFile->getEntryInfo('file1')->isEncrypted()); + $this->assertTrue($zipFile->getEntryInfo('file2')->isEncrypted()); + $this->assertFalse($zipFile->getEntryInfo('file4')->isEncrypted()); + $this->assertEquals($zipFile['file4'], 'content'); $zipFile->extractTo($this->outputDirname, ['file4']); - self::assertTrue(file_exists($this->outputDirname . DIRECTORY_SEPARATOR . 'file4')); - self::assertEquals(file_get_contents($this->outputDirname . DIRECTORY_SEPARATOR . 'file4'), $zipFile['file4']); + $this->assertTrue(file_exists($this->outputDirname . DIRECTORY_SEPARATOR . 'file4')); + $this->assertEquals(file_get_contents($this->outputDirname . DIRECTORY_SEPARATOR . 'file4'), $zipFile['file4']); $zipFile->close(); } /** * @see https://github.com/Ne-Lexa/php-zip/issues/9 + * @throws ZipException */ public function testIssues9() { @@ -371,6 +395,10 @@ class ZipPasswordTest extends ZipFileAddDirTest $zipFile->close(); } + /** + * @throws ZipEntryNotFoundException + * @throws ZipException + */ public function testReadAesEncryptedAndRewriteArchive() { $file = __DIR__ . '/resources/aes_password_archive.zip'; diff --git a/tests/PhpZip/ZipTestCase.php b/tests/PhpZip/ZipTestCase.php index 6de8537..d0eb595 100644 --- a/tests/PhpZip/ZipTestCase.php +++ b/tests/PhpZip/ZipTestCase.php @@ -79,7 +79,9 @@ class ZipTestCase extends \PHPUnit_Framework_TestCase $command = "7z t -p" . escapeshellarg($password) . " " . escapeshellarg($filename); exec($command, $output, $returnCode); - + /** + * @var array $output + */ $output = implode(PHP_EOL, $output); self::assertEquals($returnCode, 0); @@ -121,7 +123,7 @@ class ZipTestCase extends \PHPUnit_Framework_TestCase * @param bool $showErrors * @return bool|null If null - can not install zipalign */ - public static function doZipAlignVerify($filename, $showErrors = false) + public static function assertVerifyZipAlign($filename, $showErrors = false) { if (DIRECTORY_SEPARATOR !== '\\' && `which zipalign`) { exec("zipalign -c -v 4 " . escapeshellarg($filename), $output, $returnCode);