2
0

PhotosOnlineTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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_Photos
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2009 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 dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'TestHelper.php';
  23. require_once 'Zend/Gdata/Photos.php';
  24. require_once 'Zend/Http/Client.php';
  25. require_once 'Zend/Http/Client/Adapter/Test.php';
  26. require_once 'Zend/Gdata/ClientLogin.php';
  27. require_once 'Zend/Gdata/App/InvalidArgumentException.php';
  28. /**
  29. * @category Zend
  30. * @package Zend_Gdata_Photos
  31. * @subpackage UnitTests
  32. * @copyright Copyright (c) 2005-2009 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_Photos
  36. */
  37. class Zend_Gdata_PhotosOnlineTest extends PHPUnit_Framework_TestCase
  38. {
  39. protected $photos = null;
  40. public function setUp()
  41. {
  42. $user = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_EMAIL');
  43. $pass = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_PASSWORD');
  44. $service = 'lh2';
  45. $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
  46. $this->photos = new Zend_Gdata_Photos($client);
  47. }
  48. /**
  49. * Verify that a given property is set to a specific value
  50. * and that the getter and magic variable return the same value.
  51. *
  52. * @param object $obj The object to be interrogated.
  53. * @param string $name The name of the property to be verified.
  54. * @param string $secondName 2nd level accessor function name
  55. * @param object $value The expected value of the property.
  56. */
  57. protected function verifyProperty($obj, $name, $secondName, $value)
  58. {
  59. $propName = $name;
  60. $propGetter = "get" . ucfirst($name);
  61. $secondGetter = "get" . ucfirst($secondName);
  62. $this->assertEquals($obj->$propGetter(), $obj->$propName);
  63. $this->assertEquals($value, $obj->$propGetter()->$secondGetter());
  64. }
  65. public function createAlbum()
  66. {
  67. $client = $this->photos;
  68. $album = new Zend_Gdata_Photos_AlbumEntry();
  69. $album->setTitle($client->newTitle("testAlbum"));
  70. $album->setCategory(array($client->newCategory(
  71. 'http://schemas.google.com/photos/2007#album',
  72. 'http://schemas.google.com/g/2005#kind')));
  73. $newAlbum = $client->insertAlbumEntry($album);
  74. $this->assertEquals($album->getTitle(), $newAlbum->getTitle());
  75. $this->assertEquals($newAlbum->getTitle(), $client->getAlbumEntry($newAlbum->getLink('self')->href)->getTitle());
  76. $albumFeedUri = $newAlbum->getLink('http://schemas.google.com/g/2005#feed')->href;
  77. $albumFeed = $client->getAlbumFeed($albumFeedUri);
  78. $this->verifyProperty($albumFeed, "title", "text", "testAlbum");
  79. return $newAlbum;
  80. }
  81. public function createPhoto($album)
  82. {
  83. $client = $this->photos;
  84. $fd = $client->newMediaFileSource('Zend/Gdata/_files/testImage.jpg');
  85. $fd->setContentType('image/jpeg');
  86. $photo = new Zend_Gdata_Photos_PhotoEntry();
  87. $photo->setMediaSource($fd);
  88. $photo->setTitle($client->newTitle("test photo"));
  89. $photo->setCategory(array($client->newCategory(
  90. 'http://schemas.google.com/photos/2007#photo',
  91. 'http://schemas.google.com/g/2005#kind')));
  92. $newPhoto = $client->insertPhotoEntry($photo, $album);
  93. $this->assertEquals($photo->getTitle(), $newPhoto->getTitle());
  94. $this->assertEquals($newPhoto->getTitle(), $client->getPhotoEntry($newPhoto->getLink('self')->href)->getTitle());
  95. $photoFeedUri = $newPhoto->getLink('http://schemas.google.com/g/2005#feed')->href;
  96. $photoFeed = $client->getPhotoFeed($photoFeedUri);
  97. $this->verifyProperty($photoFeed, "title", "text", "test photo");
  98. return $newPhoto;
  99. }
  100. public function updatePhotoMetaData()
  101. {
  102. $client = $this->photos;
  103. $album = $this->createAlbum();
  104. $insertedEntry = $this->createPhoto($album);
  105. $insertedEntry->title->text = "New Photo";
  106. $insertedEntry->summary->text = "Photo caption";
  107. $keywords = new Zend_Gdata_Media_Extension_MediaKeywords();
  108. $keywords->setText("foo, bar, baz");
  109. $insertedEntry->mediaGroup->keywords = $keywords;
  110. $updatedEntry = $insertedEntry->save();
  111. return array($updatedEntry, $album);
  112. }
  113. public function createComment($photo)
  114. {
  115. $client = $this->photos;
  116. $comment = new Zend_Gdata_Photos_CommentEntry();
  117. $comment->setTitle($client->newTitle("test comment"));
  118. $comment->setContent($client->newContent("test comment"));
  119. $comment->setCategory(array($client->newCategory(
  120. 'http://schemas.google.com/photos/2007#comment',
  121. 'http://schemas.google.com/g/2005#kind')));
  122. $newComment = $client->insertCommentEntry($comment, $photo);
  123. $this->assertEquals($comment->getContent(), $newComment->getContent());
  124. $this->assertEquals($newComment->getContent(), $client->getCommentEntry($newComment->getLink('self')->href)->getContent());
  125. return $newComment;
  126. }
  127. public function createTag($photo)
  128. {
  129. $client = $this->photos;
  130. $tag = new Zend_Gdata_Photos_TagEntry();
  131. $tag->setTitle($client->newTitle("test tag"));
  132. $tag->setContent($client->newContent("test tag"));
  133. $tag->setCategory(array($client->newCategory(
  134. 'http://schemas.google.com/photos/2007#tag',
  135. 'http://schemas.google.com/g/2005#kind')));
  136. $newTag = $client->insertTagEntry($tag, $photo);
  137. $this->assertEquals($tag->getTitle(), $newTag->getTitle());
  138. $this->assertEquals($newTag->getTitle(), $client->getTagEntry($newTag->getLink('self')->href)->getTitle());
  139. return $newTag;
  140. }
  141. public function testCreateAlbumAndUploadPhoto()
  142. {
  143. $client = $this->photos;
  144. $album = $this->createAlbum();
  145. $photo = $this->createPhoto($album);
  146. // Clean up the mess
  147. $client->deletePhotoEntry($photo, true);
  148. $client->deleteAlbumEntry($album, true);
  149. }
  150. public function testUpdatePhotoMetadata()
  151. {
  152. $client = $this->photos;
  153. $dataArray = $this->updatePhotoMetaData();
  154. $updatedPhoto = $dataArray[0];
  155. $album = $dataArray[1];
  156. $this->assertTrue($updatedPhoto instanceof Zend_Gdata_Photos_PhotoEntry);
  157. // Clean up the mess
  158. $client->deletePhotoEntry($updatedPhoto, true);
  159. $client->deleteAlbumEntry($album, true);
  160. }
  161. public function testUserFeedAndEntry()
  162. {
  163. $client = $this->photos;
  164. $userEntryUri = "http://picasaweb.google.com/data/entry/api/user/" .
  165. constant('TESTS_ZEND_GDATA_PHOTOS_USERNAME');
  166. $userEntry = $client->getUserEntry($userEntryUri);
  167. $this->verifyProperty($userEntry, "id", "text",
  168. "http://picasaweb.google.com/data/entry/api/user/" .
  169. constant('TESTS_ZEND_GDATA_PHOTOS_USERNAME'));
  170. $userFeed = $client->getUserFeed(constant('TESTS_ZEND_GDATA_PHOTOS_USERNAME'));
  171. $this->verifyProperty($userFeed, "id", "text",
  172. "http://picasaweb.google.com/data/feed/api/user/" .
  173. constant('TESTS_ZEND_GDATA_PHOTOS_USERNAME'));
  174. }
  175. public function testCreatePhotoCommentAndTag()
  176. {
  177. $client = $this->photos;
  178. $album = $this->createAlbum();
  179. $photo = $this->createPhoto($album);
  180. $comment = $this->createComment($photo);
  181. $tag = $this->createTag($photo);
  182. // Clean up the mess
  183. $client->deleteTagEntry($tag, true);
  184. $client->deleteCommentEntry($comment, true);
  185. $client->deletePhotoEntry($photo, true);
  186. $client->deleteAlbumEntry($album, true);
  187. }
  188. public function testInvalidEntryFetchingAndInserting()
  189. {
  190. $client = $this->photos;
  191. try {
  192. $userEntry = $client->getUserEntry(null);
  193. } catch (Exception $e) {
  194. $this->assertTrue($e instanceof Zend_Gdata_App_InvalidArgumentException);
  195. }
  196. try {
  197. $userEntry = $client->getAlbumEntry(null);
  198. } catch (Exception $e) {
  199. $this->assertTrue($e instanceof Zend_Gdata_App_InvalidArgumentException);
  200. }
  201. try {
  202. $photoEntry = $client->getPhotoEntry(null);
  203. } catch (Exception $e) {
  204. $this->assertTrue($e instanceof Zend_Gdata_App_InvalidArgumentException);
  205. }
  206. try {
  207. $tagEntry = $client->getTagEntry(null);
  208. } catch (Exception $e) {
  209. $this->assertTrue($e instanceof Zend_Gdata_App_InvalidArgumentException);
  210. }
  211. try {
  212. $commentEntry = $client->getCommentEntry(null);
  213. } catch (Exception $e) {
  214. $this->assertTrue($e instanceof Zend_Gdata_App_InvalidArgumentException);
  215. }
  216. try {
  217. $photo = new Zend_Gdata_Photos_PhotoEntry();
  218. $result = $client->insertPhotoEntry($photo, null);
  219. } catch (Exception $e) {
  220. $this->assertTrue($e instanceof Zend_Gdata_App_InvalidArgumentException);
  221. }
  222. try {
  223. $comment = new Zend_Gdata_Photos_CommentEntry();
  224. $result = $client->insertCommentEntry($comment, null);
  225. } catch (Exception $e) {
  226. $this->assertTrue($e instanceof Zend_Gdata_App_InvalidArgumentException);
  227. }
  228. try {
  229. $tag = new Zend_Gdata_Photos_TagEntry();
  230. $result = $client->insertTagEntry($tag, null);
  231. } catch (Exception $e) {
  232. $this->assertTrue($e instanceof Zend_Gdata_App_InvalidArgumentException);
  233. }
  234. }
  235. public function testInvalidFeedFetching()
  236. {
  237. $client = $this->photos;
  238. try {
  239. $albumFeed = $client->getAlbumFeed(null);
  240. } catch (Exception $e) {
  241. $this->assertTrue($e instanceof Zend_Gdata_App_InvalidArgumentException);
  242. }
  243. }
  244. }