|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace RssBridge\Tests; |
| 6 | + |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | + |
| 9 | +class FeedParserTest extends TestCase |
| 10 | +{ |
| 11 | + public function testRss1() |
| 12 | + { |
| 13 | + $xml = <<<XML |
| 14 | + <?xml version="1.0" encoding="utf-8"?> |
| 15 | + <rdf:RDF |
| 16 | + xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" |
| 17 | + xmlns:cc="http://creativecommons.org/ns#" |
| 18 | + xmlns="http://purl.org/rss/1.0/" |
| 19 | + > |
| 20 | + <channel rdf:about="http://meerkat.oreillynet.com/?_fl=rss1.0"> |
| 21 | + <title>hello feed</title> |
| 22 | + <link>http://meerkat.oreillynet.com</link> |
| 23 | + <description>Meerkat: An Open Wire Service</description> |
| 24 | + |
| 25 | + <items> |
| 26 | + <rdf:Seq> |
| 27 | + <rdf:li resource="http://c.moreover.com/click/here.pl?r123" /> |
| 28 | + </rdf:Seq> |
| 29 | + </items> |
| 30 | + </channel> |
| 31 | +
|
| 32 | + <item rdf:about="http://c.moreover.com/click/here.pl?r123"> |
| 33 | + <title>XML: A Disruptive Technology</title> |
| 34 | + <link>http://c.moreover.com/click/here.pl?r123</link> |
| 35 | + <description>desc</description> |
| 36 | + </item> |
| 37 | + </rdf:RDF> |
| 38 | + XML; |
| 39 | + |
| 40 | + $sut = new \FeedParser(); |
| 41 | + $feed = $sut->parseFeed($xml); |
| 42 | + |
| 43 | + $this->assertSame('hello feed', $feed['title']); |
| 44 | + $this->assertSame('http://meerkat.oreillynet.com', $feed['uri']); |
| 45 | + $this->assertSame(null, $feed['icon']); |
| 46 | + |
| 47 | + $item = $feed['items'][0]; |
| 48 | + $this->assertSame('XML: A Disruptive Technology', $item['title']); |
| 49 | + $this->assertSame('http://c.moreover.com/click/here.pl?r123', $item['uri']); |
| 50 | + $this->assertSame('desc', $item['content']); |
| 51 | + } |
| 52 | + |
| 53 | + public function testRss2() |
| 54 | + { |
| 55 | + $xml = <<<XML |
| 56 | + <?xml version="1.0"?> |
| 57 | + <rss version="2.0"> |
| 58 | + <channel> |
| 59 | + <title>hello feed</title> |
| 60 | + <link>https://example.com/</link> |
| 61 | + <image> |
| 62 | + <url>https://example.com/2.ico</url> |
| 63 | + </image> |
| 64 | +
|
| 65 | + <item> |
| 66 | + <title>hello world</title> |
| 67 | + <link>https://example.com/1</link> |
| 68 | + <description>desc2</description> |
| 69 | + <pubDate>Tue, 26 Apr 2022 00:00:00 +0200</pubDate> |
| 70 | + <author>root</author> |
| 71 | + <enclosure url="https://example.com/1.png"></enclosure> |
| 72 | + </item> |
| 73 | + </channel> |
| 74 | + </rss> |
| 75 | + XML; |
| 76 | + |
| 77 | + $sut = new \FeedParser(); |
| 78 | + $feed = $sut->parseFeed($xml); |
| 79 | + |
| 80 | + $this->assertSame('hello feed', $feed['title']); |
| 81 | + $this->assertSame('https://example.com/', $feed['uri']); |
| 82 | + $this->assertSame('https://example.com/2.ico', $feed['icon']); |
| 83 | + |
| 84 | + $item = $feed['items'][0]; |
| 85 | + $this->assertSame('hello world', $item['title']); |
| 86 | + $this->assertSame('https://example.com/1', $item['uri']); |
| 87 | + $this->assertSame(1650924000, $item['timestamp']); |
| 88 | + $this->assertSame('root', $item['author']); |
| 89 | + $this->assertSame('desc2', $item['content']); |
| 90 | + $this->assertSame(['https://example.com/1.png'], $item['enclosures']); |
| 91 | + } |
| 92 | + |
| 93 | + public function testAtom() |
| 94 | + { |
| 95 | + $xml = <<<XML |
| 96 | + <?xml version="1.0" encoding="UTF-8"?> |
| 97 | + <feed xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/"> |
| 98 | + <title>hello feed</title> |
| 99 | + <link href="https://example.com/1"></link> |
| 100 | + <icon>https://example.com/2.ico</icon> |
| 101 | +
|
| 102 | + <entry> |
| 103 | + <title>hello world</title> |
| 104 | + <link href="https://example.com/1"></link> |
| 105 | + <author> |
| 106 | + <name>root</name> |
| 107 | + </author> |
| 108 | + <content type="html">html</content> |
| 109 | + <updated>2015-11-05T14:38:49+01:00</updated> |
| 110 | + </entry> |
| 111 | + </feed> |
| 112 | + XML; |
| 113 | + |
| 114 | + $sut = new \FeedParser(); |
| 115 | + $feed = $sut->parseFeed($xml); |
| 116 | + |
| 117 | + $this->assertSame('hello feed', $feed['title']); |
| 118 | + $this->assertSame('https://example.com/1', $feed['uri']); |
| 119 | + $this->assertSame('https://example.com/2.ico', $feed['icon']); |
| 120 | + |
| 121 | + $item = $feed['items'][0]; |
| 122 | + $this->assertSame('hello world', $item['title']); |
| 123 | + $this->assertSame('https://example.com/1', $item['uri']); |
| 124 | + $this->assertSame(1446730729, $item['timestamp']); |
| 125 | + $this->assertSame('root', $item['author']); |
| 126 | + $this->assertSame('html', $item['content']); |
| 127 | + } |
| 128 | +} |
0 commit comments