PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Zend/GData/YouTube/UserProfileEntryTest.php

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