EntryTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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 testAddsEnclosureThrowsExceptionOnMissingType()
  171. {
  172. $entry = new Zend_Feed_Writer_Entry;
  173. $entry->setEnclosure(array(
  174. 'uri' => 'http://example.com/audio.mp3',
  175. 'length' => '1337'
  176. ));
  177. }
  178. /**
  179. * @expectedException Zend_Feed_Exception
  180. */
  181. public function testAddsEnclosureThrowsExceptionOnMissingUri()
  182. {
  183. $entry = new Zend_Feed_Writer_Entry;
  184. $entry->setEnclosure(array(
  185. 'type' => 'audio/mpeg',
  186. 'length' => '1337'
  187. ));
  188. }
  189. /**
  190. * @expectedException Zend_Feed_Exception
  191. */
  192. public function testAddsEnclosureThrowsExceptionOnMissingLength()
  193. {
  194. $entry = new Zend_Feed_Writer_Entry;
  195. $entry->setEnclosure(array(
  196. 'type' => 'audio/mpeg',
  197. 'uri' => 'http://example.com/audio.mp3'
  198. ));
  199. }
  200. /**
  201. * @expectedException Zend_Feed_Exception
  202. */
  203. public function testAddsEnclosureThrowsExceptionOnNonNumericLength()
  204. {
  205. $entry = new Zend_Feed_Writer_Entry;
  206. $entry->setEnclosure(array(
  207. 'type' => 'audio/mpeg',
  208. 'uri' => 'http://example.com/audio.mp3',
  209. 'length' => 'abc'
  210. ));
  211. }
  212. /**
  213. * @expectedException Zend_Feed_Exception
  214. */
  215. public function testAddsEnclosureThrowsExceptionOnNegativeLength()
  216. {
  217. $entry = new Zend_Feed_Writer_Entry;
  218. $entry->setEnclosure(array(
  219. 'type' => 'audio/mpeg',
  220. 'uri' => 'http://example.com/audio.mp3',
  221. 'length' => -23
  222. ));
  223. }
  224. /**
  225. * @expectedException Zend_Feed_Exception
  226. */
  227. public function testAddsEnclosureThrowsExceptionWhenUriIsInvalid()
  228. {
  229. $entry = new Zend_Feed_Writer_Entry;
  230. $entry->setEnclosure(array(
  231. 'type' => 'audio/mpeg',
  232. 'uri' => 'http://',
  233. 'length' => '1337'
  234. ));
  235. }
  236. public function testSetsCopyright()
  237. {
  238. $entry = new Zend_Feed_Writer_Entry;
  239. $entry->setCopyright('Copyright (c) 2009 Paddy Brady');
  240. $this->assertEquals('Copyright (c) 2009 Paddy Brady', $entry->getCopyright());
  241. }
  242. public function testSetCopyrightThrowsExceptionOnInvalidParam()
  243. {
  244. $entry = new Zend_Feed_Writer_Entry;
  245. try {
  246. $entry->setCopyright('');
  247. $this->fail();
  248. } catch (Zend_Feed_Exception $e) {
  249. }
  250. }
  251. public function testSetsContent()
  252. {
  253. $entry = new Zend_Feed_Writer_Entry;
  254. $entry->setContent('I\'m content.');
  255. $this->assertEquals("I'm content.", $entry->getContent());
  256. }
  257. public function testSetContentThrowsExceptionOnInvalidParam()
  258. {
  259. $entry = new Zend_Feed_Writer_Entry;
  260. try {
  261. $entry->setContent('');
  262. $this->fail();
  263. } catch (Zend_Feed_Exception $e) {
  264. }
  265. }
  266. public function testSetDateCreatedDefaultsToCurrentTime()
  267. {
  268. $entry = new Zend_Feed_Writer_Entry;
  269. $entry->setDateCreated();
  270. $dateNow = new Zend_Date;
  271. $this->assertTrue($dateNow->isLater($entry->getDateCreated()) || $dateNow->equals($entry->getDateCreated()));
  272. }
  273. public function testSetDateCreatedUsesGivenUnixTimestamp()
  274. {
  275. $entry = new Zend_Feed_Writer_Entry;
  276. $entry->setDateCreated(1234567890);
  277. $myDate = new Zend_Date('1234567890', Zend_Date::TIMESTAMP);
  278. $this->assertTrue($myDate->equals($entry->getDateCreated()));
  279. }
  280. public function testSetDateCreatedUsesZendDateObject()
  281. {
  282. $entry = new Zend_Feed_Writer_Entry;
  283. $entry->setDateCreated(new Zend_Date('1234567890', Zend_Date::TIMESTAMP));
  284. $myDate = new Zend_Date('1234567890', Zend_Date::TIMESTAMP);
  285. $this->assertTrue($myDate->equals($entry->getDateCreated()));
  286. }
  287. public function testSetDateModifiedDefaultsToCurrentTime()
  288. {
  289. $entry = new Zend_Feed_Writer_Entry;
  290. $entry->setDateModified();
  291. $dateNow = new Zend_Date;
  292. $this->assertTrue($dateNow->isLater($entry->getDateModified()) || $dateNow->equals($entry->getDateModified()));
  293. }
  294. public function testSetDateModifiedUsesGivenUnixTimestamp()
  295. {
  296. $entry = new Zend_Feed_Writer_Entry;
  297. $entry->setDateModified(1234567890);
  298. $myDate = new Zend_Date('1234567890', Zend_Date::TIMESTAMP);
  299. $this->assertTrue($myDate->equals($entry->getDateModified()));
  300. }
  301. public function testSetDateModifiedUsesZendDateObject()
  302. {
  303. $entry = new Zend_Feed_Writer_Entry;
  304. $entry->setDateModified(new Zend_Date('1234567890', Zend_Date::TIMESTAMP));
  305. $myDate = new Zend_Date('1234567890', Zend_Date::TIMESTAMP);
  306. $this->assertTrue($myDate->equals($entry->getDateModified()));
  307. }
  308. public function testSetDateCreatedThrowsExceptionOnInvalidParameter()
  309. {
  310. $entry = new Zend_Feed_Writer_Entry;
  311. try {
  312. $entry->setDateCreated('abc');
  313. $this->fail();
  314. } catch (Zend_Feed_Exception $e) {
  315. }
  316. }
  317. public function testSetDateModifiedThrowsExceptionOnInvalidParameter()
  318. {
  319. $entry = new Zend_Feed_Writer_Entry;
  320. try {
  321. $entry->setDateModified('abc');
  322. $this->fail();
  323. } catch (Zend_Feed_Exception $e) {
  324. }
  325. }
  326. public function testGetDateCreatedReturnsNullIfDateNotSet()
  327. {
  328. $entry = new Zend_Feed_Writer_Entry;
  329. $this->assertTrue(is_null($entry->getDateCreated()));
  330. }
  331. public function testGetDateModifiedReturnsNullIfDateNotSet()
  332. {
  333. $entry = new Zend_Feed_Writer_Entry;
  334. $this->assertTrue(is_null($entry->getDateModified()));
  335. }
  336. public function testGetCopyrightReturnsNullIfDateNotSet()
  337. {
  338. $entry = new Zend_Feed_Writer_Entry;
  339. $this->assertTrue(is_null($entry->getCopyright()));
  340. }
  341. public function testGetContentReturnsNullIfDateNotSet()
  342. {
  343. $entry = new Zend_Feed_Writer_Entry;
  344. $this->assertTrue(is_null($entry->getContent()));
  345. }
  346. public function testSetsDescription()
  347. {
  348. $entry = new Zend_Feed_Writer_Entry;
  349. $entry->setDescription('abc');
  350. $this->assertEquals('abc', $entry->getDescription());
  351. }
  352. public function testSetDescriptionThrowsExceptionOnInvalidParameter()
  353. {
  354. $entry = new Zend_Feed_Writer_Entry;
  355. try {
  356. $entry->setDescription('');
  357. $this->fail();
  358. } catch (Zend_Feed_Exception $e) {
  359. }
  360. }
  361. public function testGetDescriptionReturnsNullIfDateNotSet()
  362. {
  363. $entry = new Zend_Feed_Writer_Entry;
  364. $this->assertTrue(is_null($entry->getDescription()));
  365. }
  366. public function testSetsId()
  367. {
  368. $entry = new Zend_Feed_Writer_Entry;
  369. $entry->setId('http://www.example.com/id');
  370. $this->assertEquals('http://www.example.com/id', $entry->getId());
  371. }
  372. public function testSetIdThrowsExceptionOnInvalidParameter()
  373. {
  374. $entry = new Zend_Feed_Writer_Entry;
  375. try {
  376. $entry->setId('');
  377. $this->fail();
  378. } catch (Zend_Feed_Exception $e) {
  379. }
  380. }
  381. public function testGetIdReturnsNullIfDateNotSet()
  382. {
  383. $entry = new Zend_Feed_Writer_Entry;
  384. $this->assertTrue(is_null($entry->getId()));
  385. }
  386. public function testSetsLink()
  387. {
  388. $entry = new Zend_Feed_Writer_Entry;
  389. $entry->setLink('http://www.example.com/id');
  390. $this->assertEquals('http://www.example.com/id', $entry->getLink());
  391. }
  392. public function testSetLinkThrowsExceptionOnEmptyString()
  393. {
  394. $entry = new Zend_Feed_Writer_Entry;
  395. try {
  396. $entry->setLink('');
  397. $this->fail();
  398. } catch (Zend_Feed_Exception $e) {
  399. }
  400. }
  401. public function testSetLinkThrowsExceptionOnInvalidUri()
  402. {
  403. $entry = new Zend_Feed_Writer_Entry;
  404. try {
  405. $entry->setLink('http://');
  406. $this->fail();
  407. } catch (Zend_Feed_Exception $e) {
  408. }
  409. }
  410. public function testGetLinkReturnsNullIfNotSet()
  411. {
  412. $entry = new Zend_Feed_Writer_Entry;
  413. $this->assertTrue(is_null($entry->getLink()));
  414. }
  415. public function testSetsCommentLink()
  416. {
  417. $entry = new Zend_Feed_Writer_Entry;
  418. $entry->setCommentLink('http://www.example.com/id/comments');
  419. $this->assertEquals('http://www.example.com/id/comments', $entry->getCommentLink());
  420. }
  421. public function testSetCommentLinkThrowsExceptionOnEmptyString()
  422. {
  423. $entry = new Zend_Feed_Writer_Entry;
  424. try {
  425. $entry->setCommentLink('');
  426. $this->fail();
  427. } catch (Zend_Feed_Exception $e) {
  428. }
  429. }
  430. public function testSetCommentLinkThrowsExceptionOnInvalidUri()
  431. {
  432. $entry = new Zend_Feed_Writer_Entry;
  433. try {
  434. $entry->setCommentLink('http://');
  435. $this->fail();
  436. } catch (Zend_Feed_Exception $e) {
  437. }
  438. }
  439. public function testGetCommentLinkReturnsNullIfDateNotSet()
  440. {
  441. $entry = new Zend_Feed_Writer_Entry;
  442. $this->assertTrue(is_null($entry->getCommentLink()));
  443. }
  444. public function testSetsCommentFeedLink()
  445. {
  446. $entry = new Zend_Feed_Writer_Entry;
  447. $entry->setCommentFeedLink(array('uri'=>'http://www.example.com/id/comments', 'type'=>'rdf'));
  448. $this->assertEquals(array(array('uri'=>'http://www.example.com/id/comments', 'type'=>'rdf')), $entry->getCommentFeedLinks());
  449. }
  450. public function testSetCommentFeedLinkThrowsExceptionOnEmptyString()
  451. {
  452. $entry = new Zend_Feed_Writer_Entry;
  453. try {
  454. $entry->setCommentFeedLink(array('uri'=>'', 'type'=>'rdf'));
  455. $this->fail();
  456. } catch (Zend_Feed_Exception $e) {
  457. }
  458. }
  459. public function testSetCommentFeedLinkThrowsExceptionOnInvalidUri()
  460. {
  461. $entry = new Zend_Feed_Writer_Entry;
  462. try {
  463. $entry->setCommentFeedLink(array('uri'=>'http://', 'type'=>'rdf'));
  464. $this->fail();
  465. } catch (Zend_Feed_Exception $e) {
  466. }
  467. }
  468. public function testSetCommentFeedLinkThrowsExceptionOnInvalidType()
  469. {
  470. $entry = new Zend_Feed_Writer_Entry;
  471. try {
  472. $entry->setCommentFeedLink(array('uri'=>'http://www.example.com/id/comments', 'type'=>'foo'));
  473. $this->fail();
  474. } catch (Zend_Feed_Exception $e) {
  475. }
  476. }
  477. public function testGetCommentFeedLinkReturnsNullIfNoneSet()
  478. {
  479. $entry = new Zend_Feed_Writer_Entry;
  480. $this->assertTrue(is_null($entry->getCommentFeedLinks()));
  481. }
  482. public function testSetsTitle()
  483. {
  484. $entry = new Zend_Feed_Writer_Entry;
  485. $entry->setTitle('abc');
  486. $this->assertEquals('abc', $entry->getTitle());
  487. }
  488. public function testSetTitleThrowsExceptionOnInvalidParameter()
  489. {
  490. $entry = new Zend_Feed_Writer_Entry;
  491. try {
  492. $entry->setTitle('');
  493. $this->fail();
  494. } catch (Zend_Feed_Exception $e) {
  495. }
  496. }
  497. public function testGetTitleReturnsNullIfDateNotSet()
  498. {
  499. $entry = new Zend_Feed_Writer_Entry;
  500. $this->assertTrue(is_null($entry->getTitle()));
  501. }
  502. public function testSetsCommentCount()
  503. {
  504. $entry = new Zend_Feed_Writer_Entry;
  505. $entry->setCommentCount('10');
  506. $this->assertEquals(10, $entry->getCommentCount());
  507. }
  508. public function testSetCommentCountThrowsExceptionOnInvalidEmptyParameter()
  509. {
  510. $entry = new Zend_Feed_Writer_Entry;
  511. try {
  512. $entry->setCommentCount('');
  513. $this->fail();
  514. } catch (Zend_Feed_Exception $e) {
  515. }
  516. }
  517. public function testSetCommentCountThrowsExceptionOnInvalidNonIntegerParameter()
  518. {
  519. $entry = new Zend_Feed_Writer_Entry;
  520. try {
  521. $entry->setCommentCount('a');
  522. $this->fail();
  523. } catch (Zend_Feed_Exception $e) {
  524. }
  525. }
  526. public function testGetCommentCountReturnsNullIfDateNotSet()
  527. {
  528. $entry = new Zend_Feed_Writer_Entry;
  529. $this->assertTrue(is_null($entry->getCommentCount()));
  530. }
  531. }