HealthOnlineTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2006 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/Health.php';
  23. require_once 'Zend/Gdata/Health/Query.php';
  24. require_once 'Zend/Gdata/ClientLogin.php';
  25. /**
  26. * @package Zend_Gdata
  27. * @subpackage UnitTests
  28. */
  29. class Zend_Gdata_HealthOnlineTest extends PHPUnit_Framework_TestCase
  30. {
  31. public function setUp()
  32. {
  33. $this->user = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_EMAIL');
  34. $this->pass = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_PASSWORD');
  35. $serviceName = Zend_Gdata_Health::HEALTH_SERVICE_NAME;
  36. $client = Zend_Gdata_ClientLogin::getHttpClient($this->user, $this->pass, $serviceName);
  37. $this->health = new Zend_Gdata_Health($client, 'google-MyPHPApp-v1.0');
  38. }
  39. private function setupProfileID()
  40. {
  41. $profileListFeed = $this->health->getHealthProfileListFeed();
  42. $profileID = $profileListFeed->entry[0]->getProfileID();
  43. $this->health->setProfileID($profileID);
  44. }
  45. public function testSetProfileID()
  46. {
  47. $this->health->setProfileID('123456790');
  48. $this->assertEquals('123456790', $this->health->getProfileID());
  49. }
  50. public function testGetHealthProfileListFeedWithoutUsingClientLogin()
  51. {
  52. $client = new Zend_Gdata_HttpClient();
  53. $this->health = new Zend_Gdata_Health($client);
  54. try {
  55. $feed = $this->health->getHealthProfileListFeed();
  56. $this->fail('Expecting to catch Zend_Gdata_App_AuthException');
  57. } catch (Exception $e) {
  58. $this->assertThat($e, $this->isInstanceOf('Zend_Gdata_App_AuthException'),
  59. 'Expecting Zend_Gdata_App_AuthException, got '.get_class($e));
  60. }
  61. }
  62. public function testGetHealthProfileFeedWithoutUsingClientLogin()
  63. {
  64. try {
  65. $feed = $this->health->getHealthProfileFeed();
  66. $this->fail('Expecting to catch Zend_Gdata_App_AuthException');
  67. } catch (Exception $e) {
  68. $this->assertThat($e, $this->isInstanceOf('Zend_Gdata_App_AuthException'),
  69. 'Expecting Zend_Gdata_App_AuthException, got '.get_class($e));
  70. }
  71. }
  72. public function testUseH9()
  73. {
  74. $serviceName = Zend_Gdata_Health::H9_SANDBOX_SERVICE_NAME;
  75. $client = Zend_Gdata_ClientLogin::getHttpClient($this->user, $this->pass, $serviceName);
  76. $h9 = new Zend_Gdata_Health($client, 'google-MyPHPApp-v1.0', true);
  77. $profileListFeed = $h9->getHealthProfileListFeed();
  78. $profileID = $profileListFeed->entry[0]->getProfileID();
  79. $h9->setProfileID($profileID);
  80. // query profile feed
  81. $feed1 = $h9->getHealthProfileFeed();
  82. $this->assertTrue($feed1 instanceof Zend_Gdata_Health_ProfileFeed);
  83. foreach ($feed1->getEntries() as $entry) {
  84. $this->assertTrue($entry instanceof Zend_Gdata_Health_ProfileEntry);
  85. $this->assertEquals($entry->getHttpClient(), $feed1->getHttpClient());
  86. }
  87. // send CCR
  88. $subject = "Title of your notice goes here";
  89. $body = "Notice body can contain <b>html</b> entities";
  90. $type = "html";
  91. $ccrXML = file_get_contents('Zend/Gdata/Health/_files/ccr_notice_sample.xml', true);
  92. $responseEntry = $h9->sendHealthNotice($subject, $body, $type, $ccrXML);
  93. $this->assertTrue($responseEntry instanceof Zend_Gdata_Health_ProfileEntry);
  94. $this->assertEquals($subject, $responseEntry->title->text);
  95. $this->assertEquals($body, $responseEntry->content->text);
  96. $this->assertEquals($type, $responseEntry->content->type);
  97. $this->assertXmlStringEqualsXmlString($responseEntry->getCcr()->saveXML(), $ccrXML);
  98. }
  99. public function testGetHealthProfileListFeed()
  100. {
  101. // no query
  102. $feed1 = $this->health->getHealthProfileListFeed();
  103. $this->assertTrue($feed1 instanceof Zend_Gdata_Health_ProfileListFeed);
  104. foreach ($feed1->getEntries() as $entry) {
  105. $this->assertTrue($entry instanceof Zend_Gdata_Health_ProfileListEntry);
  106. $this->assertEquals($entry->getHttpClient(), $feed1->getHttpClient());
  107. }
  108. // with query object
  109. $query = new Zend_Gdata_Health_Query('https://www.google.com/health/feeds/profile/list');
  110. $feed2 = $this->health->getHealthProfileListFeed($query);
  111. $this->assertTrue($feed2 instanceof Zend_Gdata_Health_ProfileListFeed);
  112. foreach ($feed2->entry as $entry) {
  113. $this->assertTrue($entry instanceof Zend_Gdata_Health_ProfileListEntry);
  114. $this->assertEquals($entry->getHttpClient(), $feed2->getHttpClient());
  115. }
  116. // with direct query string
  117. $feed3 = $this->health->getHealthProfileListFeed('https://www.google.com/health/feeds/profile/list');
  118. $this->assertTrue($feed3 instanceof Zend_Gdata_Health_ProfileListFeed);
  119. foreach ($feed3->entry as $entry) {
  120. $this->assertTrue($entry instanceof Zend_Gdata_Health_ProfileListEntry);
  121. $this->assertEquals($entry->getHttpClient(), $feed3->getHttpClient());
  122. }
  123. $this->assertEquals($feed1->saveXML(), $feed2->saveXML());
  124. $this->assertEquals($feed1->saveXML(), $feed3->saveXML());
  125. $this->assertEquals($feed2->saveXML(), $feed3->saveXML());
  126. }
  127. public function testGetProfileFeedNoQuery()
  128. {
  129. $this->setupProfileID();
  130. // no query, digest=false
  131. $profileFeed = $this->health->getHealthProfileFeed();
  132. $this->assertTrue($profileFeed instanceof Zend_Gdata_Health_ProfileFeed);
  133. $this->assertTrue(count($profileFeed->entry) > 1, 'digest=false, should have multiple <entry> elements');
  134. foreach ($profileFeed->entry as $entry) {
  135. $this->assertTrue($entry instanceof Zend_Gdata_Health_ProfileEntry);
  136. $ccr = $entry->getCcr();
  137. $this->assertTrue($ccr instanceof Zend_Gdata_Health_Extension_Ccr);
  138. $this->assertEquals($entry->getHttpClient(), $profileFeed->getHttpClient());
  139. }
  140. }
  141. public function testGetProfileFeedByQuery()
  142. {
  143. $this->setupProfileID();
  144. $profileID = $this->health->getProfileID();
  145. // with direct query string
  146. $feed1 = $this->health->getHealthProfileFeed(
  147. "https://www.google.com/health/feeds/profile/ui/{$profileID}?digest=true");
  148. $this->assertTrue($feed1 instanceof Zend_Gdata_Health_ProfileFeed);
  149. $this->assertTrue(count($feed1->entry) === 1, 'digest=true, expected a single <entry> element');
  150. foreach ($feed1->entry as $entry) {
  151. $this->assertTrue($entry instanceof Zend_Gdata_Health_ProfileEntry);
  152. $ccr = $entry->getCcr();
  153. $this->assertTrue($ccr instanceof Zend_Gdata_Health_Extension_Ccr);
  154. $this->assertEquals($entry->getHttpClient(), $feed1->getHttpClient());
  155. }
  156. // with query object
  157. $query = new Zend_Gdata_Health_Query("https://www.google.com/health/feeds/profile/ui/{$profileID}");
  158. $query->setDigest('true');
  159. $feed2 = $this->health->getHealthProfileFeed($query);
  160. $this->assertTrue($feed2 instanceof Zend_Gdata_Health_ProfileFeed);
  161. $this->assertTrue(count($feed2->entry) === 1, 'digest=true, expected a single <entry> element');
  162. foreach ($feed2->entry as $entry) {
  163. $this->assertTrue($entry instanceof Zend_Gdata_Health_ProfileEntry);
  164. $ccr = $entry->getCcr();
  165. $this->assertTrue($ccr instanceof Zend_Gdata_Health_Extension_Ccr);
  166. $this->assertEquals($entry->getHttpClient(), $feed2->getHttpClient());
  167. }
  168. $this->assertEquals($feed1->saveXML(), $feed2->saveXML());
  169. }
  170. public function testGetProfileEntryNoQuery()
  171. {
  172. try {
  173. $entry = $this->health->getHealthProfileEntry();
  174. $this->fail('Expecting to catch Zend_Gdata_App_InvalidArgumentException');
  175. } catch (Exception $e) {
  176. $this->assertThat($e, $this->isInstanceOf('Zend_Gdata_App_InvalidArgumentException'),
  177. 'Expecting Zend_Gdata_App_InvalidArgumentException, got '.get_class($e));
  178. }
  179. }
  180. public function testGetProfileEntry()
  181. {
  182. $this->setupProfileID();
  183. $profileID = $this->health->getProfileID();
  184. $feed = $this->health->getHealthProfileFeed();
  185. $entryFromProfileQuery = $feed->entry[0];
  186. $this->assertTrue($entryFromProfileQuery instanceof Zend_Gdata_Health_ProfileEntry);
  187. // direct query string
  188. $entry1 = $this->health->getHealthProfileEntry($entryFromProfileQuery->id->text);
  189. $this->assertTrue($entry1 instanceof Zend_Gdata_Health_ProfileEntry);
  190. // query object
  191. $query = new Zend_Gdata_Health_Query("https://www.google.com/health/feeds/profile/ui/{$profileID}");
  192. $entry2 = $this->health->getHealthProfileEntry($query);
  193. $this->assertTrue($entry2 instanceof Zend_Gdata_Health_ProfileEntry);
  194. $this->assertEquals($entryFromProfileQuery->getHttpClient(), $entry1->getHttpClient());
  195. $this->assertEquals($entryFromProfileQuery->getHttpClient(), $entry2->getHttpClient());
  196. $this->assertEquals($entry1->getHttpClient(), $entry2->getHttpClient());
  197. $this->assertXmlStringEqualsXmlString($entryFromProfileQuery->getCcr()->saveXML(), $entry1->getCcr()->saveXML());
  198. $this->assertXmlStringEqualsXmlString($entryFromProfileQuery->getCcr()->saveXML(), $entry2->getCcr()->saveXML());
  199. $this->assertXmlStringEqualsXmlString($entry1->getCcr()->saveXML(), $entry2->getCcr()->saveXML());
  200. }
  201. public function testSendNoticeWithoutUsingClientLogin()
  202. {
  203. try {
  204. $responseEntry = $this->health->sendHealthNotice("", "");
  205. $this->fail('Expecting to catch Zend_Gdata_App_AuthException');
  206. } catch (Exception $e) {
  207. $this->assertThat($e, $this->isInstanceOf('Zend_Gdata_App_AuthException'),
  208. 'Expecting Zend_Gdata_App_AuthException, got '.get_class($e));
  209. }
  210. }
  211. public function testSendNoticeWithoutCcr()
  212. {
  213. $this->setupProfileID();
  214. $profileID = $this->health->getProfileID();
  215. $subject = "Title of your notice goes here";
  216. $body = "Notice body goes here";
  217. $responseEntry = $this->health->sendHealthNotice($subject, $body);
  218. $this->assertTrue($responseEntry instanceof Zend_Gdata_Health_ProfileEntry);
  219. $this->assertEquals($subject, $responseEntry->title->text);
  220. $this->assertEquals($body, $responseEntry->content->text);
  221. $this->assertNull($responseEntry->getCcr());
  222. }
  223. public function testSendNoticeWithoutCcrUsingDirectInsert()
  224. {
  225. $this->setupProfileID();
  226. $profileID = $this->health->getProfileID();
  227. $subject = "Title of your notice goes here";
  228. $body = "Notice body goes here";
  229. $entry = new Zend_Gdata_Health_ProfileEntry();
  230. $author = $this->health->newAuthor();
  231. $author->name = $this->health->newName('John Doe');
  232. $author->email = $this->health->newEmail('user@example.com');
  233. $entry->setAuthor(array(0 => $author));
  234. $entry->title = $this->health->newTitle($subject);
  235. $entry->content = $this->health->newContent($body);
  236. $entry->content->type = 'text';
  237. $ccrXML = file_get_contents('Zend/Gdata/Health/_files/ccr_notice_sample.xml', true);
  238. $entry->setCcr($ccrXML);
  239. $uri = "https://www.google.com/health/feeds/register/ui/{$profileID}";
  240. $responseEntry = $this->health->insertEntry($entry, $uri, 'Zend_Gdata_Health_ProfileEntry');
  241. $this->assertTrue($responseEntry instanceof Zend_Gdata_Health_ProfileEntry);
  242. $this->assertEquals($subject, $responseEntry->title->text);
  243. $this->assertEquals($author->name->text, 'John Doe');
  244. $this->assertEquals($author->email->text, 'user@example.com');
  245. $this->assertEquals($body, $responseEntry->content->text);
  246. }
  247. public function testSendNoticeWithCcr()
  248. {
  249. $this->setupProfileID();
  250. $profileID = $this->health->getProfileID();
  251. $subject = "Title of your notice goes here";
  252. $body = "Notice body can contain <b>html</b> entities";
  253. $type = "html";
  254. $ccrXML = file_get_contents('Zend/Gdata/Health/_files/ccr_notice_sample.xml', true);
  255. $responseEntry = $this->health->sendHealthNotice($subject, $body, $type, $ccrXML);
  256. $this->assertTrue($responseEntry instanceof Zend_Gdata_Health_ProfileEntry);
  257. $this->assertEquals($subject, $responseEntry->title->text);
  258. $this->assertEquals($body, $responseEntry->content->text);
  259. $this->assertEquals($type, $responseEntry->content->type);
  260. $this->assertXmlStringEqualsXmlString($responseEntry->getCcr()->saveXML(), $ccrXML);
  261. }
  262. }