OAuthTestUnitOAuth2ServiceFacebookTest

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

Defined (1)

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

/lib/PHPoAuthLib/tests/Unit/OAuth2/Service/FacebookTest.php  
  1. class FacebookTest extends \PHPUnit_Framework_TestCase 
  2. /** 
  3. * @covers OAuth\OAuth2\Service\Facebook::__construct 
  4. */ 
  5. public function testConstructCorrectInterfaceWithoutCustomUri() 
  6. $service = new Facebook( 
  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\Facebook::__construct 
  16. */ 
  17. public function testConstructCorrectInstanceWithoutCustomUri() 
  18. $service = new Facebook( 
  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\Facebook::__construct 
  28. */ 
  29. public function testConstructCorrectInstanceWithCustomUri() 
  30. $service = new Facebook( 
  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\Facebook::__construct 
  42. * @covers OAuth\OAuth2\Service\Facebook::getAuthorizationEndpoint 
  43. */ 
  44. public function testGetAuthorizationEndpoint() 
  45. $service = new Facebook( 
  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.facebook.com/dialog/oauth', $service->getAuthorizationEndpoint()->getAbsoluteUri()); 
  52.  
  53. /** 
  54. * @covers OAuth\OAuth2\Service\Facebook::__construct 
  55. * @covers OAuth\OAuth2\Service\Facebook::getAccessTokenEndpoint 
  56. */ 
  57. public function testGetAccessTokenEndpoint() 
  58. $service = new Facebook( 
  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://graph.facebook.com/oauth/access_token', $service->getAccessTokenEndpoint()->getAbsoluteUri()); 
  65.  
  66. /** 
  67. * @covers OAuth\OAuth2\Service\Facebook::__construct 
  68. */ 
  69. public function testGetAuthorizationMethod() 
  70. $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); 
  71. $client->expects($this->once())->method('retrieveResponse')->will($this->returnArgument(2)); 
  72.  
  73. $token = $this->getMock('\\OAuth\\OAuth2\\Token\\TokenInterface'); 
  74. $token->expects($this->once())->method('getEndOfLife')->will($this->returnValue(TokenInterface::EOL_NEVER_EXPIRES)); 
  75. $token->expects($this->once())->method('getAccessToken')->will($this->returnValue('foo')); 
  76.  
  77. $storage = $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'); 
  78. $storage->expects($this->once())->method('retrieveAccessToken')->will($this->returnValue($token)); 
  79.  
  80. $service = new Facebook( 
  81. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  82. $client,  
  83. $storage 
  84. ); 
  85.  
  86. $headers = $service->request('https://pieterhordijk.com/my/awesome/path'); 
  87.  
  88. $this->assertTrue(array_key_exists('Authorization', $headers)); 
  89. $this->assertTrue(in_array('OAuth foo', $headers, true)); 
  90.  
  91. /** 
  92. * @covers OAuth\OAuth2\Service\Facebook::__construct 
  93. * @covers OAuth\OAuth2\Service\Facebook::parseAccessTokenResponse 
  94. */ 
  95. public function testParseAccessTokenResponseThrowsExceptionOnError() 
  96. $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); 
  97. $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('error=some_error')); 
  98.  
  99. $service = new Facebook( 
  100. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  101. $client,  
  102. $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') 
  103. ); 
  104.  
  105. $this->setExpectedException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException'); 
  106.  
  107. $service->requestAccessToken('foo'); 
  108.  
  109. /** 
  110. * @covers OAuth\OAuth2\Service\Facebook::__construct 
  111. * @covers OAuth\OAuth2\Service\Facebook::parseAccessTokenResponse 
  112. */ 
  113. public function testParseAccessTokenResponseValidWithoutRefreshToken() 
  114. $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); 
  115. $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('access_token=foo&expires=bar')); 
  116.  
  117. $service = new Facebook( 
  118. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  119. $client,  
  120. $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') 
  121. ); 
  122.  
  123. $this->assertInstanceOf('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->requestAccessToken('foo')); 
  124.  
  125. /** 
  126. * @covers OAuth\OAuth2\Service\Facebook::__construct 
  127. * @covers OAuth\OAuth2\Service\Facebook::parseAccessTokenResponse 
  128. */ 
  129. public function testParseAccessTokenResponseValidWithRefreshToken() 
  130. $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); 
  131. $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('access_token=foo&expires=bar&refresh_token=baz')); 
  132.  
  133. $service = new Facebook( 
  134. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  135. $client,  
  136. $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') 
  137. ); 
  138.  
  139. $this->assertInstanceOf('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->requestAccessToken('foo')); 
  140.  
  141. /** 
  142. * @covers OAuth\OAuth2\Service\Facebook::__construct 
  143. * @covers OAuth\OAuth2\Service\Facebook::getDialogUri 
  144. */ 
  145. public function testGetDialogUriRedirectUriMissing() 
  146. $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); 
  147.  
  148. $service = new Facebook( 
  149. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  150. $client,  
  151. $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') 
  152. ); 
  153.  
  154. $this->setExpectedException('\\OAuth\\Common\\Exception\\Exception'); 
  155.  
  156. $service->getDialogUri('feed', array()); 
  157.  
  158. /** 
  159. * @covers OAuth\OAuth2\Service\Facebook::__construct 
  160. * @covers OAuth\OAuth2\Service\Facebook::getDialogUri 
  161. */ 
  162. public function testGetDialogUriInstanceofUri() 
  163. $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); 
  164.  
  165. $service = new Facebook( 
  166. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  167. $client,  
  168. $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') 
  169. ); 
  170.  
  171. $dialogUri = $service->getDialogUri( 
  172. 'feed',  
  173. array( 
  174. 'redirect_uri' => 'http://www.facebook.com',  
  175. 'state' => 'Random state' 
  176. ); 
  177. $this->assertInstanceOf('\\OAuth\\Common\\Http\\Uri\\Uri', $dialogUri); 
  178.  
  179. /** 
  180. * @covers OAuth\OAuth2\Service\Facebook::__construct 
  181. * @covers OAuth\OAuth2\Service\Facebook::getDialogUri 
  182. */ 
  183. public function testGetDialogUriContainsAppIdAndOtherParameters() 
  184. $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); 
  185. $credentials = $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'); 
  186. $credentials->expects($this->any())->method('getConsumerId')->will($this->returnValue('application_id')); 
  187.  
  188.  
  189. $service = new Facebook( 
  190. $credentials,  
  191. $client,  
  192. $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') 
  193. ); 
  194.  
  195. $dialogUri = $service->getDialogUri( 
  196. 'feed',  
  197. array( 
  198. 'redirect_uri' => 'http://www.facebook.com',  
  199. 'state' => 'Random state' 
  200. ); 
  201.  
  202. $queryString = $dialogUri->getQuery(); 
  203. parse_str($queryString, $queryArray); 
  204.  
  205. $this->assertArrayHasKey('app_id', $queryArray); 
  206. $this->assertArrayHasKey('redirect_uri', $queryArray); 
  207. $this->assertArrayHasKey('state', $queryArray);