From c48590a7927b9ab63797501d3e30fd2300d609be Mon Sep 17 00:00:00 2001 From: "J. King" Date: Wed, 11 Mar 2020 10:09:22 -0400 Subject: [PATCH] Initial failure tests --- lib/Exception.php | 2 ++ lib/Parser/XML/Feed.php | 11 +++++++++-- tests/cases/XML/failures.yaml | 11 +++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 tests/cases/XML/failures.yaml diff --git a/lib/Exception.php b/lib/Exception.php index 4c4c520..85d65bf 100644 --- a/lib/Exception.php +++ b/lib/Exception.php @@ -11,7 +11,9 @@ abstract class Exception extends \Exception { // Parsing: 0x1100 "notJSONType" => [0x1111, "Document Content-Type is not either that of JSON Feed or generic JSON"], "notJSON" => [0x1112, "Document is not valid JSON"], + "notXML" => [0x1112, "Document is not well-formed XML"], "notJSONFeed" => [0x1113, "Document is not a JSON Feed document"], + "notXMLFeed" => [0x1113, "Document is not a newsfeed"], ]; public function __construct(string $symbol, \Exception $e = null) { diff --git a/lib/Parser/XML/Feed.php b/lib/Parser/XML/Feed.php index 079d735..fcd63bd 100644 --- a/lib/Parser/XML/Feed.php +++ b/lib/Parser/XML/Feed.php @@ -6,6 +6,7 @@ declare(strict_types=1); namespace MensBeam\Lax\Parser\XML; +use MensBeam\Lax\Parser\Exception; use MensBeam\Lax\Person\Collection as PersonCollection; use MensBeam\Lax\Category\Collection as CategoryCollection; use MensBeam\Lax\Feed as FeedStruct; @@ -18,6 +19,8 @@ class Feed implements \MensBeam\Lax\Parser\Feed { use Primitives\Construct; use Primitives\Feed; + protected const LIBXML_OPTIONS = \LIBXML_BIGLINES | \LIBXML_COMPACT | \LIBXML_HTML_NODEFDTD | \LIBXML_NOCDATA | \LIBXML_NOENT | \LIBXML_NONET | \LIBXML_NOERROR | LIBXML_NOWARNING; + /** @var string */ protected $data; /** @var string */ @@ -41,7 +44,9 @@ class Feed implements \MensBeam\Lax\Parser\Feed { /** Performs initialization of the instance */ protected function init(FeedStruct $feed): FeedStruct { $this->document = new \DOMDocument(); - $this->document->loadXML($this->data, \LIBXML_BIGLINES | \LIBXML_COMPACT); + if (!$this->document->loadXML($this->data, self::LIBXML_OPTIONS)) { + throw new Exception("notXML"); + } $this->document->documentURI = (string) $this->url; $this->xpath = new XPath($this->document); $this->subject = $this->document->documentElement; @@ -61,13 +66,15 @@ class Feed implements \MensBeam\Lax\Parser\Feed { $element = $this->fetchElement("rss1:item|rss0:item|rss1:image|rss0:image"); if ($element) { $feed->version = ($element->namespaceURI === XPath::NS['rss1']) ? "1.0" : "0.90"; + } else { + throw new Exception("notXMLFeed"); } } } elseif ($ns === XPath::NS['atom'] && $name === "feed") { $feed->format = "atom"; $feed->version = "1.0"; } else { - throw new \Exception; + throw new Exception("notXMLFeed"); } $feed->meta->url = $this->url; return $feed; diff --git a/tests/cases/XML/failures.yaml b/tests/cases/XML/failures.yaml new file mode 100644 index 0000000..6d8a9c0 --- /dev/null +++ b/tests/cases/XML/failures.yaml @@ -0,0 +1,11 @@ +Malformed XML: + input: > + + + + exception: notXML + +Root element of non-feed type: + input: > + + exception: notXMLFeed