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

/vendor/abraham/twitteroauth/tests/TwitterOAuthTest.php

https://gitlab.com/Hungcis/testvagrant
PHP | 241 lines | 169 code | 26 blank | 46 comment | 8 complexity | 109be20729f00ca02b40a92fd72520ef 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', array('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', array('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', array('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. public function testOauthRequestToken()
  54. {
  55. $twitter = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);
  56. $result = $twitter->oauth('oauth/request_token', array('oauth_callback' => OAUTH_CALLBACK));
  57. $this->assertEquals(200, $twitter->getLastHttpCode());
  58. $this->assertArrayHasKey('oauth_token', $result);
  59. $this->assertArrayHasKey('oauth_token_secret', $result);
  60. $this->assertArrayHasKey('oauth_callback_confirmed', $result);
  61. $this->assertEquals('true', $result['oauth_callback_confirmed']);
  62. return $result;
  63. }
  64. /**
  65. * @expectedException \Abraham\TwitterOAuth\TwitterOAuthException
  66. * @expectedExceptionMessage Could not authenticate you
  67. */
  68. public function testOauthRequestTokenException()
  69. {
  70. $twitter = new TwitterOAuth('CONSUMER_KEY', 'CONSUMER_SECRET');
  71. $result = $twitter->oauth('oauth/request_token', array('oauth_callback' => OAUTH_CALLBACK));
  72. return $result;
  73. }
  74. /**
  75. * @expectedException \Abraham\TwitterOAuth\TwitterOAuthException
  76. * @expectedExceptionMessage Invalid oauth_verifier parameter
  77. * @depends testOauthRequestToken
  78. */
  79. public function testOauthAccessTokenTokenException(array $requestToken)
  80. {
  81. // Can't test this without a browser logging into Twitter so check for the correct error instead.
  82. $twitter = new TwitterOAuth(
  83. CONSUMER_KEY,
  84. CONSUMER_SECRET,
  85. $requestToken['oauth_token'],
  86. $requestToken['oauth_token_secret']
  87. );
  88. $twitter->oauth("oauth/access_token", array("oauth_verifier" => "fake_oauth_verifier"));
  89. }
  90. public function testUrl()
  91. {
  92. $url = $this->twitter->url('oauth/authorize', array('foo' => 'bar', 'baz' => 'qux'));
  93. $this->assertEquals('https://api.twitter.com/oauth/authorize?foo=bar&baz=qux', $url);
  94. }
  95. public function testGetAccountVerifyCredentials()
  96. {
  97. // Include entities boolean added to test parameter value cohearsion
  98. $this->twitter->get('account/verify_credentials', array("include_entities" => false));
  99. $this->assertEquals(200, $this->twitter->getLastHttpCode());
  100. }
  101. // BUG: testing is too unreliable for now
  102. // public function testSetProxy()
  103. // {
  104. // $this->twitter->setProxy(array(
  105. // 'CURLOPT_PROXY' => PROXY,
  106. // 'CURLOPT_PROXYUSERPWD' => PROXYUSERPWD,
  107. // 'CURLOPT_PROXYPORT' => PROXYPORT,
  108. // ));
  109. // $this->twitter->setTimeouts(60, 60);
  110. // $result = $this->twitter->get('account/verify_credentials');
  111. // $this->assertEquals(200, $this->twitter->getLastHttpCode());
  112. // $this->assertObjectHasAttribute('id', $result);
  113. // }
  114. public function testGetStatusesMentionsTimeline()
  115. {
  116. $this->twitter->get('statuses/mentions_timeline');
  117. $this->assertEquals(200, $this->twitter->getLastHttpCode());
  118. }
  119. public function testGetSearchTweets()
  120. {
  121. $result = $this->twitter->get('search/tweets', array('q' => 'twitter'));
  122. $this->assertEquals(200, $this->twitter->getLastHttpCode());
  123. return $result->statuses;
  124. }
  125. /**
  126. * @depends testGetSearchTweets
  127. */
  128. public function testGetSearchTweetsWithMaxId($statuses)
  129. {
  130. $maxId = array_pop($statuses)->id_str;
  131. $this->twitter->get('search/tweets', array('q' => 'twitter', 'max_id' => $maxId));
  132. $this->assertEquals(200, $this->twitter->getLastHttpCode());
  133. }
  134. public function testPostFavoritesCreate()
  135. {
  136. $result = $this->twitter->post('favorites/create', array('id' => '6242973112'));
  137. if ($this->twitter->getLastHttpCode() == 403) {
  138. // Status already favorited
  139. $this->assertEquals(139, $result->errors[0]->code);
  140. } else {
  141. $this->assertEquals(200, $this->twitter->getLastHttpCode());
  142. }
  143. }
  144. /**
  145. * @depends testPostFavoritesCreate
  146. */
  147. public function testPostFavoritesDestroy()
  148. {
  149. $this->twitter->post('favorites/destroy', array('id' => '6242973112'));
  150. $this->assertEquals(200, $this->twitter->getLastHttpCode());
  151. }
  152. public function testPostStatusesUpdateWithMedia()
  153. {
  154. $this->twitter->setTimeouts(60, 30);
  155. // Image source https://www.flickr.com/photos/titrans/8548825587/
  156. $file_path = __DIR__ . '/kitten.jpg';
  157. $result = $this->twitter->upload('media/upload', array('media' => $file_path));
  158. $this->assertEquals(200, $this->twitter->getLastHttpCode());
  159. $this->assertObjectHasAttribute('media_id_string', $result);
  160. $parameters = array('status' => 'Hello World ' . time(), 'media_ids' => $result->media_id_string);
  161. $result = $this->twitter->post('statuses/update', $parameters);
  162. $this->assertEquals(200, $this->twitter->getLastHttpCode());
  163. if ($this->twitter->getLastHttpCode() == 200) {
  164. $result = $this->twitter->post('statuses/destroy/' . $result->id_str);
  165. }
  166. return $result;
  167. }
  168. public function testPostStatusesUpdateWithMediaChunked()
  169. {
  170. $this->twitter->setTimeouts(60, 30);
  171. // Video source http://www.sample-videos.com/
  172. $file_path = __DIR__ . '/video.mp4';
  173. $result = $this->twitter->upload('media/upload', array('media' => $file_path, 'media_type' => 'video/mp4'), true);
  174. $this->assertEquals(201, $this->twitter->getLastHttpCode());
  175. $this->assertObjectHasAttribute('media_id_string', $result);
  176. $parameters = array('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 testPostStatusesUpdateUtf8()
  185. {
  186. $result = $this->twitter->post('statuses/update', array('status' => 'xこんにちは世界 ' . time()));
  187. $this->assertEquals(200, $this->twitter->getLastHttpCode());
  188. return $result;
  189. }
  190. /**
  191. * @depends testPostStatusesUpdateUtf8
  192. */
  193. public function testPostStatusesDestroy($status)
  194. {
  195. $this->twitter->post('statuses/destroy/' . $status->id_str);
  196. $this->assertEquals(200, $this->twitter->getLastHttpCode());
  197. }
  198. public function testLastResult()
  199. {
  200. $this->twitter->get('search/tweets', array('q' => 'twitter'));
  201. $this->assertEquals('search/tweets', $this->twitter->getLastApiPath());
  202. $this->assertEquals(200, $this->twitter->getLastHttpCode());
  203. $this->assertObjectHasAttribute('statuses', $this->twitter->getLastBody());
  204. }
  205. /**
  206. * @depends testLastResult
  207. */
  208. public function testResetLastResponse()
  209. {
  210. $this->twitter->resetLastResponse();
  211. $this->assertEquals('', $this->twitter->getLastApiPath());
  212. $this->assertEquals(0, $this->twitter->getLastHttpCode());
  213. $this->assertEquals(array(), $this->twitter->getLastBody());
  214. }
  215. }