2
0

FeedTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. * @feed Zend
  16. * @category Zend
  17. * @package Zend_Gdata_App
  18. * @subpackage UnitTests
  19. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  20. * @license http://framework.zend.com/license/new-bsd New BSD License
  21. * @version $Id $
  22. */
  23. require_once 'Zend/Gdata/App/Feed.php';
  24. require_once 'Zend/Gdata/App.php';
  25. /**
  26. * @category Zend
  27. * @package Zend_Gdata_App
  28. * @subpackage UnitTests
  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. * @group Zend_Gdata
  32. * @group Zend_Gdata_App
  33. */
  34. class Zend_Gdata_App_FeedTest extends PHPUnit_Framework_TestCase
  35. {
  36. public function setUp() {
  37. $this->feedText = file_get_contents(
  38. 'Zend/Gdata/App/_files/FeedSample1.xml',
  39. true);
  40. $this->feed = new Zend_Gdata_App_Feed();
  41. }
  42. public function testEmptyFeedShouldHaveEmptyExtensionsList() {
  43. $this->assertTrue(is_array($this->feed->extensionElements));
  44. $this->assertTrue(count($this->feed->extensionElements) == 0);
  45. }
  46. public function testEmptyFeedToAndFromStringShouldMatch() {
  47. $feedXml = $this->feed->saveXML();
  48. $newFeed = new Zend_Gdata_App_Feed();
  49. $newFeed->transferFromXML($feedXml);
  50. $newFeedXml = $newFeed->saveXML();
  51. $this->assertTrue($feedXml == $newFeedXml);
  52. }
  53. public function testConvertFeedToAndFromString() {
  54. $this->feed->transferFromXML($this->feedText);
  55. $feedXml = $this->feed->saveXML();
  56. $newFeed = new Zend_Gdata_App_Feed();
  57. $newFeed->transferFromXML($feedXml);
  58. $this->assertEquals(1, count($newFeed->entry));
  59. $this->assertEquals('dive into mark', $newFeed->title->text);
  60. $this->assertEquals('text', $newFeed->title->type);
  61. $this->assertEquals('2005-07-31T12:29:29Z', $newFeed->updated->text);
  62. $this->assertEquals('tag:example.org,2003:3', $newFeed->id->text);
  63. $this->assertEquals(2, count($newFeed->link));
  64. $this->assertEquals('http://example.org/',
  65. $newFeed->getAlternateLink()->href);
  66. $this->assertEquals('en',
  67. $newFeed->getAlternateLink()->hrefLang);
  68. $this->assertEquals('text/html',
  69. $newFeed->getAlternateLink()->type);
  70. $this->assertEquals('http://example.org/feed.atom',
  71. $newFeed->getSelfLink()->href);
  72. $this->assertEquals('application/atom+xml',
  73. $newFeed->getSelfLink()->type);
  74. $this->assertEquals('Copyright (c) 2003, Mark Pilgrim',
  75. $newFeed->rights->text);
  76. $entry = $newFeed->entry[0];
  77. $this->assertEquals('Atom draft-07 snapshot', $entry->title->text);
  78. $this->assertEquals('tag:example.org,2003:3.2397',
  79. $entry->id->text);
  80. $this->assertEquals('2005-07-31T12:29:29Z', $entry->updated->text);
  81. $this->assertEquals('2003-12-13T08:29:29-04:00',
  82. $entry->published->text);
  83. $this->assertEquals('Mark Pilgrim',
  84. $entry->author[0]->name->text);
  85. $this->assertEquals('http://example.org/',
  86. $entry->author[0]->uri->text);
  87. $this->assertEquals(2, count($entry->contributor));
  88. $this->assertEquals('Sam Ruby',
  89. $entry->contributor[0]->name->text);
  90. $this->assertEquals('Joe Gregorio',
  91. $entry->contributor[1]->name->text);
  92. $this->assertEquals('xhtml', $entry->content->type);
  93. }
  94. public function testCanAddIndividualEntries() {
  95. $this->feed->transferFromXML($this->feedText);
  96. $this->assertEquals(1, count($this->feed->entry));
  97. $oldTitle = $this->feed->entry[0]->title->text;
  98. $newEntry = new Zend_Gdata_App_Entry();
  99. $newEntry->setTitle(new Zend_Gdata_App_Extension_Title("Foo"));
  100. $this->feed->addEntry($newEntry);
  101. $this->assertEquals(2, count($this->feed->entry));
  102. $this->assertEquals($oldTitle, $this->feed->entry[0]->title->text);
  103. $this->assertEquals("Foo", $this->feed->entry[1]->title->text);
  104. }
  105. public function testCanSetAndGetEtag() {
  106. $data = "W/&amp;FooBarBaz&amp;";
  107. $this->feed->setEtag($data);
  108. $this->assertEquals($this->feed->getEtag(), $data);
  109. }
  110. public function testSetServicePropagatesToChildren() {
  111. // Setup
  112. $entries = array(new Zend_Gdata_App_Entry(),
  113. new Zend_Gdata_App_Entry());
  114. foreach ($entries as $entry) {
  115. $this->feed->addEntry($entry);
  116. }
  117. // Set new service instance and test for propagation
  118. $s = new Zend_Gdata_App();
  119. $this->feed->setService($s);
  120. $service = $this->feed->getService();
  121. if (!is_object($service)) {
  122. $this->fail('No feed service received');
  123. }
  124. $this->assertEquals('Zend_Gdata_App',
  125. get_class($service));
  126. foreach ($entries as $entry) {
  127. $service = $entry->getService();
  128. if (!is_object($service)) {
  129. $this->fail('No entry service received');
  130. }
  131. $this->assertEquals('Zend_Gdata_App',
  132. get_class($service));
  133. }
  134. // Set null service instance and test for propagation
  135. $s = null;
  136. $this->feed->setService($s);
  137. $this->assertFalse(is_object($this->feed->getService()));
  138. foreach ($entries as $entry) {
  139. $service = $entry->getService();
  140. $this->assertFalse(is_object($service));
  141. }
  142. }
  143. public function testCanSetMajorProtocolVersion()
  144. {
  145. $expectedVersion = 42;
  146. $this->feed->setMajorProtocolVersion($expectedVersion);
  147. $receivedVersion = $this->feed->getMajorProtocolVersion();
  148. $this->assertEquals($expectedVersion, $receivedVersion);
  149. }
  150. public function testCanSetMinorProtocolVersion()
  151. {
  152. $expectedVersion = 42;
  153. $this->feed->setMinorProtocolVersion($expectedVersion);
  154. $receivedVersion = $this->feed->getMinorProtocolVersion();
  155. $this->assertEquals($expectedVersion, $receivedVersion);
  156. }
  157. public function testEntriesInheritFeedVersionOnCreate()
  158. {
  159. $major = 98;
  160. $minor = 12;
  161. $this->feed->setMajorProtocolVersion($major);
  162. $this->feed->setMinorProtocolVersion($minor);
  163. $this->feed->transferFromXML($this->feedText);
  164. foreach ($this->feed->entries as $entry) {
  165. $this->assertEquals($major, $entry->getMajorProtocolVersion());
  166. $this->assertEquals($minor, $entry->getMinorProtocolVersion());
  167. }
  168. }
  169. public function testEntriesInheritFeedVersionOnUpdate()
  170. {
  171. $major = 98;
  172. $minor = 12;
  173. $this->feed->transferFromXML($this->feedText);
  174. $this->feed->setMajorProtocolVersion($major);
  175. $this->feed->setMinorProtocolVersion($minor);
  176. foreach ($this->feed->entries as $entry) {
  177. $this->assertEquals($major, $entry->getMajorProtocolVersion());
  178. $this->assertEquals($minor, $entry->getMinorProtocolVersion());
  179. }
  180. }
  181. public function testDefaultMajorProtocolVersionIs1()
  182. {
  183. $this->assertEquals(1, $this->feed->getMajorProtocolVersion());
  184. }
  185. public function testDefaultMinorProtocolVersionIsNull()
  186. {
  187. $this->assertNull($this->feed->getMinorProtocolVersion());
  188. }
  189. public function testLookupNamespaceUsesCurrentVersion()
  190. {
  191. $prefix = 'test';
  192. $v1TestString = 'TEST-v1';
  193. $v2TestString = 'TEST-v2';
  194. Zend_Gdata_App_Base::flushNamespaceLookupCache();
  195. $feed = $this->feed;
  196. $feed->registerNamespace($prefix, $v1TestString, 1, 0);
  197. $feed->registerNamespace($prefix, $v2TestString, 2, 0);
  198. $feed->setMajorProtocolVersion(1);
  199. $result = $feed->lookupNamespace($prefix);
  200. $this->assertEquals($v1TestString, $result);
  201. $feed->setMajorProtocolVersion(2);
  202. $result = $feed->lookupNamespace($prefix);
  203. $this->assertEquals($v2TestString, $result);
  204. $feed->setMajorProtocolVersion(null); // Should default to latest
  205. $result = $feed->lookupNamespace($prefix);
  206. }
  207. public function testLookupNamespaceObeysParentBehavior()
  208. {
  209. $prefix = 'test';
  210. $testString10 = 'TEST-v1-0';
  211. $testString20 = 'TEST-v2-0';
  212. $testString11 = 'TEST-v1-1';
  213. $testString21 = 'TEST-v2-1';
  214. $testString12 = 'TEST-v1-2';
  215. $testString22 = 'TEST-v2-2';
  216. Zend_Gdata_App_Base::flushNamespaceLookupCache();
  217. $feed = $this->feed;
  218. $feed->registerNamespace($prefix, $testString10, 1, 0);
  219. $feed->registerNamespace($prefix, $testString20, 2, 0);
  220. $feed->registerNamespace($prefix, $testString11, 1, 1);
  221. $feed->registerNamespace($prefix, $testString21, 2, 1);
  222. $feed->registerNamespace($prefix, $testString12, 1, 2);
  223. $feed->registerNamespace($prefix, $testString22, 2, 2);
  224. // Assumes default version (1)
  225. $result = $feed->lookupNamespace($prefix, 1, null);
  226. $this->assertEquals($testString12, $result);
  227. $result = $feed->lookupNamespace($prefix, 2, null);
  228. $this->assertEquals($testString22, $result);
  229. $result = $feed->lookupNamespace($prefix, 1, 1);
  230. $this->assertEquals($testString11, $result);
  231. $result = $feed->lookupNamespace($prefix, 2, 1);
  232. $this->assertEquals($testString21, $result);
  233. $result = $feed->lookupNamespace($prefix, null, null);
  234. $this->assertEquals($testString12, $result);
  235. $result = $feed->lookupNamespace($prefix, null, 1);
  236. $this->assertEquals($testString11, $result);
  237. // Override to retrieve latest version
  238. $feed->setMajorProtocolVersion(null);
  239. $result = $feed->lookupNamespace($prefix, null, null);
  240. $this->assertEquals($testString22, $result);
  241. $result = $feed->lookupNamespace($prefix, null, 1);
  242. $this->assertEquals($testString21, $result);
  243. }
  244. /**
  245. * @group ZF-10242
  246. */
  247. public function testCount()
  248. {
  249. $feed = new Zend_Gdata_App_Feed();
  250. $feed->addEntry('foo')
  251. ->addEntry('bar');
  252. $this->assertEquals(2, $feed->count());
  253. $this->assertEquals(2, count($feed));
  254. }
  255. }