4 * Class TwitterAPIExchangeTest
6 * Contains ALL the integration tests
8 * @note This test account is not actively monitored so you gain nothing by hi-jacking it :-)
10 class TwitterAPIExchangeTest extends \PHPUnit_Framework_TestCase
15 const CONSUMER_KEY = 'VXD22AD9kcNyNgsfW6cwkWRkw';
20 const CONSUMER_SECRET = 'y0k3z9Y46V0DMAKGe4Az2aDtqNt9aXjg3ssCMCldUheGBT0YL9';
25 const OAUTH_ACCESS_TOKEN = '3232926711-kvMvNK5mFJlUFzCdtw3ryuwZfhIbLJtPX9e8E3Y';
30 const OAUTH_ACCESS_TOKEN_SECRET = 'EYrFp0lfNajBslYV3WgAGmpHqYZvvNxP5uxxSq8Dbs1wa';
33 * @var \TwitterAPIExchange
38 * @var int Stores a tweet id (for /update) to be deleted later (by /destroy)
40 private static $tweetId;
43 * @var int Stores uploaded media id
45 private static $mediaId;
50 public function setUp()
54 /** Because I'm lazy... **/
55 $reflector = new \ReflectionClass($this);
57 foreach ($reflector->getConstants() as $key => $value)
59 $settings[strtolower($key)] = $value;
62 $this->exchange = new \TwitterAPIExchange($settings);
66 * GET statuses/mentions_timeline
68 * @see https://dev.twitter.com/rest/reference/get/statuses/mentions_timeline
70 public function testStatusesMentionsTimeline()
72 $url = 'https://api.twitter.com/1.1/statuses/mentions_timeline.json';
74 $params = '?max_id=595150043381915648';
76 $data = $this->exchange->request($url, $method, $params);
77 $expected = "@j7php Test mention";
79 $this->assertContains($expected, $data);
83 * GET statuses/user_timeline
85 * @see https://dev.twitter.com/rest/reference/get/statuses/user_timeline
87 public function testStatusesUserTimeline()
89 $url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
91 $params = '?user_id=3232926711';
93 $data = $this->exchange->request($url, $method, $params);
94 $expected = "Test Tweet";
96 $this->assertContains($expected, $data);
100 * GET statuses/home_timeline
102 * @see https://dev.twitter.com/rest/reference/get/statuses/home_timeline
104 public function testStatusesHomeTimeline()
106 $url = 'https://api.twitter.com/1.1/statuses/home_timeline.json';
108 $params = '?user_id=3232926711&max_id=756123701888839681';
110 $data = $this->exchange->request($url, $method, $params);
111 $expected = "Test Tweet";
113 $this->assertContains($expected, $data);
117 * GET statuses/retweets_of_me
119 * @see https://dev.twitter.com/rest/reference/get/statuses/retweets_of_me
121 public function testStatusesRetweetsOfMe()
123 $url = 'https://api.twitter.com/1.1/statuses/retweets_of_me.json';
126 $data = $this->exchange->request($url, $method);
127 $expected = 'travis CI and tests';
129 $this->assertContains($expected, $data);
133 * GET statuses/retweets/:id
135 * @see https://api.twitter.com/1.1/statuses/retweets/:id.json
137 public function testStatusesRetweetsOfId()
139 $url = 'https://api.twitter.com/1.1/statuses/retweets/595155660494471168.json';
142 $data = $this->exchange->request($url, $method);
143 $expected = 'travis CI and tests';
145 $this->assertContains($expected, $data);
149 * GET Statuses/Show/:id
151 * @see https://dev.twitter.com/rest/reference/get/statuses/show/:id
153 public function testStatusesShowId()
155 $url = 'https://api.twitter.com/1.1/statuses/show.json';
157 $params = '?id=595155660494471168';
159 $data = $this->exchange->request($url, $method, $params);
160 $expected = 'travis CI and tests';
162 $this->assertContains($expected, $data);
168 * @see https://dev.twitter.com/rest/reference/post/media/upload
170 * @note Uploaded unattached media files will be available for attachment to a tweet for 60 minutes
172 public function testMediaUpload()
174 $file = file_get_contents(__DIR__ . '/img.png');
175 $data = base64_encode($file);
177 $url = 'https://upload.twitter.com/1.1/media/upload.json';
180 'media_data' => $data
183 $data = $this->exchange->request($url, $method, $params);
184 $expected = 'image\/png';
186 $this->assertContains($expected, $data);
188 /** Store the media id for later **/
189 $data = @json_decode($data, true);
191 $this->assertArrayHasKey('media_id', is_array($data) ? $data : array());
193 self::$mediaId = $data['media_id'];
197 * POST statuses/update
199 * @see https://dev.twitter.com/rest/reference/post/statuses/update
201 public function testStatusesUpdate()
205 $this->fail('Cannot /update status because /upload failed');
208 $url = 'https://api.twitter.com/1.1/statuses/update.json';
211 'status' => 'TEST TWEET TO BE DELETED' . rand(),
212 'possibly_sensitive' => false,
213 'media_ids' => self::$mediaId
216 $data = $this->exchange->request($url, $method, $params);
217 $expected = 'TEST TWEET TO BE DELETED';
219 $this->assertContains($expected, $data);
221 /** Store the tweet id for testStatusesDestroy() **/
222 $data = @json_decode($data, true);
224 $this->assertArrayHasKey('id_str', is_array($data) ? $data : array());
226 self::$tweetId = $data['id_str'];
228 /** We've done this now, yay **/
229 self::$mediaId = null;
233 * POST statuses/destroy/:id
235 * @see https://dev.twitter.com/rest/reference/post/statuses/destroy/:id
237 public function testStatusesDestroy()
241 $this->fail('Cannot /destroy status because /update failed');
244 $url = sprintf('https://api.twitter.com/1.1/statuses/destroy/%d.json', self::$tweetId);
247 'id' => self::$tweetId
250 $data = $this->exchange->request($url, $method, $params);
251 $expected = 'TEST TWEET TO BE DELETED';
253 $this->assertContains($expected, $data);
255 /** We've done this now, yay **/
256 self::$tweetId = null;
262 * @see https://dev.twitter.com/rest/reference/get/search/tweets
264 public function testCanSearchWithHashTag()
266 $url = 'https://api.twitter.com/1.1/search/tweets.json';
268 $params = '?q=#twitter';
270 $data = $this->exchange->request($url, $method, $params);
271 $data = (array)@json_decode($data, true);
273 $this->assertNotCount(1, $data);
277 * Test to check that options passed to curl do not cause any issues
279 public function testAdditionalCurlOptions()
281 $url = 'https://api.twitter.com/1.1/search/tweets.json';
283 $params = '?q=#twitter';
285 $data = $this->exchange->request($url, $method, $params, array(CURLOPT_ENCODING => ''));
286 $data = (array)@json_decode($data, true);
288 $this->assertNotCount(1, $data);
292 * Apparently users/lookup was not working with a POST
294 * @see https://github.com/J7mbo/twitter-api-php/issues/70
296 public function testIssue70()
298 $url = 'https://api.twitter.com/1.1/users/lookup.json';
301 'screen_name' => 'lifehacker'
304 $data = $this->exchange->request($url, $method, $params);
305 $this->assertContains('created_at', $data);
309 * Thanks to Sharath at eywamedia for bringint this to my attention
311 public function testPut()
313 $url = 'https://ads-api.twitter.com/1/accounts/hkk5/campaigns/8zwv';
316 'name' => 'Important',
320 $data = $this->exchange->request($url, $method, $params);
322 /** If we get this back, then it looks like we can support PUT! :-) **/
323 $this->assertContains('UNAUTHORIZED_CLIENT_APPLICATION', $data);
326 public function testDelete()
330 // foobaa is sandbox Ads account id
331 $url = 'https://ads-api-sandbox.twitter.com/1/accounts/foobaa';
334 $data = $this->exchange->request($url, $method, $params);
336 $this->assertContains('UNAUTHORIZED_CLIENT_APPLICATION', $data);