aboutsummaryrefslogtreecommitdiff
path: root/tests/FeedParserTest.php
blob: 45dc1234b29dd2e68721c2c11da93c0fcb4fdcc6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php

declare(strict_types=1);

namespace RssBridge\Tests;

use PHPUnit\Framework\TestCase;

class FeedParserTest extends TestCase
{
    private \FeedParser $sut;

    public function setUp(): void
    {
        $this->sut = new \FeedParser();
    }

    public function testRss1()
    {
        $xml = <<<XML
        <?xml version="1.0" encoding="utf-8"?> 
        <rdf:RDF 
          xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 
          xmlns:cc="http://creativecommons.org/ns#"
          xmlns="http://purl.org/rss/1.0/"
        > 
        <channel rdf:about="http://meerkat.oreillynet.com/?_fl=rss1.0">
            <title>hello feed</title>
            <link>http://meerkat.oreillynet.com</link>
            <description>Meerkat: An Open Wire Service</description>
            
            <items>
                <rdf:Seq>
                    <rdf:li resource="http://c.moreover.com/click/here.pl?r123" />
                </rdf:Seq>
            </items>
        </channel>

        <item rdf:about="http://c.moreover.com/click/here.pl?r123">
            <title>XML: A Disruptive Technology</title> 
            <link>http://c.moreover.com/click/here.pl?r123</link>
            <description>desc</description>
        </item> 
        </rdf:RDF>
        XML;

        $feed = $this->sut->parseFeed($xml);

        $this->assertSame('hello feed', $feed['title']);
        $this->assertSame('http://meerkat.oreillynet.com', $feed['uri']);
        $this->assertSame(null, $feed['icon']);

        $item = $feed['items'][0];
        $this->assertSame('XML: A Disruptive Technology', $item['title']);
        $this->assertSame('http://c.moreover.com/click/here.pl?r123', $item['uri']);
        $this->assertSame('desc', $item['content']);
    }

    public function testRss2()
    {
        $xml = <<<XML
        <?xml version="1.0"?>
        <rss version="2.0">
            <channel>
                <title>hello feed</title>
                <link>https://example.com/</link>
                <image>
                    <url>https://example.com/2.ico</url>
                </image>

                <item>
                    <title>hello world</title>
                    <link>https://example.com/1</link>
                    <description>desc2</description>
                    <pubDate>Tue, 26 Apr 2022 00:00:00 +0200</pubDate>
                    <author>root</author>
                    <enclosure url="https://example.com/1.png"></enclosure>
                </item>
            </channel>
        </rss>
        XML;

        $feed = $this->sut->parseFeed($xml);

        $this->assertSame('hello feed', $feed['title']);
        $this->assertSame('https://example.com/', $feed['uri']);
        $this->assertSame('https://example.com/2.ico', $feed['icon']);

        $item = $feed['items'][0];
        $this->assertSame('hello world', $item['title']);
        $this->assertSame('https://example.com/1', $item['uri']);
        $this->assertSame(1650924000, $item['timestamp']);
        $this->assertSame('root', $item['author']);
        $this->assertSame('desc2', $item['content']);
        $this->assertSame(['https://example.com/1.png'], $item['enclosures']);
    }

    public function testAtom()
    {
        $xml = <<<XML
        <?xml version="1.0" encoding="UTF-8"?>
        <feed xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/">
            <title>hello feed</title>
            <link href="https://example.com/1"></link>
            <icon>https://example.com/2.ico</icon>

            <entry>
                <title>hello world</title>
                <link href="https://example.com/1"></link>
                <author>
                    <name>root</name>
                </author>
                <content type="html">html</content>
                <updated>2015-11-05T14:38:49+01:00</updated>
            </entry>
        </feed>
        XML;

        $feed = $this->sut->parseFeed($xml);

        $this->assertSame('hello feed', $feed['title']);
        $this->assertSame('https://example.com/1', $feed['uri']);
        $this->assertSame('https://example.com/2.ico', $feed['icon']);

        $item = $feed['items'][0];
        $this->assertSame('hello world', $item['title']);
        $this->assertSame('https://example.com/1', $item['uri']);
        $this->assertSame(1446730729, $item['timestamp']);
        $this->assertSame('root', $item['author']);
        $this->assertSame('html', $item['content']);
    }

    public function testAppleItunesModule()
    {
        $xml = <<<XML
        <?xml version="1.0" encoding="UTF-8"?>
        <rss
            version="2.0"
            xmlns:atom="http://www.w3.org/2005/Atom"
            xmlns:cc="http://web.resource.org/cc/"
            xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd"
            xmlns:media="http://search.yahoo.com/mrss/"
            xmlns:content="http://purl.org/rss/1.0/modules/content/"
            xmlns:podcast="https://podcastindex.org/namespace/1.0"
            xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0"
            xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
        >
            <channel>

                <item>
                    <itunes:duration>30:05</itunes:duration>
                    <enclosure length="48123248" type="audio/mpeg" url="https://example.com/1.mp3" />
                </item>
            </channel>
        </rss>
        XML;

        $feed = $this->sut->parseFeed($xml);
        $expected = [
            'title' => '',
            'uri' => '',
            'icon' => '',
            'items' => [
                [
                    'uri' => '',
                    'title' => '',
                    'content' => '',
                    'timestamp' => '',
                    'author' => '',
                    'itunes' => [
                        'duration' => '30:05',
                    ],
                    'enclosure' => [
                        'url' => 'https://example.com/1.mp3',
                        'length' => '48123248',
                        'type' => 'audio/mpeg',
                    ],
                    'enclosures' => [
                        'https://example.com/1.mp3',
                    ],
                ]
            ],
        ];
        $this->assertEquals($expected, $feed);
    }
}