From 2b82e9e9c928e5fef02c9d60a47027262774f70c Mon Sep 17 00:00:00 2001 From: Kalle Fagerberg Date: Sat, 17 Sep 2022 17:03:49 +0200 Subject: [PATCH] Added PodcastData parsing/toArray tests --- lib/Core/PodcastData/PodcastData.php | 6 +- .../Unit/Core/PodcastData/PodcastDataTest.php | 111 ++++++++++++++++++ 2 files changed, 114 insertions(+), 3 deletions(-) create mode 100644 tests/Unit/Core/PodcastData/PodcastDataTest.php diff --git a/lib/Core/PodcastData/PodcastData.php b/lib/Core/PodcastData/PodcastData.php index 1357819..8b7ee10 100644 --- a/lib/Core/PodcastData/PodcastData.php +++ b/lib/Core/PodcastData/PodcastData.php @@ -35,12 +35,12 @@ class PodcastData { $channel = $xml->channel; return new PodcastData( 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, description: (string)$channel->description, image: - self::getXPathContent($xml, '/rss/channel/image/url') - ?? self::getXPathAttribute($xml, '/rss/channel/itunes:image/@href'), + (string)(self::getXPathContent($xml, '/rss/channel/image/url') + ?? self::getXPathAttribute($xml, '/rss/channel/itunes:image/@href')), fetchedAtUnix: $fetchedAtUnix ?? (new DateTime())->getTimestamp(), ); } diff --git a/tests/Unit/Core/PodcastData/PodcastDataTest.php b/tests/Unit/Core/PodcastData/PodcastDataTest.php new file mode 100644 index 0000000..b7b2e18 --- /dev/null +++ b/tests/Unit/Core/PodcastData/PodcastDataTest.php @@ -0,0 +1,111 @@ + '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 = ' + + + The title of this Podcast + All rights reserved + http://example.com + + + en-us + Some long description + The Podcast Author + Some long description + no + + nextcloud, gpodder + + Owner of the podcast + editors@example.com + + + + + Support our work + thrillfall + jilleJr + + + '; + + $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 = ' + + + The title of this Podcast + All rights reserved + http://example.com + The Podcast Author + + Some image + + + + + '; + + $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()); + } +}