Skip to content

Commit

Permalink
Add the XMLParserCDataTest test class (resolves #4)
Browse files Browse the repository at this point in the history
  • Loading branch information
macbre committed Aug 11, 2023
1 parent 0679b75 commit 951e73b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/XMLParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public function setUp(): void {

// @see https://www.php.net/manual/en/function.xml-parser-set-option.php
xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false);
xml_parser_set_option($this->parser, XML_OPTION_SKIP_WHITE, true);
}

private function close(): void {
Expand Down
42 changes: 42 additions & 0 deletions tests/XMLParserCDataTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

use Elecena\XmlIterator\Nodes\XMLNodeContent;

class XMLParserCDataTest extends XMLParserTestCase
{
protected function getParserStream() {
return self::streamFromString(<<<XML
<?xml version="1.0" encoding="UTF-8"?>
<item>
<title>Title of Feed Item</title>
<link>/mylink/article1</link>
<description>
<![CDATA[
<p>
<a href="/mylink/article1"><img style="float: left; margin-right: 5px;" height="80" src="/mylink/image" alt=""/></a>
Author Names
<br/><em>Date</em>
<br/>Paragraph of text describing the article to be displayed</p>
]]>
</description>
</item>
XML);
}

public function testCDataIsProperlyParsed(): void
{
$node = null;

foreach ($this->getParser() as $item) {
if ($item instanceof XMLNodeContent && $item->tagName === 'description') {
if (trim($item->tagContent) !== '') {
$node = $item;
break;
}
}
}

$this->assertInstanceOf(XMLNodeContent::class, $node);
$this->assertStringStartsWith("<p>\n <a href=\"/mylink/article1\">", trim($node->tagContent));
}
}
6 changes: 3 additions & 3 deletions tests/XMLParserWhitespacesTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

use Elecena\XmlIterator\Exceptions\ParsingError;
use Elecena\XmlIterator\Nodes\XMLNodeContent;

class XMLParserWhitespacesTest extends XMLParserTestCase
{
Expand All @@ -22,12 +22,12 @@ public function testProperlyReportWhitespacesBetweenClosingTags(): void
$locNodesContent = [];

foreach ($this->getParser() as $item) {
if ($item instanceof \Elecena\XmlIterator\Nodes\XMLNodeContent && $item->tagName === 'loc') {
if ($item instanceof XMLNodeContent && $item->tagName === 'loc') {
$locNodesContent[] = $item;
}
}

$this->assertCount(1, $locNodesContent);
$this->assertEquals('https://elecena.pl/sitemap-001-search.xml.gz', $locNodesContent[0]->tagContent);
}
}
}

0 comments on commit 951e73b

Please sign in to comment.