HealthOnlineTest.php 14 KB

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