ApiModelTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /**
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. class ApiModelTest extends BaseTest
  21. {
  22. private $calendarData = '{
  23. "kind": "calendar#event",
  24. "etag": "\"-kteSF26GsdKQ5bfmcd4H3_-u3g/MTE0NTUyNTAxOTk0MjAwMA\"",
  25. "id": "1234566",
  26. "status": "confirmed",
  27. "htmlLink": "https://www.google.com/calendar/event?eid=N",
  28. "created": "2006-04-13T14:22:08.000Z",
  29. "updated": "2006-04-20T09:23:39.942Z",
  30. "summary": "Evening Jolt Q3 CTFL",
  31. "description": "6.30 - Adminning\n9.30 - Game",
  32. "creator": {
  33. "email": "ian@example.com",
  34. "displayName": "Ian Test",
  35. "self": true
  36. },
  37. "organizer": {
  38. "email": "ian@example.com",
  39. "displayName": "Ian Test",
  40. "self": true
  41. },
  42. "start": {
  43. "date": "2006-04-23"
  44. },
  45. "end": {
  46. "date": "2006-04-24"
  47. },
  48. "iCalUID": "5gi2ac493nnrfdfd7jhesafget8@google.com",
  49. "sequence": 0,
  50. "reminders": {
  51. "useDefault": false
  52. }
  53. }';
  54. public function testIntentionalNulls()
  55. {
  56. $data = json_decode($this->calendarData, true);
  57. $event = new Google_Service_Calendar_Event($data);
  58. $obj = json_decode(json_encode($event->toSimpleObject()), true);
  59. $this->assertArrayHasKey('date', $obj['start']);
  60. $this->assertArrayNotHasKey('dateTime', $obj['start']);
  61. $date = new Google_Service_Calendar_EventDateTime();
  62. $date->setDate(Google_Model::NULL_VALUE);
  63. $event->setStart($date);
  64. $obj = json_decode(json_encode($event->toSimpleObject()), true);
  65. $this->assertNull($obj['start']['date']);
  66. $this->assertArrayHasKey('date', $obj['start']);
  67. $this->assertArrayNotHasKey('dateTime', $obj['start']);
  68. }
  69. public function testModelMutation()
  70. {
  71. $data = json_decode($this->calendarData, true);
  72. $event = new Google_Service_Calendar_Event($data);
  73. $date = new Google_Service_Calendar_EventDateTime();
  74. date_default_timezone_set('UTC');
  75. $dateString = Date("c");
  76. $summary = "hello";
  77. $date->setDate($dateString);
  78. $event->setStart($date);
  79. $event->setEnd($date);
  80. $event->setSummary($summary);
  81. $simpleEvent = $event->toSimpleObject();
  82. $this->assertEquals($dateString, $simpleEvent->start->date);
  83. $this->assertEquals($dateString, $simpleEvent->end->date);
  84. $this->assertEquals($summary, $simpleEvent->summary);
  85. $event2 = new Google_Service_Calendar_Event();
  86. $this->assertNull($event2->getStart());
  87. }
  88. public function testVariantTypes()
  89. {
  90. $feature = new Google_Service_MapsEngine_Feature();
  91. $geometry = new Google_Service_MapsEngine_GeoJsonPoint();
  92. $geometry->setCoordinates(array(1, 0));
  93. $feature->setGeometry($geometry);
  94. $data = json_decode(json_encode($feature->toSimpleObject()), true);
  95. $this->assertEquals('Point', $data['geometry']['type']);
  96. $this->assertEquals(1, $data['geometry']['coordinates'][0]);
  97. }
  98. public function testOddMappingNames()
  99. {
  100. $creative = new Google_Service_AdExchangeBuyer_Creative();
  101. $creative->setAccountId('12345');
  102. $creative->setBuyerCreativeId('12345');
  103. $creative->setAdvertiserName('Hi');
  104. $creative->setHTMLSnippet("<p>Foo!</p>");
  105. $creative->setClickThroughUrl(array('http://somedomain.com'));
  106. $creative->setWidth(100);
  107. $creative->setHeight(100);
  108. $data = json_decode(json_encode($creative->toSimpleObject()), true);
  109. $this->assertEquals($data['HTMLSnippet'], "<p>Foo!</p>");
  110. $this->assertEquals($data['width'], 100);
  111. $this->assertEquals($data['height'], 100);
  112. $this->assertEquals($data['accountId'], "12345");
  113. }
  114. public function testJsonStructure()
  115. {
  116. $model = new Google_Model();
  117. $model->publicA = "This is a string";
  118. $model2 = new Google_Model();
  119. $model2->publicC = 12345;
  120. $model2->publicD = null;
  121. $model->publicB = $model2;
  122. $model3 = new Google_Model();
  123. $model3->publicE = 54321;
  124. $model3->publicF = null;
  125. $model->publicG = array($model3, "hello", false);
  126. $model->publicH = false;
  127. $model->publicI = 0;
  128. $string = json_encode($model->toSimpleObject());
  129. $data = json_decode($string, true);
  130. $this->assertEquals(12345, $data['publicB']['publicC']);
  131. $this->assertEquals("This is a string", $data['publicA']);
  132. $this->assertArrayNotHasKey("publicD", $data['publicB']);
  133. $this->assertArrayHasKey("publicE", $data['publicG'][0]);
  134. $this->assertArrayNotHasKey("publicF", $data['publicG'][0]);
  135. $this->assertEquals("hello", $data['publicG'][1]);
  136. $this->assertEquals(false, $data['publicG'][2]);
  137. $this->assertArrayNotHasKey("data", $data);
  138. $this->assertEquals(false, $data['publicH']);
  139. $this->assertEquals(0, $data['publicI']);
  140. }
  141. public function testIssetPropertyOnModel()
  142. {
  143. $model = new Google_Model();
  144. $model['foo'] = 'bar';
  145. $this->assertTrue(isset($model->foo));
  146. }
  147. public function testUnsetPropertyOnModel()
  148. {
  149. $model = new Google_Model();
  150. $model['foo'] = 'bar';
  151. unset($model->foo);
  152. $this->assertFalse(isset($model->foo));
  153. }
  154. public function testCollection()
  155. {
  156. $data = json_decode(
  157. '{
  158. "kind": "calendar#events",
  159. "id": "1234566",
  160. "etag": "abcdef",
  161. "totalItems": 4,
  162. "items": [
  163. {"id": 1},
  164. {"id": 2},
  165. {"id": 3},
  166. {"id": 4}
  167. ]
  168. }',
  169. true
  170. );
  171. $collection = new Google_Service_Calendar_Events($data);
  172. $this->assertEquals(4, count($collection));
  173. $count = 0;
  174. foreach ($collection as $col) {
  175. $count++;
  176. }
  177. $this->assertEquals(4, $count);
  178. $this->assertEquals(1, $collection[0]->id);
  179. }
  180. }