ApiClientTest

Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.

Defined (1)

The class is defined in the following location(s).

/vendor/google/apiclient/tests/general/ApiClientTest.php  
  1. class ApiClientTest extends BaseTest 
  2. public function testClient() 
  3. $client = new Google_Client(); 
  4. $client->setAccessType('foo'); 
  5. $client->setDeveloperKey('foo'); 
  6. $req = new Google_Http_Request('http://foo.com'); 
  7. $client->getAuth()->sign($req); 
  8. $params = $req->getQueryParams(); 
  9. $this->assertEquals('foo', $params['key']); 
  10.  
  11. $client->setAccessToken(json_encode(array('access_token' => '1'))); 
  12. $this->assertEquals("{\"access_token\":\"1\"}", $client->getAccessToken()); 
  13.  
  14. public function testClientConstructor() 
  15. $this->assertInstanceOf('Google_Client', $this->getClient()); 
  16.  
  17. /** 
  18. * @expectedException Google_Auth_Exception 
  19. */ 
  20. public function testPrepareInvalidScopes() 
  21. $client = new Google_Client(); 
  22.  
  23. $scopes = $client->prepareScopes(); 
  24. $this->assertEquals("", $scopes); 
  25.  
  26. public function testNoAuthIsNull() 
  27. $client = new Google_Client(); 
  28.  
  29. $this->assertNull($client->getAccessToken()); 
  30.  
  31. public function testPrepareService() 
  32. $client = new Google_Client(); 
  33. $client->setScopes(array("scope1", "scope2")); 
  34. $scopes = $client->prepareScopes(); 
  35. $this->assertEquals("scope1 scope2", $scopes); 
  36.  
  37. $client->setScopes(array("", "scope2")); 
  38. $scopes = $client->prepareScopes(); 
  39. $this->assertEquals(" scope2", $scopes); 
  40.  
  41. $client->setScopes("scope2"); 
  42. $client->addScope("scope3"); 
  43. $client->addScope(array("scope4", "scope5")); 
  44. $scopes = $client->prepareScopes(); 
  45. $this->assertEquals("scope2 scope3 scope4 scope5", $scopes); 
  46.  
  47. $client->setClientId('test1'); 
  48. $client->setRedirectUri('http://localhost/'); 
  49. $client->setScopes(array("http://test.com", "scope2")); 
  50. $scopes = $client->prepareScopes(); 
  51. $this->assertEquals("http://test.com scope2", $scopes); 
  52. $this->assertEquals( 
  53. '' 
  54. . 'https://accounts.google.com/o/oauth2/auth' 
  55. . '?response_type=code&redirect_uri=http%3A%2F%2Flocalhost%2F' 
  56. . '&client_id=test1' 
  57. . '&scope=http%3A%2F%2Ftest.com+scope2&access_type=online' 
  58. . '&approval_prompt=auto',  
  59. $client->createAuthUrl() 
  60. ); 
  61.  
  62. // This should not trigger a request. 
  63. $client->setDefer(true); 
  64. $dr_service = new Google_Service_Drive($client); 
  65. $this->assertInstanceOf('Google_Http_Request', $dr_service->files->listFiles()); 
  66.  
  67. public function testSettersGetters() 
  68. $client = new Google_Client(); 
  69. $client->setClientId("client1"); 
  70. $client->setClientSecret('client1secret'); 
  71. $client->setState('1'); 
  72. $client->setApprovalPrompt('force'); 
  73. $client->setAccessType('offline'); 
  74.  
  75. $client->setRedirectUri('localhost'); 
  76. $client->setApplicationName('me'); 
  77. $this->assertEquals('object', gettype($client->getAuth())); 
  78. $this->assertEquals('object', gettype($client->getCache())); 
  79. $this->assertEquals('object', gettype($client->getIo())); 
  80.  
  81. $client->setAuth(new Google_Auth_Simple($client)); 
  82. $client->setAuth(new Google_Auth_OAuth2($client)); 
  83.  
  84. try { 
  85. $client->setAccessToken(null); 
  86. die('Should have thrown an Google_Auth_Exception.'); 
  87. } catch (Google_Auth_Exception $e) { 
  88. $this->assertEquals('Could not json decode the token', $e->getMessage()); 
  89.  
  90. $token = json_encode(array('access_token' => 'token')); 
  91. $client->setAccessToken($token); 
  92. $this->assertEquals($token, $client->getAccessToken()); 
  93.  
  94. /** 
  95. * @requires extension Memcached 
  96. */ 
  97. public function testAppEngineAutoConfig() 
  98. $_SERVER['SERVER_SOFTWARE'] = 'Google App Engine'; 
  99. $client = new Google_Client(); 
  100. $this->assertInstanceOf('Google_Cache_Memcache', $client->getCache()); 
  101. $this->assertInstanceOf('Google_Io_Stream', $client->getIo()); 
  102. unset($_SERVER['SERVER_SOFTWARE']); 
  103.  
  104. public function testJsonConfig() 
  105. // Device config 
  106. $config = new Google_Config(); 
  107. $client = new Google_Client($config); 
  108. $device = 
  109. '{"installed":{"auth_uri":"https://accounts.google.com/o/oauth2/auth", "client_secret"'. 
  110. ':"N0aHCBT1qX1VAcF5J1pJAn6S", "token_uri":"https://accounts.google.com/o/oauth2/token", '. 
  111. '"client_email":"", "redirect_uris":["urn:ietf:wg:oauth:2.0:oob", "oob"], "client_x509_cert_url"'. 
  112. ':"", "client_id":"123456789.apps.googleusercontent.com", "auth_provider_x509_cert_url":'. 
  113. '"https://www.googleapis.com/oauth2/v1/certs"}}'; 
  114. $dObj = json_decode($device); 
  115. $client->setAuthConfig($device); 
  116. $cfg = $config->getClassConfig('Google_Auth_OAuth2'); 
  117. $this->assertEquals($cfg['client_id'], $dObj->installed->client_id); 
  118. $this->assertEquals($cfg['client_secret'], $dObj->installed->client_secret); 
  119. $this->assertEquals($cfg['redirect_uri'], $dObj->installed->redirect_uris[0]); 
  120.  
  121. // Web config 
  122. $config = new Google_Config(); 
  123. $client = new Google_Client($config); 
  124. $web = '{"web":{"auth_uri":"https://accounts.google.com/o/oauth2/auth", "client_secret"' . 
  125. ':"lpoubuib8bj-Fmke_YhhyHGgXc", "token_uri":"https://accounts.google.com/o/oauth2/token"' . 
  126. ', "client_email":"123456789@developer.gserviceaccount.com", "client_x509_cert_url":'. 
  127. '"https://www.googleapis.com/robot/v1/metadata/x509/123456789@developer.gserviceaccount.com"'. 
  128. ', "client_id":"123456789.apps.googleusercontent.com", "auth_provider_x509_cert_url":'. 
  129. '"https://www.googleapis.com/oauth2/v1/certs"}}'; 
  130. $wObj = json_decode($web); 
  131. $client->setAuthConfig($web); 
  132. $cfg = $config->getClassConfig('Google_Auth_OAuth2'); 
  133. $this->assertEquals($cfg['client_id'], $wObj->web->client_id); 
  134. $this->assertEquals($cfg['client_secret'], $wObj->web->client_secret); 
  135. $this->assertEquals($cfg['redirect_uri'], ''); 
  136.  
  137. public function testIniConfig() 
  138. $config = new Google_Config(__DIR__ . "/testdata/test.ini"); 
  139. $this->assertEquals('My Test application', $config->getApplicationName()); 
  140. $this->assertEquals( 
  141. 'gjfiwnGinpena3',  
  142. $config->getClassConfig('Google_Auth_OAuth2', 'client_secret') 
  143. ); 
  144. $this->assertInternalType( 
  145. 'array',  
  146. $config->getClassConfig('Google_IO_Abstract') 
  147. ); 
  148. $this->assertEquals( 
  149. 100,  
  150. $config->getClassConfig('Google_IO_Abstract', 'request_timeout_seconds') 
  151. ); 
  152.  
  153. public function testServiceAccountJson() 
  154. $client = new Google_Client(); 
  155. $c = $client->loadServiceAccountJson( 
  156. __DIR__ . "/testdata/service-12345.json",  
  157. array() 
  158. ); 
  159. $this->assertInstanceOf('Google_Auth_AssertionCredentials', $c); 
  160.  
  161. public function testRsaServiceAccountJson() 
  162. $client = new Google_Client(); 
  163. $c = $client->loadServiceAccountJson( 
  164. __DIR__ . "/testdata/service-rsa-12345.json",  
  165. array() 
  166. ); 
  167. $this->assertInstanceOf('Google_Auth_AssertionCredentials', $c);