TwitterTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Service_Twitter
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: TwitterTest.php 22318 2010-05-29 18:24:27Z padraic $
  21. */
  22. if (!defined('PHPUnit_MAIN_METHOD')) {
  23. define('PHPUnit_MAIN_METHOD', 'Zend_Service_Twitter_TwitterTest2::main');
  24. }
  25. /**
  26. * Test helper
  27. */
  28. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  29. /** Zend_Service_Twitter */
  30. require_once 'Zend/Service/Twitter.php';
  31. /** Zend_Http_Client */
  32. require_once 'Zend/Http/Client.php';
  33. /** Zend_Http_Client_Adapter_Test */
  34. require_once 'Zend/Http/Client/Adapter/Test.php';
  35. /**
  36. * @category Zend
  37. * @package Zend_Service_Twitter
  38. * @subpackage UnitTests
  39. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  40. * @license http://framework.zend.com/license/new-bsd New BSD License
  41. * @group Zend_Service
  42. * @group Zend_Service_Twitter
  43. */
  44. class Zend_Service_Twitter_TwitterTest extends PHPUnit_Framework_TestCase
  45. {
  46. /**
  47. * Runs the test methods of this class.
  48. *
  49. * @return void
  50. */
  51. public static function main()
  52. {
  53. $suite = new PHPUnit_Framework_TestSuite(__CLASS__);
  54. $result = PHPUnit_TextUI_TestRunner::run($suite);
  55. }
  56. public function teardown()
  57. {
  58. Zend_Service_Abstract::setHttpClient(new Zend_Http_Client);
  59. }
  60. /**
  61. * Quick reusable Twitter Service stub setup. Its purpose is to fake
  62. * interactions with Twitter so the component can focus on what matters:
  63. * 1. Makes correct requests (URI, parameters and HTTP method)
  64. * 2. Parses all responses and returns a Zend_Rest_Client_Result
  65. * 3. TODO: Correctly utilises all optional parameters
  66. *
  67. * If used correctly, tests will be fast, efficient, and focused on
  68. * Zend_Service_Twitter's behaviour only. No other dependencies need be
  69. * tested. The Twitter API Changelog should be regularly reviewed to
  70. * ensure the component is synchronised to the API.
  71. *
  72. * @param string $path Path appended to Twitter API endpoint
  73. * @param string $method Do we expect HTTP GET or POST?
  74. * @param string $responseFile File containing a valid XML response to the request
  75. * @param array $params Expected GET/POST parameters for the request
  76. * @return Zend_Http_Client
  77. */
  78. protected function _stubTwitter($path, $method, $responseFile = null, array $params = null)
  79. {
  80. $client = $this->getMock('Zend_Http_Client');
  81. $client->expects($this->any())->method('resetParameters')
  82. ->will($this->returnValue($client));
  83. $client->expects($this->once())->method('setUri')
  84. ->with('http://api.twitter.com/1/' . $path);
  85. $response = $this->getMock('Zend_Http_Response', array(), array(), '', false);
  86. if (!is_null($params)) {
  87. $setter = 'setParameter' . ucfirst(strtolower($method));
  88. $client->expects($this->once())->method($setter)->with($params);
  89. }
  90. $client->expects($this->once())->method('request')->with($method)
  91. ->will($this->returnValue($response));
  92. $response->expects($this->any())->method('getBody')
  93. ->will($this->returnValue(
  94. isset($responseFile) ? file_get_contents(dirname(__FILE__) . '/_files/' . $responseFile) : ''
  95. ));
  96. return $client;
  97. }
  98. /**
  99. * @group ZF-8218
  100. */
  101. public function testUserNameNotRequired()
  102. {
  103. $twitter = new Zend_Service_Twitter();
  104. $twitter->setLocalHttpClient($this->_stubTwitter(
  105. 'users/show.xml', Zend_Http_Client::GET, 'users.show.twitter.xml',
  106. array('id'=>'twitter')
  107. ));
  108. $exists = $twitter->user->show('twitter')->id() !== null;
  109. $this->assertTrue($exists);
  110. }
  111. /**
  112. * @group ZF-7781
  113. */
  114. public function testRetrievingStatusesWithValidScreenNameThrowsNoInvalidScreenNameException()
  115. {
  116. $twitter = new Zend_Service_Twitter();
  117. $twitter->setLocalHttpClient($this->_stubTwitter(
  118. 'statuses/user_timeline.xml', Zend_Http_Client::GET, 'user_timeline.twitter.xml'
  119. ));
  120. $twitter->status->userTimeline(array('screen_name' => 'twitter'));
  121. }
  122. /**
  123. * @group ZF-7781
  124. * @expectedException Zend_Service_Twitter_Exception
  125. */
  126. public function testRetrievingStatusesWithInvalidScreenNameCharacterThrowsInvalidScreenNameException()
  127. {
  128. $twitter = new Zend_Service_Twitter();
  129. $twitter->status->userTimeline(array('screen_name' => 'abc.def'));
  130. }
  131. /**
  132. * @group ZF-7781
  133. */
  134. public function testRetrievingStatusesWithInvalidScreenNameLengthThrowsInvalidScreenNameException()
  135. {
  136. $this->setExpectedException('Zend_Service_Twitter_Exception');
  137. $twitter = new Zend_Service_Twitter();
  138. $twitter->status->userTimeline(array('screen_name' => 'abcdef_abc123_abc123x'));
  139. }
  140. /**
  141. * @group ZF-7781
  142. */
  143. public function testStatusUserTimelineConstructsExpectedGetUriAndOmitsInvalidParams()
  144. {
  145. $twitter = new Zend_Service_Twitter;
  146. $twitter->setLocalHttpClient($this->_stubTwitter(
  147. 'statuses/user_timeline/783214.xml', Zend_Http_Client::GET, 'user_timeline.twitter.xml', array(
  148. 'page' => '1',
  149. 'count' => '123',
  150. 'user_id' => '783214',
  151. 'since_id' => '10000',
  152. 'max_id' => '20000',
  153. 'screen_name' => 'twitter'
  154. )
  155. ));
  156. $twitter->status->userTimeline(array(
  157. 'id' => '783214',
  158. 'since' => '+2 days', /* invalid param since Apr 2009 */
  159. 'page' => '1',
  160. 'count' => '123',
  161. 'user_id' => '783214',
  162. 'since_id' => '10000',
  163. 'max_id' => '20000',
  164. 'screen_name' => 'twitter'
  165. ));
  166. }
  167. public function testOverloadingGetShouldReturnObjectInstanceWithValidMethodType()
  168. {
  169. $twitter = new Zend_Service_Twitter;
  170. $return = $twitter->status;
  171. $this->assertSame($twitter, $return);
  172. }
  173. /**
  174. * @expectedException Zend_Service_Twitter_Exception
  175. */
  176. public function testOverloadingGetShouldthrowExceptionWithInvalidMethodType()
  177. {
  178. $twitter = new Zend_Service_Twitter;
  179. $return = $twitter->foo;
  180. }
  181. /**
  182. * @expectedException Zend_Service_Twitter_Exception
  183. */
  184. public function testOverloadingGetShouldthrowExceptionWithInvalidFunction()
  185. {
  186. $twitter = new Zend_Service_Twitter;
  187. $return = $twitter->foo();
  188. }
  189. public function testMethodProxyingDoesNotThrowExceptionsWithValidMethods()
  190. {
  191. $twitter = new Zend_Service_Twitter;
  192. $twitter->setLocalHttpClient($this->_stubTwitter(
  193. 'statuses/public_timeline.xml', Zend_Http_Client::GET, 'public_timeline.xml'
  194. ));
  195. $twitter->status->publicTimeline();
  196. }
  197. /**
  198. * @expectedException Zend_Service_Twitter_Exception
  199. */
  200. public function testMethodProxyingThrowExceptionsWithInvalidMethods()
  201. {
  202. $twitter = new Zend_Service_Twitter;
  203. $twitter->status->foo();
  204. }
  205. public function testVerifiedCredentials()
  206. {
  207. $twitter = new Zend_Service_Twitter;
  208. $twitter->setLocalHttpClient($this->_stubTwitter(
  209. 'account/verify_credentials.xml', Zend_Http_Client::GET, 'account.verify_credentials.xml'
  210. ));
  211. $response = $twitter->account->verifyCredentials();
  212. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  213. }
  214. public function testPublicTimelineStatusReturnsResults()
  215. {
  216. $twitter = new Zend_Service_Twitter;
  217. $twitter->setLocalHttpClient($this->_stubTwitter(
  218. 'statuses/public_timeline.xml', Zend_Http_Client::GET, 'public_timeline.xml'
  219. ));
  220. $response = $twitter->status->publicTimeline();
  221. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  222. }
  223. public function testRateLimitStatusReturnsResults()
  224. {
  225. $twitter = new Zend_Service_Twitter;
  226. $twitter->setLocalHttpClient($this->_stubTwitter(
  227. 'account/rate_limit_status.xml', Zend_Http_Client::GET, 'rate_limit_status.xml'
  228. ));
  229. $response = $twitter->account->rateLimitStatus();
  230. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  231. }
  232. public function testRateLimitStatusHasHitsLeft()
  233. {
  234. $twitter = new Zend_Service_Twitter;
  235. $twitter->setLocalHttpClient($this->_stubTwitter(
  236. 'account/rate_limit_status.xml', Zend_Http_Client::GET, 'rate_limit_status.xml'
  237. ));
  238. $response = $twitter->account->rateLimitStatus();
  239. $remaining_hits = $response->toValue($response->{'remaining-hits'});
  240. $this->assertEquals(150, $remaining_hits);
  241. }
  242. public function testAccountEndSession()
  243. {
  244. $twitter = new Zend_Service_Twitter;
  245. $twitter->setLocalHttpClient($this->_stubTwitter(
  246. 'account/end_session', Zend_Http_Client::GET
  247. ));
  248. $response = $twitter->account->endSession();
  249. $this->assertTrue($response);
  250. }
  251. /**
  252. * TODO: Check actual purpose. New friend returns XML response, existing
  253. * friend returns a 403 code.
  254. */
  255. public function testFriendshipCreate()
  256. {
  257. $twitter = new Zend_Service_Twitter;
  258. $twitter->setLocalHttpClient($this->_stubTwitter(
  259. 'friendships/create/twitter.xml', Zend_Http_Client::POST, 'friendships.create.twitter.xml'
  260. ));
  261. $response = $twitter->friendship->create('twitter');
  262. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  263. }
  264. /**
  265. * TODO: Mismatched behaviour from API. Per the API this can assert the
  266. * existence of a friendship between any two users (not just the current
  267. * user). We should expand the method or add a better fit method for
  268. * general use.
  269. */
  270. public function testFriendshipExists()
  271. {
  272. $twitter = new Zend_Service_Twitter('padraicb');
  273. $twitter->setLocalHttpClient($this->_stubTwitter(
  274. 'friendships/exists.xml', Zend_Http_Client::GET, 'friendships.exists.twitter.xml',
  275. array('user_a'=>'padraicb', 'user_b'=>'twitter')
  276. ));
  277. $response = $twitter->friendship->exists('twitter');
  278. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  279. }
  280. /**
  281. * TODO: Add verification for ALL optional parameters
  282. */
  283. public function testFriendsTimelineWithPageReturnsResults()
  284. {
  285. $twitter = new Zend_Service_Twitter;
  286. $twitter->setLocalHttpClient($this->_stubTwitter(
  287. 'statuses/friends_timeline.xml', Zend_Http_Client::GET, 'statuses.friends_timeline.page.xml',
  288. array('page'=>3)
  289. ));
  290. $response = $twitter->status->friendsTimeline(array('page' => 3));
  291. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  292. }
  293. /**
  294. * TODO: Add verification for ALL optional parameters
  295. */
  296. public function testUserTimelineReturnsResults()
  297. {
  298. $twitter = new Zend_Service_Twitter;
  299. $twitter->setLocalHttpClient($this->_stubTwitter(
  300. 'statuses/user_timeline/twitter.xml', Zend_Http_Client::GET, 'user_timeline.twitter.xml'
  301. ));
  302. $response = $twitter->status->userTimeline(array('id' => 'twitter'));
  303. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  304. }
  305. /**
  306. * TODO: Add verification for ALL optional parameters
  307. */
  308. public function testPostStatusUpdateReturnsResponse()
  309. {
  310. $twitter = new Zend_Service_Twitter;
  311. $twitter->setLocalHttpClient($this->_stubTwitter(
  312. 'statuses/update.xml', Zend_Http_Client::POST, 'statuses.update.xml',
  313. array('status'=>'Test Message 1')
  314. ));
  315. $response = $twitter->status->update('Test Message 1');
  316. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  317. }
  318. /**
  319. * @expectedException Zend_Service_Twitter_Exception
  320. */
  321. public function testPostStatusUpdateToLongShouldThrowException()
  322. {
  323. $twitter = new Zend_Service_Twitter;
  324. $twitter->status->update('Test Message - ' . str_repeat(' Hello ', 140));
  325. }
  326. /**
  327. * @expectedException Zend_Service_Twitter_Exception
  328. */
  329. public function testPostStatusUpdateEmptyShouldThrowException()
  330. {
  331. $twitter = new Zend_Service_Twitter;
  332. $twitter->status->update('');
  333. }
  334. public function testShowStatusReturnsResponse()
  335. {
  336. $twitter = new Zend_Service_Twitter;
  337. $twitter->setLocalHttpClient($this->_stubTwitter(
  338. 'statuses/show/15042159587.xml', Zend_Http_Client::GET, 'statuses.show.xml'
  339. ));
  340. $response = $twitter->status->show(15042159587);
  341. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  342. }
  343. public function testCreateFavoriteStatusReturnsResponse()
  344. {
  345. $twitter = new Zend_Service_Twitter;
  346. $twitter->setLocalHttpClient($this->_stubTwitter(
  347. 'favorites/create/15042159587.xml', Zend_Http_Client::POST, 'favorites.create.xml'
  348. ));
  349. $response = $twitter->favorite->create(15042159587);
  350. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  351. }
  352. public function testFavoriteFavoriesReturnsResponse()
  353. {
  354. $twitter = new Zend_Service_Twitter;
  355. $twitter->setLocalHttpClient($this->_stubTwitter(
  356. 'favorites.xml', Zend_Http_Client::GET, 'favorites.xml'
  357. ));
  358. $response = $twitter->favorite->favorites();
  359. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  360. }
  361. /**
  362. * TODO: Can we use a HTTP DELETE?
  363. */
  364. public function testDestroyFavoriteReturnsResponse()
  365. {
  366. $twitter = new Zend_Service_Twitter;
  367. $twitter->setLocalHttpClient($this->_stubTwitter(
  368. 'favorites/destroy/15042159587.xml', Zend_Http_Client::POST, 'favorites.destroy.xml'
  369. ));
  370. $response = $twitter->favorite->destroy(15042159587);
  371. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  372. }
  373. public function testStatusDestroyReturnsResult()
  374. {
  375. $twitter = new Zend_Service_Twitter;
  376. $twitter->setLocalHttpClient($this->_stubTwitter(
  377. 'statuses/destroy/15042159587.xml', Zend_Http_Client::POST, 'statuses.destroy.xml'
  378. ));
  379. $response = $twitter->status->destroy(15042159587);
  380. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  381. }
  382. /**
  383. * TODO: Add verification for ALL optional parameters
  384. */
  385. public function testUserFriendsReturnsResults()
  386. {
  387. $twitter = new Zend_Service_Twitter;
  388. $twitter->setLocalHttpClient($this->_stubTwitter(
  389. 'statuses/friends.xml', Zend_Http_Client::GET, 'statuses.friends.xml'
  390. ));
  391. $response = $twitter->user->friends();
  392. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  393. }
  394. /**
  395. * TODO: Add verification for ALL optional parameters
  396. * Note: Implementation does not currently accept ANY optional parameters
  397. */
  398. public function testUserFollowersReturnsResults()
  399. {
  400. $twitter = new Zend_Service_Twitter;
  401. $twitter->setLocalHttpClient($this->_stubTwitter(
  402. 'statuses/followers.xml', Zend_Http_Client::GET, 'statuses.followers.xml'
  403. ));
  404. $response = $twitter->user->followers();
  405. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  406. }
  407. public function testUserShowByIdReturnsResults()
  408. {
  409. $twitter = new Zend_Service_Twitter;
  410. $twitter->setLocalHttpClient($this->_stubTwitter(
  411. 'users/show.xml', Zend_Http_Client::GET, 'users.show.twitter.xml',
  412. array('id'=>'twitter')
  413. ));
  414. $response = $twitter->user->show('twitter');
  415. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  416. }
  417. /**
  418. * TODO: Add verification for ALL optional parameters
  419. */
  420. public function testStatusRepliesReturnsResults()
  421. {
  422. $twitter = new Zend_Service_Twitter;
  423. $twitter->setLocalHttpClient($this->_stubTwitter(
  424. 'statuses/mentions.xml', Zend_Http_Client::GET, 'statuses.mentions.xml'
  425. ));
  426. $response = $twitter->status->replies();
  427. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  428. }
  429. /**
  430. * TODO: Add verification for ALL optional parameters
  431. */
  432. public function testFriendshipDestroy()
  433. {
  434. $twitter = new Zend_Service_Twitter;
  435. $twitter->setLocalHttpClient($this->_stubTwitter(
  436. 'friendships/destroy/twitter.xml', Zend_Http_Client::POST, 'friendships.destroy.twitter.xml'
  437. ));
  438. $response = $twitter->friendship->destroy('twitter');
  439. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  440. }
  441. public function testBlockingCreate()
  442. {
  443. $twitter = new Zend_Service_Twitter;
  444. $twitter->setLocalHttpClient($this->_stubTwitter(
  445. 'blocks/create/twitter.xml', Zend_Http_Client::POST, 'blocks.create.twitter.xml'
  446. ));
  447. $response = $twitter->block->create('twitter');
  448. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  449. }
  450. public function testBlockingExistsReturnsTrueWhenBlockExists()
  451. {
  452. $twitter = new Zend_Service_Twitter;
  453. $twitter->setLocalHttpClient($this->_stubTwitter(
  454. 'blocks/exists/twitter.xml', Zend_Http_Client::GET, 'blocks.exists.twitter.xml'
  455. ));
  456. $this->assertTrue($twitter->block->exists('twitter'));
  457. }
  458. public function testBlockingBlocked()
  459. {
  460. $twitter = new Zend_Service_Twitter;
  461. $twitter->setLocalHttpClient($this->_stubTwitter(
  462. 'blocks/blocking.xml', Zend_Http_Client::GET, 'blocks.blocking.xml'
  463. ));
  464. $response = $twitter->block->blocking();
  465. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  466. }
  467. public function testBlockingBlockedReturnsIds()
  468. {
  469. $twitter = new Zend_Service_Twitter;
  470. $twitter->setLocalHttpClient($this->_stubTwitter(
  471. 'blocks/blocking/ids.xml', Zend_Http_Client::GET, 'blocks.blocking.ids.xml',
  472. array('page'=>1)
  473. ));
  474. $response = $twitter->block->blocking(1, true);
  475. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  476. $this->assertEquals('23836616', (string) $response->id);
  477. }
  478. public function testBlockingDestroy()
  479. {
  480. $twitter = new Zend_Service_Twitter;
  481. $twitter->setLocalHttpClient($this->_stubTwitter(
  482. 'blocks/destroy/twitter.xml', Zend_Http_Client::POST, 'blocks.destroy.twitter.xml'
  483. ));
  484. $response = $twitter->block->destroy('twitter');
  485. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  486. }
  487. public function testBlockingExistsReturnsFalseWhenBlockDoesNotExists()
  488. {
  489. $twitter = new Zend_Service_Twitter;
  490. $twitter->setLocalHttpClient($this->_stubTwitter(
  491. 'blocks/exists/padraicb.xml', Zend_Http_Client::GET, 'blocks.exists.padraicb.xml'
  492. ));
  493. $this->assertFalse($twitter->block->exists('padraicb'));
  494. }
  495. public function testBlockingExistsReturnsObjectWhenFlagPassed()
  496. {
  497. $twitter = new Zend_Service_Twitter;
  498. $twitter->setLocalHttpClient($this->_stubTwitter(
  499. 'blocks/exists/padraicb.xml', Zend_Http_Client::GET, 'blocks.exists.padraicb.xml'
  500. ));
  501. $response = $twitter->block->exists('padraicb', true);
  502. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  503. }
  504. /**
  505. * @group ZF-6284
  506. */
  507. public function testTwitterObjectsSoNotShareSameHttpClientToPreventConflictingAuthentication()
  508. {
  509. $twitter1 = new Zend_Service_Twitter('zftestuser1');
  510. $twitter2 = new Zend_Service_Twitter('zftestuser2');
  511. $this->assertFalse($twitter1->getLocalHttpClient() === $twitter2->getLocalHttpClient());
  512. }
  513. }
  514. if (PHPUnit_MAIN_METHOD == 'Zend_Service_TwitterTest2::main') {
  515. Zend_Service_TwitterTest2::main();
  516. }