UserProfileEntryTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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_Gdata_YouTube
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2009 Zend Technologies USA Inc. (http://www.zend.com);
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * Test helper
  23. */
  24. require_once dirname(dirname(dirname(dirname(__FILE__)))) . DIRECTORY_SEPARATOR . 'TestHelper.php';
  25. require_once 'Zend/Gdata/YouTube/UserProfileEntry.php';
  26. require_once 'Zend/Gdata/YouTube.php';
  27. /**
  28. * @package Zend_Gdata_App
  29. * @subpackage UnitTests
  30. */
  31. class Zend_Gdata_YouTube_UserProfileEntryTest extends PHPUnit_Framework_TestCase
  32. {
  33. public function setUp() {
  34. $this->entryText = file_get_contents(
  35. 'Zend/Gdata/YouTube/_files/UserProfileEntryDataSample1.xml',
  36. true);
  37. $this->V2entryText = file_get_contents(
  38. 'Zend/Gdata/YouTube/_files/UserProfileEntryDataSampleV2.xml',
  39. true);
  40. $this->entry = new Zend_Gdata_YouTube_UserProfileEntry();
  41. }
  42. private function verifyAllSamplePropertiesAreCorrect ($userProfileEntry) {
  43. $this->assertEquals('http://gdata.youtube.com/feeds/users/darcy',
  44. $userProfileEntry->id->text);
  45. $this->assertEquals('2007-08-13T12:37:03.000-07:00', $userProfileEntry->updated->text);
  46. $this->assertEquals('http://schemas.google.com/g/2005#kind', $userProfileEntry->category[0]->scheme);
  47. $this->assertEquals('http://gdata.youtube.com/schemas/2007#userProfile', $userProfileEntry->category[0]->term);
  48. $this->assertEquals('text', $userProfileEntry->title->type);
  49. $this->assertEquals('Darcy', $userProfileEntry->title->text);;
  50. $this->assertEquals('self', $userProfileEntry->getLink('self')->rel);
  51. $this->assertEquals('application/atom+xml', $userProfileEntry->getLink('self')->type);
  52. $this->assertEquals('http://gdata.youtube.com/feeds/users/darcy', $userProfileEntry->getLink('self')->href);
  53. $this->assertEquals('Fitzwilliam Darcy', $userProfileEntry->author[0]->name->text);
  54. $this->assertEquals(32, $userProfileEntry->age->text);
  55. $this->assertEquals('darcy', $userProfileEntry->username->text);
  56. $this->assertEquals('A person of great interest', $userProfileEntry->description->text);
  57. $this->assertEquals('Pride and Prejudice', $userProfileEntry->books->text);
  58. $this->assertEquals('Self employed', $userProfileEntry->company->text);
  59. $this->assertEquals('Reading, arguing with Liz', $userProfileEntry->hobbies->text);
  60. $this->assertEquals('Steventon', $userProfileEntry->hometown->text);
  61. $this->assertEquals('Longbourn in Hertfordshire, Pemberley in Derbyshire', $userProfileEntry->location->text);
  62. $this->assertEquals('Pride and Prejudice, 2005', $userProfileEntry->movies->text);
  63. $this->assertEquals('Air Con Varizzioni, The Pleasure of the Town', $userProfileEntry->music->text);
  64. $this->assertEquals('Gentleman', $userProfileEntry->occupation->text);
  65. $this->assertEquals('Home schooling', $userProfileEntry->school->text);
  66. $this->assertEquals('m', $userProfileEntry->gender->text);
  67. $this->assertEquals('taken', $userProfileEntry->relationship->text);
  68. $this->assertEquals('http://gdata.youtube.com/feeds/users/darcy', $userProfileEntry->author[0]->uri->text);
  69. $this->assertEquals('http://gdata.youtube.com/feeds/users/darcy/favorites', $userProfileEntry->feedLink[0]->href);
  70. $this->assertEquals('http://gdata.youtube.com/schemas/2007#user.favorites', $userProfileEntry->feedLink[0]->rel);
  71. $this->assertEquals('http://gdata.youtube.com/feeds/users/darcy/contacts', $userProfileEntry->feedLink[1]->href);
  72. $this->assertEquals('http://gdata.youtube.com/schemas/2007#user.contacts', $userProfileEntry->feedLink[1]->rel);
  73. $this->assertEquals('http://gdata.youtube.com/feeds/users/darcy/inbox', $userProfileEntry->feedLink[2]->href);
  74. $this->assertEquals('http://gdata.youtube.com/schemas/2007#user.inbox', $userProfileEntry->feedLink[2]->rel);
  75. $this->assertEquals('http://gdata.youtube.com/feeds/users/darcy/playlists', $userProfileEntry->feedLink[3]->href);
  76. $this->assertEquals('http://gdata.youtube.com/schemas/2007#user.playlists', $userProfileEntry->feedLink[3]->rel);
  77. $this->assertEquals('http://gdata.youtube.com/feeds/users/darcy/subscriptions', $userProfileEntry->feedLink[4]->href);
  78. $this->assertEquals('http://gdata.youtube.com/schemas/2007#user.subscriptions', $userProfileEntry->feedLink[4]->rel);
  79. $this->assertEquals('http://gdata.youtube.com/feeds/users/darcy/uploads', $userProfileEntry->feedLink[5]->href);
  80. $this->assertEquals('http://gdata.youtube.com/schemas/2007#user.uploads', $userProfileEntry->feedLink[5]->rel);
  81. }
  82. private function verifyAllSamplePropertiesAreCorrectV2 ($userProfileEntry) {
  83. $this->assertEquals('tag:youtube.com,2008:user:zfgdata',
  84. $userProfileEntry->id->text);
  85. $this->assertEquals('2008-12-15T13:30:56.000-08:00',
  86. $userProfileEntry->updated->text);
  87. $this->assertEquals(
  88. 'http://gdata.youtube.com/schemas/2007/channeltypes.cat',
  89. $userProfileEntry->category[0]->scheme);
  90. $this->assertEquals(
  91. 'Standard',
  92. $userProfileEntry->category[0]->term);
  93. $this->assertEquals('zfgdata Channel',
  94. $userProfileEntry->title->text);;
  95. $this->assertEquals('self', $userProfileEntry->getLink('self')->rel);
  96. $this->assertEquals("I'm a lonely test account, with little to do " .
  97. "but sit\naround and wait for people to use me. I get bored in " .
  98. "between\nreleases and often sleep to pass the time. Please use " .
  99. "me more\noften, as I love to show off my talent in breaking " .
  100. "your\ncode.", $userProfileEntry->getAboutMe()->text);
  101. $this->assertEquals('88',
  102. $userProfileEntry->getStatistics()->getViewCount());
  103. $thumbnail = $userProfileEntry->getThumbnail();
  104. $this->assertTrue(
  105. $thumbnail instanceof Zend_Gdata_Media_Extension_MediaThumbnail);
  106. $this->assertTrue($thumbnail->getUrl() != null);
  107. $this->assertEquals('TestAccount',
  108. $userProfileEntry->getLastName()->text);
  109. $this->assertEquals('Lonely',
  110. $userProfileEntry->getFirstName()->text);
  111. }
  112. public function testEmptyEntryShouldHaveNoExtensionElements() {
  113. $this->assertTrue(is_array($this->entry->extensionElements));
  114. $this->assertEquals(0, count($this->entry->extensionElements));
  115. }
  116. public function testEmptyEntryShouldHaveNoExtensionAttributes() {
  117. $this->assertTrue(is_array($this->entry->extensionAttributes));
  118. $this->assertEquals(0, count($this->entry->extensionAttributes));
  119. }
  120. public function testSampleEntryShouldHaveNoExtensionElements() {
  121. $this->entry->transferFromXML($this->entryText);
  122. $this->assertTrue(is_array($this->entry->extensionElements));
  123. $this->assertEquals(0, count($this->entry->extensionElements));
  124. }
  125. public function testSampleEntryShouldHaveNoExtensionAttributes() {
  126. $this->entry->transferFromXML($this->entryText);
  127. $this->assertTrue(is_array($this->entry->extensionAttributes));
  128. $this->assertEquals(0, count($this->entry->extensionAttributes));
  129. }
  130. public function testSampleEntryShouldHaveNoExtensionElementsV2() {
  131. $this->entry->transferFromXML($this->V2entryText);
  132. $this->assertTrue(is_array($this->entry->extensionElements));
  133. $this->assertEquals(0, count($this->entry->extensionElements));
  134. }
  135. public function testSampleEntryShouldHaveNoExtensionAttributesV2() {
  136. $this->entry->transferFromXML($this->V2entryText);
  137. $this->assertTrue(is_array($this->entry->extensionAttributes));
  138. $this->assertEquals(0, count($this->entry->extensionAttributes));
  139. }
  140. public function testEmptyUserProfileEntryToAndFromStringShouldMatch() {
  141. $entryXml = $this->entry->saveXML();
  142. $newUserProfileEntry = new Zend_Gdata_YouTube_UserProfileEntry();
  143. $newUserProfileEntry->transferFromXML($entryXml);
  144. $newUserProfileEntryXml = $newUserProfileEntry->saveXML();
  145. $this->assertTrue($entryXml == $newUserProfileEntryXml);
  146. }
  147. public function testEmptyUserProfileEntryToAndFromStringShouldMatchV2() {
  148. $this->entry->setMajorProtocolVersion(2);
  149. $entryXml = $this->entry->saveXML();
  150. $newUserProfileEntry = new Zend_Gdata_YouTube_UserProfileEntry();
  151. $newUserProfileEntry->setMajorProtocolVersion(2);
  152. $newUserProfileEntry->transferFromXML($entryXml);
  153. $newUserProfileEntryXml = $newUserProfileEntry->saveXML();
  154. $this->assertTrue($entryXml == $newUserProfileEntryXml);
  155. }
  156. public function testGetFeedLinkReturnsAllStoredEntriesWhenUsedWithNoParameters() {
  157. // Prepare test data
  158. $entry1 = new Zend_Gdata_Extension_FeedLink();
  159. $entry1->rel = "first";
  160. $entry1->href= "foo";
  161. $entry2 = new Zend_Gdata_Extension_FeedLink();
  162. $entry2->rel = "second";
  163. $entry2->href= "bar";
  164. $data = array($entry1, $entry2);
  165. // Load test data and run test
  166. $this->entry->feedLink = $data;
  167. $this->assertEquals(2, count($this->entry->feedLink));
  168. }
  169. public function testGetFeedLinkCanReturnEntriesByRelValue() {
  170. // Prepare test data
  171. $entry1 = new Zend_Gdata_Extension_FeedLink();
  172. $entry1->rel = "first";
  173. $entry1->href= "foo";
  174. $entry2 = new Zend_Gdata_Extension_FeedLink();
  175. $entry2->rel = "second";
  176. $entry2->href= "bar";
  177. $data = array($entry1, $entry2);
  178. // Load test data and run test
  179. $this->entry->feedLink = $data;
  180. $this->assertEquals($entry1, $this->entry->getFeedLink('first'));
  181. $this->assertEquals($entry2, $this->entry->getFeedLink('second'));
  182. }
  183. public function testSamplePropertiesAreCorrect () {
  184. $this->entry->transferFromXML($this->entryText);
  185. $this->verifyAllSamplePropertiesAreCorrect($this->entry);
  186. }
  187. public function testSamplePropertiesAreCorrectV2 () {
  188. $this->entry->transferFromXML($this->V2entryText);
  189. $this->entry->setMajorProtocolVersion(2);
  190. $this->verifyAllSamplePropertiesAreCorrectV2($this->entry);
  191. }
  192. public function testConvertUserProfileEntryToAndFromString() {
  193. $this->entry->transferFromXML($this->entryText);
  194. $entryXml = $this->entry->saveXML();
  195. $newUserProfileEntry = new Zend_Gdata_YouTube_UserProfileEntry();
  196. $newUserProfileEntry->transferFromXML($entryXml);
  197. $this->verifyAllSamplePropertiesAreCorrect($newUserProfileEntry);
  198. $newUserProfileEntryXml = $newUserProfileEntry->saveXML();
  199. $this->assertEquals($entryXml, $newUserProfileEntryXml);
  200. }
  201. public function testConvertUserProfileEntryToAndFromStringV2() {
  202. $this->entry->transferFromXML($this->V2entryText);
  203. $entryXml = $this->entry->saveXML();
  204. $newUserProfileEntry = new Zend_Gdata_YouTube_UserProfileEntry();
  205. $newUserProfileEntry->setMajorProtocolVersion(2);
  206. $newUserProfileEntry->transferFromXML($entryXml);
  207. $this->verifyAllSamplePropertiesAreCorrectV2($newUserProfileEntry);
  208. $newUserProfileEntryXml = $newUserProfileEntry->saveXML();
  209. $this->assertEquals($entryXml, $newUserProfileEntryXml);
  210. }
  211. public function testYTStatisticsInUserProfileEntryV2() {
  212. $this->entry->transferFromXML($this->V2entryText);
  213. $this->entry->setMajorProtocolVersion(2);
  214. $statistics = $this->entry->getStatistics();
  215. $this->assertEquals(14, $statistics->getVideoWatchCount());
  216. $this->assertEquals(88, $statistics->getViewCount());
  217. $this->assertEquals(12, $statistics->getSubscriberCount());
  218. $this->assertEquals('2008-12-15T14:56:57.000-08:00',
  219. $statistics->getLastWebAccess());
  220. // test __toString()
  221. $this->assertEquals('View Count=88 VideoWatchCount=14 ' .
  222. 'SubscriberCount=12 LastWebAccess=2008-12-15T14:56:57.000-08:00 ' .
  223. 'FavoriteCount=',
  224. sprintf("%s", $statistics));
  225. }
  226. }