Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compatibility Changes for PHP8 and Newer Releases of PhpUnit #35

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/Common/XMLReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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;
}
Expand Down
15 changes: 1 addition & 14 deletions src/Common/XMLWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
}
}
Expand Down Expand Up @@ -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);
}
}
6 changes: 5 additions & 1 deletion tests/Common/Tests/DrawingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
9 changes: 7 additions & 2 deletions tests/Common/Tests/FileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
Expand Down
12 changes: 10 additions & 2 deletions tests/Common/Tests/XMLReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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');
}
Expand Down
67 changes: 66 additions & 1 deletion tests/Common/Tests/XMLWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,45 @@ public function testWriteAttribute()
$this->assertSame('<element name="value"/>' . 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('<element trueAttr="1"/>' . 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('<trueElement>1</trueElement>' . chr(10)
. '<trueElementAttr trueAttr="2"/>' . chr(10), $xmlWriter->getData());
}

public function testWriteElementBlockString()
{
$xmlWriter = new XMLWriter();
$xmlWriter->writeElementBlock('element', 'name', 'value');

$this->assertSame('<element name="value"/>' . chr(10), $xmlWriter->getData());
}

public function testWriteElementBlockArray()
{
$xmlWriter = new XMLWriter();
$xmlWriter->writeElementBlock('element', array('name1' => 'value1', 'name2' => 'value2'));

$this->assertSame('<element name1="value1" name2="value2"/>' . chr(10), $xmlWriter->getData());
}

public function testWriteAttributeShouldWriteFloatValueLocaleIndependent()
{
$value = 1.2;
Expand All @@ -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('<element name="1.2"/>' . 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('<element1>'
. '<element2/>'
. '</element1>', $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('<element1>' . chr(10)
. ' <element2/>' . chr(10)
. '</element1>' . chr(10), $xmlWriter->getData());
}
}