diff --git a/src/Exception/ChannelNotFoundInRSSException.php b/src/Exception/ChannelNotFoundInRSSException.php new file mode 100644 index 0000000..9f4479f --- /dev/null +++ b/src/Exception/ChannelNotFoundInRSSException.php @@ -0,0 +1,12 @@ + not found', $file)); + } + +} \ No newline at end of file diff --git a/src/Exception/PropertyNotFoundException.php b/src/Exception/PropertyNotFoundException.php new file mode 100644 index 0000000..58b39b4 --- /dev/null +++ b/src/Exception/PropertyNotFoundException.php @@ -0,0 +1,15 @@ +load($link)) { // We don't want the warning in case of bad XML. Let's manage it with an exception. $channel = $this->getElementsByTagName('channel'); @@ -54,10 +58,10 @@ class SRSSParser extends DomDocument return $this->doc; } - throw new SRSSException('invalid file '.$link); + throw new ChannelNotFoundInRSSException($link); } - throw new SRSSException('Can not open file '.$link); + throw new UnreadableRSSException($link); } /** @@ -116,7 +120,7 @@ class SRSSParser extends DomDocument { $channel = $this->getElementsByTagName('channel'); if($channel->length != 1) { - throw new SRSSException('channel node not created, or too many channel nodes'); + throw new ChannelNotFoundInRSSException('channel node not created, or too many channel nodes'); } return $channel->item(0); diff --git a/src/SRSS.php b/src/SRSS.php index 18da1d8..fad830a 100644 --- a/src/SRSS.php +++ b/src/SRSS.php @@ -3,15 +3,20 @@ namespace Shikiryu\SRSS; use Iterator; +use ReflectionException; use Shikiryu\SRSS\Builder\SRSSBuilder; use Shikiryu\SRSS\Entity\Channel; use Shikiryu\SRSS\Entity\Item; +use Shikiryu\SRSS\Exception\ChannelNotFoundInRSSException; +use Shikiryu\SRSS\Exception\PropertyNotFoundException; use Shikiryu\SRSS\Exception\SRSSException; +use Shikiryu\SRSS\Exception\UnreadableRSSException; use Shikiryu\SRSS\Parser\SRSSParser; class SRSS implements Iterator { public Channel $channel; + /** @var Item[] */ public array $items; // array of SRSSItems @@ -30,6 +35,8 @@ class SRSS implements Iterator * @param string $link url of the rss * * @return SRSS + * @throws ChannelNotFoundInRSSException + * @throws UnreadableRSSException * @throws SRSSException */ public static function read(string $link): SRSS @@ -40,7 +47,7 @@ class SRSS implements Iterator /** * @return SRSS */ - public static function create() + public static function create(): SRSS { $doc = new SRSS; @@ -65,7 +72,7 @@ class SRSS implements Iterator } return ($valid && $this->channel->isValid()); - } catch (\ReflectionException $e) { + } catch (ReflectionException $e) { return false; } } @@ -91,7 +98,7 @@ class SRSS implements Iterator public function __set($name, $val) { if (!property_exists(Channel::class, $name)) { - throw new SRSSException($name . ' is not a possible item'); + throw new PropertyNotFoundException(Channel::class, $name); } // TODO add validator ? // if ((new Validator())->isPropertyValid($this->channel, $name)) { @@ -210,9 +217,9 @@ class SRSS implements Iterator } /** - * @param \Shikiryu\SRSS\Entity\Item $rssItem + * @param Item $rssItem * - * @return array|\Shikiryu\SRSS\Entity\Item[] + * @return array|Item[] */ public function addItem(Item $rssItem): array { @@ -222,9 +229,9 @@ class SRSS implements Iterator } /** - * @param \Shikiryu\SRSS\Entity\Item $firstItem + * @param Item $firstItem * - * @return array|\Shikiryu\SRSS\Entity\Item[] + * @return array|Item[] */ public function addItemBefore(Item $firstItem): array { diff --git a/tests/BasicReaderTest.php b/tests/BasicReaderTest.php index 331ecde..e6444f7 100644 --- a/tests/BasicReaderTest.php +++ b/tests/BasicReaderTest.php @@ -2,6 +2,7 @@ use PHPUnit\Framework\TestCase; use Shikiryu\SRSS\Exception\SRSSException; +use Shikiryu\SRSS\Exception\UnreadableRSSException; use Shikiryu\SRSS\SRSS; class BasicReaderTest extends TestCase @@ -17,12 +18,6 @@ class BasicReaderTest extends TestCase self::assertTrue($rss->isValid()); } - public function testRssNotFound() - { - $this->expectException(SRSSException::class); - $rss = SRSS::read('not_found.xml'); - } - public function testSpecificationExampleRSS() { $rss = SRSS::read(__DIR__.'/resources/harvard.xml'); diff --git a/tests/ExceptionTest.php b/tests/ExceptionTest.php new file mode 100644 index 0000000..004f4dc --- /dev/null +++ b/tests/ExceptionTest.php @@ -0,0 +1,29 @@ +expectException(PropertyNotFoundException::class); + $srss->notfound = 'true'; + } + + public function testRssNotFound() + { + $this->expectException(UnreadableRSSException::class); + $rss = SRSS::read('not_found.xml'); + } + + public function testMissingChannel() + { + $this->expectException(ChannelNotFoundInRSSException::class); + $rss = SRSS::read(__DIR__ . '/resources/invalid-no-channel.xml'); + } +} \ No newline at end of file diff --git a/tests/resources/invalid-no-channel.xml b/tests/resources/invalid-no-channel.xml new file mode 100644 index 0000000..e7cf96f --- /dev/null +++ b/tests/resources/invalid-no-channel.xml @@ -0,0 +1,18 @@ + + + + test Home Page + https://www.test.com + Free web building tutorials + + RSS Tutorial + https://www.test.com/xml/xml_rss.asp + New RSS tutorial on test + + + XML Tutorial + https://www.test.com/xml + New XML tutorial on test + + + \ No newline at end of file