EntryTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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-2012 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-2012 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. /**
  234. * @group ZF-12070
  235. */
  236. public function testSetDateCreatedUsesGivenUnixTimestampWhenItIsLessThanTenDigits()
  237. {
  238. $entry = new Zend_Feed_Writer_Entry;
  239. $entry->setDateCreated(123456789);
  240. $myDate = new Zend_Date('123456789', Zend_Date::TIMESTAMP);
  241. $this->assertTrue($myDate->equals($entry->getDateCreated()));
  242. }
  243. public function testSetDateCreatedUsesZendDateObject()
  244. {
  245. $entry = new Zend_Feed_Writer_Entry;
  246. $entry->setDateCreated(new Zend_Date('1234567890', Zend_Date::TIMESTAMP));
  247. $myDate = new Zend_Date('1234567890', Zend_Date::TIMESTAMP);
  248. $this->assertTrue($myDate->equals($entry->getDateCreated()));
  249. }
  250. public function testSetDateModifiedDefaultsToCurrentTime()
  251. {
  252. $entry = new Zend_Feed_Writer_Entry;
  253. $entry->setDateModified();
  254. $dateNow = new Zend_Date;
  255. $this->assertTrue($dateNow->isLater($entry->getDateModified()) || $dateNow->equals($entry->getDateModified()));
  256. }
  257. public function testSetDateModifiedUsesGivenUnixTimestamp()
  258. {
  259. $entry = new Zend_Feed_Writer_Entry;
  260. $entry->setDateModified(1234567890);
  261. $myDate = new Zend_Date('1234567890', Zend_Date::TIMESTAMP);
  262. $this->assertTrue($myDate->equals($entry->getDateModified()));
  263. }
  264. /**
  265. * @group ZF-12070
  266. */
  267. public function testSetDateModifiedUsesGivenUnixTimestampWhenItIsLessThanTenDigits()
  268. {
  269. $entry = new Zend_Feed_Writer_Entry;
  270. $entry->setDateModified(123456789);
  271. $myDate = new Zend_Date('123456789', Zend_Date::TIMESTAMP);
  272. $this->assertTrue($myDate->equals($entry->getDateModified()));
  273. }
  274. public function testSetDateModifiedUsesZendDateObject()
  275. {
  276. $entry = new Zend_Feed_Writer_Entry;
  277. $entry->setDateModified(new Zend_Date('1234567890', Zend_Date::TIMESTAMP));
  278. $myDate = new Zend_Date('1234567890', Zend_Date::TIMESTAMP);
  279. $this->assertTrue($myDate->equals($entry->getDateModified()));
  280. }
  281. public function testSetDateCreatedThrowsExceptionOnInvalidParameter()
  282. {
  283. $entry = new Zend_Feed_Writer_Entry;
  284. try {
  285. $entry->setDateCreated('abc');
  286. $this->fail();
  287. } catch (Zend_Feed_Exception $e) {
  288. }
  289. }
  290. public function testSetDateModifiedThrowsExceptionOnInvalidParameter()
  291. {
  292. $entry = new Zend_Feed_Writer_Entry;
  293. try {
  294. $entry->setDateModified('abc');
  295. $this->fail();
  296. } catch (Zend_Feed_Exception $e) {
  297. }
  298. }
  299. public function testGetDateCreatedReturnsNullIfDateNotSet()
  300. {
  301. $entry = new Zend_Feed_Writer_Entry;
  302. $this->assertTrue(is_null($entry->getDateCreated()));
  303. }
  304. public function testGetDateModifiedReturnsNullIfDateNotSet()
  305. {
  306. $entry = new Zend_Feed_Writer_Entry;
  307. $this->assertTrue(is_null($entry->getDateModified()));
  308. }
  309. public function testGetCopyrightReturnsNullIfDateNotSet()
  310. {
  311. $entry = new Zend_Feed_Writer_Entry;
  312. $this->assertTrue(is_null($entry->getCopyright()));
  313. }
  314. public function testGetContentReturnsNullIfDateNotSet()
  315. {
  316. $entry = new Zend_Feed_Writer_Entry;
  317. $this->assertTrue(is_null($entry->getContent()));
  318. }
  319. public function testSetsDescription()
  320. {
  321. $entry = new Zend_Feed_Writer_Entry;
  322. $entry->setDescription('abc');
  323. $this->assertEquals('abc', $entry->getDescription());
  324. }
  325. public function testSetDescriptionThrowsExceptionOnInvalidParameter()
  326. {
  327. $entry = new Zend_Feed_Writer_Entry;
  328. try {
  329. $entry->setDescription('');
  330. $this->fail();
  331. } catch (Zend_Feed_Exception $e) {
  332. }
  333. }
  334. public function testGetDescriptionReturnsNullIfDateNotSet()
  335. {
  336. $entry = new Zend_Feed_Writer_Entry;
  337. $this->assertTrue(is_null($entry->getDescription()));
  338. }
  339. public function testSetsId()
  340. {
  341. $entry = new Zend_Feed_Writer_Entry;
  342. $entry->setId('http://www.example.com/id');
  343. $this->assertEquals('http://www.example.com/id', $entry->getId());
  344. }
  345. public function testSetIdThrowsExceptionOnInvalidParameter()
  346. {
  347. $entry = new Zend_Feed_Writer_Entry;
  348. try {
  349. $entry->setId('');
  350. $this->fail();
  351. } catch (Zend_Feed_Exception $e) {
  352. }
  353. }
  354. public function testGetIdReturnsNullIfNotSet()
  355. {
  356. $entry = new Zend_Feed_Writer_Entry;
  357. $this->assertTrue(is_null($entry->getId()));
  358. }
  359. public function testSetsLink()
  360. {
  361. $entry = new Zend_Feed_Writer_Entry;
  362. $entry->setLink('http://www.example.com/id');
  363. $this->assertEquals('http://www.example.com/id', $entry->getLink());
  364. }
  365. public function testSetLinkThrowsExceptionOnEmptyString()
  366. {
  367. $entry = new Zend_Feed_Writer_Entry;
  368. try {
  369. $entry->setLink('');
  370. $this->fail();
  371. } catch (Zend_Feed_Exception $e) {
  372. }
  373. }
  374. public function testSetLinkThrowsExceptionOnInvalidUri()
  375. {
  376. $entry = new Zend_Feed_Writer_Entry;
  377. try {
  378. $entry->setLink('http://');
  379. $this->fail();
  380. } catch (Zend_Feed_Exception $e) {
  381. }
  382. }
  383. public function testGetLinkReturnsNullIfNotSet()
  384. {
  385. $entry = new Zend_Feed_Writer_Entry;
  386. $this->assertTrue(is_null($entry->getLink()));
  387. }
  388. public function testSetsCommentLink()
  389. {
  390. $entry = new Zend_Feed_Writer_Entry;
  391. $entry->setCommentLink('http://www.example.com/id/comments');
  392. $this->assertEquals('http://www.example.com/id/comments', $entry->getCommentLink());
  393. }
  394. public function testSetCommentLinkThrowsExceptionOnEmptyString()
  395. {
  396. $entry = new Zend_Feed_Writer_Entry;
  397. try {
  398. $entry->setCommentLink('');
  399. $this->fail();
  400. } catch (Zend_Feed_Exception $e) {
  401. }
  402. }
  403. public function testSetCommentLinkThrowsExceptionOnInvalidUri()
  404. {
  405. $entry = new Zend_Feed_Writer_Entry;
  406. try {
  407. $entry->setCommentLink('http://');
  408. $this->fail();
  409. } catch (Zend_Feed_Exception $e) {
  410. }
  411. }
  412. public function testGetCommentLinkReturnsNullIfDateNotSet()
  413. {
  414. $entry = new Zend_Feed_Writer_Entry;
  415. $this->assertTrue(is_null($entry->getCommentLink()));
  416. }
  417. public function testSetsCommentFeedLink()
  418. {
  419. $entry = new Zend_Feed_Writer_Entry;
  420. $entry->setCommentFeedLink(array('uri'=>'http://www.example.com/id/comments', 'type'=>'rdf'));
  421. $this->assertEquals(array(array('uri'=>'http://www.example.com/id/comments', 'type'=>'rdf')), $entry->getCommentFeedLinks());
  422. }
  423. public function testSetCommentFeedLinkThrowsExceptionOnEmptyString()
  424. {
  425. $entry = new Zend_Feed_Writer_Entry;
  426. try {
  427. $entry->setCommentFeedLink(array('uri'=>'', 'type'=>'rdf'));
  428. $this->fail();
  429. } catch (Zend_Feed_Exception $e) {
  430. }
  431. }
  432. public function testSetCommentFeedLinkThrowsExceptionOnInvalidUri()
  433. {
  434. $entry = new Zend_Feed_Writer_Entry;
  435. try {
  436. $entry->setCommentFeedLink(array('uri'=>'http://', 'type'=>'rdf'));
  437. $this->fail();
  438. } catch (Zend_Feed_Exception $e) {
  439. }
  440. }
  441. public function testSetCommentFeedLinkThrowsExceptionOnInvalidType()
  442. {
  443. $entry = new Zend_Feed_Writer_Entry;
  444. try {
  445. $entry->setCommentFeedLink(array('uri'=>'http://www.example.com/id/comments', 'type'=>'foo'));
  446. $this->fail();
  447. } catch (Zend_Feed_Exception $e) {
  448. }
  449. }
  450. public function testGetCommentFeedLinkReturnsNullIfNoneSet()
  451. {
  452. $entry = new Zend_Feed_Writer_Entry;
  453. $this->assertTrue(is_null($entry->getCommentFeedLinks()));
  454. }
  455. public function testSetsTitle()
  456. {
  457. $entry = new Zend_Feed_Writer_Entry;
  458. $entry->setTitle('abc');
  459. $this->assertEquals('abc', $entry->getTitle());
  460. }
  461. public function testSetTitleThrowsExceptionOnInvalidParameter()
  462. {
  463. $entry = new Zend_Feed_Writer_Entry;
  464. try {
  465. $entry->setTitle('');
  466. $this->fail();
  467. } catch (Zend_Feed_Exception $e) {
  468. }
  469. }
  470. public function testGetTitleReturnsNullIfDateNotSet()
  471. {
  472. $entry = new Zend_Feed_Writer_Entry;
  473. $this->assertTrue(is_null($entry->getTitle()));
  474. }
  475. public function testSetsCommentCount()
  476. {
  477. $entry = new Zend_Feed_Writer_Entry;
  478. $entry->setCommentCount('10');
  479. $this->assertEquals(10, $entry->getCommentCount());
  480. }
  481. /**
  482. * @group ZF-11150
  483. */
  484. public function testSetCommentCountAcceptsZero()
  485. {
  486. $entry = new Zend_Feed_Writer_Entry();
  487. $entry->setCommentCount(0);
  488. $this->assertEquals(0, $entry->getCommentCount());
  489. }
  490. public function testSetCommentCountThrowsExceptionOnInvalidEmptyParameter()
  491. {
  492. $entry = new Zend_Feed_Writer_Entry;
  493. try {
  494. $entry->setCommentCount('');
  495. $this->fail();
  496. } catch (Zend_Feed_Exception $e) {
  497. }
  498. }
  499. public function testSetCommentCountThrowsExceptionOnInvalidNonIntegerParameter()
  500. {
  501. $entry = new Zend_Feed_Writer_Entry;
  502. try {
  503. $entry->setCommentCount('a');
  504. $this->fail();
  505. } catch (Zend_Feed_Exception $e) {
  506. }
  507. }
  508. public function testGetCommentCountReturnsNullIfDateNotSet()
  509. {
  510. $entry = new Zend_Feed_Writer_Entry;
  511. $this->assertTrue(is_null($entry->getCommentCount()));
  512. }
  513. }