File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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 " );
Original file line number Diff line number Diff 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 ),
Original file line number Diff line number Diff line change @@ -87,7 +87,6 @@ private function createClient()
8787 $ client ->setCache ($ this ->getCache ());
8888 }
8989
90-
9190 return $ client ;
9291 }
9392
Original file line number Diff line number Diff 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+
3439class 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 {
You can’t perform that action at this time.
0 commit comments