Skip to content

Commit 2d9f5f8

Browse files
committed
Merge pull request #71 from ianbarber/master
Fixing error handling and object conversion for array types
2 parents 4fb7a0a + 8698757 commit 2d9f5f8

7 files changed

Lines changed: 170 additions & 10 deletions

File tree

src/Google/Http/REST.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,13 @@ public static function decodeHttpResponse($response)
7171
$err .= ": ($code) $body";
7272
}
7373

74-
throw new Google_Service_Exception($err, $code, null, $decoded['error']['errors']);
74+
$errors = null;
75+
// Specific check for APIs which don't return error details, such as Blogger.
76+
if (isset($decoded['error']) && isset($decoded['error']['errors'])) {
77+
$errors = $decoded['error']['errors'];
78+
}
79+
80+
throw new Google_Service_Exception($err, $code, null, $errors);
7581
}
7682

7783
// Only attempt to decode the response, if the response code wasn't (204) 'no content'

src/Google/Model.php

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,23 +113,42 @@ public function toSimpleObject()
113113
$props = $reflect->getProperties(ReflectionProperty::IS_PUBLIC);
114114
foreach ($props as $member) {
115115
$name = $member->getName();
116-
if ($this->$name instanceof Google_Model) {
117-
$object->$name = $this->$name->toSimpleObject();
118-
} else if ($this->$name !== null) {
119-
$object->$name = $this->$name;
116+
$result = $this->getSimpleValue($this->$name);
117+
if ($result != null) {
118+
$object->$name = $result;
120119
}
121120
}
122121

123122
// Process all other data.
124123
foreach ($this->data as $key => $val) {
125-
if ($val instanceof Google_Model) {
126-
$object->$key = $val->toSimpleObject();
127-
} else if ($val !== null) {
128-
$object->$key = $val;
124+
$result = $this->getSimpleValue($val);
125+
if ($result != null) {
126+
$object->$key = $result;
129127
}
130128
}
131129
return $object;
132130
}
131+
132+
/**
133+
* Handle different types of values, primarily
134+
* other objects and map and array data types.
135+
*/
136+
private function getSimpleValue($value)
137+
{
138+
if ($value instanceof Google_Model) {
139+
return $value->toSimpleObject();
140+
} else if (is_array($value)) {
141+
$return = array();
142+
foreach ($value as $key => $a_value) {
143+
$a_value = $this->getSimpleValue($a_value);
144+
if ($a_value != null) {
145+
$return[$key] = $a_value;
146+
}
147+
}
148+
return $return;
149+
}
150+
return $value;
151+
}
133152

134153
/**
135154
* Returns true only if the array is associative.

tests/AllTests.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@
2929
require_once 'pagespeed/AllPageSpeedTests.php';
3030
require_once 'urlshortener/AllUrlShortenerTests.php';
3131
require_once 'plus/PlusTest.php';
32+
require_once 'youtube/YouTubeTest.php';
3233

3334
class AllTests {
3435
public static function suite() {
3536
$suite = new PHPUnit_Framework_TestSuite();
3637
$suite->setName('All Google API PHP Client tests');
38+
$suite->addTestSuite(YouTubeTests::suite());
3739
$suite->addTestSuite(AllTasksTests::suite());
3840
$suite->addTestSuite(AllPageSpeedTests::suite());
3941
$suite->addTestSuite(AllUrlShortenerTests::suite());

tests/OAuthHelper.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@
2222
"https://www.googleapis.com/auth/plus.me",
2323
"https://www.googleapis.com/auth/urlshortener",
2424
"https://www.googleapis.com/auth/tasks",
25-
"https://www.googleapis.com/auth/adsense"
25+
"https://www.googleapis.com/auth/adsense",
26+
"https://www.googleapis.com/auth/youtube"
2627
));
2728
$client->setRedirectUri("urn:ietf:wg:oauth:2.0:oob");
29+
$client->setAccessType("offline");
2830
// Visit https://code.google.com/apis/console to
2931
// generate your oauth2_client_id, oauth2_client_secret, and to
3032
// register your oauth2_redirect_uri.

tests/general/ApiModelTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,18 @@ public function testJsonStructure() {
2828
$model2->publicC = 12345;
2929
$model2->publicD = null;
3030
$model->publicB = $model2;
31+
$model3 = new Google_Model();
32+
$model3->publicE = 54321;
33+
$model3->publicF = null;
34+
$model->publicG = array($model3, "hello");
3135
$string = json_encode($model->toSimpleObject());
3236
$data = json_decode($string, true);
3337
$this->assertEquals(12345, $data['publicB']['publicC']);
3438
$this->assertEquals("This is a string", $data['publicA']);
3539
$this->assertArrayNotHasKey("publicD", $data['publicB']);
40+
$this->assertArrayHasKey("publicE", $data['publicG'][0]);
41+
$this->assertArrayNotHasKey("publicF", $data['publicG'][0]);
42+
$this->assertEquals("hello", $data['publicG'][1]);
3643
$this->assertArrayNotHasKey("data", $data);
3744
}
3845
}

tests/general/RestTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,5 +113,45 @@ public function testCreateRequestUri() {
113113
$value = $this->rest->createRequestUri($basePath, '/plus', $params);
114114
$this->assertEquals("http://localhost/plus?u=%40me%2F", $value);
115115
}
116+
117+
/**
118+
* @expectedException Google_Service_Exception
119+
*/
120+
public function testBadErrorFormatting()
121+
{
122+
$request = new Google_Http_Request("/a/b");
123+
$request->setResponseHttpCode(500);
124+
$request->setResponseBody('{
125+
"error": {
126+
"code": 500,
127+
"message": null
128+
}
129+
}');
130+
Google_Http_Rest::decodeHttpResponse($request);
131+
}
132+
133+
/**
134+
* @expectedException Google_Service_Exception
135+
*/
136+
public function tesProperErrorFormatting()
137+
{
138+
$request = new Google_Http_Request("/a/b");
139+
$request->setResponseHttpCode(401);
140+
$request->setResponseBody('{
141+
error: {
142+
errors: [
143+
{
144+
"domain": "global",
145+
"reason": "authError",
146+
"message": "Invalid Credentials",
147+
"locationType": "header",
148+
"location": "Authorization",
149+
}
150+
],
151+
"code": 401,
152+
"message": "Invalid Credentials"
153+
}');
154+
Google_Http_Rest::decodeHttpResponse($request);
155+
}
116156
}
117157

tests/youtube/YouTubeTest.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
/*
3+
* Copyright 2011 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
require_once 'Google/Service/YouTube.php';
19+
20+
class YouTubeTests extends PHPUnit_Framework_TestSuite {
21+
public static function suite() {
22+
$suite = new PHPUnit_Framework_TestSuite();
23+
$suite->setName('Google YouTube API tests');
24+
$suite->addTestSuite('YouTubeTest');
25+
return $suite;
26+
}
27+
}
28+
29+
class YouTubeTest extends BaseTest {
30+
/** @var Google_PlusService */
31+
public $plus;
32+
public function __construct() {
33+
parent::__construct();
34+
$this->youtube = new Google_Service_YouTube($this->getClient());
35+
}
36+
37+
public function testMissingFieldsAreNull() {
38+
$parts = "id,brandingSettings";
39+
$opts = array("mine" => true);
40+
$channels = $this->youtube->channels->listChannels($parts, $opts);
41+
42+
$newChannel = new Google_Service_YouTube_Channel();
43+
$newChannel->setId($channels[0]->getId());
44+
$newChannel->setBrandingSettings($channels[0]->getBrandingSettings());
45+
46+
$simpleOriginal = $channels[0]->toSimpleObject();
47+
$simpleNew = $newChannel->toSimpleObject();
48+
49+
$this->assertObjectHasAttribute('etag', $simpleOriginal);
50+
$this->assertObjectNotHasAttribute('etag', $simpleNew);
51+
52+
$owner_details = new Google_Service_YouTube_ChannelContentOwnerDetails();
53+
$owner_details->setTimeLinked("123456789");
54+
$o_channel = new Google_Service_YouTube_Channel();
55+
$o_channel->setContentOwnerDetails($owner_details);
56+
$simpleManual = $o_channel->toSimpleObject();
57+
$this->assertObjectHasAttribute('timeLinked', $simpleManual->contentOwnerDetails);
58+
$this->assertObjectNotHasAttribute('contentOwner', $simpleManual->contentOwnerDetails);
59+
60+
$owner_details = new Google_Service_YouTube_ChannelContentOwnerDetails();
61+
$owner_details->timeLinked = "123456789";
62+
$o_channel = new Google_Service_YouTube_Channel();
63+
$o_channel->setContentOwnerDetails($owner_details);
64+
$simpleManual = $o_channel->toSimpleObject();
65+
$this->assertObjectHasAttribute('timeLinked', $simpleManual->contentOwnerDetails);
66+
$this->assertObjectNotHasAttribute('contentOwner', $simpleManual->contentOwnerDetails);
67+
68+
$owner_details = new Google_Service_YouTube_ChannelContentOwnerDetails();
69+
$owner_details['timeLinked'] = "123456789";
70+
$o_channel = new Google_Service_YouTube_Channel();
71+
$o_channel->setContentOwnerDetails($owner_details);
72+
$simpleManual = $o_channel->toSimpleObject();
73+
$this->assertObjectHasAttribute('timeLinked', $simpleManual->contentOwnerDetails);
74+
$this->assertObjectNotHasAttribute('contentOwner', $simpleManual->contentOwnerDetails);
75+
76+
$ping = new Google_Service_YouTube_ChannelConversionPing();
77+
$ping->setContext("hello");
78+
$pings = new Google_Service_YouTube_ChannelConversionPings();
79+
$pings->setPings(array($ping));
80+
$simplePings = $pings->toSimpleObject();
81+
$this->assertObjectHasAttribute('context', $simplePings->pings[0]);
82+
$this->assertObjectNotHasAttribute('conversionUrl', $simplePings->pings[0]);
83+
}
84+
}

0 commit comments

Comments
 (0)