diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index 9902828..a1e5b36 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -75,7 +75,7 @@ jobs: with: php-version: ${{ matrix.php }} extensions: gd, xml, zip - coverage: xdebug + coverage: ${{ (matrix.php == '7.3') && 'xdebug' || 'none' }} - name: Generate Locale (for tests) run: sudo locale-gen de_DE.UTF-8 && sudo update-locale @@ -86,6 +86,11 @@ jobs: run: composer install --ansi --prefer-dist --no-interaction --no-progress - name: Run phpunit + if: matrix.php != '7.3' + run: ./vendor/bin/phpunit -c phpunit.xml.dist --no-coverage + + - name: Run phpunit + if: matrix.php == '7.3' run: ./vendor/bin/phpunit -c phpunit.xml.dist --coverage-clover build/clover.xml - name: Upload coverage results to Coveralls diff --git a/README.md b/README.md index 67e6878..83d878f 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,9 @@ -[![Latest Stable Version](https://poser.pugx.org/phpoffice/common/v/stable.png)](https://packagist.org/packages/phpoffice/common) -[![PHPOffice\Common](https://github.com/PHPOffice/Common/actions/workflows/php.yml/badge.svg)](https://github.com/PHPOffice/Common/actions/workflows/php.yml) -[![Coverage Status](https://coveralls.io/repos/github/PHPOffice/Common/badge.svg?branch=develop)](https://coveralls.io/github/PHPOffice/Common?branch=develop) -[![Total Downloads](https://poser.pugx.org/phpoffice/common/downloads.png)](https://packagist.org/packages/phpoffice/common) -[![License](https://poser.pugx.org/phpoffice/common/license.png)](https://packagist.org/packages/phpoffice/common) -[![Join the chat at https://gitter.im/PHPOffice/Common](https://img.shields.io/badge/GITTER-join%20chat-green.svg)](https://gitter.im/PHPOffice/Common) +[![Latest Stable Version](https://poser.pugx.org/phpoffice/common/v)](https://packagist.org/packages/phpoffice/common) +[![Coverage Status](https://coveralls.io/repos/github/PHPOffice/Common/badge.svg?branch=master)](https://coveralls.io/github/PHPOffice/Common?branch=master) +[![Total Downloads](https://poser.pugx.org/phpoffice/common/downloads)](https://packagist.org/packages/phpoffice/common) +[![License](https://poser.pugx.org/phpoffice/common/license)](https://packagist.org/packages/phpoffice/common) +Branch Master : [![PHPOffice\Common](https://github.com/PHPOffice/Common/actions/workflows/php.yml/badge.svg?branch=master)](https://github.com/PHPOffice/Common/actions/workflows/php.yml) PHPOffice Common is a library written in pure PHP that provides a set of components for PHPOffice librairies. diff --git a/VERSION b/VERSION index d81f1c3..e4c0d46 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.2.9 \ No newline at end of file +1.0.3 \ No newline at end of file diff --git a/src/Common/Adapter/Zip/PclZipAdapter.php b/src/Common/Adapter/Zip/PclZipAdapter.php index 2ccca4e..c9dc1e9 100644 --- a/src/Common/Adapter/Zip/PclZipAdapter.php +++ b/src/Common/Adapter/Zip/PclZipAdapter.php @@ -27,7 +27,7 @@ public function close() return $this; } - public function addFromString($localname, $contents) + public function addFromString(string $localname, string $contents, bool $withCompression = true) { $pathData = pathinfo($localname); @@ -35,7 +35,18 @@ public function addFromString($localname, $contents) fwrite($hFile, $contents); fclose($hFile); - $res = $this->oPclZip->add($this->tmpDir . '/' . $pathData['basename'], PCLZIP_OPT_REMOVE_PATH, $this->tmpDir, PCLZIP_OPT_ADD_PATH, $pathData['dirname']); + $params = [ + $this->tmpDir . '/' . $pathData['basename'], + PCLZIP_OPT_REMOVE_PATH, + $this->tmpDir, + PCLZIP_OPT_ADD_PATH, + $pathData['dirname'], + ]; + if (!$withCompression) { + $params[] = PCLZIP_OPT_NO_COMPRESSION; + } + + $res = $this->oPclZip->add(...$params); if ($res == 0) { throw new \Exception('Error zipping files : ' . $this->oPclZip->errorInfo(true)); } diff --git a/src/Common/Adapter/Zip/ZipArchiveAdapter.php b/src/Common/Adapter/Zip/ZipArchiveAdapter.php index 7636bf4..514a000 100644 --- a/src/Common/Adapter/Zip/ZipArchiveAdapter.php +++ b/src/Common/Adapter/Zip/ZipArchiveAdapter.php @@ -37,11 +37,14 @@ public function close() return $this; } - public function addFromString($localname, $contents) + public function addFromString(string $localname, string $contents, bool $withCompression = true) { if ($this->oZipArchive->addFromString($localname, $contents) === false) { throw new \Exception('Error zipping files : ' . $localname); } + if (!$withCompression) { + $this->oZipArchive->setCompressionName($localname, \ZipArchive::CM_STORE); + } return $this; } diff --git a/src/Common/Adapter/Zip/ZipInterface.php b/src/Common/Adapter/Zip/ZipInterface.php index 1e22790..c05888e 100644 --- a/src/Common/Adapter/Zip/ZipInterface.php +++ b/src/Common/Adapter/Zip/ZipInterface.php @@ -34,5 +34,5 @@ public function close(); * * @throws \Exception */ - public function addFromString($localname, $contents); + public function addFromString(string $localname, string $contents, bool $withCompression = true); } diff --git a/src/Common/Microsoft/PasswordEncoder.php b/src/Common/Microsoft/PasswordEncoder.php index a32d5af..192849d 100644 --- a/src/Common/Microsoft/PasswordEncoder.php +++ b/src/Common/Microsoft/PasswordEncoder.php @@ -114,7 +114,7 @@ class PasswordEncoder * * @return string */ - public static function hashPassword(string $password, string $algorithmName = self::ALGORITHM_SHA_1, string $salt = null, int $spinCount = 10000) + public static function hashPassword(string $password, string $algorithmName = self::ALGORITHM_SHA_1, ?string $salt = null, int $spinCount = 10000) { $origEncoding = mb_internal_encoding(); mb_internal_encoding('UTF-8'); diff --git a/src/Common/XMLReader.php b/src/Common/XMLReader.php index abca427..1b43c0b 100644 --- a/src/Common/XMLReader.php +++ b/src/Common/XMLReader.php @@ -108,7 +108,7 @@ public function getDomFromString(string $content) * * @return \DOMNodeList<\DOMElement> */ - public function getElements(string $path, \DOMElement $contextNode = null) + public function getElements(string $path, ?\DOMElement $contextNode = null) { if ($this->dom === null) { return new \DOMNodeList(); @@ -154,7 +154,7 @@ public function registerNamespace($prefix, $namespaceURI) * * @return \DOMElement|null */ - public function getElement($path, \DOMElement $contextNode = null): ?\DOMElement + public function getElement($path, ?\DOMElement $contextNode = null): ?\DOMElement { $elements = $this->getElements($path, $contextNode); if ($elements->length > 0) { @@ -173,7 +173,7 @@ public function getElement($path, \DOMElement $contextNode = null): ?\DOMElement * * @return string|null */ - public function getAttribute($attribute, \DOMElement $contextNode = null, $path = null) + public function getAttribute($attribute, ?\DOMElement $contextNode = null, ?string $path = null) { $return = null; if ($path !== null) { @@ -200,7 +200,7 @@ public function getAttribute($attribute, \DOMElement $contextNode = null, $path * * @return string|null */ - public function getValue($path, \DOMElement $contextNode = null) + public function getValue($path, ?\DOMElement $contextNode = null) { $elements = $this->getElements($path, $contextNode); if ($elements->length > 0) { @@ -218,7 +218,7 @@ public function getValue($path, \DOMElement $contextNode = null) * * @return int */ - public function countElements($path, \DOMElement $contextNode = null) + public function countElements($path, ?\DOMElement $contextNode = null) { $elements = $this->getElements($path, $contextNode); @@ -233,7 +233,7 @@ public function countElements($path, \DOMElement $contextNode = null) * * @return bool */ - public function elementExists($path, \DOMElement $contextNode = null) + public function elementExists($path, ?\DOMElement $contextNode = null) { return $this->getElements($path, $contextNode)->length > 0; } diff --git a/src/Common/XMLWriter.php b/src/Common/XMLWriter.php index 511022a..80aed37 100644 --- a/src/Common/XMLWriter.php +++ b/src/Common/XMLWriter.php @@ -53,7 +53,7 @@ class XMLWriter extends \XMLWriter * @param string $pTemporaryStorageDir Temporary storage folder * @param bool $compatibility */ - public function __construct($pTemporaryStorage = self::STORAGE_MEMORY, $pTemporaryStorageDir = null, $compatibility = false) + public function __construct(int $pTemporaryStorage = self::STORAGE_MEMORY, ?string $pTemporaryStorageDir = null, bool $compatibility = false) { // Open temporary storage if ($pTemporaryStorage == self::STORAGE_MEMORY) { @@ -121,7 +121,7 @@ public function getData() * * @return void */ - public function writeElementBlock(string $element, $attributes, string $value = null) + public function writeElementBlock(string $element, $attributes, ?string $value = null) { $this->startElement($element); if (!is_array($attributes)) { @@ -143,7 +143,7 @@ public function writeElementBlock(string $element, $attributes, string $value = * * @return void */ - public function writeElementIf(bool $condition, string $element, string $attribute = null, $value = null) + public function writeElementIf(bool $condition, string $element, ?string $attribute = null, $value = null) { if ($condition) { if (is_null($attribute)) { diff --git a/tests/Common/Tests/Adapter/Zip/AbstractZipAdapter.php b/tests/Common/Tests/Adapter/Zip/AbstractZipAdapter.php index af34b8c..955552b 100644 --- a/tests/Common/Tests/Adapter/Zip/AbstractZipAdapter.php +++ b/tests/Common/Tests/Adapter/Zip/AbstractZipAdapter.php @@ -15,7 +15,7 @@ abstract class AbstractZipAdapter extends \PHPUnit\Framework\TestCase /** * Returns a new instance of the adapter to test * - * @return \PhpOffice\Common\Adapter\Zip\ZipInterface + * @return ZipInterface */ abstract protected function createAdapter(): ZipInterface; @@ -50,17 +50,43 @@ public function testClose(): void $this->assertSame($adapter, $adapter->close()); } - public function testAddFromString(): void + public function testAddFromStringWithCompression(): void { - $expectedPath = 'file.test'; - $expectedContent = 'Content'; + $expectedPath = 'file.png'; + $expectedContent = file_get_contents( + PHPOFFICE_COMMON_TESTS_BASE_DIR + . DIRECTORY_SEPARATOR . 'resources' + . DIRECTORY_SEPARATOR . 'images' + . DIRECTORY_SEPARATOR . 'PHPPowerPointLogo.png' + ); $adapter = $this->createAdapter(); $adapter->open($this->zipTest); - $this->assertSame($adapter, $adapter->addFromString($expectedPath, $expectedContent)); + $this->assertSame($adapter, $adapter->addFromString($expectedPath, $expectedContent, true)); $adapter->close(); $this->assertTrue(TestHelperZip::assertFileExists($this->zipTest, $expectedPath)); + $this->assertTrue(TestHelperZip::assertFileIsCompressed($this->zipTest, $expectedPath)); + $this->assertTrue(TestHelperZip::assertFileContent($this->zipTest, $expectedPath, $expectedContent)); + } + + public function testAddFromStringWithNoCompression(): void + { + $expectedPath = 'file.png'; + $expectedContent = file_get_contents( + PHPOFFICE_COMMON_TESTS_BASE_DIR + . DIRECTORY_SEPARATOR . 'resources' + . DIRECTORY_SEPARATOR . 'images' + . DIRECTORY_SEPARATOR . 'PHPPowerPointLogo.png' + ); + + $adapter = $this->createAdapter(); + $adapter->open($this->zipTest); + $this->assertSame($adapter, $adapter->addFromString($expectedPath, $expectedContent, false)); + $adapter->close(); + + $this->assertTrue(TestHelperZip::assertFileExists($this->zipTest, $expectedPath)); + $this->assertFalse(TestHelperZip::assertFileIsCompressed($this->zipTest, $expectedPath)); $this->assertTrue(TestHelperZip::assertFileContent($this->zipTest, $expectedPath, $expectedContent)); } } diff --git a/tests/Common/Tests/_includes/TestHelperZip.php b/tests/Common/Tests/_includes/TestHelperZip.php index dc78e81..a2bd8b2 100644 --- a/tests/Common/Tests/_includes/TestHelperZip.php +++ b/tests/Common/Tests/_includes/TestHelperZip.php @@ -33,4 +33,16 @@ public static function assertFileContent(string $fileZip, string $path, string $ return true; } + + public static function assertFileIsCompressed(string $fileZip, string $path): bool + { + $oZip = new \ZipArchive(); + $oZip->open($fileZip); + $stat = $oZip->statName($path); + + // size: uncompressed + // comp_size: compressed + + return $stat['size'] > $stat['comp_size']; + } } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 24dd4a8..6b00a00 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -49,4 +49,4 @@ }); require_once __DIR__ . '/../src/Common/Autoloader.php'; -\PhpOffice\Common\Autoloader::register(); +PhpOffice\Common\Autoloader::register();