OAuthTestUnitOAuth2ServiceHerokuTest

The Gravity Forms Salesforce Add-On OAuthTest Unit OAuth2 Service HerokuTest class.

Defined (1)

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

/lib/PHPoAuthLib/tests/Unit/OAuth2/Service/HerokuTest.php  
  1. class HerokuTest extends \PHPUnit_Framework_TestCase 
  2. /** 
  3. * @covers OAuth\OAuth2\Service\Heroku::__construct 
  4. */ 
  5. public function testConstructCorrectInterfaceWithoutCustomUri() 
  6. $service = new Heroku( 
  7. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  8. $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),  
  9. $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') 
  10. ); 
  11.  
  12. $this->assertInstanceOf('\\OAuth\\OAuth2\\Service\\ServiceInterface', $service); 
  13.  
  14. /** 
  15. * @covers OAuth\OAuth2\Service\Heroku::__construct 
  16. */ 
  17. public function testConstructCorrectInstanceWithoutCustomUri() 
  18. $service = new Heroku( 
  19. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  20. $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),  
  21. $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') 
  22. ); 
  23.  
  24. $this->assertInstanceOf('\\OAuth\\OAuth2\\Service\\AbstractService', $service); 
  25.  
  26. /** 
  27. * @covers OAuth\OAuth2\Service\Heroku::__construct 
  28. */ 
  29. public function testConstructCorrectInstanceWithCustomUri() 
  30. $service = new Heroku( 
  31. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  32. $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),  
  33. $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),  
  34. array(),  
  35. $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface') 
  36. ); 
  37.  
  38. $this->assertInstanceOf('\\OAuth\\OAuth2\\Service\\AbstractService', $service); 
  39.  
  40. /** 
  41. * @covers OAuth\OAuth2\Service\Heroku::__construct 
  42. * @covers OAuth\OAuth2\Service\Heroku::getAuthorizationEndpoint 
  43. */ 
  44. public function testGetAuthorizationEndpoint() 
  45. $service = new Heroku( 
  46. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  47. $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),  
  48. $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') 
  49. ); 
  50.  
  51. $this->assertSame( 
  52. 'https://id.heroku.com/oauth/authorize',  
  53. $service->getAuthorizationEndpoint()->getAbsoluteUri() 
  54. ); 
  55.  
  56. /** 
  57. * @covers OAuth\OAuth2\Service\Heroku::__construct 
  58. * @covers OAuth\OAuth2\Service\Heroku::getAccessTokenEndpoint 
  59. */ 
  60. public function testGetAccessTokenEndpoint() 
  61. $service = new Heroku( 
  62. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  63. $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),  
  64. $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') 
  65. ); 
  66.  
  67. $this->assertSame('https://id.heroku.com/oauth/token', $service->getAccessTokenEndpoint()->getAbsoluteUri()); 
  68.  
  69. /** 
  70. * @covers OAuth\OAuth2\Service\Heroku::__construct 
  71. * @covers OAuth\OAuth2\Service\Heroku::getAuthorizationMethod 
  72. */ 
  73. public function testGetAuthorizationMethod() 
  74. $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); 
  75. $client->expects($this->once())->method('retrieveResponse')->will($this->returnArgument(2)); 
  76.  
  77. $token = $this->getMock('\\OAuth\\OAuth2\\Token\\TokenInterface'); 
  78. $token->expects($this->once())->method('getEndOfLife')->will($this->returnValue(TokenInterface::EOL_NEVER_EXPIRES)); 
  79. $token->expects($this->once())->method('getAccessToken')->will($this->returnValue('foo')); 
  80.  
  81. $storage = $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'); 
  82. $storage->expects($this->once())->method('retrieveAccessToken')->will($this->returnValue($token)); 
  83.  
  84. $service = new Heroku( 
  85. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  86. $client,  
  87. $storage 
  88. ); 
  89.  
  90. $headers = $service->request('https://pieterhordijk.com/my/awesome/path'); 
  91.  
  92. $this->assertTrue(array_key_exists('Authorization', $headers)); 
  93. $this->assertTrue(in_array('Bearer foo', $headers, true)); 
  94.  
  95. /** 
  96. * @covers OAuth\OAuth2\Service\Heroku::__construct 
  97. * @covers OAuth\OAuth2\Service\Heroku::parseAccessTokenResponse 
  98. */ 
  99. public function testParseAccessTokenResponseThrowsExceptionOnNulledResponse() 
  100. $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); 
  101. $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue(null)); 
  102.  
  103. $service = new Heroku( 
  104. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  105. $client,  
  106. $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') 
  107. ); 
  108.  
  109. $this->setExpectedException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException'); 
  110.  
  111. $service->requestAccessToken('foo'); 
  112.  
  113. /** 
  114. * @covers OAuth\OAuth2\Service\Heroku::__construct 
  115. * @covers OAuth\OAuth2\Service\Heroku::parseAccessTokenResponse 
  116. */ 
  117. public function testParseAccessTokenResponseThrowsExceptionOnErrorDescription() 
  118. $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); 
  119. $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('error_description=some_error')); 
  120.  
  121. $service = new Heroku( 
  122. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  123. $client,  
  124. $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') 
  125. ); 
  126.  
  127. $this->setExpectedException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException'); 
  128.  
  129. $service->requestAccessToken('foo'); 
  130.  
  131. /** 
  132. * @covers OAuth\OAuth2\Service\Heroku::__construct 
  133. * @covers OAuth\OAuth2\Service\Heroku::parseAccessTokenResponse 
  134. */ 
  135. public function testParseAccessTokenResponseThrowsExceptionOnError() 
  136. $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); 
  137. $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('error=some_error')); 
  138.  
  139. $service = new Heroku( 
  140. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  141. $client,  
  142. $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') 
  143. ); 
  144.  
  145. $this->setExpectedException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException'); 
  146.  
  147. $service->requestAccessToken('foo'); 
  148.  
  149. /** 
  150. * @covers OAuth\OAuth2\Service\Heroku::__construct 
  151. * @covers OAuth\OAuth2\Service\Heroku::parseAccessTokenResponse 
  152. */ 
  153. public function testParseAccessTokenResponseValidWithoutRefreshToken() 
  154. $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); 
  155. $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('{"access_token":"foo", "expires_in":"bar"}')); 
  156.  
  157. $service = new Heroku( 
  158. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  159. $client,  
  160. $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') 
  161. ); 
  162.  
  163. $this->assertInstanceOf('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->requestAccessToken('foo')); 
  164.  
  165. /** 
  166. * @covers OAuth\OAuth2\Service\Heroku::__construct 
  167. * @covers OAuth\OAuth2\Service\Heroku::parseAccessTokenResponse 
  168. */ 
  169. public function testParseAccessTokenResponseValidWithRefreshToken() 
  170. $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); 
  171. $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('{"access_token":"foo", "expires_in":"bar", "refresh_token":"baz"}')); 
  172.  
  173. $service = new Heroku( 
  174. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  175. $client,  
  176. $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') 
  177. ); 
  178.  
  179. $this->assertInstanceOf('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->requestAccessToken('foo')); 
  180.  
  181. /** 
  182. * @covers OAuth\OAuth2\Service\Heroku::__construct 
  183. * @covers OAuth\OAuth2\Service\Heroku::getExtraOAuthHeaders 
  184. */ 
  185. public function testGetExtraOAuthHeaders() 
  186. $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); 
  187. $client->expects($this->once())->method('retrieveResponse')->will($this->returnCallback(function($uri, $params, $extraHeaders) { 
  188. \PHPUnit_Framework_Assert::assertTrue(array_key_exists('Accept', $extraHeaders)); 
  189. \PHPUnit_Framework_Assert::assertTrue(in_array('application/vnd.heroku+json; version=3', $extraHeaders, true)); 
  190.  
  191. return '{"access_token":"foo", "expires_in":"bar"}'; 
  192. })); 
  193.  
  194. $service = new Heroku( 
  195. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  196. $client,  
  197. $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') 
  198. ); 
  199.  
  200. $this->assertInstanceOf('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->requestAccessToken('foo')); 
  201.  
  202. /** 
  203. * @covers OAuth\OAuth2\Service\Heroku::__construct 
  204. * @covers OAuth\OAuth2\Service\Heroku::getExtraApiHeaders 
  205. */ 
  206. public function testGetExtraApiHeaders() 
  207. $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); 
  208. $client->expects($this->once())->method('retrieveResponse')->will($this->returnArgument(2)); 
  209.  
  210. $token = $this->getMock('\\OAuth\\OAuth2\\Token\\TokenInterface'); 
  211. $token->expects($this->once())->method('getEndOfLife')->will($this->returnValue(TokenInterface::EOL_NEVER_EXPIRES)); 
  212. $token->expects($this->once())->method('getAccessToken')->will($this->returnValue('foo')); 
  213.  
  214. $storage = $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'); 
  215. $storage->expects($this->once())->method('retrieveAccessToken')->will($this->returnValue($token)); 
  216.  
  217. $service = new Heroku( 
  218. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  219. $client,  
  220. $storage 
  221. ); 
  222.  
  223. $headers = $service->request('https://pieterhordijk.com/my/awesome/path'); 
  224.  
  225. $this->assertTrue(array_key_exists('Accept', $headers)); 
  226. $this->assertSame('application/vnd.heroku+json; version=3', $headers['Accept']);