diff --git a/src/Common/XMLReader.php b/src/Common/XMLReader.php index 6c720ae..42da3c7 100644 --- a/src/Common/XMLReader.php +++ b/src/Common/XMLReader.php @@ -63,6 +63,17 @@ public function getDomFromZip($zipFile, $xmlFile) return $this->getDomFromString($content); } + /** + * Call libxml_disable_entity_loader, but only for PHP < 8 + * + * @param bool $value + * @return bool + */ + public static function libxmlDisable($value) + { + return version_compare(PHP_VERSION, '8') < 0 && libxml_disable_entity_loader($value); + } + /** * Get DOMDocument from content string * @@ -71,10 +82,10 @@ public function getDomFromZip($zipFile, $xmlFile) */ public function getDomFromString($content) { - $originalLibXMLEntityValue = libxml_disable_entity_loader(true); + $originalLibXMLEntityValue = self::libxmlDisable(true); $this->dom = new \DOMDocument(); $this->dom->loadXML($content); - libxml_disable_entity_loader($originalLibXMLEntityValue); + self::libxmlDisable($originalLibXMLEntityValue); return $this->dom; } diff --git a/src/Common/XMLWriter.php b/src/Common/XMLWriter.php index 4440d65..82fc0a0 100644 --- a/src/Common/XMLWriter.php +++ b/src/Common/XMLWriter.php @@ -86,7 +86,7 @@ public function __destruct() if (empty($this->tempFileName)) { return; } - if (PHP_OS != 'WINNT' && @unlink($this->tempFileName) === false) { + if ((version_compare(PHP_VERSION, '7.3') >= 0 || PHP_OS != 'WINNT') && @unlink($this->tempFileName) === false) { throw new \Exception('The file '.$this->tempFileName.' could not be deleted.'); } } @@ -167,17 +167,4 @@ public function writeAttributeIf($condition, $attribute, $value) $this->writeAttribute($attribute, $value); } } - - /** - * @param string $name - * @param mixed $value - * @return bool - */ - public function writeAttribute($name, $value) - { - if (is_float($value)) { - $value = json_encode($value); - } - return parent::writeAttribute($name, $value); - } } diff --git a/tests/Common/Tests/DrawingTest.php b/tests/Common/Tests/DrawingTest.php index bbcf4e3..911b887 100644 --- a/tests/Common/Tests/DrawingTest.php +++ b/tests/Common/Tests/DrawingTest.php @@ -115,7 +115,11 @@ public function testHTML() $this->assertFalse(Drawing::htmlToRGB('0000')); $this->assertFalse(Drawing::htmlToRGB('00000')); - $this->assertInternalType('array', Drawing::htmlToRGB('ABCDEF')); + if (method_exists($this, 'assertIsArray')) { + $this->assertIsArray(Drawing::htmlToRGB('ABCDEF')); + } else { + $this->assertInternalType('array', Drawing::htmlToRGB('ABCDEF')); + } $this->assertCount(3, Drawing::htmlToRGB('ABCDEF')); $this->assertEquals(array(0xAB, 0xCD, 0xEF), Drawing::htmlToRGB('ABCDEF')); $this->assertEquals(array(0xAB, 0xCD, 0xEF), Drawing::htmlToRGB('#ABCDEF')); diff --git a/tests/Common/Tests/FileTest.php b/tests/Common/Tests/FileTest.php index 439cc7b..e7135dc 100644 --- a/tests/Common/Tests/FileTest.php +++ b/tests/Common/Tests/FileTest.php @@ -42,9 +42,14 @@ public function testFileExists() public function testGetFileContents() { $pathResources = PHPOFFICE_COMMON_TESTS_BASE_DIR.DIRECTORY_SEPARATOR.'resources'.DIRECTORY_SEPARATOR; - $this->assertInternalType('string', File::fileGetContents($pathResources.'images'.DIRECTORY_SEPARATOR.'PHPPowerPointLogo.png')); $this->assertFalse(File::fileGetContents($pathResources.'images'.DIRECTORY_SEPARATOR.'PHPPowerPointLogo_404.png')); - $this->assertInternalType('string', File::fileGetContents('zip://'.$pathResources.'files'.DIRECTORY_SEPARATOR.'Sample_01_Simple.pptx#[Content_Types].xml')); + if (method_exists($this, 'assertIsString')) { + $this->assertIsString(File::fileGetContents($pathResources.'images'.DIRECTORY_SEPARATOR.'PHPPowerPointLogo.png')); + $this->assertIsString(File::fileGetContents('zip://'.$pathResources.'files'.DIRECTORY_SEPARATOR.'Sample_01_Simple.pptx#[Content_Types].xml')); + } else { + $this->assertInternalType('string', File::fileGetContents($pathResources.'images'.DIRECTORY_SEPARATOR.'PHPPowerPointLogo.png')); + $this->assertInternalType('string', File::fileGetContents('zip://'.$pathResources.'files'.DIRECTORY_SEPARATOR.'Sample_01_Simple.pptx#[Content_Types].xml')); + } $this->assertFalse(File::fileGetContents('zip://'.$pathResources.'files'.DIRECTORY_SEPARATOR.'Sample_01_Simple.pptx#404.xml')); $this->assertFalse(File::fileGetContents('zip://'.$pathResources.'files'.DIRECTORY_SEPARATOR.'404.pptx#404.xml')); } diff --git a/tests/Common/Tests/XMLReaderTest.php b/tests/Common/Tests/XMLReaderTest.php index b0acde7..e4c9346 100644 --- a/tests/Common/Tests/XMLReaderTest.php +++ b/tests/Common/Tests/XMLReaderTest.php @@ -58,10 +58,14 @@ public function testDomFromZip() /** * Test that read from non existing archive throws exception * - * @expectedException Exception */ public function testThrowsExceptionOnNonExistingArchive() { + if (method_exists($this, 'expectException')) { + $this->expectException('Exception'); + } else { + $this->setExpectedException('Exception'); + } $pathResources = PHPOFFICE_COMMON_TESTS_BASE_DIR.DIRECTORY_SEPARATOR.'resources'.DIRECTORY_SEPARATOR.'files'.DIRECTORY_SEPARATOR; $reader = new XMLReader(); @@ -125,10 +129,14 @@ public function testShouldParseXmlWithCustomNamespace() /** * Test that xpath fails if custom namespace is not registered * - * @expectedException InvalidArgumentException */ public function testShouldThowExceptionIfTryingToRegisterNamespaceBeforeReadingDoc() { + if (method_exists($this, 'expectException')) { + $this->expectException('InvalidArgumentException'); + } else { + $this->setExpectedException('InvalidArgumentException'); + } $reader = new XMLReader(); $reader->registerNamespace('test', 'http://phpword.com/my/custom/namespace'); } diff --git a/tests/Common/Tests/XMLWriterTest.php b/tests/Common/Tests/XMLWriterTest.php index 2ff2d2c..aaf53de 100644 --- a/tests/Common/Tests/XMLWriterTest.php +++ b/tests/Common/Tests/XMLWriterTest.php @@ -54,6 +54,45 @@ public function testWriteAttribute() $this->assertSame('' . chr(10), $xmlWriter->getData()); } + public function testWriteAttributeIf() + { + $xmlWriter = new XMLWriter(); + $xmlWriter->startElement('element'); + $xmlWriter->writeAttributeIf(true, 'trueAttr', '1'); + $xmlWriter->writeAttributeIf(false, 'falseAttr', '0'); + $xmlWriter->endElement(); + + $this->assertSame('' . chr(10), $xmlWriter->getData()); + } + + public function testWriteElementIf() + { + $xmlWriter = new XMLWriter(); + $xmlWriter->writeElementIf(true, 'trueElement', null, '1'); + $xmlWriter->writeElementIf(false, 'falseElement', null, '0'); + $xmlWriter->writeElementIf(true, 'trueElementAttr', 'trueAttr', '2'); + $xmlWriter->writeElementIf(false, 'falseElementAttr', 'falseAttr', '0'); + + $this->assertSame('1' . chr(10) + . '' . chr(10), $xmlWriter->getData()); + } + + public function testWriteElementBlockString() + { + $xmlWriter = new XMLWriter(); + $xmlWriter->writeElementBlock('element', 'name', 'value'); + + $this->assertSame('' . chr(10), $xmlWriter->getData()); + } + + public function testWriteElementBlockArray() + { + $xmlWriter = new XMLWriter(); + $xmlWriter->writeElementBlock('element', array('name1' => 'value1', 'name2' => 'value2')); + + $this->assertSame('' . chr(10), $xmlWriter->getData()); + } + public function testWriteAttributeShouldWriteFloatValueLocaleIndependent() { $value = 1.2; @@ -65,7 +104,33 @@ public function testWriteAttributeShouldWriteFloatValueLocaleIndependent() setlocale(LC_NUMERIC, 'de_DE.UTF-8', 'de'); - $this->assertSame('1,2', (string)$value); + $this->assertSame((version_compare(PHP_VERSION, 8) < 0) ? '1,2' : '1.2', (string)$value); $this->assertSame('' . chr(10), $xmlWriter->getData()); } + + public function testCompatibilityTrue() + { + $xmlWriter = new XMLWriter(\PhpOffice\Common\XMLWriter::STORAGE_MEMORY, null, true); + $xmlWriter->startElement('element1'); + $xmlWriter->startElement('element2'); + $xmlWriter->endElement(); + $xmlWriter->endElement(); + + $this->assertSame('' + . '' + . '', $xmlWriter->getData()); + } + + public function testCompatibilityFalse() + { + $xmlWriter = new XMLWriter(\PhpOffice\Common\XMLWriter::STORAGE_MEMORY, null, false); + $xmlWriter->startElement('element1'); + $xmlWriter->startElement('element2'); + $xmlWriter->endElement(); + $xmlWriter->endElement(); + + $this->assertSame('' . chr(10) + . ' ' . chr(10) + . '' . chr(10), $xmlWriter->getData()); + } }