Skip to content

Commit a4da82e

Browse files
committed
Merge branch 'release/2.0.2'
2 parents 67cf717 + 6bbd924 commit a4da82e

File tree

4 files changed

+39
-4
lines changed

4 files changed

+39
-4
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# CHANGELOG
22

3+
## 2.0.2 - 2021/01/30
4+
- issue #2 - DOMXPath::query(): Node From Wrong Document
5+
36
## 2.0.1 - 2021/01/28
47
- Minor readme and Travis CI updates
58

src/AbstractDomDocumentHandler.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public function getNodesByName(string $name, ?string $checkInstance = null): arr
8080
$nodes = array();
8181
if ($this->domDocument->getElementsByTagName($name)->length > 0) {
8282
foreach ($this->domDocument->getElementsByTagName($name) as $node) {
83-
if ($checkInstance === null || $node instanceof $checkInstance) {
83+
if (is_null($checkInstance) || $node instanceof $checkInstance) {
8484
$nodes[] = $this->getHandler($node, count($nodes));
8585
}
8686
}
@@ -91,14 +91,15 @@ public function getNodesByName(string $name, ?string $checkInstance = null): arr
9191

9292
public function getElementsByName(string $name): array
9393
{
94-
return $this->getNodesByName($name, 'DOMElement');
94+
return $this->getNodesByName($name, DOMElement::class);
9595
}
9696

9797
public function getElementsByNameAndAttributes(string $name, array $attributes, ?DOMNode $node = null): array
9898
{
9999
$matchingElements = $this->getElementsByName($name);
100100
if ((!empty($attributes) || $node instanceof DOMNode) && !empty($matchingElements)) {
101101
$nodes = $this->searchTagsByXpath($name, $attributes, $node);
102+
102103
if (!empty($nodes)) {
103104
$matchingElements = $this->getElementsHandlers($nodes);
104105
}
@@ -125,7 +126,7 @@ public function getElementsHandlers(DOMNodeList $nodeList): array
125126

126127
public function searchTagsByXpath(string $name, array $attributes, ?DOMNode $node = null): DOMNodeList
127128
{
128-
$xpath = new DOMXPath($this->domDocument);
129+
$xpath = new DOMXPath($node ? $node->ownerDocument : $this->domDocument);
129130
$xQuery = sprintf("%s//*[local-name()='%s']", $node instanceof DOMNode ? '.' : '', $name);
130131
foreach ($attributes as $attributeName => $attributeValue) {
131132
if (strpos($attributeValue, '*') !== false) {
@@ -142,6 +143,6 @@ public function getElementByNameAndAttributes(string $name, array $attributes):
142143
{
143144
$elements = $this->getElementsByNameAndAttributes($name, $attributes);
144145

145-
return empty($elements) ? null : array_shift($elements);
146+
return array_shift($elements);
146147
}
147148
}

tests/DomDocumentHandlerTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class DomDocumentHandlerTest extends TestCase
1717
protected static ?DomDocumentHandler $bingInstance;
1818
protected static ?DomDocumentHandler $emptyInstance;
1919
protected static ?DomDocumentHandler $yandexDirectApiAdGroupsInstance;
20+
protected static ?DomDocumentHandler $yandexDirectApiGeneralInstance;
2021

2122
public static function actonInstance(): DomDocumentHandler
2223
{
@@ -58,6 +59,16 @@ public static function yandexDirectApiAdGroupsInstance(): DomDocumentHandler
5859
return self::$yandexDirectApiAdGroupsInstance;
5960
}
6061

62+
public static function yandexDirectApiGeneralInstance(): DomDocumentHandler
63+
{
64+
if (!isset(self::$yandexDirectApiGeneralInstance)) {
65+
$doc = new DOMDocument('1.0', 'utf-8');
66+
$doc->load(self::wsdlYandexDirectApiGeneralPath());
67+
self::$yandexDirectApiGeneralInstance = new DomDocumentHandler($doc);
68+
}
69+
return self::$yandexDirectApiGeneralInstance;
70+
}
71+
6172
public function testGetNodeByName()
6273
{
6374
$instance = self::bingInstance();
@@ -103,6 +114,21 @@ public function testGetElementsByNameAndAttributes()
103114
$this->assertContainsOnlyInstancesOf(ElementHandler::class, $parts);
104115
}
105116

117+
public function testGetElementsByNameAndAttributesFromDomNode()
118+
{
119+
$instance = self::yandexDirectApiAdGroupsInstance();
120+
$xsd = self::yandexDirectApiGeneralInstance();
121+
122+
$elements = $instance->getElementsByNameAndAttributes('element', array(
123+
'minOccurs' => 1,
124+
'maxOccurs' => 1,
125+
), $xsd->getElementByNameAndAttributes('complexType', [
126+
'name' => 'ExceptionNotification',
127+
])->getNode());
128+
$this->assertCount(2, $elements);
129+
$this->assertContainsOnlyInstancesOf(ElementHandler::class, $elements);
130+
}
131+
106132
public function testGetElementByNameAndAttributes()
107133
{
108134
$instance = self::bingInstance();

tests/TestCase.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ public static function wsdlYandexDirectApiAdGroupsPath(): string
3333
return __DIR__ . '/resources/directapi/adgroups.wsdl';
3434
}
3535

36+
public static function wsdlYandexDirectApiGeneralPath(): string
37+
{
38+
return __DIR__ . '/resources/directapi/general.xsd';
39+
}
40+
3641
public static function wsdlYandexDirectApiLivePath(): string
3742
{
3843
return __DIR__ . '/resources/directapi/live.wsdl';

0 commit comments

Comments
 (0)