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.
16 * @package Zend_Gdata_Health
17 * @subpackage UnitTests
18 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
23 require_once 'Zend/Gdata/Health.php';
24 require_once 'Zend/Gdata/Health/Query.php';
25 require_once 'Zend/Gdata/ClientLogin.php';
29 * @package Zend_Gdata_Health
30 * @subpackage UnitTests
31 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
32 * @license http://framework.zend.com/license/new-bsd New BSD License
34 * @group Zend_Gdata_Health
36 class Zend_Gdata_HealthOnlineTest
extends PHPUnit_Framework_TestCase
39 public function setUp()
41 $this->user
= constant('TESTS_ZEND_GDATA_CLIENTLOGIN_EMAIL');
42 $this->pass
= constant('TESTS_ZEND_GDATA_CLIENTLOGIN_PASSWORD');
43 $serviceName = Zend_Gdata_Health
::HEALTH_SERVICE_NAME
;
44 $client = Zend_Gdata_ClientLogin
::getHttpClient($this->user
, $this->pass
, $serviceName);
45 $this->health
= new Zend_Gdata_Health($client, 'google-MyPHPApp-v1.0');
48 private function setupProfileID()
50 $profileListFeed = $this->health
->getHealthProfileListFeed();
51 $profileID = $profileListFeed->entry
[0]->getProfileID();
52 $this->health
->setProfileID($profileID);
55 public function testSetProfileID()
57 $this->health
->setProfileID('123456790');
58 $this->assertEquals('123456790', $this->health
->getProfileID());
61 public function testGetHealthProfileListFeedWithoutUsingClientLogin()
63 $client = new Zend_Gdata_HttpClient();
64 $this->health
= new Zend_Gdata_Health($client);
67 $feed = $this->health
->getHealthProfileListFeed();
68 $this->fail('Expecting to catch Zend_Gdata_App_AuthException');
69 } catch (Exception
$e) {
70 $this->assertThat($e, $this->isInstanceOf('Zend_Gdata_App_AuthException'),
71 'Expecting Zend_Gdata_App_AuthException, got '.get_class($e));
75 public function testGetHealthProfileFeedWithoutUsingClientLogin()
78 $feed = $this->health
->getHealthProfileFeed();
79 $this->fail('Expecting to catch Zend_Gdata_App_AuthException');
80 } catch (Exception
$e) {
81 $this->assertThat($e, $this->isInstanceOf('Zend_Gdata_App_AuthException'),
82 'Expecting Zend_Gdata_App_AuthException, got '.get_class($e));
86 public function testUseH9()
88 $serviceName = Zend_Gdata_Health
::H9_SANDBOX_SERVICE_NAME
;
89 $client = Zend_Gdata_ClientLogin
::getHttpClient($this->user
, $this->pass
, $serviceName);
90 $h9 = new Zend_Gdata_Health($client, 'google-MyPHPApp-v1.0', true);
92 $profileListFeed = $h9->getHealthProfileListFeed();
93 $profileID = $profileListFeed->entry
[0]->getProfileID();
94 $h9->setProfileID($profileID);
97 $feed1 = $h9->getHealthProfileFeed();
98 $this->assertTrue($feed1 instanceof Zend_Gdata_Health_ProfileFeed
);
99 foreach ($feed1->getEntries() as $entry) {
100 $this->assertTrue($entry instanceof Zend_Gdata_Health_ProfileEntry
);
101 $this->assertEquals($entry->getHttpClient(), $feed1->getHttpClient());
105 $subject = "Title of your notice goes here";
106 $body = "Notice body can contain <b>html</b> entities";
108 $ccrXML = file_get_contents('Zend/Gdata/Health/_files/ccr_notice_sample.xml', true);
110 $responseEntry = $h9->sendHealthNotice($subject, $body, $type, $ccrXML);
112 $this->assertTrue($responseEntry instanceof Zend_Gdata_Health_ProfileEntry
);
113 $this->assertEquals($subject, $responseEntry->title
->text
);
114 $this->assertEquals($body, $responseEntry->content
->text
);
115 $this->assertEquals($type, $responseEntry->content
->type
);
116 $this->assertXmlStringEqualsXmlString($responseEntry->getCcr()->saveXML(), $ccrXML);
119 public function testGetHealthProfileListFeed()
122 $feed1 = $this->health
->getHealthProfileListFeed();
123 $this->assertTrue($feed1 instanceof Zend_Gdata_Health_ProfileListFeed
);
124 foreach ($feed1->getEntries() as $entry) {
125 $this->assertTrue($entry instanceof Zend_Gdata_Health_ProfileListEntry
);
126 $this->assertEquals($entry->getHttpClient(), $feed1->getHttpClient());
130 $query = new Zend_Gdata_Health_Query('https://www.google.com/health/feeds/profile/list');
131 $feed2 = $this->health
->getHealthProfileListFeed($query);
132 $this->assertTrue($feed2 instanceof Zend_Gdata_Health_ProfileListFeed
);
133 foreach ($feed2->entry
as $entry) {
134 $this->assertTrue($entry instanceof Zend_Gdata_Health_ProfileListEntry
);
135 $this->assertEquals($entry->getHttpClient(), $feed2->getHttpClient());
138 // with direct query string
139 $feed3 = $this->health
->getHealthProfileListFeed('https://www.google.com/health/feeds/profile/list');
140 $this->assertTrue($feed3 instanceof Zend_Gdata_Health_ProfileListFeed
);
141 foreach ($feed3->entry
as $entry) {
142 $this->assertTrue($entry instanceof Zend_Gdata_Health_ProfileListEntry
);
143 $this->assertEquals($entry->getHttpClient(), $feed3->getHttpClient());
146 $this->assertEquals($feed1->saveXML(), $feed2->saveXML());
147 $this->assertEquals($feed1->saveXML(), $feed3->saveXML());
148 $this->assertEquals($feed2->saveXML(), $feed3->saveXML());
152 public function testGetProfileFeedNoQuery()
154 $this->setupProfileID();
156 // no query, digest=false
157 $profileFeed = $this->health
->getHealthProfileFeed();
158 $this->assertTrue($profileFeed instanceof Zend_Gdata_Health_ProfileFeed
);
159 $this->assertTrue(count($profileFeed->entry
) > 1, 'digest=false, should have multiple <entry> elements');
160 foreach ($profileFeed->entry
as $entry) {
161 $this->assertTrue($entry instanceof Zend_Gdata_Health_ProfileEntry
);
162 $ccr = $entry->getCcr();
163 $this->assertTrue($ccr instanceof Zend_Gdata_Health_Extension_Ccr
);
164 $this->assertEquals($entry->getHttpClient(), $profileFeed->getHttpClient());
168 public function testGetProfileFeedByQuery()
170 $this->setupProfileID();
171 $profileID = $this->health
->getProfileID();
173 // with direct query string
174 $feed1 = $this->health
->getHealthProfileFeed(
175 "https://www.google.com/health/feeds/profile/ui/{$profileID}?digest=true");
176 $this->assertTrue($feed1 instanceof Zend_Gdata_Health_ProfileFeed
);
177 $this->assertTrue(count($feed1->entry
) === 1, 'digest=true, expected a single <entry> element');
178 foreach ($feed1->entry
as $entry) {
179 $this->assertTrue($entry instanceof Zend_Gdata_Health_ProfileEntry
);
180 $ccr = $entry->getCcr();
181 $this->assertTrue($ccr instanceof Zend_Gdata_Health_Extension_Ccr
);
182 $this->assertEquals($entry->getHttpClient(), $feed1->getHttpClient());
186 $query = new Zend_Gdata_Health_Query("https://www.google.com/health/feeds/profile/ui/{$profileID}");
187 $query->setDigest('true');
188 $feed2 = $this->health
->getHealthProfileFeed($query);
189 $this->assertTrue($feed2 instanceof Zend_Gdata_Health_ProfileFeed
);
190 $this->assertTrue(count($feed2->entry
) === 1, 'digest=true, expected a single <entry> element');
191 foreach ($feed2->entry
as $entry) {
192 $this->assertTrue($entry instanceof Zend_Gdata_Health_ProfileEntry
);
193 $ccr = $entry->getCcr();
194 $this->assertTrue($ccr instanceof Zend_Gdata_Health_Extension_Ccr
);
195 $this->assertEquals($entry->getHttpClient(), $feed2->getHttpClient());
198 $this->assertEquals($feed1->saveXML(), $feed2->saveXML());
201 public function testGetProfileEntryNoQuery()
204 $entry = $this->health
->getHealthProfileEntry();
205 $this->fail('Expecting to catch Zend_Gdata_App_InvalidArgumentException');
206 } catch (Exception
$e) {
207 $this->assertThat($e, $this->isInstanceOf('Zend_Gdata_App_InvalidArgumentException'),
208 'Expecting Zend_Gdata_App_InvalidArgumentException, got '.get_class($e));
212 public function testGetProfileEntry()
214 $this->setupProfileID();
215 $profileID = $this->health
->getProfileID();
217 $feed = $this->health
->getHealthProfileFeed();
218 $entryFromProfileQuery = $feed->entry
[0];
219 $this->assertTrue($entryFromProfileQuery instanceof Zend_Gdata_Health_ProfileEntry
);
221 // direct query string
222 $entry1 = $this->health
->getHealthProfileEntry($entryFromProfileQuery->id
->text
);
223 $this->assertTrue($entry1 instanceof Zend_Gdata_Health_ProfileEntry
);
226 $query = new Zend_Gdata_Health_Query("https://www.google.com/health/feeds/profile/ui/{$profileID}");
227 $entry2 = $this->health
->getHealthProfileEntry($query);
228 $this->assertTrue($entry2 instanceof Zend_Gdata_Health_ProfileEntry
);
230 $this->assertEquals($entryFromProfileQuery->getHttpClient(), $entry1->getHttpClient());
231 $this->assertEquals($entryFromProfileQuery->getHttpClient(), $entry2->getHttpClient());
232 $this->assertEquals($entry1->getHttpClient(), $entry2->getHttpClient());
234 $this->assertXmlStringEqualsXmlString($entryFromProfileQuery->getCcr()->saveXML(), $entry1->getCcr()->saveXML());
235 $this->assertXmlStringEqualsXmlString($entryFromProfileQuery->getCcr()->saveXML(), $entry2->getCcr()->saveXML());
236 $this->assertXmlStringEqualsXmlString($entry1->getCcr()->saveXML(), $entry2->getCcr()->saveXML());
239 public function testSendNoticeWithoutUsingClientLogin()
242 $responseEntry = $this->health
->sendHealthNotice("", "");
243 $this->fail('Expecting to catch Zend_Gdata_App_AuthException');
244 } catch (Exception
$e) {
245 $this->assertThat($e, $this->isInstanceOf('Zend_Gdata_App_AuthException'),
246 'Expecting Zend_Gdata_App_AuthException, got '.get_class($e));
250 public function testSendNoticeWithoutCcr()
252 $this->setupProfileID();
253 $profileID = $this->health
->getProfileID();
255 $subject = "Title of your notice goes here";
256 $body = "Notice body goes here";
258 $responseEntry = $this->health
->sendHealthNotice($subject, $body);
260 $this->assertTrue($responseEntry instanceof Zend_Gdata_Health_ProfileEntry
);
261 $this->assertEquals($subject, $responseEntry->title
->text
);
262 $this->assertEquals($body, $responseEntry->content
->text
);
263 $this->assertNull($responseEntry->getCcr());
266 public function testSendNoticeWithoutCcrUsingDirectInsert()
268 $this->setupProfileID();
269 $profileID = $this->health
->getProfileID();
271 $subject = "Title of your notice goes here";
272 $body = "Notice body goes here";
274 $entry = new Zend_Gdata_Health_ProfileEntry();
276 $author = $this->health
->newAuthor();
277 $author->name
= $this->health
->newName('John Doe');
278 $author->email
= $this->health
->newEmail('user@example.com');
279 $entry->setAuthor(array(0 => $author));
281 $entry->title
= $this->health
->newTitle($subject);
282 $entry->content
= $this->health
->newContent($body);
283 $entry->content
->type
= 'text';
285 $ccrXML = file_get_contents('Zend/Gdata/Health/_files/ccr_notice_sample.xml', true);
286 $entry->setCcr($ccrXML);
288 $uri = "https://www.google.com/health/feeds/register/ui/{$profileID}";
289 $responseEntry = $this->health
->insertEntry($entry, $uri, 'Zend_Gdata_Health_ProfileEntry');
291 $this->assertTrue($responseEntry instanceof Zend_Gdata_Health_ProfileEntry
);
292 $this->assertEquals($subject, $responseEntry->title
->text
);
293 $this->assertEquals($author->name
->text
, 'John Doe');
294 $this->assertEquals($author->email
->text
, 'user@example.com');
295 $this->assertEquals($body, $responseEntry->content
->text
);
298 public function testSendNoticeWithCcr()
300 $this->setupProfileID();
301 $profileID = $this->health
->getProfileID();
303 $subject = "Title of your notice goes here";
304 $body = "Notice body can contain <b>html</b> entities";
306 $ccrXML = file_get_contents('Zend/Gdata/Health/_files/ccr_notice_sample.xml', true);
308 $responseEntry = $this->health
->sendHealthNotice($subject, $body, $type, $ccrXML);
310 $this->assertTrue($responseEntry instanceof Zend_Gdata_Health_ProfileEntry
);
311 $this->assertEquals($subject, $responseEntry->title
->text
);
312 $this->assertEquals($body, $responseEntry->content
->text
);
313 $this->assertEquals($type, $responseEntry->content
->type
);
314 $this->assertXmlStringEqualsXmlString($responseEntry->getCcr()->saveXML(), $ccrXML);