ResourceTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. class Test_Google_Service extends Google_Service
  21. {
  22. public function __construct(Google_Client $client)
  23. {
  24. parent::__construct($client);
  25. $this->rootUrl = "";
  26. $this->servicePath = "";
  27. $this->version = "v1beta1";
  28. $this->serviceName = "test";
  29. }
  30. }
  31. class ResourceTest extends PHPUnit_Framework_TestCase
  32. {
  33. private $client;
  34. private $service;
  35. private $logger;
  36. public function setUp()
  37. {
  38. $this->client = $this->getMockBuilder("Google_Client")
  39. ->disableOriginalConstructor()
  40. ->getMock();
  41. $this->logger = $this->getMockBuilder("Google_Logger_Null")
  42. ->disableOriginalConstructor()
  43. ->getMock();
  44. $this->client->expects($this->any())
  45. ->method("getClassConfig")
  46. ->will($this->returnCallback(function($class, $type) {
  47. if (!is_string($class)) {
  48. $class = get_class($class);
  49. }
  50. $configMap = array(
  51. "Google_Auth_Simple" => array(
  52. "developer_key" => "testKey"
  53. ),
  54. );
  55. return isset($configMap[$class][$type]) ? $configMap[$class][$type] : null;
  56. }));
  57. $this->client->expects($this->any())
  58. ->method("getLogger")
  59. ->will($this->returnValue($this->logger));
  60. $this->client->expects($this->any())
  61. ->method("shouldDefer")
  62. ->will($this->returnValue(true));
  63. $this->client->expects($this->any())
  64. ->method("getBasePath")
  65. ->will($this->returnValue("https://test.example.com"));
  66. $this->client->expects($this->any())
  67. ->method("getAuth")
  68. ->will($this->returnValue(new Google_Auth_Simple($this->client)));
  69. $this->service = new Test_Google_Service($this->client);
  70. }
  71. public function testCallFailure()
  72. {
  73. $resource = new Google_Service_Resource(
  74. $this->service,
  75. "test",
  76. "testResource",
  77. array("methods" =>
  78. array(
  79. "testMethod" => array(
  80. "parameters" => array(),
  81. "path" => "method/path",
  82. "httpMethod" => "POST",
  83. )
  84. )
  85. )
  86. );
  87. $this->setExpectedException(
  88. "Google_Exception",
  89. "Unknown function: test->testResource->someothermethod()"
  90. );
  91. $resource->call("someothermethod", array());
  92. }
  93. public function testCallSimple()
  94. {
  95. $resource = new Google_Service_Resource(
  96. $this->service,
  97. "test",
  98. "testResource",
  99. array("methods" =>
  100. array(
  101. "testMethod" => array(
  102. "parameters" => array(),
  103. "path" => "method/path",
  104. "httpMethod" => "POST",
  105. )
  106. )
  107. )
  108. );
  109. $request = $resource->call("testMethod", array(array()));
  110. $this->assertEquals("https://test.example.com/method/path?key=testKey", $request->getUrl());
  111. $this->assertEquals("POST", $request->getRequestMethod());
  112. }
  113. public function testCallServiceDefinedRoot()
  114. {
  115. $this->service->rootUrl = "https://sample.example.com";
  116. $resource = new Google_Service_Resource(
  117. $this->service,
  118. "test",
  119. "testResource",
  120. array("methods" =>
  121. array(
  122. "testMethod" => array(
  123. "parameters" => array(),
  124. "path" => "method/path",
  125. "httpMethod" => "POST",
  126. )
  127. )
  128. )
  129. );
  130. $request = $resource->call("testMethod", array(array()));
  131. $this->assertEquals("https://sample.example.com/method/path?key=testKey", $request->getUrl());
  132. $this->assertEquals("POST", $request->getRequestMethod());
  133. }
  134. }