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