BatchTest.php 4.1 KB

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