HealthOnlineTest.php 14 KB

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