mirror of
https://github.com/Chouchen/ShikiryuRSS.git
synced 2024-12-30 07:01:36 +01:00
✅ Add original cases
This commit is contained in:
parent
63d0b03b50
commit
5de5993e2b
@ -8,8 +8,7 @@
|
||||
convertNoticesToExceptions = "true"
|
||||
convertWarningsToExceptions = "true"
|
||||
processIsolation = "false"
|
||||
stopOnFailure = "false"
|
||||
syntaxCheck = "false">
|
||||
stopOnFailure = "false">
|
||||
|
||||
<testsuites>
|
||||
<testsuite name="Project Test Suite">
|
||||
@ -17,12 +16,6 @@
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
|
||||
<filter>
|
||||
<whitelist>
|
||||
<directory suffix=".php">src/</directory>
|
||||
</whitelist>
|
||||
</filter>
|
||||
|
||||
<php>
|
||||
<env name="APP_ENV" value="testing"/>
|
||||
</php>
|
||||
|
@ -6,13 +6,12 @@ use DOMDocument;
|
||||
use DOMElement;
|
||||
use Shikiryu\SRSS\Entity\Channel;
|
||||
use Shikiryu\SRSS\Entity\Item;
|
||||
use Shikiryu\SRSS\Parser\ItemParser;
|
||||
use Shikiryu\SRSS\SRSS;
|
||||
use Shikiryu\SRSS\SRSSTools;
|
||||
|
||||
class SRSSBuilder extends DomDocument
|
||||
{
|
||||
public function build(SRSS $srss, string $filepath)
|
||||
private function buildRSS(SRSS $srss)
|
||||
{
|
||||
$root = $this->createElement('rss');
|
||||
$root->setAttribute('version', '2.0');
|
||||
@ -25,53 +24,27 @@ class SRSSBuilder extends DomDocument
|
||||
$root->appendChild($channel);
|
||||
$this->appendChild($root);
|
||||
$this->encoding = 'UTF-8';
|
||||
$this->generator = 'Shikiryu RSS';
|
||||
$srss->generator = 'Shikiryu RSS';
|
||||
$this->formatOutput = true;
|
||||
$this->preserveWhiteSpace = false;
|
||||
// $docs = 'http://www.scriptol.fr/rss/RSS-2.0.html';
|
||||
|
||||
return $this;
|
||||
}
|
||||
public function build(SRSS $srss, string $filepath)
|
||||
{
|
||||
$this->buildRSS($srss);
|
||||
|
||||
$this->save($filepath);
|
||||
}
|
||||
|
||||
/**
|
||||
* add a SRSS Item as an item into current RSS as first item
|
||||
*
|
||||
* @param ItemParser $item
|
||||
* @return false|string
|
||||
*/
|
||||
public function addItemBefore(ItemParser $item)
|
||||
public function show(SRSS $srss)
|
||||
{
|
||||
$node = $this->importNode($item->getItem(), true);
|
||||
$items = $this->getElementsByTagName('item');
|
||||
if($items->length != 0){
|
||||
$firstNode = $items->item(0);
|
||||
if($firstNode != null)
|
||||
$firstNode->parentNode->insertBefore($node, $firstNode);
|
||||
else
|
||||
$this->addItem($item);
|
||||
}
|
||||
else
|
||||
$this->addItem($item);
|
||||
}
|
||||
$this->buildRSS($srss);
|
||||
|
||||
/**
|
||||
* add a SRSS Item as an item into current RSS
|
||||
*
|
||||
* @param ItemParser $item
|
||||
*/
|
||||
public function addItem(ItemParser $item)
|
||||
{
|
||||
$node = $this->importNode($item->getItem(), true);
|
||||
$channel = $this->_getChannel();
|
||||
$channel->appendChild($node);
|
||||
}
|
||||
|
||||
/**
|
||||
* display XML
|
||||
* see DomDocument's docs
|
||||
*/
|
||||
public function show()
|
||||
{
|
||||
// TODO build
|
||||
return $this->saveXml();
|
||||
}
|
||||
|
||||
|
45
src/SRSS.php
45
src/SRSS.php
@ -3,6 +3,7 @@
|
||||
namespace Shikiryu\SRSS;
|
||||
|
||||
use Iterator;
|
||||
use Shikiryu\SRSS\Builder\SRSSBuilder;
|
||||
use Shikiryu\SRSS\Entity\Channel;
|
||||
use Shikiryu\SRSS\Entity\Item;
|
||||
use Shikiryu\SRSS\Exception\SRSSException;
|
||||
@ -118,7 +119,7 @@ class SRSS implements Iterator
|
||||
/**
|
||||
* current from Iterator
|
||||
*/
|
||||
public function current(): mixed
|
||||
public function current(): Item
|
||||
{
|
||||
return $this->items[$this->position];
|
||||
}
|
||||
@ -202,11 +203,49 @@ class SRSS implements Iterator
|
||||
$doc['items'][] = $item->toArray();
|
||||
}
|
||||
|
||||
return $doc;
|
||||
return array_filter($doc);
|
||||
}
|
||||
|
||||
public function addItem(Item $rssItem)
|
||||
/**
|
||||
* @param \Shikiryu\SRSS\Entity\Item $rssItem
|
||||
*
|
||||
* @return array|\Shikiryu\SRSS\Entity\Item[]
|
||||
*/
|
||||
public function addItem(Item $rssItem): array
|
||||
{
|
||||
$this->items[] = $rssItem;
|
||||
|
||||
return $this->items;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Shikiryu\SRSS\Entity\Item $firstItem
|
||||
*
|
||||
* @return array|\Shikiryu\SRSS\Entity\Item[]
|
||||
*/
|
||||
public function addItemBefore(Item $firstItem): array
|
||||
{
|
||||
array_unshift($this->items, $firstItem);
|
||||
|
||||
return $this->items;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function save(string $path): void
|
||||
{
|
||||
(new SRSSBuilder('1.0', 'UTF-8'))->build($this, $path);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return false|string
|
||||
*/
|
||||
public function show()
|
||||
{
|
||||
return (new SRSSBuilder('1.0', 'UTF-8'))->show($this);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -8,6 +8,14 @@ use Shikiryu\SRSS\SRSSTools;
|
||||
|
||||
class BasicBuilderTest extends TestCase
|
||||
{
|
||||
private string $saved = __DIR__.'/resources/tmp/build/testCreateBasicRSS.rss';
|
||||
protected function tearDown(): void
|
||||
{
|
||||
parent::tearDown();
|
||||
if (file_exists($this->saved)) {
|
||||
unlink($this->saved);
|
||||
}
|
||||
}
|
||||
public function testCreateBasicRSS()
|
||||
{
|
||||
$srss = SRSS::create();
|
||||
@ -31,10 +39,11 @@ class BasicBuilderTest extends TestCase
|
||||
|
||||
self::assertTrue($srss->isValid());
|
||||
|
||||
$filepath = __DIR__.'/resources/tmp/build/testCreateBasicRSS.rss';
|
||||
$builder = new SRSSBuilder();
|
||||
$builder->build($srss, $filepath);
|
||||
$builder->build($srss, $this->saved);
|
||||
|
||||
self::assertFileExists($filepath);
|
||||
self::assertFileExists($this->saved);
|
||||
|
||||
self::assertIsString($srss->show());
|
||||
}
|
||||
}
|
41
tests/OriginalReaderSRSSTest.php
Normal file
41
tests/OriginalReaderSRSSTest.php
Normal file
@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Shikiryu\SRSS\SRSS;
|
||||
use Shikiryu\SRSS\SRSSTools;
|
||||
|
||||
class OriginalReaderSRSSTest extends TestCase
|
||||
{
|
||||
private string $original = __DIR__.'/resources/harvard.xml';
|
||||
private string $saved = __DIR__.'/resources/tmp/build/rss.xml';
|
||||
protected function tearDown(): void
|
||||
{
|
||||
parent::tearDown();
|
||||
if (file_exists($this->saved)) {
|
||||
unlink($this->saved);
|
||||
}
|
||||
}
|
||||
|
||||
public function testOriginalReader()
|
||||
{
|
||||
$rss = SRSS::read($this->original);
|
||||
self::assertEquals('Liftoff News', $rss->title);
|
||||
|
||||
$article1 = $rss->getItem(1);
|
||||
$articleFirst = $rss->getFirst();
|
||||
self::assertEquals($article1, $articleFirst);
|
||||
|
||||
$links = [];
|
||||
foreach($rss as $article) {
|
||||
$links[] = sprintf('<a href="%s">%s %s</a>', $article->link, SRSSTools::formatDate('d/m/y', $article->pubDate), $article->title);
|
||||
}
|
||||
self::assertCount(4, $links, var_export($links, true));
|
||||
|
||||
$rssArray = $rss->toArray();
|
||||
self::assertCount(11, $rssArray, var_export($rssArray, true)); // 11 elements in RSS
|
||||
|
||||
$rss->save($this->saved);
|
||||
|
||||
self::assertFileExists($this->saved);
|
||||
}
|
||||
}
|
45
tests/OriginalWriterSRSSTest.php
Normal file
45
tests/OriginalWriterSRSSTest.php
Normal file
@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Shikiryu\SRSS\Entity\Item;
|
||||
use Shikiryu\SRSS\SRSS;
|
||||
use Shikiryu\SRSS\SRSSTools;
|
||||
|
||||
class OriginalWriterSRSSTest extends TestCase
|
||||
{
|
||||
public function testOriginalWriter()
|
||||
{
|
||||
$rss = SRSS::create();
|
||||
$rss->title = 'My Awesome Blog';
|
||||
$rss->link = 'http://shikiryu.com/devblog/';
|
||||
$rss->description = 'is awesome';
|
||||
|
||||
$items = [
|
||||
['title' => 'title 1', 'link' => 'http://shikiryu.com/devblog/article-1', 'pubDate' => SRSSTools::getRSSDate('2012-03-05 12:02:01'), 'description' => 'description 1'],
|
||||
['title' => 'title 2', 'link' => 'http://shikiryu.com/devblog/article-2', 'pubDate' => SRSSTools::getRSSDate('2022-03-05 22:02:02'), 'description' => 'description 2'],
|
||||
['title' => 'title 3', 'link' => 'http://shikiryu.com/devblog/article-3', 'pubDate' => SRSSTools::getRSSDate('2032-03-05 32:02:03'), 'description' => 'description 3'],
|
||||
['title' => 'title 4', 'link' => 'http://shikiryu.com/devblog/article-4', 'pubDate' => SRSSTools::getRSSDate('2042-03-05 42:02:04'), 'description' => 'description 4'],
|
||||
];
|
||||
|
||||
foreach($items as $item){
|
||||
$rssitem = new Item();
|
||||
$rssitem->title = $item["title"];
|
||||
$rssitem->link = $item['link'];
|
||||
$rssitem->pubDate = $item["pubDate"];
|
||||
$rssitem->description = $item["description"];
|
||||
$rss->addItem($rssitem);
|
||||
}
|
||||
|
||||
$firstItem = new Item();
|
||||
$firstItem->title = 'title 0';
|
||||
$firstItem->link = 'http://shikiryu.com/devblog/article-0';
|
||||
$firstItem->pubDate = SRSSTools::getRSSDate('2011-03-05 12:02:01');
|
||||
$firstItem->description = 'description 0';
|
||||
$rss->addItemBefore($firstItem);
|
||||
|
||||
self::assertCount(5, $rss->items, var_export($rss->items, true));
|
||||
self::assertEquals('title 0', $rss->getFirst()->title, var_export($rss->items, true));
|
||||
self::assertEquals('title 1', $rss->getItem(2)->title, var_export($rss->items, true));
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user