ChannelTest.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. namespace Suin\RSSWriter;
  3. class ChannelTest extends \XoopsUnit\TestCase
  4. {
  5. private $itemInterface = '\Suin\RSSWriter\ItemInterface';
  6. private $feedInterface = '\Suin\RSSWriter\FeedInterface';
  7. public function testTitle()
  8. {
  9. $title = uniqid();
  10. $channel = new Channel();
  11. $this->assertSame($channel, $channel->title($title));
  12. $this->assertAttributeSame($title, 'title', $channel);
  13. }
  14. public function testUrl()
  15. {
  16. $url = uniqid();
  17. $channel = new Channel();
  18. $this->assertSame($channel, $channel->url($url));
  19. $this->assertAttributeSame($url, 'url', $channel);
  20. }
  21. public function testDescription()
  22. {
  23. $description = uniqid();
  24. $channel = new Channel();
  25. $this->assertSame($channel, $channel->description($description));
  26. $this->assertAttributeSame($description, 'description', $channel);
  27. }
  28. public function testLanguage()
  29. {
  30. $language = uniqid();
  31. $channel = new Channel();
  32. $this->assertSame($channel, $channel->language($language));
  33. $this->assertAttributeSame($language, 'language', $channel);
  34. }
  35. public function testCopyright()
  36. {
  37. $copyright = uniqid();
  38. $channel = new Channel();
  39. $this->assertSame($channel, $channel->copyright($copyright));
  40. $this->assertAttributeSame($copyright, 'copyright', $channel);
  41. }
  42. public function testPubDate()
  43. {
  44. $pubDate = mt_rand(0, 9999999);
  45. $channel = new Channel();
  46. $this->assertSame($channel, $channel->pubDate($pubDate));
  47. $this->assertAttributeSame($pubDate, 'pubDate', $channel);
  48. }
  49. public function testLastBuildDate()
  50. {
  51. $lastBuildDate = mt_rand(0, 9999999);
  52. $channel = new Channel();
  53. $this->assertSame($channel, $channel->lastBuildDate($lastBuildDate));
  54. $this->assertAttributeSame($lastBuildDate, 'lastBuildDate', $channel);
  55. }
  56. public function testTtl()
  57. {
  58. $ttl = mt_rand(0, 99999999);
  59. $channel = new Channel();
  60. $this->assertSame($channel, $channel->ttl($ttl));
  61. $this->assertAttributeSame($ttl, 'ttl', $channel);
  62. }
  63. public function testPubsubhubbub()
  64. {
  65. $channel = new Channel();
  66. $channel->pubsubhubbub('http://example.com/feed.xml', 'http://pubsubhubbub.appspot.com');
  67. $xml = $channel->asXML()->asXML();
  68. $this->assertContains('<atom:link rel="self" href="http://example.com/feed.xml" type="application/rss+xml"/>', $xml);
  69. $this->assertContains('<atom:link rel="hub" href="http://pubsubhubbub.appspot.com"/>', $xml);
  70. }
  71. public function testAddItem()
  72. {
  73. $item = $this->getMock($this->itemInterface);
  74. $channel = new Channel();
  75. $this->assertSame($channel, $channel->addItem($item));
  76. $this->assertAttributeSame([$item], 'items', $channel);
  77. }
  78. public function testAppendTo()
  79. {
  80. $channel = new Channel();
  81. $feed = $this->getMock($this->feedInterface);
  82. $feed->expects($this->once())->method('addChannel')->with($channel);
  83. $this->assertSame($channel, $channel->appendTo($feed));
  84. }
  85. /**
  86. * @param $expect
  87. * @param array $data
  88. * @dataProvider dataForAsXML
  89. */
  90. public function testAsXML($expect, array $data)
  91. {
  92. $data = (object)$data;
  93. $channel = new Channel();
  94. foreach ($data as $key => $value) {
  95. $this->reveal($channel)->attr($key, $value);
  96. }
  97. $this->assertXmlStringEqualsXmlString($expect, $channel->asXML()->asXML());
  98. }
  99. public static function dataForAsXML()
  100. {
  101. $now = time();
  102. $nowString = date(DATE_RSS, $now);
  103. return [
  104. [
  105. "
  106. <channel>
  107. <title>GoUpstate.com News Headlines</title>
  108. <link>http://www.goupstate.com/</link>
  109. <description>The latest news from GoUpstate.com, a Spartanburg Herald-Journal Web site.</description>
  110. </channel>
  111. ",
  112. [
  113. 'title' => "GoUpstate.com News Headlines",
  114. 'url' => 'http://www.goupstate.com/',
  115. 'description' => "The latest news from GoUpstate.com, a Spartanburg Herald-Journal Web site.",
  116. ]
  117. ],
  118. [
  119. "
  120. <channel>
  121. <title>GoUpstate.com News Headlines</title>
  122. <link>http://www.goupstate.com/</link>
  123. <description>The latest news from GoUpstate.com, a Spartanburg Herald-Journal Web site.</description>
  124. <language>en-us</language>
  125. </channel>
  126. ",
  127. [
  128. 'title' => "GoUpstate.com News Headlines",
  129. 'url' => 'http://www.goupstate.com/',
  130. 'description' => "The latest news from GoUpstate.com, a Spartanburg Herald-Journal Web site.",
  131. 'language' => 'en-us',
  132. ]
  133. ],
  134. [
  135. "
  136. <channel>
  137. <title>GoUpstate.com News Headlines</title>
  138. <link>http://www.goupstate.com/</link>
  139. <description>The latest news from GoUpstate.com, a Spartanburg Herald-Journal Web site.</description>
  140. <pubDate>{$nowString}</pubDate>
  141. </channel>
  142. ",
  143. [
  144. 'title' => "GoUpstate.com News Headlines",
  145. 'url' => 'http://www.goupstate.com/',
  146. 'description' => "The latest news from GoUpstate.com, a Spartanburg Herald-Journal Web site.",
  147. 'pubDate' => $now,
  148. ]
  149. ],
  150. [
  151. "
  152. <channel>
  153. <title>GoUpstate.com News Headlines</title>
  154. <link>http://www.goupstate.com/</link>
  155. <description>The latest news from GoUpstate.com, a Spartanburg Herald-Journal Web site.</description>
  156. <lastBuildDate>{$nowString}</lastBuildDate>
  157. </channel>
  158. ",
  159. [
  160. 'title' => "GoUpstate.com News Headlines",
  161. 'url' => 'http://www.goupstate.com/',
  162. 'description' => "The latest news from GoUpstate.com, a Spartanburg Herald-Journal Web site.",
  163. 'lastBuildDate' => $now,
  164. ]
  165. ],
  166. [
  167. "
  168. <channel>
  169. <title>GoUpstate.com News Headlines</title>
  170. <link>http://www.goupstate.com/</link>
  171. <description>The latest news from GoUpstate.com, a Spartanburg Herald-Journal Web site.</description>
  172. <ttl>60</ttl>
  173. </channel>
  174. ",
  175. [
  176. 'title' => "GoUpstate.com News Headlines",
  177. 'url' => 'http://www.goupstate.com/',
  178. 'description' => "The latest news from GoUpstate.com, a Spartanburg Herald-Journal Web site.",
  179. 'ttl' => 60,
  180. ]
  181. ],
  182. [
  183. "
  184. <channel>
  185. <title>GoUpstate.com News Headlines</title>
  186. <link>http://www.goupstate.com/</link>
  187. <description>The latest news from GoUpstate.com, a Spartanburg Herald-Journal Web site.</description>
  188. <copyright>Copyright 2002, Spartanburg Herald-Journal</copyright>
  189. </channel>
  190. ",
  191. [
  192. 'title' => "GoUpstate.com News Headlines",
  193. 'url' => 'http://www.goupstate.com/',
  194. 'description' => "The latest news from GoUpstate.com, a Spartanburg Herald-Journal Web site.",
  195. 'copyright' => "Copyright 2002, Spartanburg Herald-Journal",
  196. ]
  197. ],
  198. ];
  199. }
  200. public function testAppendTo_with_items()
  201. {
  202. $channel = new Channel();
  203. $xml1 = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" ?><item><title>item1</title></item>');
  204. $xml2 = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" ?><item><title>item2</title></item>');
  205. $xml3 = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" ?><item><title>item3</title></item>');
  206. $item1 = $this->getMock($this->itemInterface);
  207. $item1->expects($this->once())->method('asXML')->will($this->returnValue($xml1));
  208. $item2 = $this->getMock($this->itemInterface);
  209. $item2->expects($this->once())->method('asXML')->will($this->returnValue($xml2));
  210. $item3 = $this->getMock($this->itemInterface);
  211. $item3->expects($this->once())->method('asXML')->will($this->returnValue($xml3));
  212. $this->reveal($channel)
  213. ->attr('title', "GoUpstate.com News Headlines")
  214. ->attr('url', 'http://www.goupstate.com/')
  215. ->attr('description', "The latest news from GoUpstate.com, a Spartanburg Herald-Journal Web site.")
  216. ->attr('items', [$item1, $item2, $item3]);
  217. $expect = '<?xml version="1.0" encoding="UTF-8" ?>
  218. <channel>
  219. <title>GoUpstate.com News Headlines</title>
  220. <link>http://www.goupstate.com/</link>
  221. <description>The latest news from GoUpstate.com, a Spartanburg Herald-Journal Web site.</description>
  222. <item>
  223. <title>item1</title>
  224. </item>
  225. <item>
  226. <title>item2</title>
  227. </item>
  228. <item>
  229. <title>item3</title>
  230. </item>
  231. </channel>
  232. ';
  233. $this->assertXmlStringEqualsXmlString($expect, $channel->asXML()->asXML());
  234. }
  235. }