HealthOnlineTest.php 13 KB

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