Skip to content

Commit

Permalink
Tests and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
urmaul committed Feb 5, 2019
1 parent 2a717b1 commit 9e11e9a
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 38 deletions.
35 changes: 0 additions & 35 deletions src/builder/RssBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@ class RssBuilder implements Builder

public function getContentType(): string
{
return 'text/plain';
return 'application/rss+xml';
}

public function buildFeed(Feed $feed): string
{
$rss = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><rss/>');
$rss->addAttribute('version', '2.0');
foreach ($feed->attributes as $name => $value) {
$rss->addAttribute($name, $value);
}
Expand All @@ -44,38 +42,5 @@ public function buildFeed(Feed $feed): string
}

return $rss->asXml();
//
// $builder = new XmlBuilder();
// $xml = $builder->setEncoding('UTF-8')->setRoot('rss')->create();
//
//
// $data = [];
//
// foreach ($this->metadata as $key => $val) {
// if (is_array($val)) {
// $data[] = [
// '_name' => $key,
// '_values' => $val,
// ];
// } else {
// $data[$key] = $val;
// }
// }
//
// foreach ($this->items as $item) {
// $data[] = [
// '_name' => 'item',
// '_values' => $item->getData(),
// ];
// }
//
// $builder->add($xml, [
// [
// '_name' => 'channel',
// '_values' => $data
// ],
// ]);
//
// return $xml;
}
}
16 changes: 14 additions & 2 deletions src/sections/Atom.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function parseBody($body, $feed)
}
$feed->namespaces += $xml->getDocNamespaces();

$metadata = $this->parseChildrenValues($xml);
$metadata = $this->parseChannelMetadata($this->parseChildrenValues($xml));
unset($metadata['entry']);

$feed->metadata += $metadata;
Expand All @@ -30,11 +30,17 @@ public function parseBody($body, $feed)
$item->setData($this->parseChildrenValues($element));
$item->attributes += $this->parseAllAttributes($element);

if ($element->updated) {
$pubDate = new \DateTimeImmutable((string) $element->updated);
$item->pubDate = $pubDate->format(DATE_RFC822);
unset($item->attributes['updated']);
}

$feed->items[] = $item;
}
}

public function parseChildrenValues($element)
protected function parseChildrenValues($element)
{
$values = parent::parseChildrenValues($element);

Expand All @@ -44,4 +50,10 @@ public function parseChildrenValues($element)

return $values;
}

private function parseChannelMetadata(array $metadata): array
{
unset($metadata['id'], $metadata['updated'], $metadata['author']);
return $metadata;
}
}
2 changes: 1 addition & 1 deletion src/sections/Rss.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function parseBody($body, $feed)
}
}

public function parseChildrenValues($element)
protected function parseChildrenValues($element)
{
$children = (array) $element->children();
$values = [];
Expand Down
59 changes: 59 additions & 0 deletions tests/builder/JsonBuilderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace tests\builder;

use PHPUnit\Framework\TestCase;
use rsspipes\builder\JsonBuilder;
use rsspipes\rss\Feed;
use rsspipes\rss\Item;

/**
* @see JsonBuilder
*/
class JsonBuilderTest extends TestCase
{
public function testSomething()
{
$feed = new Feed();
$feed->items = [
new Item([
'title' => 'foobar',
'link' => 'https://rss.test/1',
'description' => "Foo<br/>\nBar",
]),
new Item([
'title' => 'spamham',
'link' => 'https://rss.test/2',
'description' => "Spam<br/>\nHam",
]),
];

$result = (new JsonBuilder())->buildFeed($feed);

$expected = <<<TXT
{
"items": [
{
"guid": null,
"pubDate": null,
"title": "foobar",
"link": "https:\/\/rss.test\/1",
"description": "Foo<br\/>\\nBar",
"comments": null
},
{
"guid": null,
"pubDate": null,
"title": "spamham",
"link": "https:\/\/rss.test\/2",
"description": "Spam<br\/>\\nHam",
"comments": null
}
]
}
TXT;
$this->assertEquals($expected, $result);
}
}
44 changes: 44 additions & 0 deletions tests/builder/RssBuilderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace tests\builder;

use PHPUnit\Framework\TestCase;
use rsspipes\builder\RssBuilder;
use rsspipes\rss\Feed;
use rsspipes\rss\Item;

/**
* @see RssBuilder
*/
class RssBuilderTest extends TestCase
{
public function testSomething()
{
$feed = new Feed();
$feed->items = [
new Item([
'title' => 'foobar',
'link' => 'https://rss.test/1',
'description' => "Foo<br/>\nBar",
]),
new Item([
'title' => 'spamham',
'link' => 'https://rss.test/2',
'description' => "Spam<br/>\nHam",
]),
];

$result = (new RssBuilder())->buildFeed($feed);

$expected = <<<TXT
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"><channel><item><guid/><pubDate/><title>foobar</title><link>https://rss.test/1</link><description>Foo&lt;br/&gt;
Bar</description><comments/></item><item><guid/><pubDate/><title>spamham</title><link>https://rss.test/2</link><description>Spam&lt;br/&gt;
Ham</description><comments/></item></channel></rss>
TXT;
$this->assertEquals($expected, $result);
}
}
5 changes: 5 additions & 0 deletions tests/sections/AtomTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ public function testRead()
$this->assertSame('Темы для 453', $feed->items[0]->title);
$this->assertSame('http://www.radio-t.com/p/2015/07/14/prep-453/', $feed->items[0]->link);
$this->assertSame('http://www.radio-t.com/p/2015/07/14/prep-453', $feed->items[0]->guid);
$this->assertSame('Tue, 14 Jul 15 15:45:00 +0000', $feed->items[0]->pubDate);
$this->assertArrayNotHasKey('updated', $feed->items[0]->attributes);
$this->assertSame('Радио-Т 452', $feed->items[1]->title);
$this->assertSame('http://www.radio-t.com/p/2015/07/11/podcast-452/', $feed->items[1]->link);
$this->assertSame('http://www.radio-t.com/p/2015/07/11/podcast-452', $feed->items[1]->guid);
Expand All @@ -96,5 +98,8 @@ public function testRead()
<p><a href="http://cdn.radio-t.com/rt_podcast452.mp3">аудио</a> ● <a href="http://chat.radio-t.com/logs/radio-t-452.html">лог чата</a>
<audio src="http://cdn.radio-t.com/rt_podcast452.mp3" preload="none"></audio></p>
', $feed->items[1]->description);
$this->assertSame('Tue, 14 Jul 15 15:45:00 +0000', $feed->items[0]->pubDate);
$this->assertArrayNotHasKey('updated', $feed->items[0]->attributes);

}
}

0 comments on commit 9e11e9a

Please sign in to comment.