-
Notifications
You must be signed in to change notification settings - Fork 6
/
RssWriterTest.php
154 lines (140 loc) · 6.15 KB
/
RssWriterTest.php
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
<?php
namespace Tests\MarcW\RssWriter;
use MarcW\RssWriter\Extension\Atom\AtomLink;
use MarcW\RssWriter\Extension\Atom\AtomWriter;
use MarcW\RssWriter\Extension\Core\Channel;
use MarcW\RssWriter\Extension\Core\Cloud;
use MarcW\RssWriter\Extension\Core\CoreWriter;
use MarcW\RssWriter\Extension\Core\Enclosure;
use MarcW\RssWriter\Extension\Core\Guid;
use MarcW\RssWriter\Extension\Core\Image;
use MarcW\RssWriter\Extension\Core\Item;
use MarcW\RssWriter\Extension\Core\Source;
use MarcW\RssWriter\Extension\DublinCore\DublinCore;
use MarcW\RssWriter\Extension\DublinCore\DublinCoreWriter;
use MarcW\RssWriter\Extension\Itunes\ItunesChannel;
use MarcW\RssWriter\Extension\Itunes\ItunesItem;
use MarcW\RssWriter\Extension\Itunes\ItunesWriter;
use MarcW\RssWriter\Extension\Slash\Slash;
use MarcW\RssWriter\Extension\Slash\SlashWriter;
use MarcW\RssWriter\Extension\Sy\Sy;
use MarcW\RssWriter\Extension\Sy\SyWriter;
use MarcW\RssWriter\RssWriter;
class RssWriterTest extends \PHPUnit_Framework_TestCase
{
public function testRssWriter()
{
$rssWriter = new RssWriter(null, [], true);
$rssWriter->registerWriter(new CoreWriter());
$rssWriter->registerWriter(new ItunesWriter());
$rssWriter->registerWriter(new SyWriter());
$rssWriter->registerWriter(new SlashWriter());
$rssWriter->registerWriter(new AtomWriter());
$rssWriter->registerWriter(new DublinCoreWriter());
$source = new Source();
$source->setUrl('https://example.com')
->setTitle('Example Title');
$channel = new Channel();
$item = new Item();
$cloud = new Cloud();
$cloud->setDomain('example.com')
->setPort(80)
->setPath('/')
->setRegisterProcedure('myProcedure')
->setProtocol('http')
->setProtocol('soap');
$enclosure = new Enclosure();
$enclosure->setUrl('http://www.example.com/audio.mp3')
->setLength(123)
->setType('audio/wave')
->setType('audio/mp3')
;
$image = new Image();
$image->setUrl('https://example.com/img.jpg')
->setTitle('Example Image')
->setLink('https://example.com')
->setWidth(133)
->setHeight(133)
->setHeight(146)
;
$pubDate = new \DateTime('2001-01-01', new \DateTimeZone('UTC'));
$lastBuildDate = new \DateTime('2001-01-01', new \DateTimeZone('UTC'));
$channel->setTitle('My Title')
->setLink('https://www.example.com')
->setDescription('My Description')
->setLanguage('en')
->setCopyright('(c) 2016 Acme')
->setManagingEditor('[email protected] (John Doe)')
->setWebMaster('[email protected] (Jane Doe)')
->setPubDate($pubDate)
->setLastBuildDate($lastBuildDate)
->setDocs('http://example.com/rss2-spec')
->setGenerator('Generator v1')
->setCloud($cloud)
->setTtl(60)
->setImage($image)
->setRating('R')
->setWebMaster('[email protected] (Jane Doe)')
;
$channel->addExtension((new Sy())->setUpdatePeriod(Sy::PERIOD_HOURLY));
$channel->addExtension((new AtomLink())->setRel('self')->setHref('http://www.example.com/feed.xml')->setType('application/rss+xml'));
$item->setTitle('My Title')
->setLink('https://example.com/my-title')
->setDescription('My Description')
->setAuthor('[email protected] (John Doe)')
->setComments('https://example.com/my-title#comments')
->setEnclosure($enclosure)
->setPubDate($pubDate)
->setSource($source)
->setCategories(['cat1', 'cat2'])
->addCategory('cat3')
->setGuid((new Guid())->setIsPermaLink(false)->setGuid(14))
;
$channel->addItem($item);
$item->addExtension((new Slash())->setComments(140));
$item->addExtension((new DublinCore())->setCreator('John Doe'));
$xml = $rssWriter->writeChannel($channel);
$expected = <<<EOF
<?xml version="1.0"?>
<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
<channel>
<title><![CDATA[My Title]]></title>
<link>https://www.example.com</link>
<description><![CDATA[My Description]]></description>
<language>en</language>
<copyright><![CDATA[(c) 2016 Acme]]></copyright>
<managingEditor><![CDATA[[email protected] (John Doe)]]></managingEditor>
<webMaster><![CDATA[[email protected] (Jane Doe)]]></webMaster>
<pubDate>Mon, 01 Jan 2001 00:00:00 +0000</pubDate>
<lastBuildDate>Mon, 01 Jan 2001 00:00:00 +0000</lastBuildDate>
<generator><![CDATA[Generator v1]]></generator>
<docs>http://example.com/rss2-spec</docs>
<cloud domain="example.com" port="80" path="/" registerProcedure="myProcedure" protocol="soap"/>
<ttl>60</ttl>
<image>
<url>https://example.com/img.jpg</url>
<link>https://example.com</link>
<title><![CDATA[Example Image]]></title>
</image>
<rating>R</rating>
<sy:updatePeriod>hourly</sy:updatePeriod>
<atom:link href="http://www.example.com/feed.xml" rel="self" type="application/rss+xml"/>
<item>
<title><![CDATA[My Title]]></title>
<link>https://example.com/my-title</link>
<description><![CDATA[My Description]]></description>
<author><![CDATA[[email protected] (John Doe)]]></author>
<comments>https://example.com/my-title#comments</comments>
<enclosure url="http://www.example.com/audio.mp3" length="123" type="audio/mp3"/>
<guid isPermaLink="false"><![CDATA[14]]></guid>
<pubDate>Mon, 01 Jan 2001 00:00:00 +0000</pubDate>
<source url="https://example.com"><![CDATA[Example Title]]></source>
<slash:comments>140</slash:comments>
<dc:creator><![CDATA[John Doe]]></dc:creator>
</item>
</channel>
</rss>
EOF;
$this->assertEquals($expected, $xml);
}
}