/vendor/lusitanian/oauth/tests/Unit/OAuth1/Service/YahooTest.php

https://gitlab.com/yousafsyed/easternglamor · PHP · 302 lines · 193 code · 49 blank · 60 comment · 0 complexity · 27ecefb0f1d4a7c94722a5f5ade953f2 MD5 · raw file

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