Skip to content
This repository has been archived by the owner on Dec 5, 2024. It is now read-only.

Commit

Permalink
Merge pull request #8 from Slamdunk/php_80_support
Browse files Browse the repository at this point in the history
Add PHP 8.0 support
  • Loading branch information
Ocramius authored Jan 11, 2021
2 parents 2234e1f + 2b668b8 commit 65c5e46
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 65 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
/docs/html/
/laminas-mkdoc-theme.tgz
/laminas-mkdoc-theme/
/.phpunit.result.cache
/phpunit.xml
/vendor/
24 changes: 6 additions & 18 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,24 @@ env:
matrix:
fast_finish: true
include:
- php: 5.6
env:
- DEPS=lowest
- php: 5.6
env:
- DEPS=latest
- php: 7
env:
- DEPS=lowest
- php: 7
env:
- DEPS=latest
- php: 7.1
- php: 7.3
env:
- DEPS=lowest
- php: 7.1
- php: 7.3
env:
- DEPS=latest
- CS_CHECK=true
- TEST_COVERAGE=true
- php: 7.2
- php: 7.4
env:
- DEPS=lowest
- php: 7.2
- php: 7.4
env:
- DEPS=latest
- php: 7.3
- php: 8.0
env:
- DEPS=lowest
- php: 7.3
- php: 8.0
env:
- DEPS=latest

Expand Down
8 changes: 5 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@
}
},
"require": {
"php": "^5.6 || ^7.0",
"laminas/laminas-zendframework-bridge": "^1.0"
"php": "^7.3 || ~8.0.0",
"ext-dom": "*",
"ext-libxml": "*",
"laminas/laminas-zendframework-bridge": "^1.1"
},
"require-dev": {
"laminas/laminas-coding-standard": "~1.0.0",
"phpunit/phpunit": "^5.7.23 || ^6.4.3"
"phpunit/phpunit": "^9.4.3"
},
"autoload": {
"psr-4": {
Expand Down
27 changes: 13 additions & 14 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true">
<testsuites>
<testsuite name="laminas-dom Test Suite">
<directory>./test</directory>
</testsuite>
</testsuites>

<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./src</directory>
</include>
</coverage>
<testsuites>
<testsuite name="laminas-dom Test Suite">
<directory>./test</directory>
</testsuite>
</testsuites>
</phpunit>
22 changes: 20 additions & 2 deletions src/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ protected function setErrors($errors)
protected function getDomDocumentFromString($stringDocument)
{
libxml_use_internal_errors(true);
libxml_disable_entity_loader(true);
$disableEntityLoaderFlag = self::disableEntityLoader();

$encoding = $this->getEncoding();
$domDoc = null === $encoding ? new DOMDocument('1.0') : new DOMDocument('1.0', $encoding);
Expand Down Expand Up @@ -261,7 +261,7 @@ protected function getDomDocumentFromString($stringDocument)
libxml_clear_errors();
}

libxml_disable_entity_loader(false);
self::disableEntityLoader($disableEntityLoaderFlag);
libxml_use_internal_errors(false);

if (! $success) {
Expand Down Expand Up @@ -311,4 +311,22 @@ public function registerXpathPhpFunctions($xpathPhpFunctions = true)
{
$this->xpathPhpFunctions = $xpathPhpFunctions;
}

/**
* Disable the ability to load external XML entities based on libxml version
*
* If we are using libxml < 2.9, unsafe XML entity loading must be
* disabled with a flag.
*
* If we are using libxml >= 2.9, XML entity loading is disabled by default.
*
* @return bool
*/
private static function disableEntityLoader($flag = true)
{
if (LIBXML_VERSION < 20900) {
return libxml_disable_entity_loader($flag);
}
return $flag;
}
}
22 changes: 20 additions & 2 deletions src/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ public function queryXpath($xpathQuery, $query = null, DOMNode $contextNode = nu

$encoding = $this->getEncoding();
libxml_use_internal_errors(true);
libxml_disable_entity_loader(true);
$disableEntityLoaderFlag = self::disableEntityLoader();
if (null === $encoding) {
$domDoc = new DOMDocument('1.0');
} else {
Expand Down Expand Up @@ -261,7 +261,7 @@ public function queryXpath($xpathQuery, $query = null, DOMNode $contextNode = nu
$this->documentErrors = $errors;
libxml_clear_errors();
}
libxml_disable_entity_loader(false);
self::disableEntityLoader($disableEntityLoaderFlag);
libxml_use_internal_errors(false);

if (! $success) {
Expand Down Expand Up @@ -320,4 +320,22 @@ protected function getNodeList($document, $xpathQuery, DOMNode $contextNode = nu
$nodeList = $xpath->queryWithErrorException($xpathQuery, $contextNode);
return $nodeList;
}

/**
* Disable the ability to load external XML entities based on libxml version
*
* If we are using libxml < 2.9, unsafe XML entity loading must be
* disabled with a flag.
*
* If we are using libxml >= 2.9, XML entity loading is disabled by default.
*
* @return bool
*/
private static function disableEntityLoader($flag = true)
{
if (LIBXML_VERSION < 20900) {
return libxml_disable_entity_loader($flag);
}
return $flag;
}
}
2 changes: 1 addition & 1 deletion test/DOMXPathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class DOMXPathTest extends TestCase
/** @var DOMDocument */
private $document;

protected function setUp()
protected function setUp(): void
{
$this->document = new DOMDocument('<any></any>');
}
Expand Down
2 changes: 1 addition & 1 deletion test/Document/NodeListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class NodeListTest extends TestCase
/** @var NodeList|DOMNode[] */
private $nodeList;

protected function setUp()
protected function setUp(): void
{
$document = new DOMDocument();
$document->loadHTML('<html><body><a></a><b></b></body></html>');
Expand Down
18 changes: 9 additions & 9 deletions test/Document/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class QueryTest extends TestCase
public function testTransformShouldReturnStringByDefault()
{
$test = Query::cssToXpath('');
$this->assertInternalType('string', $test);
$this->assertIsString($test);
}

/**
Expand All @@ -28,8 +28,8 @@ public function testTransformShouldReturnStringByDefault()
public function testTransformShouldReturnMultiplePathsWhenExpressionContainsCommas()
{
$test = Query::cssToXpath('#foo, #bar');
$this->assertInternalType('string', $test);
$this->assertContains('|', $test);
$this->assertIsString($test);
$this->assertStringContainsString('|', $test);
$this->assertCount(2, explode('|', $test));
}

Expand All @@ -48,13 +48,13 @@ public function testTransformShouldRecognizeDotSymbolAsClass()
public function testTransformShouldAssumeSpacesToIndicateRelativeXpathQueries()
{
$test = Query::cssToXpath('div#foo .bar');
$this->assertContains('|', $test);
$this->assertStringContainsString('|', $test);
$expected = [
"//div[@id='foo']//*[contains(concat(' ', normalize-space(@class), ' '), ' bar ')]",
"//div[@id='foo'][contains(concat(' ', normalize-space(@class), ' '), ' bar ')]",
];
foreach ($expected as $path) {
$this->assertContains($path, $test);
$this->assertStringContainsString($path, $test);
}
}

Expand All @@ -70,8 +70,8 @@ public function testTransformShouldWriteChildSelectorsAsAbsoluteXpathRelations()
public function testMultipleComplexCssSpecificationShouldTransformToExpectedXpath()
{
$test = Query::cssToXpath('div#foo span.bar, #bar li.baz a');
$this->assertInternalType('string', $test);
$this->assertContains('|', $test);
$this->assertIsString($test);
$this->assertStringContainsString('|', $test);
$actual = explode('|', $test);
$expected = [
"//div[@id='foo']//span[contains(concat(' ', normalize-space(@class), ' '), ' bar ')]",
Expand All @@ -86,7 +86,7 @@ public function testMultipleComplexCssSpecificationShouldTransformToExpectedXpat
public function testClassNotationWithoutSpecifiedTagShouldResultInMultipleQueries()
{
$test = Query::cssToXpath('div.foo .bar a .baz span');
$this->assertContains('|', $test);
$this->assertStringContainsString('|', $test);
// @codingStandardsIgnoreStart
$segments = [
"//div[contains(concat(' ', normalize-space(@class), ' '), ' foo ')]//*[contains(concat(' ', normalize-space(@class), ' '), ' bar ')]//a//*[contains(concat(' ', normalize-space(@class), ' '), ' baz ')]//span",
Expand All @@ -96,7 +96,7 @@ public function testClassNotationWithoutSpecifiedTagShouldResultInMultipleQuerie
];
// @codingStandardsIgnoreEnd
foreach ($segments as $xpath) {
$this->assertContains($xpath, $test);
$this->assertStringContainsString($xpath, $test);
}
}

Expand Down
26 changes: 11 additions & 15 deletions test/DocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@ class DocumentTest extends TestCase
/**
* Sets up the fixture, for example, open a network connection.
* This method is called before a test is executed.
*
* @return void
*/
public function setUp()
public function setUp(): void
{
$this->document = new Document();
}
Expand Down Expand Up @@ -233,17 +231,15 @@ public function testXpathPhpFunctionsShouldBeEnabledWithoutParameter()
public function testXpathPhpFunctionsShouldBeNotCalledWhenSpecifiedFunction()
{
$this->loadHtml();
try {
$this->document->registerXpathPhpFunctions('stripos');
$result = Document\Query::execute(
'//meta[php:functionString("strtolower", @http-equiv) = "content-type"]',
$this->document
);
} catch (\Exception $e) {
// $e->getMessage() - Not allowed to call handler 'strtolower()
return;
}
$this->fail('Not allowed to call handler strtolower()');
$this->document->registerXpathPhpFunctions('stripos');

$this->expectException(PHP_VERSION_ID >= 80000 ? \Error::class : \ErrorException::class);
$this->expectExceptionMessageMatches('/Not allowed to call handler .strtolower()/');

Document\Query::execute(
'//meta[php:functionString("strtolower", @http-equiv) = "content-type"]',
$this->document
);
}

/**
Expand All @@ -255,7 +251,7 @@ public function testLoadingDocumentWithErrorsShouldNotRaisePhpErrors()
$this->document = new Document($file);
$result = Document\Query::execute('p', $this->document, Document\Query::TYPE_CSS);
$errors = $this->document->getErrors();
$this->assertInternalType('array', $errors);
$this->assertIsArray($errors);
$this->assertNotEmpty($errors);
}

Expand Down

0 comments on commit 65c5e46

Please sign in to comment.