comparison default/assets/vendors/theme-widgets/vendor/abraham/twitteroauth/tests/TwitterOAuthTest.php @ 0:1d038bc9b3d2 default tip

Up:default
author Liny <dev@neowd.com>
date Sat, 31 May 2025 09:21:51 +0800
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:1d038bc9b3d2
1 <?php
2 /**
3 * WARNING: Running these tests will post and delete through the actual Twitter account.
4 */
5 namespace Abraham\TwitterOAuth\Test;
6
7 use Abraham\TwitterOAuth\TwitterOAuth;
8
9 class TwitterOAuthTest extends \PHPUnit_Framework_TestCase
10 {
11 /** @var TwitterOAuth */
12 protected $twitter;
13
14 protected function setUp()
15 {
16 $this->twitter = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);
17 }
18
19 public function testBuildClient()
20 {
21 $this->assertObjectHasAttribute('consumer', $this->twitter);
22 $this->assertObjectHasAttribute('token', $this->twitter);
23 }
24
25 public function testSetOauthToken()
26 {
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());
33 }
34
35 public function testOauth2Token()
36 {
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);
43 return $result;
44 }
45
46 /**
47 * @depends testOauth2Token
48 */
49 public function testBearerToken($accessToken)
50 {
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));
56 }
57 $this->assertEquals(200, $twitter->getLastHttpCode());
58 return $accessToken;
59 }
60
61 // This causes issues for parallel run tests.
62 // /**
63 // * @depends testBearerToken
64 // */
65 // public function testOauth2TokenInvalidate($accessToken)
66 // {
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))
72 // );
73 // $this->assertEquals(200, $twitter->getLastHttpCode());
74 // $this->assertObjectHasAttribute('access_token', $result);
75 // return $result;
76 // }
77
78 public function testOauthRequestToken()
79 {
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']);
87 return $result;
88 }
89
90 /**
91 * @expectedException \Abraham\TwitterOAuth\TwitterOAuthException
92 * @expectedExceptionMessage Could not authenticate you
93 */
94 public function testOauthRequestTokenException()
95 {
96 $twitter = new TwitterOAuth('CONSUMER_KEY', 'CONSUMER_SECRET');
97 $result = $twitter->oauth('oauth/request_token', ['oauth_callback' => OAUTH_CALLBACK]);
98 return $result;
99 }
100
101 /**
102 * @expectedException \Abraham\TwitterOAuth\TwitterOAuthException
103 * @expectedExceptionMessage Invalid oauth_verifier parameter
104 * @depends testOauthRequestToken
105 */
106 public function testOauthAccessTokenTokenException(array $requestToken)
107 {
108 // Can't test this without a browser logging into Twitter so check for the correct error instead.
109 $twitter = new TwitterOAuth(
110 CONSUMER_KEY,
111 CONSUMER_SECRET,
112 $requestToken['oauth_token'],
113 $requestToken['oauth_token_secret']
114 );
115 $twitter->oauth("oauth/access_token", ["oauth_verifier" => "fake_oauth_verifier"]);
116 }
117
118 public function testUrl()
119 {
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);
122 }
123
124 public function testGetAccountVerifyCredentials()
125 {
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());
129 }
130
131 // BUG: testing is too unreliable for now
132 // public function testSetProxy()
133 // {
134 // $this->twitter->setProxy(array(
135 // 'CURLOPT_PROXY' => PROXY,
136 // 'CURLOPT_PROXYUSERPWD' => PROXYUSERPWD,
137 // 'CURLOPT_PROXYPORT' => PROXYPORT,
138 // ));
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);
143 // }
144
145 public function testGetStatusesMentionsTimeline()
146 {
147 $this->twitter->get('statuses/mentions_timeline');
148 $this->assertEquals(200, $this->twitter->getLastHttpCode());
149 }
150
151 public function testGetSearchTweets()
152 {
153 $result = $this->twitter->get('search/tweets', ['q' => 'twitter']);
154 $this->assertEquals(200, $this->twitter->getLastHttpCode());
155 return $result->statuses;
156 }
157
158 /**
159 * @depends testGetSearchTweets
160 */
161 public function testGetSearchTweetsWithMaxId($statuses)
162 {
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());
166 }
167
168 public function testPostFavoritesCreate()
169 {
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);
174 } else {
175 $this->assertEquals(200, $this->twitter->getLastHttpCode());
176 }
177 }
178
179 /**
180 * @depends testPostFavoritesCreate
181 */
182 public function testPostFavoritesDestroy()
183 {
184 $this->twitter->post('favorites/destroy', ['id' => '6242973112']);
185 $this->assertEquals(200, $this->twitter->getLastHttpCode());
186 }
187
188 public function testPostStatusesUpdateWithMedia()
189 {
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);
201 }
202 return $result;
203 }
204
205 public function testPostStatusesUpdateWithMediaChunked()
206 {
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);
218 }
219 return $result;
220 }
221
222 public function testPostStatusesUpdateUtf8()
223 {
224 $result = $this->twitter->post('statuses/update', ['status' => 'xこんにちは世界 ' . time()]);
225 $this->assertEquals(200, $this->twitter->getLastHttpCode());
226 return $result;
227 }
228
229 /**
230 * @depends testPostStatusesUpdateUtf8
231 */
232 public function testPostStatusesDestroy($status)
233 {
234 $this->twitter->post('statuses/destroy/' . $status->id_str);
235 $this->assertEquals(200, $this->twitter->getLastHttpCode());
236 }
237
238 public function testLastResult()
239 {
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());
244 }
245
246 /**
247 * @depends testLastResult
248 */
249 public function testResetLastResponse()
250 {
251 $this->twitter->resetLastResponse();
252 $this->assertEquals('', $this->twitter->getLastApiPath());
253 $this->assertEquals(0, $this->twitter->getLastHttpCode());
254 $this->assertEquals([], $this->twitter->getLastBody());
255 }
256 }