3 * WARNING: Running these tests will post and delete through the actual Twitter account.
5 namespace Abraham\TwitterOAuth\Test;
7 use Abraham\TwitterOAuth\TwitterOAuth;
9 class TwitterOAuthTest extends \PHPUnit_Framework_TestCase
11 /** @var TwitterOAuth */
14 protected function setUp()
16 $this->twitter = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);
19 public function testBuildClient()
21 $this->assertObjectHasAttribute('consumer', $this->twitter);
22 $this->assertObjectHasAttribute('token', $this->twitter);
25 public function testSetOauthToken()
27 $twitter = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);
28 $twitter->setOauthToken(ACCESS_TOKEN, ACCESS_TOKEN_SECRET);
29 $this->assertObjectHasAttribute('consumer', $twitter);
30 $this->assertObjectHasAttribute('token', $twitter);
31 $twitter->get('friendships/show', ['target_screen_name' => 'twitterapi']);
32 $this->assertEquals(200, $twitter->getLastHttpCode());
35 public function testOauth2Token()
37 $twitter = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);
38 $result = $twitter->oauth2('oauth2/token', ['grant_type' => 'client_credentials']);
39 $this->assertEquals(200, $twitter->getLastHttpCode());
40 $this->assertObjectHasAttribute('token_type', $result);
41 $this->assertObjectHasAttribute('access_token', $result);
42 $this->assertEquals('bearer', $result->token_type);
47 * @depends testOauth2Token
49 public function testBearerToken($accessToken)
51 $twitter = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, null, $accessToken->access_token);
52 $result = $twitter->get('statuses/user_timeline', ['screen_name' => 'twitterapi']);
53 if ($twitter->getLastHttpCode() !== 200) {
54 $this->assertEquals('foo', substr($accessToken->access_token, 0, 75));
55 $this->assertEquals('foo', print_r($result, true));
57 $this->assertEquals(200, $twitter->getLastHttpCode());
61 // This causes issues for parallel run tests.
63 // * @depends testBearerToken
65 // public function testOauth2TokenInvalidate($accessToken)
67 // $twitter = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);
68 // // HACK: access_token is already urlencoded but gets urlencoded again breaking the invalidate request.
69 // $result = $twitter->oauth2(
70 // 'oauth2/invalidate_token',
71 // array('access_token' => urldecode($accessToken->access_token))
73 // $this->assertEquals(200, $twitter->getLastHttpCode());
74 // $this->assertObjectHasAttribute('access_token', $result);
78 public function testOauthRequestToken()
80 $twitter = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);
81 $result = $twitter->oauth('oauth/request_token', ['oauth_callback' => OAUTH_CALLBACK]);
82 $this->assertEquals(200, $twitter->getLastHttpCode());
83 $this->assertArrayHasKey('oauth_token', $result);
84 $this->assertArrayHasKey('oauth_token_secret', $result);
85 $this->assertArrayHasKey('oauth_callback_confirmed', $result);
86 $this->assertEquals('true', $result['oauth_callback_confirmed']);
91 * @expectedException \Abraham\TwitterOAuth\TwitterOAuthException
92 * @expectedExceptionMessage Could not authenticate you
94 public function testOauthRequestTokenException()
96 $twitter = new TwitterOAuth('CONSUMER_KEY', 'CONSUMER_SECRET');
97 $result = $twitter->oauth('oauth/request_token', ['oauth_callback' => OAUTH_CALLBACK]);
102 * @expectedException \Abraham\TwitterOAuth\TwitterOAuthException
103 * @expectedExceptionMessage Invalid oauth_verifier parameter
104 * @depends testOauthRequestToken
106 public function testOauthAccessTokenTokenException(array $requestToken)
108 // Can't test this without a browser logging into Twitter so check for the correct error instead.
109 $twitter = new TwitterOAuth(
112 $requestToken['oauth_token'],
113 $requestToken['oauth_token_secret']
115 $twitter->oauth("oauth/access_token", ["oauth_verifier" => "fake_oauth_verifier"]);
118 public function testUrl()
120 $url = $this->twitter->url('oauth/authorize', ['foo' => 'bar', 'baz' => 'qux']);
121 $this->assertEquals('https://api.twitter.com/oauth/authorize?foo=bar&baz=qux', $url);
124 public function testGetAccountVerifyCredentials()
126 // Include entities boolean added to test parameter value cohearsion
127 $this->twitter->get('account/verify_credentials', ["include_entities" => false]);
128 $this->assertEquals(200, $this->twitter->getLastHttpCode());
131 // BUG: testing is too unreliable for now
132 // public function testSetProxy()
134 // $this->twitter->setProxy(array(
135 // 'CURLOPT_PROXY' => PROXY,
136 // 'CURLOPT_PROXYUSERPWD' => PROXYUSERPWD,
137 // 'CURLOPT_PROXYPORT' => PROXYPORT,
139 // $this->twitter->setTimeouts(60, 60);
140 // $result = $this->twitter->get('account/verify_credentials');
141 // $this->assertEquals(200, $this->twitter->getLastHttpCode());
142 // $this->assertObjectHasAttribute('id', $result);
145 public function testGetStatusesMentionsTimeline()
147 $this->twitter->get('statuses/mentions_timeline');
148 $this->assertEquals(200, $this->twitter->getLastHttpCode());
151 public function testGetSearchTweets()
153 $result = $this->twitter->get('search/tweets', ['q' => 'twitter']);
154 $this->assertEquals(200, $this->twitter->getLastHttpCode());
155 return $result->statuses;
159 * @depends testGetSearchTweets
161 public function testGetSearchTweetsWithMaxId($statuses)
163 $maxId = array_pop($statuses)->id_str;
164 $this->twitter->get('search/tweets', ['q' => 'twitter', 'max_id' => $maxId]);
165 $this->assertEquals(200, $this->twitter->getLastHttpCode());
168 public function testPostFavoritesCreate()
170 $result = $this->twitter->post('favorites/create', ['id' => '6242973112']);
171 if ($this->twitter->getLastHttpCode() == 403) {
172 // Status already favorited
173 $this->assertEquals(139, $result->errors[0]->code);
175 $this->assertEquals(200, $this->twitter->getLastHttpCode());
180 * @depends testPostFavoritesCreate
182 public function testPostFavoritesDestroy()
184 $this->twitter->post('favorites/destroy', ['id' => '6242973112']);
185 $this->assertEquals(200, $this->twitter->getLastHttpCode());
188 public function testPostStatusesUpdateWithMedia()
190 $this->twitter->setTimeouts(60, 30);
191 // Image source https://www.flickr.com/photos/titrans/8548825587/
192 $file_path = __DIR__ . '/kitten.jpg';
193 $result = $this->twitter->upload('media/upload', ['media' => $file_path]);
194 $this->assertEquals(200, $this->twitter->getLastHttpCode());
195 $this->assertObjectHasAttribute('media_id_string', $result);
196 $parameters = ['status' => 'Hello World ' . time(), 'media_ids' => $result->media_id_string];
197 $result = $this->twitter->post('statuses/update', $parameters);
198 $this->assertEquals(200, $this->twitter->getLastHttpCode());
199 if ($this->twitter->getLastHttpCode() == 200) {
200 $result = $this->twitter->post('statuses/destroy/' . $result->id_str);
205 public function testPostStatusesUpdateWithMediaChunked()
207 $this->twitter->setTimeouts(60, 30);
208 // Video source http://www.sample-videos.com/
209 $file_path = __DIR__ . '/video.mp4';
210 $result = $this->twitter->upload('media/upload', ['media' => $file_path, 'media_type' => 'video/mp4'], true);
211 $this->assertEquals(201, $this->twitter->getLastHttpCode());
212 $this->assertObjectHasAttribute('media_id_string', $result);
213 $parameters = ['status' => 'Hello World ' . time(), 'media_ids' => $result->media_id_string];
214 $result = $this->twitter->post('statuses/update', $parameters);
215 $this->assertEquals(200, $this->twitter->getLastHttpCode());
216 if ($this->twitter->getLastHttpCode() == 200) {
217 $result = $this->twitter->post('statuses/destroy/' . $result->id_str);
222 public function testPostStatusesUpdateUtf8()
224 $result = $this->twitter->post('statuses/update', ['status' => 'xこんにちは世界 ' . time()]);
225 $this->assertEquals(200, $this->twitter->getLastHttpCode());
230 * @depends testPostStatusesUpdateUtf8
232 public function testPostStatusesDestroy($status)
234 $this->twitter->post('statuses/destroy/' . $status->id_str);
235 $this->assertEquals(200, $this->twitter->getLastHttpCode());
238 public function testLastResult()
240 $this->twitter->get('search/tweets', ['q' => 'twitter']);
241 $this->assertEquals('search/tweets', $this->twitter->getLastApiPath());
242 $this->assertEquals(200, $this->twitter->getLastHttpCode());
243 $this->assertObjectHasAttribute('statuses', $this->twitter->getLastBody());
247 * @depends testLastResult
249 public function testResetLastResponse()
251 $this->twitter->resetLastResponse();
252 $this->assertEquals('', $this->twitter->getLastApiPath());
253 $this->assertEquals(0, $this->twitter->getLastHttpCode());
254 $this->assertEquals([], $this->twitter->getLastBody());