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+ class BatchTest extends PHPUnit_Framework_TestCase
19+ {
20+
21+ public function setUp ()
22+ {
23+ $ this ->client = $ this ->getMockBuilder ("Google_Client " )
24+ ->disableOriginalConstructor ()
25+ ->getMock ();
26+ }
27+
28+ public function testBasicFunctionality ()
29+ {
30+ $ this ->client ->expects ($ this ->once ())
31+ ->method ("getBasePath " )
32+ ->will ($ this ->returnValue ("base_path " ));
33+ $ batch = new Google_Http_Batch ($ this ->client );
34+ $ this ->assertAttributeEquals ("base_path " , "root_url " , $ batch );
35+ $ this ->assertAttributeEquals ("batch " , "batch_path " , $ batch );
36+ }
37+
38+ public function testExtractionOfRootUrlFromService ()
39+ {
40+ $ this ->client ->expects ($ this ->never ())
41+ ->method ("getBasePath " );
42+ $ service = new Google_Service ($ this ->client );
43+ $ service ->rootUrl = "root_url_dummy " ;
44+ $ service ->batchPath = "batch_path_dummy " ;
45+ $ batch = $ service ->createBatch ();
46+ $ this ->assertInstanceOf ("Google_Http_Batch " , $ batch );
47+ $ this ->assertAttributeEquals ("root_url_dummy " , "root_url " , $ batch );
48+ $ this ->assertAttributeEquals ("batch_path_dummy " , "batch_path " , $ batch );
49+ }
50+
51+ public function testExecuteCustomRootUrlBatchPath ()
52+ {
53+ $ io = $ this ->getMockBuilder ('Google_IO_Abstract ' )
54+ ->disableOriginalConstructor ()
55+ ->setMethods (['makeRequest ' , 'needsQuirk ' , 'executeRequest ' , 'setOptions ' , 'setTimeout ' , 'getTimeout ' ])
56+ ->getMock ();
57+ $ req = null ;
58+ $ io ->expects ($ this ->once ())
59+ ->method ("makeRequest " )
60+ ->will ($ this ->returnCallback (function ($ request ) use (&$ req ) {
61+ $ req = $ request ;
62+ return $ request ;
63+ }));
64+ $ this ->client ->expects ($ this ->once ())
65+ ->method ("getIo " )
66+ ->will ($ this ->returnValue ($ io ));
67+ $ batch = new Google_Http_Batch ($ this ->client , false , 'https://www.example.com/ ' , 'bat ' );
68+ $ this ->assertNull ($ batch ->execute ());
69+ $ this ->assertInstanceOf ("Google_Http_Request " , $ req );
70+ $ this ->assertEquals ("https://www.example.com/bat " , $ req ->getUrl ());
71+ }
72+
73+ public function testExecuteBodySerialization ()
74+ {
75+ $ io = $ this ->getMockBuilder ('Google_IO_Abstract ' )
76+ ->disableOriginalConstructor ()
77+ ->setMethods (['makeRequest ' , 'needsQuirk ' , 'executeRequest ' , 'setOptions ' , 'setTimeout ' , 'getTimeout ' ])
78+ ->getMock ();
79+ $ req = null ;
80+ $ io ->expects ($ this ->once ())
81+ ->method ("makeRequest " )
82+ ->will ($ this ->returnCallback (function ($ request ) use (&$ req ) {
83+ $ req = $ request ;
84+ return $ request ;
85+ }));
86+ $ this ->client ->expects ($ this ->once ())
87+ ->method ("getIo " )
88+ ->will ($ this ->returnValue ($ io ));
89+ $ batch = new Google_Http_Batch ($ this ->client , "BOUNDARY_TEXT " , 'https://www.example.com/ ' , 'bat ' );
90+ $ req1 = new Google_Http_Request ("https://www.example.com/req1 " );
91+ $ req2 = new Google_Http_Request ("https://www.example.com/req2 " , 'POST ' , array (), 'POSTBODY ' );
92+ $ batch ->add ($ req1 , '1 ' );
93+ $ batch ->add ($ req2 , '2 ' );
94+ $ this ->assertNull ($ batch ->execute ());
95+ $ this ->assertInstanceOf ("Google_Http_Request " , $ req );
96+ $ format = <<<'EOF'
97+ --BOUNDARY_TEXT
98+ Content-Type: application/http
99+ Content-Transfer-Encoding: binary
100+ MIME-Version: 1.0
101+ Content-ID: 1
102+
103+ GET /req1? HTTP/1.1
104+
105+
106+ --BOUNDARY_TEXT
107+ Content-Type: application/http
108+ Content-Transfer-Encoding: binary
109+ MIME-Version: 1.0
110+ Content-ID: 2
111+
112+ POST /req2? HTTP/1.1
113+
114+ POSTBODY
115+
116+ --BOUNDARY_TEXT--
117+ EOF;
118+ $ this ->assertEquals ($ format , $ req ->getPostBody ());
119+ }
120+
121+ }
0 commit comments