UserProfileEntryTest.php 13 KB

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