Skip to content

Commit c522a3d

Browse files
authored
fix createBatch call (#1137)
1 parent 8f011b5 commit c522a3d

File tree

4 files changed

+37
-6
lines changed

4 files changed

+37
-6
lines changed

examples/batch.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
want to execute with keys of our choice - these
5959
keys will be reflected in the returned array.
6060
************************************************/
61-
$batch = new Google_Http_Batch($client);
61+
$batch = $service->createBatch();
6262
$optParams = array('filter' => 'free-ebooks');
6363
$req1 = $service->volumes->listVolumes('Henry David Thoreau', $optParams);
6464
$batch->add($req1, "thoreau");

src/Google/Http/Batch.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,20 @@ class Google_Http_Batch
4242
/** @var Google_Client */
4343
private $client;
4444

45-
public function __construct(Google_Client $client)
46-
{
45+
private $rootUrl;
46+
47+
private $batchPath;
48+
49+
public function __construct(
50+
Google_Client $client,
51+
$boundary = false,
52+
$rootUrl = null,
53+
$batchPath = null
54+
) {
4755
$this->client = $client;
48-
$this->boundary = mt_rand();
56+
$this->boundary = $boundary ?: mt_rand();
57+
$this->rootUrl = rtrim($rootUrl ?: $this->client->getConfig('base_path'), '/');
58+
$this->batchPath = $batchPath ?: self::BATCH_PATH;
4959
}
5060

5161
public function add(RequestInterface $request, $key = false)
@@ -104,7 +114,7 @@ public function execute()
104114

105115
$body .= "--{$this->boundary}--";
106116
$body = trim($body);
107-
$url = Google_Client::API_BASE_PATH . '/' . self::BATCH_PATH;
117+
$url = $this->rootUrl . '/' . $this->batchPath;
108118
$headers = array(
109119
'Content-Type' => sprintf('multipart/mixed; boundary=%s', $this->boundary),
110120
'Content-Length' => strlen($body),

tests/BaseTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ private function createClient()
8787
$client->setCache($this->getCache());
8888
}
8989

90-
9190
return $client;
9291
}
9392

tests/Google/ServiceTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,30 @@ public function isAssociativeArray($array)
3131
}
3232
}
3333

34+
class TestService extends Google_Service
35+
{
36+
public $batchPath = 'batch/test';
37+
}
38+
3439
class Google_ServiceTest extends PHPUnit_Framework_TestCase
3540
{
41+
public function testCreateBatch()
42+
{
43+
$response = $this->getMock('Psr\Http\Message\ResponseInterface');
44+
$client = $this->getMock('Google_Client');
45+
$client
46+
->expects($this->once())
47+
->method('execute')
48+
->with($this->callback(function ($request) {
49+
$this->assertEquals('/batch/test', $request->getRequestTarget());
50+
return $request;
51+
}))
52+
->will($this->returnValue($response));
53+
$model = new TestService($client);
54+
$batch = $model->createBatch();
55+
$this->assertInstanceOf('Google_Http_Batch', $batch);
56+
$batch->execute();
57+
}
3658

3759
public function testModel()
3860
{

0 commit comments

Comments
 (0)