Skip to content

Commit

Permalink
Added test
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshyPHP committed Dec 29, 2023
1 parent 4098baf commit d992e67
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 12 deletions.
34 changes: 22 additions & 12 deletions src/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,9 @@ public function __construct(string $version = '1.0', string $encoding = '')
parent::__construct($version, $encoding);

$this->nodeCreator = new NodeCreator($this);

$classes = ['Attr', 'CdataSection', 'Comment', 'DocumentFragment', 'Element', 'Text'];
$namespace = $this->getNodesNamespace();
foreach ($classes as $className)
foreach ($this->getExtendedClassMap() as $baseClass => $extendedClass)
{
$this->registerNodeClass('DOM' . $className, $namespace . '\\' . $className);
$this->registerNodeClass($baseClass, $extendedClass);
}
}

Expand Down Expand Up @@ -85,36 +82,49 @@ public function query(string $expression, ?DOMNode $contextNode = null, bool $re
return $result;
}

protected function getNodesNamespace(): string
protected function getExtendedClassMap(): array
{
$baseNames = ['Attr', 'CdataSection', 'Comment', 'DocumentFragment', 'Element', 'Text'];
$classMap = [];
$namespace = $this->getExtendedNamespace(PHP_VERSION);
foreach ($baseNames as $baseName)
{
$classMap['DOM' . $baseName] = $namespace . '\\' . $baseName;
}

return $classMap;
}

protected function getExtendedNamespace(string $phpVersion): string
{
$namespace = __NAMESPACE__;
if ($this->needsWorkarounds())
if ($this->needsWorkarounds($phpVersion))
{
$namespace .= '\\PatchedNodes';
}
elseif (version_compare(PHP_VERSION, '8.3.0', '<'))
elseif (version_compare($phpVersion, '8.3.0', '<'))
{
$namespace .= '\\ForwardCompatibleNodes';
}

return $namespace;
}

protected function needsWorkarounds(): bool
protected function needsWorkarounds(string $phpVersion): bool
{
if (version_compare(PHP_VERSION, '8.2.10', '>='))
if (version_compare($phpVersion, '8.2.10', '>='))
{
// PHP ^8.2.10 needs no workarounds
return false;
}
if (version_compare(PHP_VERSION, '8.1.23', '<'))
if (version_compare($phpVersion, '8.1.23', '<'))
{
// Anything older than 8.1.23 does
return true;
}

// ~8.1.23 is okay, anything between 8.2.0 and 8.2.9 needs workarounds
return version_compare(PHP_VERSION, '8.2.0-dev', '>=');
return version_compare($phpVersion, '8.2.0-dev', '>=');
}

/**
Expand Down
117 changes: 117 additions & 0 deletions tests/DocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace s9e\SweetDOM\Tests;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use s9e\SweetDOM\Document;

Expand Down Expand Up @@ -127,4 +128,120 @@ public function testFirstOfError()

@$dom->firstOf('x x');
}

#[DataProvider('getClassMapsTestCases')]
public function testClassMaps(string $phpVersion, array $expected): void
{
$dom = new class(phpVersion: $phpVersion) extends Document
{
public function __construct(string $version = '1.0', string $encoding = '', public string $phpVersion = '')
{
parent::__construct();
}
public function getExtendedClassMap(): array
{
return parent::getExtendedClassMap();
}
public function getExtendedNamespace(string $phpVersion): string
{
return parent::getExtendedNamespace($this->phpVersion);
}
};

$this->assertEquals($expected, $dom->getExtendedClassMap());
}

public static function getClassMapsTestCases(): array
{
return [
[
'8.1.2',
[
'DOMAttr' => 's9e\SweetDOM\PatchedNodes\Attr',
'DOMCdataSection' => 's9e\SweetDOM\PatchedNodes\CdataSection',
'DOMComment' => 's9e\SweetDOM\PatchedNodes\Comment',
'DOMDocumentFragment' => 's9e\SweetDOM\PatchedNodes\DocumentFragment',
'DOMElement' => 's9e\SweetDOM\PatchedNodes\Element',
'DOMText' => 's9e\SweetDOM\PatchedNodes\Text'
]
],
[
'8.1.22',
[
'DOMAttr' => 's9e\SweetDOM\PatchedNodes\Attr',
'DOMCdataSection' => 's9e\SweetDOM\PatchedNodes\CdataSection',
'DOMComment' => 's9e\SweetDOM\PatchedNodes\Comment',
'DOMDocumentFragment' => 's9e\SweetDOM\PatchedNodes\DocumentFragment',
'DOMElement' => 's9e\SweetDOM\PatchedNodes\Element',
'DOMText' => 's9e\SweetDOM\PatchedNodes\Text'
]
],
[
'8.1.23',
[
'DOMAttr' => 's9e\SweetDOM\ForwardCompatibleNodes\Attr',
'DOMCdataSection' => 's9e\SweetDOM\ForwardCompatibleNodes\CdataSection',
'DOMComment' => 's9e\SweetDOM\ForwardCompatibleNodes\Comment',
'DOMDocumentFragment' => 's9e\SweetDOM\ForwardCompatibleNodes\DocumentFragment',
'DOMElement' => 's9e\SweetDOM\ForwardCompatibleNodes\Element',
'DOMText' => 's9e\SweetDOM\ForwardCompatibleNodes\Text'
]
],
[
'8.2.0',
[
'DOMAttr' => 's9e\SweetDOM\PatchedNodes\Attr',
'DOMCdataSection' => 's9e\SweetDOM\PatchedNodes\CdataSection',
'DOMComment' => 's9e\SweetDOM\PatchedNodes\Comment',
'DOMDocumentFragment' => 's9e\SweetDOM\PatchedNodes\DocumentFragment',
'DOMElement' => 's9e\SweetDOM\PatchedNodes\Element',
'DOMText' => 's9e\SweetDOM\PatchedNodes\Text'
]
],
[
'8.2.9',
[
'DOMAttr' => 's9e\SweetDOM\PatchedNodes\Attr',
'DOMCdataSection' => 's9e\SweetDOM\PatchedNodes\CdataSection',
'DOMComment' => 's9e\SweetDOM\PatchedNodes\Comment',
'DOMDocumentFragment' => 's9e\SweetDOM\PatchedNodes\DocumentFragment',
'DOMElement' => 's9e\SweetDOM\PatchedNodes\Element',
'DOMText' => 's9e\SweetDOM\PatchedNodes\Text'
]
],
[
'8.2.10',
[
'DOMAttr' => 's9e\SweetDOM\ForwardCompatibleNodes\Attr',
'DOMCdataSection' => 's9e\SweetDOM\ForwardCompatibleNodes\CdataSection',
'DOMComment' => 's9e\SweetDOM\ForwardCompatibleNodes\Comment',
'DOMDocumentFragment' => 's9e\SweetDOM\ForwardCompatibleNodes\DocumentFragment',
'DOMElement' => 's9e\SweetDOM\ForwardCompatibleNodes\Element',
'DOMText' => 's9e\SweetDOM\ForwardCompatibleNodes\Text'
]
],
[
'8.3.0',
[
'DOMAttr' => 's9e\SweetDOM\Attr',
'DOMCdataSection' => 's9e\SweetDOM\CdataSection',
'DOMComment' => 's9e\SweetDOM\Comment',
'DOMDocumentFragment' => 's9e\SweetDOM\DocumentFragment',
'DOMElement' => 's9e\SweetDOM\Element',
'DOMText' => 's9e\SweetDOM\Text'
]
],
[
'8.4.0',
[
'DOMAttr' => 's9e\SweetDOM\Attr',
'DOMCdataSection' => 's9e\SweetDOM\CdataSection',
'DOMComment' => 's9e\SweetDOM\Comment',
'DOMDocumentFragment' => 's9e\SweetDOM\DocumentFragment',
'DOMElement' => 's9e\SweetDOM\Element',
'DOMText' => 's9e\SweetDOM\Text'
]
],
];
}
}

0 comments on commit d992e67

Please sign in to comment.