Yaffs site version 1.1
[yaffs-website] / vendor / j7mbo / twitter-api-php / test / TwitterAPIExchangeTest.php
1 <?php
2
3 /**
4  * Class TwitterAPIExchangeTest
5  *
6  * Contains ALL the integration tests
7  *
8  * @note This test account is not actively monitored so you gain nothing by hi-jacking it :-)
9  */
10 class TwitterAPIExchangeTest extends \PHPUnit_Framework_TestCase
11 {
12     /**
13      * @var string
14      */
15     const CONSUMER_KEY = 'VXD22AD9kcNyNgsfW6cwkWRkw';
16
17     /**
18      * @var string
19      */
20     const CONSUMER_SECRET = 'y0k3z9Y46V0DMAKGe4Az2aDtqNt9aXjg3ssCMCldUheGBT0YL9';
21
22     /**
23      * @var string
24      */
25     const OAUTH_ACCESS_TOKEN = '3232926711-kvMvNK5mFJlUFzCdtw3ryuwZfhIbLJtPX9e8E3Y';
26
27     /**
28      * @var string
29      */
30     const OAUTH_ACCESS_TOKEN_SECRET = 'EYrFp0lfNajBslYV3WgAGmpHqYZvvNxP5uxxSq8Dbs1wa';
31
32     /**
33      * @var \TwitterAPIExchange
34      */
35     protected $exchange;
36
37     /**
38      * @var int Stores a tweet id (for /update) to be deleted later (by /destroy)
39      */
40     private static $tweetId;
41
42     /**
43      * @var int Stores uploaded media id
44      */
45     private static $mediaId;
46
47     /**
48      * {@inheritDoc}
49      */
50     public function setUp()
51     {
52         $settings  = array();
53
54         /** Because I'm lazy... **/
55         $reflector = new \ReflectionClass($this);
56
57         foreach ($reflector->getConstants() as $key => $value)
58         {
59             $settings[strtolower($key)] = $value;
60         }
61
62         $this->exchange = new \TwitterAPIExchange($settings);
63     }
64
65     /**
66      * GET statuses/mentions_timeline
67      *
68      * @see https://dev.twitter.com/rest/reference/get/statuses/mentions_timeline
69      */
70     public function testStatusesMentionsTimeline()
71     {
72         $url    = 'https://api.twitter.com/1.1/statuses/mentions_timeline.json';
73         $method = 'GET';
74         $params = '?max_id=595150043381915648';
75
76         $data     = $this->exchange->request($url, $method, $params);
77         $expected = "@j7php Test mention";
78
79         $this->assertContains($expected, $data);
80     }
81
82     /**
83      * GET statuses/user_timeline
84      *
85      * @see https://dev.twitter.com/rest/reference/get/statuses/user_timeline
86      */
87     public function testStatusesUserTimeline()
88     {
89         $url    = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
90         $method = 'GET';
91         $params = '?user_id=3232926711';
92
93         $data     = $this->exchange->request($url, $method, $params);
94         $expected = "Test Tweet";
95
96         $this->assertContains($expected, $data);
97     }
98
99     /**
100      * GET statuses/home_timeline
101      *
102      * @see https://dev.twitter.com/rest/reference/get/statuses/home_timeline
103      */
104     public function testStatusesHomeTimeline()
105     {
106         $url    = 'https://api.twitter.com/1.1/statuses/home_timeline.json';
107         $method = 'GET';
108         $params = '?user_id=3232926711&max_id=756123701888839681';
109
110         $data     = $this->exchange->request($url, $method, $params);
111         $expected = "Test Tweet";
112
113         $this->assertContains($expected, $data);
114     }
115
116     /**
117      * GET statuses/retweets_of_me
118      *
119      * @see https://dev.twitter.com/rest/reference/get/statuses/retweets_of_me
120      */
121     public function testStatusesRetweetsOfMe()
122     {
123         $url    = 'https://api.twitter.com/1.1/statuses/retweets_of_me.json';
124         $method = 'GET';
125
126         $data     = $this->exchange->request($url, $method);
127         $expected = 'travis CI and tests';
128
129         $this->assertContains($expected, $data);
130     }
131
132     /**
133      * GET statuses/retweets/:id
134      *
135      * @see https://api.twitter.com/1.1/statuses/retweets/:id.json
136      */
137     public function testStatusesRetweetsOfId()
138     {
139         $url    = 'https://api.twitter.com/1.1/statuses/retweets/595155660494471168.json';
140         $method = 'GET';
141
142         $data     = $this->exchange->request($url, $method);
143         $expected = 'travis CI and tests';
144
145         $this->assertContains($expected, $data);
146     }
147
148     /**
149      * GET Statuses/Show/:id
150      *
151      * @see https://dev.twitter.com/rest/reference/get/statuses/show/:id
152      */
153     public function testStatusesShowId()
154     {
155         $url    = 'https://api.twitter.com/1.1/statuses/show.json';
156         $method = 'GET';
157         $params = '?id=595155660494471168';
158
159         $data     = $this->exchange->request($url, $method, $params);
160         $expected = 'travis CI and tests';
161
162         $this->assertContains($expected, $data);
163     }
164
165     /**
166      * POST media/upload
167      *
168      * @see https://dev.twitter.com/rest/reference/post/media/upload
169      *
170      * @note Uploaded unattached media files will be available for attachment to a tweet for 60 minutes
171      */
172     public function testMediaUpload()
173     {
174         $file = file_get_contents(__DIR__ . '/img.png');
175         $data = base64_encode($file);
176
177         $url    = 'https://upload.twitter.com/1.1/media/upload.json';
178         $method = 'POST';
179         $params = array(
180             'media_data' => $data
181         );
182
183         $data     = $this->exchange->request($url, $method, $params);
184         $expected = 'image\/png';
185
186         $this->assertContains($expected, $data);
187
188         /** Store the media id for later **/
189         $data = @json_decode($data, true);
190
191         $this->assertArrayHasKey('media_id', is_array($data) ? $data : array());
192
193         self::$mediaId = $data['media_id'];
194     }
195
196     /**
197      * POST statuses/update
198      *
199      * @see https://dev.twitter.com/rest/reference/post/statuses/update
200      */
201     public function testStatusesUpdate()
202     {
203         if (!self::$mediaId)
204         {
205             $this->fail('Cannot /update status because /upload failed');
206         }
207
208         $url    = 'https://api.twitter.com/1.1/statuses/update.json';
209         $method = 'POST';
210         $params = array(
211             'status' => 'TEST TWEET TO BE DELETED' . rand(),
212             'possibly_sensitive' => false,
213             'media_ids' => self::$mediaId
214         );
215
216         $data     = $this->exchange->request($url, $method, $params);
217         $expected = 'TEST TWEET TO BE DELETED';
218
219         $this->assertContains($expected, $data);
220
221         /** Store the tweet id for testStatusesDestroy() **/
222         $data = @json_decode($data, true);
223
224         $this->assertArrayHasKey('id_str', is_array($data) ? $data : array());
225
226         self::$tweetId = $data['id_str'];
227
228         /** We've done this now, yay **/
229         self::$mediaId = null;
230     }
231
232     /**
233      * POST statuses/destroy/:id
234      *
235      * @see https://dev.twitter.com/rest/reference/post/statuses/destroy/:id
236      */
237     public function testStatusesDestroy()
238     {
239         if (!self::$tweetId)
240         {
241             $this->fail('Cannot /destroy status because /update failed');
242         }
243
244         $url    = sprintf('https://api.twitter.com/1.1/statuses/destroy/%d.json', self::$tweetId);
245         $method = 'POST';
246         $params = array(
247             'id' => self::$tweetId
248         );
249
250         $data     = $this->exchange->request($url, $method, $params);
251         $expected = 'TEST TWEET TO BE DELETED';
252
253         $this->assertContains($expected, $data);
254
255         /** We've done this now, yay **/
256         self::$tweetId = null;
257     }
258
259     /**
260      * GET search/tweets
261      *
262      * @see https://dev.twitter.com/rest/reference/get/search/tweets
263      */
264     public function testCanSearchWithHashTag()
265     {
266         $url    = 'https://api.twitter.com/1.1/search/tweets.json';
267         $method = 'GET';
268         $params = '?q=#twitter';
269
270         $data = $this->exchange->request($url, $method, $params);
271         $data = (array)@json_decode($data, true);
272
273         $this->assertNotCount(1, $data);
274     }
275
276     /**
277      * Test to check that options passed to curl do not cause any issues
278      */
279     public function testAdditionalCurlOptions()
280     {
281         $url    = 'https://api.twitter.com/1.1/search/tweets.json';
282         $method = 'GET';
283         $params = '?q=#twitter';
284
285         $data = $this->exchange->request($url, $method, $params, array(CURLOPT_ENCODING => ''));
286         $data = (array)@json_decode($data, true);
287
288         $this->assertNotCount(1, $data);
289     }
290
291     /**
292      * Apparently users/lookup was not working with a POST
293      *
294      * @see https://github.com/J7mbo/twitter-api-php/issues/70
295      */
296     public function testIssue70()
297     {
298         $url    = 'https://api.twitter.com/1.1/users/lookup.json';
299         $method = 'POST';
300         $params = array(
301             'screen_name' => 'lifehacker'
302         );
303
304         $data = $this->exchange->request($url, $method, $params);
305         $this->assertContains('created_at', $data);
306     }
307
308     /**
309      * Thanks to Sharath at eywamedia for bringint this to my attention
310      */
311     public function testPut()
312     {
313         $url    = 'https://ads-api.twitter.com/1/accounts/hkk5/campaigns/8zwv';
314         $method = 'PUT';
315         $params = array (
316             'name'   => 'Important',
317             'paused' => true
318         );
319
320         $data = $this->exchange->request($url, $method, $params);
321
322         /** If we get this back, then it looks like we can support PUT! :-) **/
323         $this->assertContains('UNAUTHORIZED_CLIENT_APPLICATION', $data);
324     }
325
326     public function testDelete()
327     {
328         $params = array();
329
330         // foobaa is sandbox Ads account id
331         $url = 'https://ads-api-sandbox.twitter.com/1/accounts/foobaa';
332         $method = 'DELETE';
333
334         $data = $this->exchange->request($url, $method, $params);
335
336         $this->assertContains('UNAUTHORIZED_CLIENT_APPLICATION', $data);
337     }
338 }