TwitterTest.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  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_Oauth_Client', array(), array(), '', false);
  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. * OAuth tests
  100. */
  101. public function testProvidingAccessTokenInOptionsSetsHttpClientFromAccessToken()
  102. {
  103. $token = $this->getMock('Zend_Oauth_Token_Access', array(), array(), '', false);
  104. $client = $this->getMock('Zend_Oauth_Client', array(), array(), '', false);
  105. $token->expects($this->once())->method('getHttpClient')
  106. ->with(array('accessToken'=>$token, 'opt1'=>'val1', 'siteUrl'=>'http://twitter.com/oauth'))
  107. ->will($this->returnValue($client));
  108. $twitter = new Zend_Service_Twitter(array('accessToken'=>$token, 'opt1'=>'val1'));
  109. $this->assertTrue($client === $twitter->getLocalHttpClient());
  110. }
  111. public function testNotAuthorisedWithoutToken()
  112. {
  113. $twitter = new Zend_Service_Twitter;
  114. $this->assertFalse($twitter->isAuthorised());
  115. }
  116. public function testChecksAuthenticatedStateBasedOnAvailabilityOfAccessTokenBasedClient()
  117. {
  118. $token = $this->getMock('Zend_Oauth_Token_Access', array(), array(), '', false);
  119. $client = $this->getMock('Zend_Oauth_Client', array(), array(), '', false);
  120. $token->expects($this->once())->method('getHttpClient')
  121. ->with(array('accessToken'=>$token, 'siteUrl'=>'http://twitter.com/oauth'))
  122. ->will($this->returnValue($client));
  123. $twitter = new Zend_Service_Twitter(array('accessToken'=>$token));
  124. $this->assertTrue($twitter->isAuthorised());
  125. }
  126. public function testRelaysMethodsToInternalOAuthInstance()
  127. {
  128. $oauth = $this->getMock('Zend_Oauth_Consumer', array(), array(), '', false);
  129. $oauth->expects($this->once())->method('getRequestToken')->will($this->returnValue('foo'));
  130. $oauth->expects($this->once())->method('getRedirectUrl')->will($this->returnValue('foo'));
  131. $oauth->expects($this->once())->method('redirect')->will($this->returnValue('foo'));
  132. $oauth->expects($this->once())->method('getAccessToken')->will($this->returnValue('foo'));
  133. $oauth->expects($this->once())->method('getToken')->will($this->returnValue('foo'));
  134. $twitter = new Zend_Service_Twitter(array('opt1'=>'val1'), $oauth);
  135. $this->assertEquals('foo', $twitter->getRequestToken());
  136. $this->assertEquals('foo', $twitter->getRedirectUrl());
  137. $this->assertEquals('foo', $twitter->redirect());
  138. $this->assertEquals('foo', $twitter->getAccessToken(array(), $this->getMock('Zend_Oauth_Token_Request')));
  139. $this->assertEquals('foo', $twitter->getToken());
  140. }
  141. public function testResetsHttpClientOnReceiptOfAccessTokenToOauthClient()
  142. {
  143. $oauth = $this->getMock('Zend_Oauth_Consumer', array(), array(), '', false);
  144. $client = $this->getMock('Zend_Oauth_Client', array(), array(), '', false);
  145. $token = $this->getMock('Zend_Oauth_Token_Access', array(), array(), '', false);
  146. $token->expects($this->once())->method('getHttpClient')->will($this->returnValue($client));
  147. $oauth->expects($this->once())->method('getAccessToken')->will($this->returnValue($token));
  148. $client->expects($this->once())->method('setHeaders')->with('Accept-Charset', 'ISO-8859-1,utf-8');
  149. $twitter = new Zend_Service_Twitter(array(), $oauth);
  150. $twitter->getAccessToken(array(), $this->getMock('Zend_Oauth_Token_Request'));
  151. $this->assertTrue($client === $twitter->getLocalHttpClient());
  152. }
  153. /**
  154. * @expectedException Zend_Service_Twitter_Exception
  155. */
  156. public function testAuthorisationFailureWithUsernameAndNoAccessToken()
  157. {
  158. $twitter = new Zend_Service_Twitter(array('username'=>'me'));
  159. $twitter->statusPublicTimeline();
  160. }
  161. /**
  162. * @group ZF-8218
  163. */
  164. public function testUserNameNotRequired()
  165. {
  166. $twitter = new Zend_Service_Twitter();
  167. $twitter->setLocalHttpClient($this->_stubTwitter(
  168. 'users/show.xml', Zend_Http_Client::GET, 'users.show.twitter.xml',
  169. array('id'=>'twitter')
  170. ));
  171. $exists = $twitter->user->show('twitter')->id() !== null;
  172. $this->assertTrue($exists);
  173. }
  174. /**
  175. * @group ZF-7781
  176. */
  177. public function testRetrievingStatusesWithValidScreenNameThrowsNoInvalidScreenNameException()
  178. {
  179. $twitter = new Zend_Service_Twitter();
  180. $twitter->setLocalHttpClient($this->_stubTwitter(
  181. 'statuses/user_timeline.xml', Zend_Http_Client::GET, 'user_timeline.twitter.xml'
  182. ));
  183. $twitter->status->userTimeline(array('screen_name' => 'twitter'));
  184. }
  185. /**
  186. * @group ZF-7781
  187. * @expectedException Zend_Service_Twitter_Exception
  188. */
  189. public function testRetrievingStatusesWithInvalidScreenNameCharacterThrowsInvalidScreenNameException()
  190. {
  191. $twitter = new Zend_Service_Twitter();
  192. $twitter->status->userTimeline(array('screen_name' => 'abc.def'));
  193. }
  194. /**
  195. * @group ZF-7781
  196. */
  197. public function testRetrievingStatusesWithInvalidScreenNameLengthThrowsInvalidScreenNameException()
  198. {
  199. $this->setExpectedException('Zend_Service_Twitter_Exception');
  200. $twitter = new Zend_Service_Twitter();
  201. $twitter->status->userTimeline(array('screen_name' => 'abcdef_abc123_abc123x'));
  202. }
  203. /**
  204. * @group ZF-7781
  205. */
  206. public function testStatusUserTimelineConstructsExpectedGetUriAndOmitsInvalidParams()
  207. {
  208. $twitter = new Zend_Service_Twitter;
  209. $twitter->setLocalHttpClient($this->_stubTwitter(
  210. 'statuses/user_timeline/783214.xml', Zend_Http_Client::GET, 'user_timeline.twitter.xml', array(
  211. 'page' => '1',
  212. 'count' => '123',
  213. 'user_id' => '783214',
  214. 'since_id' => '10000',
  215. 'max_id' => '20000',
  216. 'screen_name' => 'twitter'
  217. )
  218. ));
  219. $twitter->status->userTimeline(array(
  220. 'id' => '783214',
  221. 'since' => '+2 days', /* invalid param since Apr 2009 */
  222. 'page' => '1',
  223. 'count' => '123',
  224. 'user_id' => '783214',
  225. 'since_id' => '10000',
  226. 'max_id' => '20000',
  227. 'screen_name' => 'twitter'
  228. ));
  229. }
  230. public function testOverloadingGetShouldReturnObjectInstanceWithValidMethodType()
  231. {
  232. $twitter = new Zend_Service_Twitter;
  233. $return = $twitter->status;
  234. $this->assertSame($twitter, $return);
  235. }
  236. /**
  237. * @expectedException Zend_Service_Twitter_Exception
  238. */
  239. public function testOverloadingGetShouldthrowExceptionWithInvalidMethodType()
  240. {
  241. $twitter = new Zend_Service_Twitter;
  242. $return = $twitter->foo;
  243. }
  244. /**
  245. * @expectedException Zend_Service_Twitter_Exception
  246. */
  247. public function testOverloadingGetShouldthrowExceptionWithInvalidFunction()
  248. {
  249. $twitter = new Zend_Service_Twitter;
  250. $return = $twitter->foo();
  251. }
  252. public function testMethodProxyingDoesNotThrowExceptionsWithValidMethods()
  253. {
  254. $twitter = new Zend_Service_Twitter;
  255. $twitter->setLocalHttpClient($this->_stubTwitter(
  256. 'statuses/public_timeline.xml', Zend_Http_Client::GET, 'public_timeline.xml'
  257. ));
  258. $twitter->status->publicTimeline();
  259. }
  260. /**
  261. * @expectedException Zend_Service_Twitter_Exception
  262. */
  263. public function testMethodProxyingThrowExceptionsWithInvalidMethods()
  264. {
  265. $twitter = new Zend_Service_Twitter;
  266. $twitter->status->foo();
  267. }
  268. public function testVerifiedCredentials()
  269. {
  270. $twitter = new Zend_Service_Twitter;
  271. $twitter->setLocalHttpClient($this->_stubTwitter(
  272. 'account/verify_credentials.xml', Zend_Http_Client::GET, 'account.verify_credentials.xml'
  273. ));
  274. $response = $twitter->account->verifyCredentials();
  275. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  276. }
  277. public function testPublicTimelineStatusReturnsResults()
  278. {
  279. $twitter = new Zend_Service_Twitter;
  280. $twitter->setLocalHttpClient($this->_stubTwitter(
  281. 'statuses/public_timeline.xml', Zend_Http_Client::GET, 'public_timeline.xml'
  282. ));
  283. $response = $twitter->status->publicTimeline();
  284. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  285. }
  286. public function testRateLimitStatusReturnsResults()
  287. {
  288. $twitter = new Zend_Service_Twitter;
  289. $twitter->setLocalHttpClient($this->_stubTwitter(
  290. 'account/rate_limit_status.xml', Zend_Http_Client::GET, 'rate_limit_status.xml'
  291. ));
  292. $response = $twitter->account->rateLimitStatus();
  293. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  294. }
  295. public function testRateLimitStatusHasHitsLeft()
  296. {
  297. $twitter = new Zend_Service_Twitter;
  298. $twitter->setLocalHttpClient($this->_stubTwitter(
  299. 'account/rate_limit_status.xml', Zend_Http_Client::GET, 'rate_limit_status.xml'
  300. ));
  301. $response = $twitter->account->rateLimitStatus();
  302. $remaining_hits = $response->toValue($response->{'remaining-hits'});
  303. $this->assertEquals(150, $remaining_hits);
  304. }
  305. public function testAccountEndSession()
  306. {
  307. $twitter = new Zend_Service_Twitter;
  308. $twitter->setLocalHttpClient($this->_stubTwitter(
  309. 'account/end_session', Zend_Http_Client::GET
  310. ));
  311. $response = $twitter->account->endSession();
  312. $this->assertTrue($response);
  313. }
  314. /**
  315. * TODO: Check actual purpose. New friend returns XML response, existing
  316. * friend returns a 403 code.
  317. */
  318. public function testFriendshipCreate()
  319. {
  320. $twitter = new Zend_Service_Twitter;
  321. $twitter->setLocalHttpClient($this->_stubTwitter(
  322. 'friendships/create/twitter.xml', Zend_Http_Client::POST, 'friendships.create.twitter.xml'
  323. ));
  324. $response = $twitter->friendship->create('twitter');
  325. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  326. }
  327. /**
  328. * TODO: Mismatched behaviour from API. Per the API this can assert the
  329. * existence of a friendship between any two users (not just the current
  330. * user). We should expand the method or add a better fit method for
  331. * general use.
  332. */
  333. public function testFriendshipExists()
  334. {
  335. $twitter = new Zend_Service_Twitter(array('username'=>'padraicb'));
  336. $twitter->setLocalHttpClient($this->_stubTwitter(
  337. 'friendships/exists.xml', Zend_Http_Client::GET, 'friendships.exists.twitter.xml',
  338. array('user_a'=>'padraicb', 'user_b'=>'twitter')
  339. ));
  340. $response = $twitter->friendship->exists('twitter');
  341. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  342. }
  343. /**
  344. * TODO: Add verification for ALL optional parameters
  345. */
  346. public function testFriendsTimelineWithPageReturnsResults()
  347. {
  348. $twitter = new Zend_Service_Twitter;
  349. $twitter->setLocalHttpClient($this->_stubTwitter(
  350. 'statuses/friends_timeline.xml', Zend_Http_Client::GET, 'statuses.friends_timeline.page.xml',
  351. array('page'=>3)
  352. ));
  353. $response = $twitter->status->friendsTimeline(array('page' => 3));
  354. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  355. }
  356. /**
  357. * TODO: Add verification for ALL optional parameters
  358. */
  359. public function testUserTimelineReturnsResults()
  360. {
  361. $twitter = new Zend_Service_Twitter;
  362. $twitter->setLocalHttpClient($this->_stubTwitter(
  363. 'statuses/user_timeline/twitter.xml', Zend_Http_Client::GET, 'user_timeline.twitter.xml'
  364. ));
  365. $response = $twitter->status->userTimeline(array('id' => 'twitter'));
  366. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  367. }
  368. /**
  369. * TODO: Add verification for ALL optional parameters
  370. */
  371. public function testPostStatusUpdateReturnsResponse()
  372. {
  373. $twitter = new Zend_Service_Twitter;
  374. $twitter->setLocalHttpClient($this->_stubTwitter(
  375. 'statuses/update.xml', Zend_Http_Client::POST, 'statuses.update.xml',
  376. array('status'=>'Test Message 1')
  377. ));
  378. $response = $twitter->status->update('Test Message 1');
  379. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  380. }
  381. /**
  382. * @expectedException Zend_Service_Twitter_Exception
  383. */
  384. public function testPostStatusUpdateToLongShouldThrowException()
  385. {
  386. $twitter = new Zend_Service_Twitter;
  387. $twitter->status->update('Test Message - ' . str_repeat(' Hello ', 140));
  388. }
  389. /**
  390. * @expectedException Zend_Service_Twitter_Exception
  391. */
  392. public function testPostStatusUpdateEmptyShouldThrowException()
  393. {
  394. $twitter = new Zend_Service_Twitter;
  395. $twitter->status->update('');
  396. }
  397. public function testShowStatusReturnsResponse()
  398. {
  399. $twitter = new Zend_Service_Twitter;
  400. $twitter->setLocalHttpClient($this->_stubTwitter(
  401. 'statuses/show/15042159587.xml', Zend_Http_Client::GET, 'statuses.show.xml'
  402. ));
  403. $response = $twitter->status->show(15042159587);
  404. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  405. }
  406. public function testCreateFavoriteStatusReturnsResponse()
  407. {
  408. $twitter = new Zend_Service_Twitter;
  409. $twitter->setLocalHttpClient($this->_stubTwitter(
  410. 'favorites/create/15042159587.xml', Zend_Http_Client::POST, 'favorites.create.xml'
  411. ));
  412. $response = $twitter->favorite->create(15042159587);
  413. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  414. }
  415. public function testFavoriteFavoriesReturnsResponse()
  416. {
  417. $twitter = new Zend_Service_Twitter;
  418. $twitter->setLocalHttpClient($this->_stubTwitter(
  419. 'favorites.xml', Zend_Http_Client::GET, 'favorites.xml'
  420. ));
  421. $response = $twitter->favorite->favorites();
  422. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  423. }
  424. /**
  425. * TODO: Can we use a HTTP DELETE?
  426. */
  427. public function testDestroyFavoriteReturnsResponse()
  428. {
  429. $twitter = new Zend_Service_Twitter;
  430. $twitter->setLocalHttpClient($this->_stubTwitter(
  431. 'favorites/destroy/15042159587.xml', Zend_Http_Client::POST, 'favorites.destroy.xml'
  432. ));
  433. $response = $twitter->favorite->destroy(15042159587);
  434. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  435. }
  436. public function testStatusDestroyReturnsResult()
  437. {
  438. $twitter = new Zend_Service_Twitter;
  439. $twitter->setLocalHttpClient($this->_stubTwitter(
  440. 'statuses/destroy/15042159587.xml', Zend_Http_Client::POST, 'statuses.destroy.xml'
  441. ));
  442. $response = $twitter->status->destroy(15042159587);
  443. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  444. }
  445. /**
  446. * TODO: Add verification for ALL optional parameters
  447. */
  448. public function testUserFriendsReturnsResults()
  449. {
  450. $twitter = new Zend_Service_Twitter;
  451. $twitter->setLocalHttpClient($this->_stubTwitter(
  452. 'statuses/friends.xml', Zend_Http_Client::GET, 'statuses.friends.xml'
  453. ));
  454. $response = $twitter->user->friends();
  455. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  456. }
  457. /**
  458. * TODO: Add verification for ALL optional parameters
  459. * Note: Implementation does not currently accept ANY optional parameters
  460. */
  461. public function testUserFollowersReturnsResults()
  462. {
  463. $twitter = new Zend_Service_Twitter;
  464. $twitter->setLocalHttpClient($this->_stubTwitter(
  465. 'statuses/followers.xml', Zend_Http_Client::GET, 'statuses.followers.xml'
  466. ));
  467. $response = $twitter->user->followers();
  468. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  469. }
  470. public function testUserShowByIdReturnsResults()
  471. {
  472. $twitter = new Zend_Service_Twitter;
  473. $twitter->setLocalHttpClient($this->_stubTwitter(
  474. 'users/show.xml', Zend_Http_Client::GET, 'users.show.twitter.xml',
  475. array('id'=>'twitter')
  476. ));
  477. $response = $twitter->user->show('twitter');
  478. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  479. }
  480. /**
  481. * TODO: Add verification for ALL optional parameters
  482. */
  483. public function testStatusRepliesReturnsResults()
  484. {
  485. $twitter = new Zend_Service_Twitter;
  486. $twitter->setLocalHttpClient($this->_stubTwitter(
  487. 'statuses/mentions.xml', Zend_Http_Client::GET, 'statuses.mentions.xml'
  488. ));
  489. $response = $twitter->status->replies();
  490. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  491. }
  492. /**
  493. * TODO: Add verification for ALL optional parameters
  494. */
  495. public function testFriendshipDestroy()
  496. {
  497. $twitter = new Zend_Service_Twitter;
  498. $twitter->setLocalHttpClient($this->_stubTwitter(
  499. 'friendships/destroy/twitter.xml', Zend_Http_Client::POST, 'friendships.destroy.twitter.xml'
  500. ));
  501. $response = $twitter->friendship->destroy('twitter');
  502. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  503. }
  504. public function testBlockingCreate()
  505. {
  506. $twitter = new Zend_Service_Twitter;
  507. $twitter->setLocalHttpClient($this->_stubTwitter(
  508. 'blocks/create/twitter.xml', Zend_Http_Client::POST, 'blocks.create.twitter.xml'
  509. ));
  510. $response = $twitter->block->create('twitter');
  511. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  512. }
  513. public function testBlockingExistsReturnsTrueWhenBlockExists()
  514. {
  515. $twitter = new Zend_Service_Twitter;
  516. $twitter->setLocalHttpClient($this->_stubTwitter(
  517. 'blocks/exists/twitter.xml', Zend_Http_Client::GET, 'blocks.exists.twitter.xml'
  518. ));
  519. $this->assertTrue($twitter->block->exists('twitter'));
  520. }
  521. public function testBlockingBlocked()
  522. {
  523. $twitter = new Zend_Service_Twitter;
  524. $twitter->setLocalHttpClient($this->_stubTwitter(
  525. 'blocks/blocking.xml', Zend_Http_Client::GET, 'blocks.blocking.xml'
  526. ));
  527. $response = $twitter->block->blocking();
  528. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  529. }
  530. public function testBlockingBlockedReturnsIds()
  531. {
  532. $twitter = new Zend_Service_Twitter;
  533. $twitter->setLocalHttpClient($this->_stubTwitter(
  534. 'blocks/blocking/ids.xml', Zend_Http_Client::GET, 'blocks.blocking.ids.xml',
  535. array('page'=>1)
  536. ));
  537. $response = $twitter->block->blocking(1, true);
  538. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  539. $this->assertEquals('23836616', (string) $response->id);
  540. }
  541. public function testBlockingDestroy()
  542. {
  543. $twitter = new Zend_Service_Twitter;
  544. $twitter->setLocalHttpClient($this->_stubTwitter(
  545. 'blocks/destroy/twitter.xml', Zend_Http_Client::POST, 'blocks.destroy.twitter.xml'
  546. ));
  547. $response = $twitter->block->destroy('twitter');
  548. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  549. }
  550. public function testBlockingExistsReturnsFalseWhenBlockDoesNotExists()
  551. {
  552. $twitter = new Zend_Service_Twitter;
  553. $twitter->setLocalHttpClient($this->_stubTwitter(
  554. 'blocks/exists/padraicb.xml', Zend_Http_Client::GET, 'blocks.exists.padraicb.xml'
  555. ));
  556. $this->assertFalse($twitter->block->exists('padraicb'));
  557. }
  558. public function testBlockingExistsReturnsObjectWhenFlagPassed()
  559. {
  560. $twitter = new Zend_Service_Twitter;
  561. $twitter->setLocalHttpClient($this->_stubTwitter(
  562. 'blocks/exists/padraicb.xml', Zend_Http_Client::GET, 'blocks.exists.padraicb.xml'
  563. ));
  564. $response = $twitter->block->exists('padraicb', true);
  565. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  566. }
  567. /**
  568. * @group ZF-6284
  569. */
  570. public function testTwitterObjectsSoNotShareSameHttpClientToPreventConflictingAuthentication()
  571. {
  572. $twitter1 = new Zend_Service_Twitter(array('username'=>'zftestuser1'));
  573. $twitter2 = new Zend_Service_Twitter(array('username'=>'zftestuser2'));
  574. $this->assertFalse($twitter1->getLocalHttpClient() === $twitter2->getLocalHttpClient());
  575. }
  576. /**
  577. * @group ZF-10644
  578. */
  579. public function testStatusUserTimelineShouldHonorAllFlags()
  580. {
  581. $params = array(
  582. 'screen_name' => 'allzend',
  583. 'page' => 1,
  584. 'include_rts' => '1',
  585. 'trim_user' => '1',
  586. 'include_entities' => '1',
  587. );
  588. $twitter = new Zend_Service_Twitter();
  589. $twitter->setLocalHttpClient($this->_stubTwitter(
  590. 'statuses/user_timeline.xml', Zend_Http_Client::GET, 'user_timeline.twitter.xml',
  591. $params
  592. ));
  593. // Assertions are part of mocking
  594. $timeline = $twitter->statusUserTimeline($params);
  595. }
  596. }
  597. if (PHPUnit_MAIN_METHOD == 'Zend_Service_TwitterTest2::main') {
  598. Zend_Service_TwitterTest2::main();
  599. }