Added PodcastData parsing/toArray tests

This commit is contained in:
Kalle Fagerberg 2022-09-17 17:03:49 +02:00 committed by thrillfall
parent aaadcf17b6
commit 2b82e9e9c9
2 changed files with 114 additions and 3 deletions

View File

@ -35,12 +35,12 @@ class PodcastData {
$channel = $xml->channel; $channel = $xml->channel;
return new PodcastData( return new PodcastData(
title: (string)$channel->title, title: (string)$channel->title,
author: self::getXPathContent($xml, '/rss/channel/itunes:author'), author: (string)self::getXPathContent($xml, '/rss/channel/itunes:author'),
link: (string)$channel->link, link: (string)$channel->link,
description: (string)$channel->description, description: (string)$channel->description,
image: image:
self::getXPathContent($xml, '/rss/channel/image/url') (string)(self::getXPathContent($xml, '/rss/channel/image/url')
?? self::getXPathAttribute($xml, '/rss/channel/itunes:image/@href'), ?? self::getXPathAttribute($xml, '/rss/channel/itunes:image/@href')),
fetchedAtUnix: $fetchedAtUnix ?? (new DateTime())->getTimestamp(), fetchedAtUnix: $fetchedAtUnix ?? (new DateTime())->getTimestamp(),
); );
} }

View File

@ -0,0 +1,111 @@
<?php
declare(strict_types=1);
namespace OCA\GPodderSync\Tests\Unit\Core\PodcastData;
use OCA\GPodderSync\Core\PodcastData\PodcastData;
use Test\TestCase;
class EpisodeActionTest extends TestCase {
public function testToArray(): void {
$podcastData = new PodcastData('title1', 'author1', 'http://example.com/', 'description1', 'http://example.com/image.jpg', 1337);
$expected = [
'title' => 'title1',
'author' => 'author1',
'link' => 'http://example.com/',
'description' => 'description1',
'image' => 'http://example.com/image.jpg',
'fetchedAtUnix' => 1337,
];
$this->assertSame($expected, $podcastData->toArray());
}
public function testFromArray(): void {
$podcastData = new PodcastData('title1', 'author1', 'http://example.com/', 'description1', 'http://example.com/image.jpg', 1337);
$expected = $podcastData->toArray();
$fromArray = PodcastData::fromArray($expected);
$this->assertSame($expected, $fromArray->toArray());
}
public function testParseRssXml(): void {
$xml = '<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd"
xmlns:podcast="https://podcastindex.org/namespace/1.0"
>
<channel>
<title>The title of this Podcast</title>
<copyright>All rights reserved</copyright>
<link>http://example.com</link>
<atom:link href="https://example.com/feed" rel="self" type="application/rss+xml" />
<atom:link href="https://example.com" rel="alternate" type="text/html" />
<language>en-us</language>
<description>Some long description</description>
<itunes:author>The Podcast Author</itunes:author>
<itunes:summary>Some long description</itunes:summary>
<itunes:explicit>no</itunes:explicit>
<itunes:image href="https://example.com/image.jpg"/>
<itunes:keywords>nextcloud, gpodder</itunes:keywords>
<itunes:owner>
<itunes:name>Owner of the podcast</itunes:name>
<itunes:email>editors@example.com</itunes:email>
</itunes:owner>
<itunes:category text="Technology">
<itunes:category text="Podcast Tools"/>
</itunes:category>
<podcast:funding url="https://example.com/funding">Support our work</podcast:funding>
<podcast:person role="host" img="https://avatars.githubusercontent.com/u/15801468?s=80&amp;v=4" href="https://github.com/thrillfall">thrillfall</podcast:person>
<podcast:person role="host" img="https://avatars.githubusercontent.com/u/2477952?s=80&amp;v=4" href="https://github.com/jilleJr">jilleJr</podcast:person>
</channel>
</rss>
';
$podcastData = PodcastData::parseRssXml($xml, fetchedAtUnix: 1337);
$expected = [
'title' => 'The title of this Podcast',
'author' => 'The Podcast Author',
'link' => 'http://example.com',
'description' => 'Some long description',
'image' => 'https://example.com/image.jpg',
'fetchedAtUnix' => 1337,
];
$this->assertSame($expected, $podcastData->toArray());
}
public function testParseRssXmlPartial(): void {
$xml = '<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd"
xmlns:podcast="https://podcastindex.org/namespace/1.0"
>
<channel>
<title>The title of this Podcast</title>
<copyright>All rights reserved</copyright>
<link>http://example.com</link>
<itunes:author>The Podcast Author</itunes:author>
<image>
<alt>Some image</alt>
<!-- intentionally skipping <url> -->
</image>
</channel>
</rss>
';
$podcastData = PodcastData::parseRssXml($xml, fetchedAtUnix: 1337);
$expected = [
'title' => 'The title of this Podcast',
'author' => 'The Podcast Author',
'link' => 'http://example.com',
'description' => '',
'image' => '',
'fetchedAtUnix' => 1337,
];
$this->assertSame($expected, $podcastData->toArray());
}
}