OAuthTestUnitOAuth1ServiceTwitterTest

The Gravity Forms Salesforce Add-On OAuthTest Unit OAuth1 Service TwitterTest class.

Defined (1)

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

/lib/PHPoAuthLib/tests/Unit/OAuth1/Service/TwitterTest.php  
  1. class TwitterTest extends \PHPUnit_Framework_TestCase 
  2. /** 
  3. * @covers OAuth\OAuth1\Service\Twitter::__construct 
  4. */ 
  5. public function testConstructCorrectInterfaceWithoutCustomUri() 
  6. $service = new Twitter( 
  7. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  8. $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),  
  9. $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),  
  10. $this->getMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface') 
  11. ); 
  12.  
  13. $this->assertInstanceOf('\\OAuth\\OAuth1\\Service\\ServiceInterface', $service); 
  14.  
  15. /** 
  16. * @covers OAuth\OAuth1\Service\Twitter::__construct 
  17. */ 
  18. public function testConstructCorrectInstanceWithoutCustomUri() 
  19. $service = new Twitter( 
  20. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  21. $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),  
  22. $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),  
  23. $this->getMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface') 
  24. ); 
  25.  
  26. $this->assertInstanceOf('\\OAuth\\OAuth1\\Service\\AbstractService', $service); 
  27.  
  28. /** 
  29. * @covers OAuth\OAuth1\Service\Twitter::__construct 
  30. */ 
  31. public function testConstructCorrectInstanceWithCustomUri() 
  32. $service = new Twitter( 
  33. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  34. $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),  
  35. $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),  
  36. $this->getMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface'),  
  37. $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface') 
  38. ); 
  39.  
  40. $this->assertInstanceOf('\\OAuth\\OAuth1\\Service\\AbstractService', $service); 
  41.  
  42. /** 
  43. * @covers OAuth\OAuth1\Service\Twitter::__construct 
  44. * @covers OAuth\OAuth1\Service\Twitter::getRequestTokenEndpoint 
  45. */ 
  46. public function testGetRequestTokenEndpoint() 
  47. $service = new Twitter( 
  48. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  49. $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),  
  50. $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),  
  51. $this->getMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface') 
  52. ); 
  53.  
  54. $this->assertSame( 
  55. 'https://api.twitter.com/oauth/request_token',  
  56. $service->getRequestTokenEndpoint()->getAbsoluteUri() 
  57. ); 
  58.  
  59. /** 
  60. * @covers OAuth\OAuth1\Service\Twitter::__construct 
  61. * @covers OAuth\OAuth1\Service\Twitter::getAuthorizationEndpoint 
  62. */ 
  63. public function testGetAuthorizationEndpoint() 
  64. $service = new Twitter( 
  65. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  66. $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),  
  67. $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),  
  68. $this->getMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface') 
  69. ); 
  70.  
  71. $this->assertTrue( 
  72. in_array( 
  73. strtolower($service->getAuthorizationEndpoint()->getAbsoluteUri()),  
  74. array(\OAuth\OAuth1\Service\Twitter::ENDPOINT_AUTHENTICATE, \OAuth\OAuth1\Service\Twitter::ENDPOINT_AUTHORIZE) 
  75. ); 
  76.  
  77. $service->setAuthorizationEndpoint( \OAuth\OAuth1\Service\Twitter::ENDPOINT_AUTHORIZE ); 
  78.  
  79. $this->assertTrue( 
  80. in_array( 
  81. strtolower($service->getAuthorizationEndpoint()->getAbsoluteUri()),  
  82. array(\OAuth\OAuth1\Service\Twitter::ENDPOINT_AUTHENTICATE, \OAuth\OAuth1\Service\Twitter::ENDPOINT_AUTHORIZE) 
  83. ); 
  84.  
  85. /** 
  86. * @covers OAuth\OAuth1\Service\Twitter::__construct 
  87. * @covers OAuth\OAuth1\Service\Twitter::setAuthorizationEndpoint 
  88. */ 
  89. public function testSetAuthorizationEndpoint() 
  90. $service = new Twitter( 
  91. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  92. $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),  
  93. $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),  
  94. $this->getMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface') 
  95. ); 
  96.  
  97. $this->setExpectedException('\\OAuth\\Common\\Exception\\Exception'); 
  98.  
  99. $service->setAuthorizationEndpoint('foo'); 
  100.  
  101. /** 
  102. * @covers OAuth\OAuth1\Service\Twitter::__construct 
  103. * @covers OAuth\OAuth1\Service\Twitter::getAccessTokenEndpoint 
  104. */ 
  105. public function testGetAccessTokenEndpoint() 
  106. $service = new Twitter( 
  107. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  108. $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),  
  109. $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),  
  110. $this->getMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface') 
  111. ); 
  112.  
  113. $this->assertSame( 
  114. 'https://api.twitter.com/oauth/access_token',  
  115. $service->getAccessTokenEndpoint()->getAbsoluteUri() 
  116. ); 
  117.  
  118. /** 
  119. * @covers OAuth\OAuth1\Service\Twitter::__construct 
  120. * @covers OAuth\OAuth1\Service\Twitter::getRequestTokenEndpoint 
  121. * @covers OAuth\OAuth1\Service\Twitter::parseRequestTokenResponse 
  122. */ 
  123. public function testParseRequestTokenResponseThrowsExceptionOnNulledResponse() 
  124. $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); 
  125. $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue(null)); 
  126.  
  127. $service = new Twitter( 
  128. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  129. $client,  
  130. $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),  
  131. $this->getMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface') 
  132. ); 
  133.  
  134. $this->setExpectedException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException'); 
  135.  
  136. $service->requestRequestToken(); 
  137.  
  138. /** 
  139. * @covers OAuth\OAuth1\Service\Twitter::__construct 
  140. * @covers OAuth\OAuth1\Service\Twitter::getRequestTokenEndpoint 
  141. * @covers OAuth\OAuth1\Service\Twitter::parseRequestTokenResponse 
  142. */ 
  143. public function testParseRequestTokenResponseThrowsExceptionOnResponseNotAnArray() 
  144. $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); 
  145. $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('notanarray')); 
  146.  
  147. $service = new Twitter( 
  148. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  149. $client,  
  150. $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),  
  151. $this->getMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface') 
  152. ); 
  153.  
  154. $this->setExpectedException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException'); 
  155.  
  156. $service->requestRequestToken(); 
  157.  
  158. /** 
  159. * @covers OAuth\OAuth1\Service\Twitter::__construct 
  160. * @covers OAuth\OAuth1\Service\Twitter::getRequestTokenEndpoint 
  161. * @covers OAuth\OAuth1\Service\Twitter::parseRequestTokenResponse 
  162. */ 
  163. public function testParseRequestTokenResponseThrowsExceptionOnResponseCallbackNotSet() 
  164. $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); 
  165. $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('foo=bar')); 
  166.  
  167. $service = new Twitter( 
  168. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  169. $client,  
  170. $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),  
  171. $this->getMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface') 
  172. ); 
  173.  
  174. $this->setExpectedException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException'); 
  175.  
  176. $service->requestRequestToken(); 
  177.  
  178. /** 
  179. * @covers OAuth\OAuth1\Service\Twitter::__construct 
  180. * @covers OAuth\OAuth1\Service\Twitter::getRequestTokenEndpoint 
  181. * @covers OAuth\OAuth1\Service\Twitter::parseRequestTokenResponse 
  182. */ 
  183. public function testParseRequestTokenResponseThrowsExceptionOnResponseCallbackNotTrue() 
  184. $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); 
  185. $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue( 
  186. 'oauth_callback_confirmed=false' 
  187. )); 
  188.  
  189. $service = new Twitter( 
  190. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  191. $client,  
  192. $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),  
  193. $this->getMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface') 
  194. ); 
  195.  
  196. $this->setExpectedException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException'); 
  197.  
  198. $service->requestRequestToken(); 
  199.  
  200. /** 
  201. * @covers OAuth\OAuth1\Service\Twitter::__construct 
  202. * @covers OAuth\OAuth1\Service\Twitter::getRequestTokenEndpoint 
  203. * @covers OAuth\OAuth1\Service\Twitter::parseRequestTokenResponse 
  204. * @covers OAuth\OAuth1\Service\Twitter::parseAccessTokenResponse 
  205. */ 
  206. public function testParseRequestTokenResponseValid() 
  207. $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); 
  208. $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue( 
  209. 'oauth_callback_confirmed=true&oauth_token=foo&oauth_token_secret=bar' 
  210. )); 
  211.  
  212. $service = new Twitter( 
  213. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  214. $client,  
  215. $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),  
  216. $this->getMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface') 
  217. ); 
  218.  
  219. $this->assertInstanceOf('\\OAuth\\OAuth1\\Token\\StdOAuth1Token', $service->requestRequestToken()); 
  220.  
  221. /** 
  222. * @covers OAuth\OAuth1\Service\Twitter::__construct 
  223. * @covers OAuth\OAuth1\Service\Twitter::getRequestTokenEndpoint 
  224. * @covers OAuth\OAuth1\Service\Twitter::parseAccessTokenResponse 
  225. */ 
  226. public function testParseAccessTokenResponseThrowsExceptionOnError() 
  227. $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); 
  228. $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('error=bar')); 
  229.  
  230. $token = $this->getMock('\\OAuth\\OAuth1\\Token\\TokenInterface'); 
  231.  
  232. $storage = $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'); 
  233. $storage->expects($this->any())->method('retrieveAccessToken')->will($this->returnValue($token)); 
  234.  
  235. $service = new Twitter( 
  236. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  237. $client,  
  238. $storage,  
  239. $this->getMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface') 
  240. ); 
  241.  
  242. $this->setExpectedException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException'); 
  243.  
  244. $service->requestAccessToken('foo', 'bar', $token); 
  245.  
  246. /** 
  247. * @covers OAuth\OAuth1\Service\Twitter::__construct 
  248. * @covers OAuth\OAuth1\Service\Twitter::getRequestTokenEndpoint 
  249. * @covers OAuth\OAuth1\Service\Twitter::parseAccessTokenResponse 
  250. */ 
  251. public function testParseAccessTokenResponseValid() 
  252. $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); 
  253. $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue( 
  254. 'oauth_token=foo&oauth_token_secret=bar' 
  255. )); 
  256.  
  257. $token = $this->getMock('\\OAuth\\OAuth1\\Token\\TokenInterface'); 
  258.  
  259. $storage = $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'); 
  260. $storage->expects($this->any())->method('retrieveAccessToken')->will($this->returnValue($token)); 
  261.  
  262. $service = new Twitter( 
  263. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  264. $client,  
  265. $storage,  
  266. $this->getMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface') 
  267. ); 
  268.  
  269. $this->assertInstanceOf('\\OAuth\\OAuth1\\Token\\StdOAuth1Token', $service->requestAccessToken('foo', 'bar', $token));