EntryTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Feed
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. require_once dirname(dirname(dirname(dirname(__FILE__)))) . '/TestHelper.php';
  23. require_once 'Zend/Feed/Writer/Entry.php';
  24. /**
  25. * @category Zend
  26. * @package Zend_Exception
  27. * @subpackage UnitTests
  28. * @group Zend_Feed
  29. * @group Zend_Feed_Writer
  30. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_Feed_Writer_EntryTest extends PHPUnit_Framework_TestCase
  34. {
  35. protected $_feedSamplePath = null;
  36. public function setup()
  37. {
  38. $this->_feedSamplePath = dirname(__FILE__) . '/_files';
  39. }
  40. public function testAddsAuthorName()
  41. {
  42. $entry = new Zend_Feed_Writer_Entry;
  43. $entry->addAuthor('Joe');
  44. $this->assertEquals(array(array('name'=>'Joe')), $entry->getAuthors());
  45. }
  46. public function testAddsAuthorEmail()
  47. {
  48. $entry = new Zend_Feed_Writer_Entry;
  49. $entry->addAuthor('Joe', 'joe@example.com');
  50. $this->assertEquals(array(array('name'=>'Joe', 'email' => 'joe@example.com')), $entry->getAuthors());
  51. }
  52. public function testAddsAuthorUri()
  53. {
  54. $entry = new Zend_Feed_Writer_Entry;
  55. $entry->addAuthor('Joe', null, 'http://www.example.com');
  56. $this->assertEquals(array(array('name'=>'Joe', 'uri' => 'http://www.example.com')), $entry->getAuthors());
  57. }
  58. public function testAddAuthorThrowsExceptionOnInvalidName()
  59. {
  60. $entry = new Zend_Feed_Writer_Entry;
  61. try {
  62. $entry->addAuthor('');
  63. $this->fail();
  64. } catch (Zend_Feed_Exception $e) {
  65. }
  66. }
  67. public function testAddAuthorThrowsExceptionOnInvalidEmail()
  68. {
  69. $entry = new Zend_Feed_Writer_Entry;
  70. try {
  71. $entry->addAuthor('Joe', '');
  72. $this->fail();
  73. } catch (Zend_Feed_Exception $e) {
  74. }
  75. }
  76. public function testAddAuthorThrowsExceptionOnInvalidUri()
  77. {
  78. $entry = new Zend_Feed_Writer_Entry;
  79. try {
  80. $entry->addAuthor('Joe', null, 'notauri');
  81. $this->fail();
  82. } catch (Zend_Feed_Exception $e) {
  83. }
  84. }
  85. public function testAddsAuthorNameFromArray()
  86. {
  87. $entry = new Zend_Feed_Writer_Entry;
  88. $entry->addAuthor(array('name'=>'Joe'));
  89. $this->assertEquals(array(array('name'=>'Joe')), $entry->getAuthors());
  90. }
  91. public function testAddsAuthorEmailFromArray()
  92. {
  93. $entry = new Zend_Feed_Writer_Entry;
  94. $entry->addAuthor(array('name'=>'Joe','email'=>'joe@example.com'));
  95. $this->assertEquals(array(array('name'=>'Joe', 'email' => 'joe@example.com')), $entry->getAuthors());
  96. }
  97. public function testAddsAuthorUriFromArray()
  98. {
  99. $entry = new Zend_Feed_Writer_Entry;
  100. $entry->addAuthor(array('name'=>'Joe','uri'=>'http://www.example.com'));
  101. $this->assertEquals(array(array('name'=>'Joe', 'uri' => 'http://www.example.com')), $entry->getAuthors());
  102. }
  103. public function testAddAuthorThrowsExceptionOnInvalidNameFromArray()
  104. {
  105. $entry = new Zend_Feed_Writer_Entry;
  106. try {
  107. $entry->addAuthor(array('name'=>''));
  108. $this->fail();
  109. } catch (Zend_Feed_Exception $e) {
  110. }
  111. }
  112. public function testAddAuthorThrowsExceptionOnInvalidEmailFromArray()
  113. {
  114. $entry = new Zend_Feed_Writer_Entry;
  115. try {
  116. $entry->addAuthor(array('name'=>'Joe','email'=>''));
  117. $this->fail();
  118. } catch (Zend_Feed_Exception $e) {
  119. }
  120. }
  121. public function testAddAuthorThrowsExceptionOnInvalidUriFromArray()
  122. {
  123. $entry = new Zend_Feed_Writer_Entry;
  124. try {
  125. $entry->addAuthor(array('name'=>'Joe','uri'=>'notauri'));
  126. $this->fail();
  127. } catch (Zend_Feed_Exception $e) {
  128. }
  129. }
  130. public function testAddAuthorThrowsExceptionIfNameOmittedFromArray()
  131. {
  132. $entry = new Zend_Feed_Writer_Entry;
  133. try {
  134. $entry->addAuthor(array('uri'=>'notauri'));
  135. $this->fail();
  136. } catch (Zend_Feed_Exception $e) {
  137. }
  138. }
  139. public function testAddsAuthorsFromArrayOfAuthors()
  140. {
  141. $entry = new Zend_Feed_Writer_Entry;
  142. $entry->addAuthors(array(
  143. array('name'=>'Joe','uri'=>'http://www.example.com'),
  144. array('name'=>'Jane','uri'=>'http://www.example.com')
  145. ));
  146. $expected = array(
  147. array('name'=>'Joe','uri'=>'http://www.example.com'),
  148. array('name'=>'Jane','uri'=>'http://www.example.com')
  149. );
  150. $this->assertEquals($expected, $entry->getAuthors());
  151. }
  152. public function testAddsEnclosure()
  153. {
  154. $entry = new Zend_Feed_Writer_Entry;
  155. $entry->setEnclosure(array(
  156. 'type' => 'audio/mpeg',
  157. 'uri' => 'http://example.com/audio.mp3',
  158. 'length' => '1337'
  159. ));
  160. $expected = array(
  161. 'type' => 'audio/mpeg',
  162. 'uri' => 'http://example.com/audio.mp3',
  163. 'length' => '1337'
  164. );
  165. $this->assertEquals($expected, $entry->getEnclosure());
  166. }
  167. /**
  168. * @expectedException Zend_Feed_Exception
  169. */
  170. public function testAddsEnclosureThrowsExceptionOnMissingUri()
  171. {
  172. $entry = new Zend_Feed_Writer_Entry;
  173. $entry->setEnclosure(array(
  174. 'type' => 'audio/mpeg',
  175. 'length' => '1337'
  176. ));
  177. }
  178. /**
  179. * @expectedException Zend_Feed_Exception
  180. */
  181. public function testAddsEnclosureThrowsExceptionWhenUriIsInvalid()
  182. {
  183. $entry = new Zend_Feed_Writer_Entry;
  184. $entry->setEnclosure(array(
  185. 'type' => 'audio/mpeg',
  186. 'uri' => 'http://',
  187. 'length' => '1337'
  188. ));
  189. }
  190. public function testSetsCopyright()
  191. {
  192. $entry = new Zend_Feed_Writer_Entry;
  193. $entry->setCopyright('Copyright (c) 2009 Paddy Brady');
  194. $this->assertEquals('Copyright (c) 2009 Paddy Brady', $entry->getCopyright());
  195. }
  196. public function testSetCopyrightThrowsExceptionOnInvalidParam()
  197. {
  198. $entry = new Zend_Feed_Writer_Entry;
  199. try {
  200. $entry->setCopyright('');
  201. $this->fail();
  202. } catch (Zend_Feed_Exception $e) {
  203. }
  204. }
  205. public function testSetsContent()
  206. {
  207. $entry = new Zend_Feed_Writer_Entry;
  208. $entry->setContent('I\'m content.');
  209. $this->assertEquals("I'm content.", $entry->getContent());
  210. }
  211. public function testSetContentThrowsExceptionOnInvalidParam()
  212. {
  213. $entry = new Zend_Feed_Writer_Entry;
  214. try {
  215. $entry->setContent('');
  216. $this->fail();
  217. } catch (Zend_Feed_Exception $e) {
  218. }
  219. }
  220. public function testSetDateCreatedDefaultsToCurrentTime()
  221. {
  222. $entry = new Zend_Feed_Writer_Entry;
  223. $entry->setDateCreated();
  224. $dateNow = new Zend_Date;
  225. $this->assertTrue($dateNow->isLater($entry->getDateCreated()) || $dateNow->equals($entry->getDateCreated()));
  226. }
  227. public function testSetDateCreatedUsesGivenUnixTimestamp()
  228. {
  229. $entry = new Zend_Feed_Writer_Entry;
  230. $entry->setDateCreated(1234567890);
  231. $myDate = new Zend_Date('1234567890', Zend_Date::TIMESTAMP);
  232. $this->assertTrue($myDate->equals($entry->getDateCreated()));
  233. }
  234. public function testSetDateCreatedUsesZendDateObject()
  235. {
  236. $entry = new Zend_Feed_Writer_Entry;
  237. $entry->setDateCreated(new Zend_Date('1234567890', Zend_Date::TIMESTAMP));
  238. $myDate = new Zend_Date('1234567890', Zend_Date::TIMESTAMP);
  239. $this->assertTrue($myDate->equals($entry->getDateCreated()));
  240. }
  241. public function testSetDateModifiedDefaultsToCurrentTime()
  242. {
  243. $entry = new Zend_Feed_Writer_Entry;
  244. $entry->setDateModified();
  245. $dateNow = new Zend_Date;
  246. $this->assertTrue($dateNow->isLater($entry->getDateModified()) || $dateNow->equals($entry->getDateModified()));
  247. }
  248. public function testSetDateModifiedUsesGivenUnixTimestamp()
  249. {
  250. $entry = new Zend_Feed_Writer_Entry;
  251. $entry->setDateModified(1234567890);
  252. $myDate = new Zend_Date('1234567890', Zend_Date::TIMESTAMP);
  253. $this->assertTrue($myDate->equals($entry->getDateModified()));
  254. }
  255. public function testSetDateModifiedUsesZendDateObject()
  256. {
  257. $entry = new Zend_Feed_Writer_Entry;
  258. $entry->setDateModified(new Zend_Date('1234567890', Zend_Date::TIMESTAMP));
  259. $myDate = new Zend_Date('1234567890', Zend_Date::TIMESTAMP);
  260. $this->assertTrue($myDate->equals($entry->getDateModified()));
  261. }
  262. public function testSetDateCreatedThrowsExceptionOnInvalidParameter()
  263. {
  264. $entry = new Zend_Feed_Writer_Entry;
  265. try {
  266. $entry->setDateCreated('abc');
  267. $this->fail();
  268. } catch (Zend_Feed_Exception $e) {
  269. }
  270. }
  271. public function testSetDateModifiedThrowsExceptionOnInvalidParameter()
  272. {
  273. $entry = new Zend_Feed_Writer_Entry;
  274. try {
  275. $entry->setDateModified('abc');
  276. $this->fail();
  277. } catch (Zend_Feed_Exception $e) {
  278. }
  279. }
  280. public function testGetDateCreatedReturnsNullIfDateNotSet()
  281. {
  282. $entry = new Zend_Feed_Writer_Entry;
  283. $this->assertTrue(is_null($entry->getDateCreated()));
  284. }
  285. public function testGetDateModifiedReturnsNullIfDateNotSet()
  286. {
  287. $entry = new Zend_Feed_Writer_Entry;
  288. $this->assertTrue(is_null($entry->getDateModified()));
  289. }
  290. public function testGetCopyrightReturnsNullIfDateNotSet()
  291. {
  292. $entry = new Zend_Feed_Writer_Entry;
  293. $this->assertTrue(is_null($entry->getCopyright()));
  294. }
  295. public function testGetContentReturnsNullIfDateNotSet()
  296. {
  297. $entry = new Zend_Feed_Writer_Entry;
  298. $this->assertTrue(is_null($entry->getContent()));
  299. }
  300. public function testSetsDescription()
  301. {
  302. $entry = new Zend_Feed_Writer_Entry;
  303. $entry->setDescription('abc');
  304. $this->assertEquals('abc', $entry->getDescription());
  305. }
  306. public function testSetDescriptionThrowsExceptionOnInvalidParameter()
  307. {
  308. $entry = new Zend_Feed_Writer_Entry;
  309. try {
  310. $entry->setDescription('');
  311. $this->fail();
  312. } catch (Zend_Feed_Exception $e) {
  313. }
  314. }
  315. public function testGetDescriptionReturnsNullIfDateNotSet()
  316. {
  317. $entry = new Zend_Feed_Writer_Entry;
  318. $this->assertTrue(is_null($entry->getDescription()));
  319. }
  320. public function testSetsId()
  321. {
  322. $entry = new Zend_Feed_Writer_Entry;
  323. $entry->setId('http://www.example.com/id');
  324. $this->assertEquals('http://www.example.com/id', $entry->getId());
  325. }
  326. public function testSetIdThrowsExceptionOnInvalidParameter()
  327. {
  328. $entry = new Zend_Feed_Writer_Entry;
  329. try {
  330. $entry->setId('');
  331. $this->fail();
  332. } catch (Zend_Feed_Exception $e) {
  333. }
  334. }
  335. public function testGetIdReturnsNullIfNotSet()
  336. {
  337. $entry = new Zend_Feed_Writer_Entry;
  338. $this->assertTrue(is_null($entry->getId()));
  339. }
  340. public function testSetsLink()
  341. {
  342. $entry = new Zend_Feed_Writer_Entry;
  343. $entry->setLink('http://www.example.com/id');
  344. $this->assertEquals('http://www.example.com/id', $entry->getLink());
  345. }
  346. public function testSetLinkThrowsExceptionOnEmptyString()
  347. {
  348. $entry = new Zend_Feed_Writer_Entry;
  349. try {
  350. $entry->setLink('');
  351. $this->fail();
  352. } catch (Zend_Feed_Exception $e) {
  353. }
  354. }
  355. public function testSetLinkThrowsExceptionOnInvalidUri()
  356. {
  357. $entry = new Zend_Feed_Writer_Entry;
  358. try {
  359. $entry->setLink('http://');
  360. $this->fail();
  361. } catch (Zend_Feed_Exception $e) {
  362. }
  363. }
  364. public function testGetLinkReturnsNullIfNotSet()
  365. {
  366. $entry = new Zend_Feed_Writer_Entry;
  367. $this->assertTrue(is_null($entry->getLink()));
  368. }
  369. public function testSetsCommentLink()
  370. {
  371. $entry = new Zend_Feed_Writer_Entry;
  372. $entry->setCommentLink('http://www.example.com/id/comments');
  373. $this->assertEquals('http://www.example.com/id/comments', $entry->getCommentLink());
  374. }
  375. public function testSetCommentLinkThrowsExceptionOnEmptyString()
  376. {
  377. $entry = new Zend_Feed_Writer_Entry;
  378. try {
  379. $entry->setCommentLink('');
  380. $this->fail();
  381. } catch (Zend_Feed_Exception $e) {
  382. }
  383. }
  384. public function testSetCommentLinkThrowsExceptionOnInvalidUri()
  385. {
  386. $entry = new Zend_Feed_Writer_Entry;
  387. try {
  388. $entry->setCommentLink('http://');
  389. $this->fail();
  390. } catch (Zend_Feed_Exception $e) {
  391. }
  392. }
  393. public function testGetCommentLinkReturnsNullIfDateNotSet()
  394. {
  395. $entry = new Zend_Feed_Writer_Entry;
  396. $this->assertTrue(is_null($entry->getCommentLink()));
  397. }
  398. public function testSetsCommentFeedLink()
  399. {
  400. $entry = new Zend_Feed_Writer_Entry;
  401. $entry->setCommentFeedLink(array('uri'=>'http://www.example.com/id/comments', 'type'=>'rdf'));
  402. $this->assertEquals(array(array('uri'=>'http://www.example.com/id/comments', 'type'=>'rdf')), $entry->getCommentFeedLinks());
  403. }
  404. public function testSetCommentFeedLinkThrowsExceptionOnEmptyString()
  405. {
  406. $entry = new Zend_Feed_Writer_Entry;
  407. try {
  408. $entry->setCommentFeedLink(array('uri'=>'', 'type'=>'rdf'));
  409. $this->fail();
  410. } catch (Zend_Feed_Exception $e) {
  411. }
  412. }
  413. public function testSetCommentFeedLinkThrowsExceptionOnInvalidUri()
  414. {
  415. $entry = new Zend_Feed_Writer_Entry;
  416. try {
  417. $entry->setCommentFeedLink(array('uri'=>'http://', 'type'=>'rdf'));
  418. $this->fail();
  419. } catch (Zend_Feed_Exception $e) {
  420. }
  421. }
  422. public function testSetCommentFeedLinkThrowsExceptionOnInvalidType()
  423. {
  424. $entry = new Zend_Feed_Writer_Entry;
  425. try {
  426. $entry->setCommentFeedLink(array('uri'=>'http://www.example.com/id/comments', 'type'=>'foo'));
  427. $this->fail();
  428. } catch (Zend_Feed_Exception $e) {
  429. }
  430. }
  431. public function testGetCommentFeedLinkReturnsNullIfNoneSet()
  432. {
  433. $entry = new Zend_Feed_Writer_Entry;
  434. $this->assertTrue(is_null($entry->getCommentFeedLinks()));
  435. }
  436. public function testSetsTitle()
  437. {
  438. $entry = new Zend_Feed_Writer_Entry;
  439. $entry->setTitle('abc');
  440. $this->assertEquals('abc', $entry->getTitle());
  441. }
  442. public function testSetTitleThrowsExceptionOnInvalidParameter()
  443. {
  444. $entry = new Zend_Feed_Writer_Entry;
  445. try {
  446. $entry->setTitle('');
  447. $this->fail();
  448. } catch (Zend_Feed_Exception $e) {
  449. }
  450. }
  451. public function testGetTitleReturnsNullIfDateNotSet()
  452. {
  453. $entry = new Zend_Feed_Writer_Entry;
  454. $this->assertTrue(is_null($entry->getTitle()));
  455. }
  456. public function testSetsCommentCount()
  457. {
  458. $entry = new Zend_Feed_Writer_Entry;
  459. $entry->setCommentCount('10');
  460. $this->assertEquals(10, $entry->getCommentCount());
  461. }
  462. public function testSetCommentCountThrowsExceptionOnInvalidEmptyParameter()
  463. {
  464. $entry = new Zend_Feed_Writer_Entry;
  465. try {
  466. $entry->setCommentCount('');
  467. $this->fail();
  468. } catch (Zend_Feed_Exception $e) {
  469. }
  470. }
  471. public function testSetCommentCountThrowsExceptionOnInvalidNonIntegerParameter()
  472. {
  473. $entry = new Zend_Feed_Writer_Entry;
  474. try {
  475. $entry->setCommentCount('a');
  476. $this->fail();
  477. } catch (Zend_Feed_Exception $e) {
  478. }
  479. }
  480. public function testGetCommentCountReturnsNullIfDateNotSet()
  481. {
  482. $entry = new Zend_Feed_Writer_Entry;
  483. $this->assertTrue(is_null($entry->getCommentCount()));
  484. }
  485. }