EntryTest.php 17 KB

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