OAuthTestUnitOAuth2ServiceBoxTest

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

Defined (1)

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

/lib/PHPoAuthLib/tests/Unit/OAuth2/Service/BoxTest.php  
  1. class BoxTest extends \PHPUnit_Framework_TestCase 
  2. /** 
  3. * @covers OAuth\OAuth2\Service\Box::__construct 
  4. */ 
  5. public function testConstructCorrectInterfaceWithoutCustomUri() 
  6. $service = new Box( 
  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\Box::__construct 
  16. */ 
  17. public function testConstructCorrectInstanceWithoutCustomUri() 
  18. $service = new Box( 
  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\Box::__construct 
  28. */ 
  29. public function testConstructCorrectInstanceWithCustomUri() 
  30. $service = new Box( 
  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\Box::__construct 
  42. * @covers OAuth\OAuth2\Service\Box::getAuthorizationEndpoint 
  43. */ 
  44. public function testGetAuthorizationEndpoint() 
  45. $service = new Box( 
  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('https://www.box.com/api/oauth2/authorize', $service->getAuthorizationEndpoint()->getAbsoluteUri()); 
  52.  
  53. /** 
  54. * @covers OAuth\OAuth2\Service\Box::__construct 
  55. * @covers OAuth\OAuth2\Service\Box::getAccessTokenEndpoint 
  56. */ 
  57. public function testGetAccessTokenEndpoint() 
  58. $service = new Box( 
  59. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  60. $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),  
  61. $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') 
  62. ); 
  63.  
  64. $this->assertSame('https://www.box.com/api/oauth2/token', $service->getAccessTokenEndpoint()->getAbsoluteUri()); 
  65.  
  66. /** 
  67. * @covers OAuth\OAuth2\Service\Box::__construct 
  68. * @covers OAuth\OAuth2\Service\Box::getAuthorizationMethod 
  69. */ 
  70. public function testGetAuthorizationMethod() 
  71. $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); 
  72. $client->expects($this->once())->method('retrieveResponse')->will($this->returnArgument(2)); 
  73.  
  74. $token = $this->getMock('\\OAuth\\OAuth2\\Token\\TokenInterface'); 
  75. $token->expects($this->once())->method('getEndOfLife')->will($this->returnValue(TokenInterface::EOL_NEVER_EXPIRES)); 
  76. $token->expects($this->once())->method('getAccessToken')->will($this->returnValue('foo')); 
  77.  
  78. $storage = $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'); 
  79. $storage->expects($this->once())->method('retrieveAccessToken')->will($this->returnValue($token)); 
  80.  
  81. $service = new Box( 
  82. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  83. $client,  
  84. $storage 
  85. ); 
  86.  
  87. $headers = $service->request('https://pieterhordijk.com/my/awesome/path'); 
  88.  
  89. $this->assertTrue(array_key_exists('Authorization', $headers)); 
  90. $this->assertTrue(in_array('Bearer foo', $headers, true)); 
  91.  
  92. /** 
  93. * @covers OAuth\OAuth2\Service\Box::__construct 
  94. * @covers OAuth\OAuth2\Service\Box::parseAccessTokenResponse 
  95. */ 
  96. public function testParseAccessTokenResponseThrowsExceptionOnNulledResponse() 
  97. $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); 
  98. $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue(null)); 
  99.  
  100. $service = new Box( 
  101. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  102. $client,  
  103. $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') 
  104. ); 
  105.  
  106. $this->setExpectedException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException'); 
  107.  
  108. $service->requestAccessToken('foo'); 
  109.  
  110. /** 
  111. * @covers OAuth\OAuth2\Service\Box::__construct 
  112. * @covers OAuth\OAuth2\Service\Box::parseAccessTokenResponse 
  113. */ 
  114. public function testParseAccessTokenResponseThrowsExceptionOnErrorDescription() 
  115. $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); 
  116. $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('error_description=some_error')); 
  117.  
  118. $service = new Box( 
  119. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  120. $client,  
  121. $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') 
  122. ); 
  123.  
  124. $this->setExpectedException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException'); 
  125.  
  126. $service->requestAccessToken('foo'); 
  127.  
  128. /** 
  129. * @covers OAuth\OAuth2\Service\Box::__construct 
  130. * @covers OAuth\OAuth2\Service\Box::parseAccessTokenResponse 
  131. */ 
  132. public function testParseAccessTokenResponseThrowsExceptionOnError() 
  133. $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); 
  134. $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('error=some_error')); 
  135.  
  136. $service = new Box( 
  137. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  138. $client,  
  139. $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') 
  140. ); 
  141.  
  142. $this->setExpectedException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException'); 
  143.  
  144. $service->requestAccessToken('foo'); 
  145.  
  146. /** 
  147. * @covers OAuth\OAuth2\Service\Box::__construct 
  148. * @covers OAuth\OAuth2\Service\Box::parseAccessTokenResponse 
  149. */ 
  150. public function testParseAccessTokenResponseValidWithoutRefreshToken() 
  151. $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); 
  152. $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('{"access_token":"foo", "expires_in":"bar"}')); 
  153.  
  154. $service = new Box( 
  155. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  156. $client,  
  157. $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') 
  158. ); 
  159.  
  160. $this->assertInstanceOf('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->requestAccessToken('foo')); 
  161.  
  162. /** 
  163. * @covers OAuth\OAuth2\Service\Box::__construct 
  164. * @covers OAuth\OAuth2\Service\Box::parseAccessTokenResponse 
  165. */ 
  166. public function testParseAccessTokenResponseValidWithRefreshToken() 
  167. $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); 
  168. $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('{"access_token":"foo", "expires_in":"bar", "refresh_token":"baz"}')); 
  169.  
  170. $service = new Box( 
  171. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  172. $client,  
  173. $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') 
  174. ); 
  175.  
  176. $this->assertInstanceOf('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->requestAccessToken('foo'));