1
0
mirror of https://github.com/Chouchen/ShikiryuRSS.git synced 2024-11-22 07:48:51 +01:00

Add new exceptions

This commit is contained in:
Shikiryu 2023-04-12 00:27:35 +02:00
parent f1816dec0c
commit cb6fff0dae
8 changed files with 109 additions and 17 deletions

View File

@ -0,0 +1,12 @@
<?php
namespace Shikiryu\SRSS\Exception;
class ChannelNotFoundInRSSException extends SRSSException
{
public function __construct($file)
{
parent::__construct(sprintf('Invalid file `%s`: <channel> not found', $file));
}
}

View File

@ -0,0 +1,15 @@
<?php
namespace Shikiryu\SRSS\Exception;
class PropertyNotFoundException extends SRSSException
{
/**
* @param string $class
* @param string $name
*/
public function __construct($class, $name)
{
parent::__construct(sprintf('Property `%s` not found in `%s`', $name, $class));
}
}

View File

@ -0,0 +1,12 @@
<?php
namespace Shikiryu\SRSS\Exception;
class UnreadableRSSException extends SRSSException
{
public function __construct($file)
{
parent::__construct(sprintf('File `%s` is unreadable.', $file));
}
}

View File

@ -9,7 +9,9 @@ use DOMXPath;
use Shikiryu\SRSS\Entity\Channel;
use Shikiryu\SRSS\Entity\Channel\Image;
use Shikiryu\SRSS\Entity\Item;
use Shikiryu\SRSS\Exception\ChannelNotFoundInRSSException;
use Shikiryu\SRSS\Exception\SRSSException;
use Shikiryu\SRSS\Exception\UnreadableRSSException;
use Shikiryu\SRSS\SRSS;
class SRSSParser extends DomDocument
@ -41,9 +43,11 @@ class SRSSParser extends DomDocument
* @param string $link
*
* @return SRSS
* @throws ChannelNotFoundInRSSException
* @throws SRSSException
* @throws UnreadableRSSException
*/
public function parse(string $link)
public function parse(string $link): SRSS
{
if(@$this->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);

View File

@ -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
{

View File

@ -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');

29
tests/ExceptionTest.php Normal file
View File

@ -0,0 +1,29 @@
<?php
use PHPUnit\Framework\TestCase;
use Shikiryu\SRSS\Exception\ChannelNotFoundInRSSException;
use Shikiryu\SRSS\Exception\PropertyNotFoundException;
use Shikiryu\SRSS\Exception\UnreadableRSSException;
use Shikiryu\SRSS\SRSS;
class ExceptionTest extends TestCase
{
public function testPropertyNotFound()
{
$srss = new SRSS();
$this->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');
}
}

View File

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<title>test Home Page</title>
<link>https://www.test.com</link>
<description>Free web building tutorials</description>
<item>
<title>RSS Tutorial</title>
<link>https://www.test.com/xml/xml_rss.asp</link>
<description>New RSS tutorial on test</description>
</item>
<item>
<title>XML Tutorial</title>
<link>https://www.test.com/xml</link>
<description>New XML tutorial on test</description>
</item>
</rss>