PageRenderTime 27ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/web/twitteroauth/tests/TwitterOAuthTest.php

https://bitbucket.org/JuarezJuarezItandehui/juarezjuarezitandehui.bitbucket.io
PHP | 264 lines | 176 code | 26 blank | 62 comment | 8 complexity | ad5f54b28208ba4f56b6b619257b773f MD5 | raw file
  1. <?php
  2. /**
  3. * WARNING: Running these tests will post and delete through the actual Twitter account.
  4. */
  5. namespace Abraham\TwitterOAuth\Test;
  6. use Abraham\TwitterOAuth\TwitterOAuth;
  7. class TwitterOAuthTest extends \PHPUnit_Framework_TestCase
  8. {
  9. /** @var TwitterOAuth */
  10. protected $twitter;
  11. protected function setUp()
  12. {
  13. $this->twitter = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);
  14. }
  15. public function testBuildClient()
  16. {
  17. $this->assertObjectHasAttribute('consumer', $this->twitter);
  18. $this->assertObjectHasAttribute('token', $this->twitter);
  19. }
  20. public function testSetOauthToken()
  21. {
  22. $twitter = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);
  23. $twitter->setOauthToken(ACCESS_TOKEN, ACCESS_TOKEN_SECRET);
  24. $this->assertObjectHasAttribute('consumer', $twitter);
  25. $this->assertObjectHasAttribute('token', $twitter);
  26. $twitter->get('friendships/show', ['target_screen_name' => 'twitterapi']);
  27. $this->assertEquals(200, $twitter->getLastHttpCode());
  28. }
  29. public function testOauth2Token()
  30. {
  31. $twitter = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);
  32. $result = $twitter->oauth2('oauth2/token', ['grant_type' => 'client_credentials']);
  33. $this->assertEquals(200, $twitter->getLastHttpCode());
  34. $this->assertObjectHasAttribute('token_type', $result);
  35. $this->assertObjectHasAttribute('access_token', $result);
  36. $this->assertEquals('bearer', $result->token_type);
  37. return $result;
  38. }
  39. /**
  40. * @depends testOauth2Token
  41. */
  42. public function testBearerToken($accessToken)
  43. {
  44. $twitter = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, null, $accessToken->access_token);
  45. $result = $twitter->get('statuses/user_timeline', ['screen_name' => 'twitterapi']);
  46. if ($twitter->getLastHttpCode() !== 200) {
  47. $this->assertEquals('foo', substr($accessToken->access_token, 0, 75));
  48. $this->assertEquals('foo', print_r($result, true));
  49. }
  50. $this->assertEquals(200, $twitter->getLastHttpCode());
  51. return $accessToken;
  52. }
  53. // This causes issues for parallel run tests.
  54. // /**
  55. // * @depends testBearerToken
  56. // */
  57. // public function testOauth2TokenInvalidate($accessToken)
  58. // {
  59. // $twitter = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);
  60. // // HACK: access_token is already urlencoded but gets urlencoded again breaking the invalidate request.
  61. // $result = $twitter->oauth2(
  62. // 'oauth2/invalidate_token',
  63. // array('access_token' => urldecode($accessToken->access_token))
  64. // );
  65. // $this->assertEquals(200, $twitter->getLastHttpCode());
  66. // $this->assertObjectHasAttribute('access_token', $result);
  67. // return $result;
  68. // }
  69. public function testOauthRequestToken()
  70. {
  71. $twitter = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);
  72. $result = $twitter->oauth('oauth/request_token', ['oauth_callback' => OAUTH_CALLBACK]);
  73. $this->assertEquals(200, $twitter->getLastHttpCode());
  74. $this->assertArrayHasKey('oauth_token', $result);
  75. $this->assertArrayHasKey('oauth_token_secret', $result);
  76. $this->assertArrayHasKey('oauth_callback_confirmed', $result);
  77. $this->assertEquals('true', $result['oauth_callback_confirmed']);
  78. return $result;
  79. }
  80. /**
  81. * @expectedException \Abraham\TwitterOAuth\TwitterOAuthException
  82. * @expectedExceptionMessage Could not authenticate you
  83. */
  84. public function testOauthRequestTokenException()
  85. {
  86. $twitter = new TwitterOAuth('CONSUMER_KEY', 'CONSUMER_SECRET');
  87. $result = $twitter->oauth('oauth/request_token', ['oauth_callback' => OAUTH_CALLBACK]);
  88. return $result;
  89. }
  90. /**
  91. * @expectedException \Abraham\TwitterOAuth\TwitterOAuthException
  92. * @expectedExceptionMessage Invalid oauth_verifier parameter
  93. * @depends testOauthRequestToken
  94. */
  95. public function testOauthAccessTokenTokenException(array $requestToken)
  96. {
  97. // Can't test this without a browser logging into Twitter so check for the correct error instead.
  98. $twitter = new TwitterOAuth(
  99. CONSUMER_KEY,
  100. CONSUMER_SECRET,
  101. $requestToken['oauth_token'],
  102. $requestToken['oauth_token_secret']
  103. );
  104. $twitter->oauth("oauth/access_token", ["oauth_verifier" => "fake_oauth_verifier"]);
  105. }
  106. public function testUrl()
  107. {
  108. $url = $this->twitter->url('oauth/authorize', ['foo' => 'bar', 'baz' => 'qux']);
  109. $this->assertEquals('https://api.twitter.com/oauth/authorize?foo=bar&baz=qux', $url);
  110. }
  111. public function testGetAccountVerifyCredentials()
  112. {
  113. // Include entities boolean added to test parameter value cohearsion
  114. $this->twitter->get('account/verify_credentials', ["include_entities" => false]);
  115. $this->assertEquals(200, $this->twitter->getLastHttpCode());
  116. }
  117. // BUG: testing is too unreliable for now
  118. // public function testSetProxy()
  119. // {
  120. // $this->twitter->setProxy(array(
  121. // 'CURLOPT_PROXY' => PROXY,
  122. // 'CURLOPT_PROXYUSERPWD' => PROXYUSERPWD,
  123. // 'CURLOPT_PROXYPORT' => PROXYPORT,
  124. // ));
  125. // $this->twitter->setTimeouts(60, 60);
  126. // $result = $this->twitter->get('account/verify_credentials');
  127. // $this->assertEquals(200, $this->twitter->getLastHttpCode());
  128. // $this->assertObjectHasAttribute('id', $result);
  129. // }
  130. public function testGetStatusesMentionsTimeline()
  131. {
  132. $this->twitter->get('statuses/mentions_timeline');
  133. $this->assertEquals(200, $this->twitter->getLastHttpCode());
  134. }
  135. public function testGetSearchTweets()
  136. {
  137. $result = $this->twitter->get('search/tweets', ['q' => 'twitter']);
  138. $this->assertEquals(200, $this->twitter->getLastHttpCode());
  139. return $result->statuses;
  140. }
  141. /**
  142. * @depends testGetSearchTweets
  143. */
  144. public function testGetSearchTweetsWithMaxId($statuses)
  145. {
  146. $maxId = array_pop($statuses)->id_str;
  147. $this->twitter->get('search/tweets', ['q' => 'twitter', 'max_id' => $maxId]);
  148. $this->assertEquals(200, $this->twitter->getLastHttpCode());
  149. }
  150. public function testPostFavoritesCreate()
  151. {
  152. $result = $this->twitter->post('favorites/create', ['id' => '6242973112']);
  153. if ($this->twitter->getLastHttpCode() == 403) {
  154. // Status already favorited
  155. $this->assertEquals(139, $result->errors[0]->code);
  156. } else {
  157. $this->assertEquals(200, $this->twitter->getLastHttpCode());
  158. }
  159. }
  160. /**
  161. * @depends testPostFavoritesCreate
  162. */
  163. public function testPostFavoritesDestroy()
  164. {
  165. $this->twitter->post('favorites/destroy', ['id' => '6242973112']);
  166. $this->assertEquals(200, $this->twitter->getLastHttpCode());
  167. }
  168. public function testPostStatusesUpdateWithMedia()
  169. {
  170. $this->twitter->setTimeouts(60, 30);
  171. // Image source https://www.flickr.com/photos/titrans/8548825587/
  172. $file_path = __DIR__ . '/kitten.jpg';
  173. $result = $this->twitter->upload('media/upload', ['media' => $file_path]);
  174. $this->assertEquals(200, $this->twitter->getLastHttpCode());
  175. $this->assertObjectHasAttribute('media_id_string', $result);
  176. $parameters = ['status' => 'Hello World ' . time(), 'media_ids' => $result->media_id_string];
  177. $result = $this->twitter->post('statuses/update', $parameters);
  178. $this->assertEquals(200, $this->twitter->getLastHttpCode());
  179. if ($this->twitter->getLastHttpCode() == 200) {
  180. $result = $this->twitter->post('statuses/destroy/' . $result->id_str);
  181. }
  182. return $result;
  183. }
  184. public function testPostStatusUpdateWithInvalidMediaThrowsException()
  185. {
  186. $this->expectException(\InvalidArgumentException::class);
  187. $file_path = __DIR__ . '/12345678900987654321.jpg';
  188. $this->assertFalse(\is_readable($file_path));
  189. $result = $this->twitter->upload('media/upload', ['media' => $file_path]);
  190. }
  191. public function testPostStatusesUpdateWithMediaChunked()
  192. {
  193. $this->twitter->setTimeouts(60, 30);
  194. // Video source http://www.sample-videos.com/
  195. $file_path = __DIR__ . '/video.mp4';
  196. $result = $this->twitter->upload('media/upload', ['media' => $file_path, 'media_type' => 'video/mp4'], true);
  197. $this->assertEquals(201, $this->twitter->getLastHttpCode());
  198. $this->assertObjectHasAttribute('media_id_string', $result);
  199. $parameters = ['status' => 'Hello World ' . time(), 'media_ids' => $result->media_id_string];
  200. $result = $this->twitter->post('statuses/update', $parameters);
  201. $this->assertEquals(200, $this->twitter->getLastHttpCode());
  202. if ($this->twitter->getLastHttpCode() == 200) {
  203. $result = $this->twitter->post('statuses/destroy/' . $result->id_str);
  204. }
  205. return $result;
  206. }
  207. public function testPostStatusesUpdateUtf8()
  208. {
  209. $result = $this->twitter->post('statuses/update', ['status' => 'xこんにちは世界 ' . time()]);
  210. $this->assertEquals(200, $this->twitter->getLastHttpCode());
  211. return $result;
  212. }
  213. /**
  214. * @depends testPostStatusesUpdateUtf8
  215. */
  216. public function testPostStatusesDestroy($status)
  217. {
  218. $this->twitter->post('statuses/destroy/' . $status->id_str);
  219. $this->assertEquals(200, $this->twitter->getLastHttpCode());
  220. }
  221. public function testLastResult()
  222. {
  223. $this->twitter->get('search/tweets', ['q' => 'twitter']);
  224. $this->assertEquals('search/tweets', $this->twitter->getLastApiPath());
  225. $this->assertEquals(200, $this->twitter->getLastHttpCode());
  226. $this->assertObjectHasAttribute('statuses', $this->twitter->getLastBody());
  227. }
  228. /**
  229. * @depends testLastResult
  230. */
  231. public function testResetLastResponse()
  232. {
  233. $this->twitter->resetLastResponse();
  234. $this->assertEquals('', $this->twitter->getLastApiPath());
  235. $this->assertEquals(0, $this->twitter->getLastHttpCode());
  236. $this->assertEquals([], $this->twitter->getLastBody());
  237. }
  238. }