ZF-5190: Add support for the Google Health Data API to Zend_Gdata.
[zend/radio.git] / tests / Zend / Gdata / HealthOnlineTest.php
blob441dae5a200ae782df594d5a1e341b5352ed4bb0
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
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.
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
22 require_once 'Zend/Gdata/Health.php';
23 require_once 'Zend/Gdata/Health/Query.php';
24 require_once 'Zend/Gdata/ClientLogin.php';
26 /**
27 * @package Zend_Gdata
28 * @subpackage UnitTests
30 class Zend_Gdata_HealthOnlineTest extends PHPUnit_Framework_TestCase
33 public function setUp()
35 $this->user = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_EMAIL');
36 $this->pass = constant('TESTS_ZEND_GDATA_CLIENTLOGIN_PASSWORD');
37 $serviceName = Zend_Gdata_Health::HEALTH_SERVICE_NAME;
38 $client = Zend_Gdata_ClientLogin::getHttpClient($this->user, $this->pass, $serviceName);
39 $this->health = new Zend_Gdata_Health($client, 'google-MyPHPApp-v1.0');
42 private function setupProfileID()
44 $profileListFeed = $this->health->getHealthProfileListFeed();
45 $profileID = $profileListFeed->entry[0]->getProfileID();
46 $this->health->setProfileID($profileID);
49 public function testSetProfileID()
51 $this->health->setProfileID('123456790');
52 $this->assertEquals('123456790', $this->health->getProfileID());
55 public function testGetHealthProfileListFeedWithoutUsingClientLogin()
57 $client = new Zend_Gdata_HttpClient();
58 $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));
69 public function testGetHealthProfileFeedWithoutUsingClientLogin()
71 try {
72 $feed = $this->health->getHealthProfileFeed();
73 $this->fail('Expecting to catch Zend_Gdata_App_AuthException');
74 } catch (Exception $e) {
75 $this->assertThat($e, $this->isInstanceOf('Zend_Gdata_App_AuthException'),
76 'Expecting Zend_Gdata_App_AuthException, got '.get_class($e));
80 public function testUseH9()
82 $serviceName = Zend_Gdata_Health::H9_SANDBOX_SERVICE_NAME;
83 $client = Zend_Gdata_ClientLogin::getHttpClient($this->user, $this->pass, $serviceName);
84 $h9 = new Zend_Gdata_Health($client, 'google-MyPHPApp-v1.0', true);
86 $profileListFeed = $h9->getHealthProfileListFeed();
87 $profileID = $profileListFeed->entry[0]->getProfileID();
88 $h9->setProfileID($profileID);
90 // query profile feed
91 $feed1 = $h9->getHealthProfileFeed();
92 $this->assertTrue($feed1 instanceof Zend_Gdata_Health_ProfileFeed);
93 foreach ($feed1->getEntries() as $entry) {
94 $this->assertTrue($entry instanceof Zend_Gdata_Health_ProfileEntry);
95 $this->assertEquals($entry->getHttpClient(), $feed1->getHttpClient());
98 // send CCR
99 $subject = "Title of your notice goes here";
100 $body = "Notice body can contain <b>html</b> entities";
101 $type = "html";
102 $ccrXML = file_get_contents('Zend/Gdata/Health/_files/ccr_notice_sample.xml', true);
104 $responseEntry = $h9->sendHealthNotice($subject, $body, $type, $ccrXML);
106 $this->assertTrue($responseEntry instanceof Zend_Gdata_Health_ProfileEntry);
107 $this->assertEquals($subject, $responseEntry->title->text);
108 $this->assertEquals($body, $responseEntry->content->text);
109 $this->assertEquals($type, $responseEntry->content->type);
110 $this->assertXmlStringEqualsXmlString($responseEntry->getCcr()->saveXML(), $ccrXML);
113 public function testGetHealthProfileListFeed()
115 // no query
116 $feed1 = $this->health->getHealthProfileListFeed();
117 $this->assertTrue($feed1 instanceof Zend_Gdata_Health_ProfileListFeed);
118 foreach ($feed1->getEntries() as $entry) {
119 $this->assertTrue($entry instanceof Zend_Gdata_Health_ProfileListEntry);
120 $this->assertEquals($entry->getHttpClient(), $feed1->getHttpClient());
123 // with query object
124 $query = new Zend_Gdata_Health_Query('https://www.google.com/health/feeds/profile/list');
125 $feed2 = $this->health->getHealthProfileListFeed($query);
126 $this->assertTrue($feed2 instanceof Zend_Gdata_Health_ProfileListFeed);
127 foreach ($feed2->entry as $entry) {
128 $this->assertTrue($entry instanceof Zend_Gdata_Health_ProfileListEntry);
129 $this->assertEquals($entry->getHttpClient(), $feed2->getHttpClient());
132 // with direct query string
133 $feed3 = $this->health->getHealthProfileListFeed('https://www.google.com/health/feeds/profile/list');
134 $this->assertTrue($feed3 instanceof Zend_Gdata_Health_ProfileListFeed);
135 foreach ($feed3->entry as $entry) {
136 $this->assertTrue($entry instanceof Zend_Gdata_Health_ProfileListEntry);
137 $this->assertEquals($entry->getHttpClient(), $feed3->getHttpClient());
140 $this->assertEquals($feed1->saveXML(), $feed2->saveXML());
141 $this->assertEquals($feed1->saveXML(), $feed3->saveXML());
142 $this->assertEquals($feed2->saveXML(), $feed3->saveXML());
146 public function testGetProfileFeedNoQuery()
148 $this->setupProfileID();
150 // no query, digest=false
151 $profileFeed = $this->health->getHealthProfileFeed();
152 $this->assertTrue($profileFeed instanceof Zend_Gdata_Health_ProfileFeed);
153 $this->assertTrue(count($profileFeed->entry) > 1, 'digest=false, should have multiple <entry> elements');
154 foreach ($profileFeed->entry as $entry) {
155 $this->assertTrue($entry instanceof Zend_Gdata_Health_ProfileEntry);
156 $ccr = $entry->getCcr();
157 $this->assertTrue($ccr instanceof Zend_Gdata_Health_Extension_Ccr);
158 $this->assertEquals($entry->getHttpClient(), $profileFeed->getHttpClient());
162 public function testGetProfileFeedByQuery()
164 $this->setupProfileID();
165 $profileID = $this->health->getProfileID();
167 // with direct query string
168 $feed1 = $this->health->getHealthProfileFeed(
169 "https://www.google.com/health/feeds/profile/ui/{$profileID}?digest=true");
170 $this->assertTrue($feed1 instanceof Zend_Gdata_Health_ProfileFeed);
171 $this->assertTrue(count($feed1->entry) === 1, 'digest=true, expected a single <entry> element');
172 foreach ($feed1->entry as $entry) {
173 $this->assertTrue($entry instanceof Zend_Gdata_Health_ProfileEntry);
174 $ccr = $entry->getCcr();
175 $this->assertTrue($ccr instanceof Zend_Gdata_Health_Extension_Ccr);
176 $this->assertEquals($entry->getHttpClient(), $feed1->getHttpClient());
179 // with query object
180 $query = new Zend_Gdata_Health_Query("https://www.google.com/health/feeds/profile/ui/{$profileID}");
181 $query->setDigest('true');
182 $feed2 = $this->health->getHealthProfileFeed($query);
183 $this->assertTrue($feed2 instanceof Zend_Gdata_Health_ProfileFeed);
184 $this->assertTrue(count($feed2->entry) === 1, 'digest=true, expected a single <entry> element');
185 foreach ($feed2->entry as $entry) {
186 $this->assertTrue($entry instanceof Zend_Gdata_Health_ProfileEntry);
187 $ccr = $entry->getCcr();
188 $this->assertTrue($ccr instanceof Zend_Gdata_Health_Extension_Ccr);
189 $this->assertEquals($entry->getHttpClient(), $feed2->getHttpClient());
192 $this->assertEquals($feed1->saveXML(), $feed2->saveXML());
195 public function testGetProfileEntryNoQuery()
197 try {
198 $entry = $this->health->getHealthProfileEntry();
199 $this->fail('Expecting to catch Zend_Gdata_App_InvalidArgumentException');
200 } catch (Exception $e) {
201 $this->assertThat($e, $this->isInstanceOf('Zend_Gdata_App_InvalidArgumentException'),
202 'Expecting Zend_Gdata_App_InvalidArgumentException, got '.get_class($e));
206 public function testGetProfileEntry()
208 $this->setupProfileID();
209 $profileID = $this->health->getProfileID();
211 $feed = $this->health->getHealthProfileFeed();
212 $entryFromProfileQuery = $feed->entry[0];
213 $this->assertTrue($entryFromProfileQuery instanceof Zend_Gdata_Health_ProfileEntry);
215 // direct query string
216 $entry1 = $this->health->getHealthProfileEntry($entryFromProfileQuery->id->text);
217 $this->assertTrue($entry1 instanceof Zend_Gdata_Health_ProfileEntry);
219 // query object
220 $query = new Zend_Gdata_Health_Query("https://www.google.com/health/feeds/profile/ui/{$profileID}");
221 $entry2 = $this->health->getHealthProfileEntry($query);
222 $this->assertTrue($entry2 instanceof Zend_Gdata_Health_ProfileEntry);
224 $this->assertEquals($entryFromProfileQuery->getHttpClient(), $entry1->getHttpClient());
225 $this->assertEquals($entryFromProfileQuery->getHttpClient(), $entry2->getHttpClient());
226 $this->assertEquals($entry1->getHttpClient(), $entry2->getHttpClient());
228 $this->assertXmlStringEqualsXmlString($entryFromProfileQuery->getCcr()->saveXML(), $entry1->getCcr()->saveXML());
229 $this->assertXmlStringEqualsXmlString($entryFromProfileQuery->getCcr()->saveXML(), $entry2->getCcr()->saveXML());
230 $this->assertXmlStringEqualsXmlString($entry1->getCcr()->saveXML(), $entry2->getCcr()->saveXML());
233 public function testSendNoticeWithoutUsingClientLogin()
235 try {
236 $responseEntry = $this->health->sendHealthNotice("", "");
237 $this->fail('Expecting to catch Zend_Gdata_App_AuthException');
238 } catch (Exception $e) {
239 $this->assertThat($e, $this->isInstanceOf('Zend_Gdata_App_AuthException'),
240 'Expecting Zend_Gdata_App_AuthException, got '.get_class($e));
244 public function testSendNoticeWithoutCcr()
246 $this->setupProfileID();
247 $profileID = $this->health->getProfileID();
249 $subject = "Title of your notice goes here";
250 $body = "Notice body goes here";
252 $responseEntry = $this->health->sendHealthNotice($subject, $body);
254 $this->assertTrue($responseEntry instanceof Zend_Gdata_Health_ProfileEntry);
255 $this->assertEquals($subject, $responseEntry->title->text);
256 $this->assertEquals($body, $responseEntry->content->text);
257 $this->assertNull($responseEntry->getCcr());
260 public function testSendNoticeWithoutCcrUsingDirectInsert()
262 $this->setupProfileID();
263 $profileID = $this->health->getProfileID();
265 $subject = "Title of your notice goes here";
266 $body = "Notice body goes here";
268 $entry = new Zend_Gdata_Health_ProfileEntry();
270 $author = $this->health->newAuthor();
271 $author->name = $this->health->newName('John Doe');
272 $author->email = $this->health->newEmail('user@example.com');
273 $entry->setAuthor(array(0 => $author));
275 $entry->title = $this->health->newTitle($subject);
276 $entry->content = $this->health->newContent($body);
277 $entry->content->type = 'text';
279 $ccrXML = file_get_contents('Zend/Gdata/Health/_files/ccr_notice_sample.xml', true);
280 $entry->setCcr($ccrXML);
282 $uri = "https://www.google.com/health/feeds/register/ui/{$profileID}";
283 $responseEntry = $this->health->insertEntry($entry, $uri, 'Zend_Gdata_Health_ProfileEntry');
285 $this->assertTrue($responseEntry instanceof Zend_Gdata_Health_ProfileEntry);
286 $this->assertEquals($subject, $responseEntry->title->text);
287 $this->assertEquals($author->name->text, 'John Doe');
288 $this->assertEquals($author->email->text, 'user@example.com');
289 $this->assertEquals($body, $responseEntry->content->text);
292 public function testSendNoticeWithCcr()
294 $this->setupProfileID();
295 $profileID = $this->health->getProfileID();
297 $subject = "Title of your notice goes here";
298 $body = "Notice body can contain <b>html</b> entities";
299 $type = "html";
300 $ccrXML = file_get_contents('Zend/Gdata/Health/_files/ccr_notice_sample.xml', true);
302 $responseEntry = $this->health->sendHealthNotice($subject, $body, $type, $ccrXML);
304 $this->assertTrue($responseEntry instanceof Zend_Gdata_Health_ProfileEntry);
305 $this->assertEquals($subject, $responseEntry->title->text);
306 $this->assertEquals($body, $responseEntry->content->text);
307 $this->assertEquals($type, $responseEntry->content->type);
308 $this->assertXmlStringEqualsXmlString($responseEntry->getCcr()->saveXML(), $ccrXML);