99use DMT \XmlParser \Source \StringParser ;
1010use DMT \XmlParser \Tokenizer ;
1111use DMT \XmlParser \Tokenizer \XmlParserTokenizer ;
12+ use DMT \XmlParser \Tokenizer \XmlReaderTokenizer ;
1213use PHPUnit \Framework \TestCase ;
1314
1415class 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