Browse Source

Initial failure tests

master
J. King 4 years ago
parent
commit
c48590a792
  1. 2
      lib/Exception.php
  2. 11
      lib/Parser/XML/Feed.php
  3. 11
      tests/cases/XML/failures.yaml

2
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) {

11
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;

11
tests/cases/XML/failures.yaml

@ -0,0 +1,11 @@
Malformed XML:
input: >
<rss>
<channel>
</rss>
exception: notXML
Root element of non-feed type:
input: >
<html/>
exception: notXMLFeed
Loading…
Cancel
Save