EntryTest.php 18 KB

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