Skip to content

Commit e0c9af7

Browse files
committed
Added test case for xml reader
1 parent 712daac commit e0c9af7

2 files changed

Lines changed: 51 additions & 4 deletions

File tree

src/Tokenizer/XmlReaderTokenizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function tokenize(): Generator
6767
private function renderAttributes(Element $element): void
6868
{
6969
while ($this->reader->moveToNextAttribute()) {
70-
if ($this->flags & self::XML_DROP_NAMESPACES && strpos($this->reader->name, 'xmlns') === 0) {
70+
if (($this->flags & self::XML_DROP_NAMESPACES) && strpos($this->reader->name, 'xmlns') === 0) {
7171
continue;
7272
}
7373

tests/ParserTest.php

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use DMT\XmlParser\Source\StringParser;
1010
use DMT\XmlParser\Tokenizer;
1111
use DMT\XmlParser\Tokenizer\XmlParserTokenizer;
12+
use DMT\XmlParser\Tokenizer\XmlReaderTokenizer;
1213
use PHPUnit\Framework\TestCase;
1314

1415
class ParserTest extends TestCase
@@ -61,7 +62,28 @@ public function testParseXmlWithInheritNamespaces()
6162
$this->assertStringContainsString('xmlns:x="alt-lang"', $book);
6263
}
6364

64-
public function testDropNamespaces()
65+
public function testReadXmlWithInheritNamespaces()
66+
{
67+
$xml = '<books xmlns:ns1="urn:ns-uri" xmlns:x="lang">
68+
<ns1:book xmlns:x="alt-lang">
69+
<ns1:title x:lang="en_US">A title</ns1:title>
70+
<author/>
71+
</ns1:book>
72+
</books>';
73+
74+
$parser = new Parser(new XmlReaderTokenizer(new StringParser($xml)));
75+
76+
while ($element = $parser->parse()) {
77+
if ($element->localName === 'book') {
78+
$book = $parser->parseXml();
79+
}
80+
}
81+
82+
$this->assertStringContainsString('xmlns:ns1="urn:ns-uri"', $book);
83+
$this->assertStringContainsString('xmlns:x="alt-lang"', $book);
84+
}
85+
86+
public function testParseXmlWithoutNamespaces()
6587
{
6688
$xml = '<book xmlns="http://example.org/ns" xmlns:ns1="http://example.org/ns">
6789
<ns1:title>A title</ns1:title>
@@ -74,7 +96,20 @@ public function testDropNamespaces()
7496
$this->assertStringNotContainsString('xmlns', $xml);
7597
}
7698

77-
public function testUseCData()
99+
public function testReadXmlWithoutNamespaces()
100+
{
101+
$xml = '<book xmlns="http://example.org/ns" xmlns:ns1="http://example.org/ns">
102+
<ns1:title>A title</ns1:title>
103+
<ns1:author/>
104+
</book>';
105+
106+
$xml = (new Parser(new XmlReaderTokenizer(new StringParser($xml), null, Tokenizer::XML_DROP_NAMESPACES)))->parseXml();
107+
108+
$this->assertStringNotContainsString('ns1', $xml);
109+
$this->assertStringNotContainsString('xmlns', $xml);
110+
}
111+
112+
public function testParseXmlWithCData()
78113
{
79114
$xml = '<book><title>A title</title><author/></book>';
80115

@@ -83,6 +118,18 @@ public function testUseCData()
83118
$element = $parser->parse();
84119
} while (!$element instanceof Text);
85120

86-
$this->assertMatchesRegularExpression('~\<\!\[CDATA\[.*\]\]\>~', strval($element));
121+
$this->assertMatchesRegularExpression('~<!\[CDATA\[.*]]>~', strval($element));
122+
}
123+
124+
public function testPReadXmlWithCData()
125+
{
126+
$xml = '<book><title>A title</title><author/></book>';
127+
128+
$parser = new Parser(new XmlReaderTokenizer(new StringParser($xml), null, Tokenizer::XML_USE_CDATA));
129+
do {
130+
$element = $parser->parse();
131+
} while (!$element instanceof Text);
132+
133+
$this->assertMatchesRegularExpression('~<!\[CDATA\[.*]]>~', strval($element));
87134
}
88135
}

0 commit comments

Comments
 (0)