Skip to content

Commit 4172460

Browse files
author
Ian Barber
committed
Fix tests
cURL tests weren't actually testing cURL - now are. Fixed a bug that had been missed because of that. Added null cache test.
1 parent 43bea55 commit 4172460

7 files changed

Lines changed: 96 additions & 23 deletions

File tree

src/Google/Cache/Null.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
17+
1818
require_once "Google/Cache/Abstract.php";
1919
require_once "Google/Cache/Exception.php";
2020

@@ -24,6 +24,11 @@
2424
*/
2525
class Google_Cache_Null extends Google_Cache_Abstract
2626
{
27+
public function __construct(Google_Client $client)
28+
{
29+
30+
}
31+
2732
/**
2833
* @inheritDoc
2934
*/

src/Google/Http/MediaFileUpload.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public function nextChunk($chunk = false)
142142
$chunk
143143
);
144144

145-
if ($client->getClassConfig("Google_Http_Request", "enable_gzip_for_uploads") {
145+
if ($client->getClassConfig("Google_Http_Request", "enable_gzip_for_uploads")) {
146146
$httpRequest->enableGzip();
147147
} else {
148148
$httpRequest->disableGzip();

src/Google/IO/Abstract.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ abstract public function setOptions($options);
6161
* @param $timeout in seconds
6262
*/
6363
abstract public function setTimeout($timeout);
64+
65+
/**
66+
* Get the maximum request time in seconds.
67+
* @return timeout in seconds
68+
*/
69+
abstract public function getTimeout();
6470

6571
/**
6672
* @visible for testing.

src/Google/IO/Curl.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ public function executeRequest(Google_Http_Request $request)
7474
}
7575

7676
$response = curl_exec($curl);
77+
if ($response === false) {
78+
throw new Google_IO_Exception(curl_error($curl));
79+
}
7780
$headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
7881

7982
$responseBody = substr($response, $headerSize);
@@ -106,4 +109,12 @@ public function setTimeout($timeout)
106109
$this->options[CURLOPT_CONNECTTIMEOUT] = $timeout;
107110
$this->options[CURLOPT_TIMEOUT] = $timeout;
108111
}
112+
113+
/**
114+
* Get the maximum request time in seconds.
115+
* @return timeout in seconds
116+
*/
117+
public function getTimeout() {
118+
return $this->options[CURLOPT_TIMEOUT];
119+
}
109120
}

src/Google/IO/Stream.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,13 @@ public function executeRequest(Google_Http_Request $request)
101101
// time situation.
102102
@$fh = fopen($url, 'r', false, $context);
103103

104-
if (isset($this->options[self::TIMEOUT])) {
105-
stream_set_timeout($fh, $this->options[self::TIMEOUT]);
106-
}
107-
108104
$response_data = false;
109105
$respHttpCode = self::UNKNOWN_CODE;
110106
if ($fh) {
107+
if (isset($this->options[self::TIMEOUT])) {
108+
stream_set_timeout($fh, $this->options[self::TIMEOUT]);
109+
}
110+
111111
$response_data = stream_get_contents($fh);
112112
fclose($fh);
113113

@@ -147,6 +147,14 @@ public function setTimeout($timeout)
147147
$this->options[self::TIMEOUT] = $timeout;
148148
}
149149

150+
/**
151+
* Get the maximum request time in seconds.
152+
* @return timeout in seconds
153+
*/
154+
public function getTimeout() {
155+
return $this->options[self::TIMEOUT];
156+
}
157+
150158
protected function getHttpResponseCode($response_headers)
151159
{
152160
$header_count = count($response_headers);

tests/general/CacheTest.php

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@
2222
require_once 'Google/Cache/File.php';
2323
require_once 'Google/Cache/Memcache.php';
2424
require_once 'Google/Cache/Apc.php';
25+
require_once 'Google/Cache/Null.php';
2526

2627
class CacheTest extends BaseTest {
27-
28+
2829
public function testFile() {
2930
$dir = sys_get_temp_dir() . '/google-api-php-client/tests';
3031
$client = $this->getClient();
@@ -36,10 +37,34 @@ public function testFile() {
3637
$cache = new Google_Cache_File($client);
3738
$cache->set('foo', 'bar');
3839
$this->assertEquals($cache->get('foo'), 'bar');
39-
40+
4041
$this->getSetDelete($cache);
4142
}
4243

44+
public function testNull()
45+
{
46+
if (!function_exists('memcache_connect')) {
47+
$this->markTestSkipped('Test requires memcache');
48+
}
49+
$client = $this->getClient();
50+
$cache = new Google_Cache_Null($client);
51+
$client->setCache($cache);
52+
53+
$cache->set('foo', 'bar');
54+
$cache->delete('foo');
55+
$this->assertEquals(false, $cache->get('foo'));
56+
57+
$cache->set('foo.1', 'bar.1');
58+
$this->assertEquals($cache->get('foo.1'), false);
59+
60+
$cache->set('foo', 'baz');
61+
$this->assertEquals($cache->get('foo'), false);
62+
63+
$cache->set('foo', null);
64+
$cache->delete('foo');
65+
$this->assertEquals($cache->get('foo'), false);
66+
}
67+
4368
public function testMemcache() {
4469
if (!function_exists('memcache_connect')) {
4570
$this->markTestSkipped('Test requires memcache');
@@ -48,12 +73,12 @@ public function testMemcache() {
4873
if (!$client->getClassConfig('Google_Cache_Memcache', 'host')) {
4974
$this->markTestSkipped('Test requires memcache host specified');
5075
}
51-
76+
5277
$cache = new Google_Cache_Memcache($client);
53-
54-
$this->getSetDelete($cache);
78+
79+
$this->getSetDelete($cache);
5580
}
56-
81+
5782
public function testAPC() {
5883
if (!function_exists('apc_add')) {
5984
$this->markTestSkipped('Test requires APC');
@@ -63,15 +88,15 @@ public function testAPC() {
6388
}
6489
$client = $this->getClient();
6590
$cache = new Google_Cache_APC($client);
66-
91+
6792
$this->getSetDelete($cache);
6893
}
69-
94+
7095
public function getSetDelete($cache) {
7196
$cache->set('foo', 'bar');
7297
$cache->delete('foo');
7398
$this->assertEquals(false, $cache->get('foo'));
74-
99+
75100
$cache->set('foo.1', 'bar.1');
76101
$cache->delete('foo.1');
77102
$this->assertEquals($cache->get('foo.1'), false);
@@ -89,7 +114,7 @@ public function getSetDelete($cache) {
89114
$cache->set('foo', $obj);
90115
$cache->delete('foo');
91116
$this->assertEquals($cache->get('foo'), false);
92-
117+
93118
$cache->set('foo.1', 'bar.1');
94119
$this->assertEquals($cache->get('foo.1'), 'bar.1');
95120

tests/general/IoTest.php

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
class IoTest extends BaseTest
2727
{
28-
28+
2929
public function testExecutorSelection()
3030
{
3131
$client = $this->getClient();
@@ -36,6 +36,12 @@ public function testExecutorSelection()
3636
$this->assertInstanceOf('Google_IO_Stream', $client->getIo());
3737
}
3838

39+
public function testStreamSetTimeout()
40+
{
41+
$io = new Google_IO_Stream($this->getClient());
42+
$this->timeoutChecker($io);
43+
}
44+
3945
public function testStreamParseHttpResponseBody()
4046
{
4147
$io = new Google_IO_Stream($this->getClient());
@@ -71,31 +77,37 @@ public function testStreamInvalidRequest()
7177
$io = new Google_IO_Stream($this->getClient());
7278
$this->invalidRequest($io);
7379
}
74-
80+
81+
public function testCurlSetTimeout()
82+
{
83+
$io = new Google_IO_Curl($this->getClient());
84+
$this->timeoutChecker($io);
85+
}
86+
7587
public function testCurlParseHttpResponseBody()
7688
{
77-
$io = new Google_IO_Stream($this->getClient());
89+
$io = new Google_IO_Curl($this->getClient());
7890
$this->responseChecker($io);
7991
}
8092

8193
public function testCurlProcessEntityRequest()
8294
{
8395
$client = $this->getClient();
84-
$io = new Google_IO_Stream($client);
96+
$io = new Google_IO_Curl($client);
8597
$this->processEntityRequest($io, $client);
8698
}
8799

88100
public function testCurlCacheHit()
89101
{
90102
$client = $this->getClient();
91-
$io = new Google_IO_Stream($client);
103+
$io = new Google_IO_Curl($client);
92104
$this->cacheHit($io, $client);
93105
}
94106

95107
public function testCurlAuthCache()
96108
{
97109
$client = $this->getClient();
98-
$io = new Google_IO_Stream($client);
110+
$io = new Google_IO_Curl($client);
99111
$this->authCache($io, $client);
100112
}
101113

@@ -104,12 +116,18 @@ public function testCurlAuthCache()
104116
*/
105117
public function testCurlInvalidRequest()
106118
{
107-
$io = new Google_IO_Stream($this->getClient());
119+
$io = new Google_IO_Curl($this->getClient());
108120
$this->invalidRequest($io);
109121
}
110122

111123
// Asserting Functions
112124

125+
public function timeoutChecker($io)
126+
{
127+
$this->assertEquals(100, $io->getTimeout());
128+
$io->setTimeout(120);
129+
$this->assertEquals(120, $io->getTimeout());
130+
}
113131

114132
public function invalidRequest($io)
115133
{

0 commit comments

Comments
 (0)