TwitterTest.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  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-2012 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. /** Zend_Service_Twitter */
  26. require_once 'Zend/Service/Twitter.php';
  27. /** Zend_Http_Client */
  28. require_once 'Zend/Http/Client.php';
  29. /** Zend_Http_Client_Adapter_Test */
  30. require_once 'Zend/Http/Client/Adapter/Test.php';
  31. /**
  32. * @category Zend
  33. * @package Zend_Service_Twitter
  34. * @subpackage UnitTests
  35. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  36. * @license http://framework.zend.com/license/new-bsd New BSD License
  37. * @group Zend_Service
  38. * @group Zend_Service_Twitter
  39. */
  40. class Zend_Service_Twitter_TwitterTest extends PHPUnit_Framework_TestCase
  41. {
  42. /**
  43. * Runs the test methods of this class.
  44. *
  45. * @return void
  46. */
  47. public static function main()
  48. {
  49. $suite = new PHPUnit_Framework_TestSuite(__CLASS__);
  50. $result = PHPUnit_TextUI_TestRunner::run($suite);
  51. }
  52. public function teardown()
  53. {
  54. Zend_Service_Abstract::setHttpClient(new Zend_Http_Client);
  55. }
  56. /**
  57. * Quick reusable Twitter Service stub setup. Its purpose is to fake
  58. * interactions with Twitter so the component can focus on what matters:
  59. * 1. Makes correct requests (URI, parameters and HTTP method)
  60. * 2. Parses all responses and returns a Zend_Rest_Client_Result
  61. * 3. TODO: Correctly utilises all optional parameters
  62. *
  63. * If used correctly, tests will be fast, efficient, and focused on
  64. * Zend_Service_Twitter's behaviour only. No other dependencies need be
  65. * tested. The Twitter API Changelog should be regularly reviewed to
  66. * ensure the component is synchronised to the API.
  67. *
  68. * @param string $path Path appended to Twitter API endpoint
  69. * @param string $method Do we expect HTTP GET or POST?
  70. * @param string $responseFile File containing a valid XML response to the request
  71. * @param array $params Expected GET/POST parameters for the request
  72. * @return Zend_Http_Client
  73. */
  74. protected function _stubTwitter($path, $method, $responseFile = null, array $params = null)
  75. {
  76. $client = $this->getMock('Zend_Oauth_Client', array(), array(), '', false);
  77. $client->expects($this->any())->method('resetParameters')
  78. ->will($this->returnValue($client));
  79. $client->expects($this->once())->method('setUri')
  80. ->with('http://api.twitter.com/1/' . $path);
  81. $response = $this->getMock('Zend_Http_Response', array(), array(), '', false);
  82. if (!is_null($params)) {
  83. $setter = 'setParameter' . ucfirst(strtolower($method));
  84. $client->expects($this->once())->method($setter)->with($params);
  85. }
  86. $client->expects($this->once())->method('request')->with($method)
  87. ->will($this->returnValue($response));
  88. $response->expects($this->any())->method('getBody')
  89. ->will($this->returnValue(
  90. isset($responseFile) ? file_get_contents(dirname(__FILE__) . '/_files/' . $responseFile) : ''
  91. ));
  92. return $client;
  93. }
  94. /**
  95. * OAuth tests
  96. */
  97. public function testProvidingAccessTokenInOptionsSetsHttpClientFromAccessToken()
  98. {
  99. $token = $this->getMock('Zend_Oauth_Token_Access', array(), array(), '', false);
  100. $client = $this->getMock('Zend_Oauth_Client', array(), array(), '', false);
  101. $token->expects($this->once())->method('getHttpClient')
  102. ->with(array('accessToken'=>$token, 'opt1'=>'val1', 'siteUrl'=>'https://api.twitter.com/oauth'))
  103. ->will($this->returnValue($client));
  104. $twitter = new Zend_Service_Twitter(array('accessToken'=>$token, 'opt1'=>'val1'));
  105. $this->assertTrue($client === $twitter->getLocalHttpClient());
  106. }
  107. public function testNotAuthorisedWithoutToken()
  108. {
  109. $twitter = new Zend_Service_Twitter;
  110. $this->assertFalse($twitter->isAuthorised());
  111. }
  112. public function testChecksAuthenticatedStateBasedOnAvailabilityOfAccessTokenBasedClient()
  113. {
  114. $token = $this->getMock('Zend_Oauth_Token_Access', array(), array(), '', false);
  115. $client = $this->getMock('Zend_Oauth_Client', array(), array(), '', false);
  116. $token->expects($this->once())->method('getHttpClient')
  117. ->with(array('accessToken'=>$token, 'siteUrl'=>'https://api.twitter.com/oauth'))
  118. ->will($this->returnValue($client));
  119. $twitter = new Zend_Service_Twitter(array('accessToken'=>$token));
  120. $this->assertTrue($twitter->isAuthorised());
  121. }
  122. public function testRelaysMethodsToInternalOAuthInstance()
  123. {
  124. $oauth = $this->getMock('Zend_Oauth_Consumer', array(), array(), '', false);
  125. $oauth->expects($this->once())->method('getRequestToken')->will($this->returnValue('foo'));
  126. $oauth->expects($this->once())->method('getRedirectUrl')->will($this->returnValue('foo'));
  127. $oauth->expects($this->once())->method('redirect')->will($this->returnValue('foo'));
  128. $oauth->expects($this->once())->method('getAccessToken')->will($this->returnValue('foo'));
  129. $oauth->expects($this->once())->method('getToken')->will($this->returnValue('foo'));
  130. $twitter = new Zend_Service_Twitter(array('opt1'=>'val1'), $oauth);
  131. $this->assertEquals('foo', $twitter->getRequestToken());
  132. $this->assertEquals('foo', $twitter->getRedirectUrl());
  133. $this->assertEquals('foo', $twitter->redirect());
  134. $this->assertEquals('foo', $twitter->getAccessToken(array(), $this->getMock('Zend_Oauth_Token_Request')));
  135. $this->assertEquals('foo', $twitter->getToken());
  136. }
  137. public function testResetsHttpClientOnReceiptOfAccessTokenToOauthClient()
  138. {
  139. $oauth = $this->getMock('Zend_Oauth_Consumer', array(), array(), '', false);
  140. $client = $this->getMock('Zend_Oauth_Client', array(), array(), '', false);
  141. $token = $this->getMock('Zend_Oauth_Token_Access', array(), array(), '', false);
  142. $token->expects($this->once())->method('getHttpClient')->will($this->returnValue($client));
  143. $oauth->expects($this->once())->method('getAccessToken')->will($this->returnValue($token));
  144. $client->expects($this->once())->method('setHeaders')->with('Accept-Charset', 'ISO-8859-1,utf-8');
  145. $twitter = new Zend_Service_Twitter(array(), $oauth);
  146. $twitter->getAccessToken(array(), $this->getMock('Zend_Oauth_Token_Request'));
  147. $this->assertTrue($client === $twitter->getLocalHttpClient());
  148. }
  149. /**
  150. * @expectedException Zend_Service_Twitter_Exception
  151. */
  152. public function testAuthorisationFailureWithUsernameAndNoAccessToken()
  153. {
  154. $twitter = new Zend_Service_Twitter(array('username'=>'me'));
  155. $twitter->statusPublicTimeline();
  156. }
  157. /**
  158. * @group ZF-8218
  159. */
  160. public function testUserNameNotRequired()
  161. {
  162. $twitter = new Zend_Service_Twitter();
  163. $twitter->setLocalHttpClient($this->_stubTwitter(
  164. 'users/show.xml', Zend_Http_Client::GET, 'users.show.twitter.xml',
  165. array('id'=>'twitter')
  166. ));
  167. $exists = $twitter->user->show('twitter')->id() !== null;
  168. $this->assertTrue($exists);
  169. }
  170. /**
  171. * @group ZF-7781
  172. */
  173. public function testRetrievingStatusesWithValidScreenNameThrowsNoInvalidScreenNameException()
  174. {
  175. $twitter = new Zend_Service_Twitter();
  176. $twitter->setLocalHttpClient($this->_stubTwitter(
  177. 'statuses/user_timeline.xml', Zend_Http_Client::GET, 'user_timeline.twitter.xml'
  178. ));
  179. $twitter->status->userTimeline(array('screen_name' => 'twitter'));
  180. }
  181. /**
  182. * @group ZF-7781
  183. * @expectedException Zend_Service_Twitter_Exception
  184. */
  185. public function testRetrievingStatusesWithInvalidScreenNameCharacterThrowsInvalidScreenNameException()
  186. {
  187. $twitter = new Zend_Service_Twitter();
  188. $twitter->status->userTimeline(array('screen_name' => 'abc.def'));
  189. }
  190. /**
  191. * @group ZF-7781
  192. */
  193. public function testRetrievingStatusesWithInvalidScreenNameLengthThrowsInvalidScreenNameException()
  194. {
  195. $this->setExpectedException('Zend_Service_Twitter_Exception');
  196. $twitter = new Zend_Service_Twitter();
  197. $twitter->status->userTimeline(array('screen_name' => 'abcdef_abc123_abc123x'));
  198. }
  199. /**
  200. * @group ZF-7781
  201. */
  202. public function testStatusUserTimelineConstructsExpectedGetUriAndOmitsInvalidParams()
  203. {
  204. $twitter = new Zend_Service_Twitter;
  205. $twitter->setLocalHttpClient($this->_stubTwitter(
  206. 'statuses/user_timeline/783214.xml', Zend_Http_Client::GET, 'user_timeline.twitter.xml', array(
  207. 'page' => '1',
  208. 'count' => '123',
  209. 'user_id' => '783214',
  210. 'since_id' => '10000',
  211. 'max_id' => '20000',
  212. 'screen_name' => 'twitter'
  213. )
  214. ));
  215. $twitter->status->userTimeline(array(
  216. 'id' => '783214',
  217. 'since' => '+2 days', /* invalid param since Apr 2009 */
  218. 'page' => '1',
  219. 'count' => '123',
  220. 'user_id' => '783214',
  221. 'since_id' => '10000',
  222. 'max_id' => '20000',
  223. 'screen_name' => 'twitter'
  224. ));
  225. }
  226. public function testOverloadingGetShouldReturnObjectInstanceWithValidMethodType()
  227. {
  228. $twitter = new Zend_Service_Twitter;
  229. $return = $twitter->status;
  230. $this->assertSame($twitter, $return);
  231. }
  232. /**
  233. * @expectedException Zend_Service_Twitter_Exception
  234. */
  235. public function testOverloadingGetShouldthrowExceptionWithInvalidMethodType()
  236. {
  237. $twitter = new Zend_Service_Twitter;
  238. $return = $twitter->foo;
  239. }
  240. /**
  241. * @expectedException Zend_Service_Twitter_Exception
  242. */
  243. public function testOverloadingGetShouldthrowExceptionWithInvalidFunction()
  244. {
  245. $twitter = new Zend_Service_Twitter;
  246. $return = $twitter->foo();
  247. }
  248. public function testMethodProxyingDoesNotThrowExceptionsWithValidMethods()
  249. {
  250. $twitter = new Zend_Service_Twitter;
  251. $twitter->setLocalHttpClient($this->_stubTwitter(
  252. 'statuses/public_timeline.xml', Zend_Http_Client::GET, 'public_timeline.xml'
  253. ));
  254. $twitter->status->publicTimeline();
  255. }
  256. /**
  257. * @expectedException Zend_Service_Twitter_Exception
  258. */
  259. public function testMethodProxyingThrowExceptionsWithInvalidMethods()
  260. {
  261. $twitter = new Zend_Service_Twitter;
  262. $twitter->status->foo();
  263. }
  264. public function testVerifiedCredentials()
  265. {
  266. $twitter = new Zend_Service_Twitter;
  267. $twitter->setLocalHttpClient($this->_stubTwitter(
  268. 'account/verify_credentials.xml', Zend_Http_Client::GET, 'account.verify_credentials.xml'
  269. ));
  270. $response = $twitter->account->verifyCredentials();
  271. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  272. }
  273. public function testPublicTimelineStatusReturnsResults()
  274. {
  275. $twitter = new Zend_Service_Twitter;
  276. $twitter->setLocalHttpClient($this->_stubTwitter(
  277. 'statuses/public_timeline.xml', Zend_Http_Client::GET, 'public_timeline.xml'
  278. ));
  279. $response = $twitter->status->publicTimeline();
  280. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  281. }
  282. public function testRateLimitStatusReturnsResults()
  283. {
  284. $twitter = new Zend_Service_Twitter;
  285. $twitter->setLocalHttpClient($this->_stubTwitter(
  286. 'account/rate_limit_status.xml', Zend_Http_Client::GET, 'rate_limit_status.xml'
  287. ));
  288. $response = $twitter->account->rateLimitStatus();
  289. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  290. }
  291. public function testRateLimitStatusHasHitsLeft()
  292. {
  293. $twitter = new Zend_Service_Twitter;
  294. $twitter->setLocalHttpClient($this->_stubTwitter(
  295. 'account/rate_limit_status.xml', Zend_Http_Client::GET, 'rate_limit_status.xml'
  296. ));
  297. $response = $twitter->account->rateLimitStatus();
  298. $remaining_hits = $response->toValue($response->{'remaining-hits'});
  299. $this->assertEquals(150, $remaining_hits);
  300. }
  301. public function testAccountEndSession()
  302. {
  303. $twitter = new Zend_Service_Twitter;
  304. $twitter->setLocalHttpClient($this->_stubTwitter(
  305. 'account/end_session', Zend_Http_Client::GET
  306. ));
  307. $response = $twitter->account->endSession();
  308. $this->assertTrue($response);
  309. }
  310. /**
  311. * TODO: Check actual purpose. New friend returns XML response, existing
  312. * friend returns a 403 code.
  313. */
  314. public function testFriendshipCreate()
  315. {
  316. $twitter = new Zend_Service_Twitter;
  317. $twitter->setLocalHttpClient($this->_stubTwitter(
  318. 'friendships/create/twitter.xml', Zend_Http_Client::POST, 'friendships.create.twitter.xml'
  319. ));
  320. $response = $twitter->friendship->create('twitter');
  321. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  322. }
  323. /**
  324. * TODO: Mismatched behaviour from API. Per the API this can assert the
  325. * existence of a friendship between any two users (not just the current
  326. * user). We should expand the method or add a better fit method for
  327. * general use.
  328. */
  329. public function testFriendshipExists()
  330. {
  331. $twitter = new Zend_Service_Twitter(array('username'=>'padraicb'));
  332. $twitter->setLocalHttpClient($this->_stubTwitter(
  333. 'friendships/exists.xml', Zend_Http_Client::GET, 'friendships.exists.twitter.xml',
  334. array('user_a'=>'padraicb', 'user_b'=>'twitter')
  335. ));
  336. $response = $twitter->friendship->exists('twitter');
  337. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  338. }
  339. /**
  340. * TODO: Add verification for ALL optional parameters
  341. */
  342. public function testFriendsTimelineWithPageReturnsResults()
  343. {
  344. $twitter = new Zend_Service_Twitter;
  345. $twitter->setLocalHttpClient($this->_stubTwitter(
  346. 'statuses/friends_timeline.xml', Zend_Http_Client::GET, 'statuses.friends_timeline.page.xml',
  347. array('page'=>3)
  348. ));
  349. $response = $twitter->status->friendsTimeline(array('page' => 3));
  350. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  351. }
  352. /**
  353. * TODO: Add verification for ALL optional parameters
  354. */
  355. public function testUserTimelineReturnsResults()
  356. {
  357. $twitter = new Zend_Service_Twitter;
  358. $twitter->setLocalHttpClient($this->_stubTwitter(
  359. 'statuses/user_timeline/twitter.xml', Zend_Http_Client::GET, 'user_timeline.twitter.xml'
  360. ));
  361. $response = $twitter->status->userTimeline(array('id' => 'twitter'));
  362. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  363. }
  364. /**
  365. * TODO: Add verification for ALL optional parameters
  366. */
  367. public function testPostStatusUpdateReturnsResponse()
  368. {
  369. $twitter = new Zend_Service_Twitter;
  370. $twitter->setLocalHttpClient($this->_stubTwitter(
  371. 'statuses/update.xml', Zend_Http_Client::POST, 'statuses.update.xml',
  372. array('status'=>'Test Message 1')
  373. ));
  374. $response = $twitter->status->update('Test Message 1');
  375. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  376. }
  377. /**
  378. * @expectedException Zend_Service_Twitter_Exception
  379. */
  380. public function testPostStatusUpdateToLongShouldThrowException()
  381. {
  382. $twitter = new Zend_Service_Twitter;
  383. $twitter->status->update('Test Message - ' . str_repeat(' Hello ', 140));
  384. }
  385. /**
  386. * @expectedException Zend_Service_Twitter_Exception
  387. */
  388. public function testPostStatusUpdateEmptyShouldThrowException()
  389. {
  390. $twitter = new Zend_Service_Twitter;
  391. $twitter->status->update('');
  392. }
  393. public function testShowStatusReturnsResponse()
  394. {
  395. $twitter = new Zend_Service_Twitter;
  396. $twitter->setLocalHttpClient($this->_stubTwitter(
  397. 'statuses/show/15042159587.xml', Zend_Http_Client::GET, 'statuses.show.xml'
  398. ));
  399. $response = $twitter->status->show(15042159587);
  400. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  401. }
  402. public function testCreateFavoriteStatusReturnsResponse()
  403. {
  404. $twitter = new Zend_Service_Twitter;
  405. $twitter->setLocalHttpClient($this->_stubTwitter(
  406. 'favorites/create/15042159587.xml', Zend_Http_Client::POST, 'favorites.create.xml'
  407. ));
  408. $response = $twitter->favorite->create(15042159587);
  409. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  410. }
  411. public function testFavoriteFavoriesReturnsResponse()
  412. {
  413. $twitter = new Zend_Service_Twitter;
  414. $twitter->setLocalHttpClient($this->_stubTwitter(
  415. 'favorites.xml', Zend_Http_Client::GET, 'favorites.xml'
  416. ));
  417. $response = $twitter->favorite->favorites();
  418. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  419. }
  420. /**
  421. * TODO: Can we use a HTTP DELETE?
  422. */
  423. public function testDestroyFavoriteReturnsResponse()
  424. {
  425. $twitter = new Zend_Service_Twitter;
  426. $twitter->setLocalHttpClient($this->_stubTwitter(
  427. 'favorites/destroy/15042159587.xml', Zend_Http_Client::POST, 'favorites.destroy.xml'
  428. ));
  429. $response = $twitter->favorite->destroy(15042159587);
  430. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  431. }
  432. public function testStatusDestroyReturnsResult()
  433. {
  434. $twitter = new Zend_Service_Twitter;
  435. $twitter->setLocalHttpClient($this->_stubTwitter(
  436. 'statuses/destroy/15042159587.xml', Zend_Http_Client::POST, 'statuses.destroy.xml'
  437. ));
  438. $response = $twitter->status->destroy(15042159587);
  439. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  440. }
  441. /**
  442. * TODO: Add verification for ALL optional parameters
  443. */
  444. public function testUserFriendsReturnsResults()
  445. {
  446. $twitter = new Zend_Service_Twitter;
  447. $twitter->setLocalHttpClient($this->_stubTwitter(
  448. 'statuses/friends.xml', Zend_Http_Client::GET, 'statuses.friends.xml'
  449. ));
  450. $response = $twitter->user->friends();
  451. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  452. }
  453. /**
  454. * TODO: Add verification for ALL optional parameters
  455. * Note: Implementation does not currently accept ANY optional parameters
  456. */
  457. public function testUserFollowersReturnsResults()
  458. {
  459. $twitter = new Zend_Service_Twitter;
  460. $twitter->setLocalHttpClient($this->_stubTwitter(
  461. 'statuses/followers.xml', Zend_Http_Client::GET, 'statuses.followers.xml'
  462. ));
  463. $response = $twitter->user->followers();
  464. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  465. }
  466. public function testUserShowByIdReturnsResults()
  467. {
  468. $twitter = new Zend_Service_Twitter;
  469. $twitter->setLocalHttpClient($this->_stubTwitter(
  470. 'users/show.xml', Zend_Http_Client::GET, 'users.show.twitter.xml',
  471. array('id'=>'twitter')
  472. ));
  473. $response = $twitter->user->show('twitter');
  474. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  475. }
  476. /**
  477. * TODO: Add verification for ALL optional parameters
  478. */
  479. public function testStatusRepliesReturnsResults()
  480. {
  481. $twitter = new Zend_Service_Twitter;
  482. $twitter->setLocalHttpClient($this->_stubTwitter(
  483. 'statuses/mentions.xml', Zend_Http_Client::GET, 'statuses.mentions.xml'
  484. ));
  485. $response = $twitter->status->replies();
  486. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  487. }
  488. /**
  489. * TODO: Add verification for ALL optional parameters
  490. */
  491. public function testFriendshipDestroy()
  492. {
  493. $twitter = new Zend_Service_Twitter;
  494. $twitter->setLocalHttpClient($this->_stubTwitter(
  495. 'friendships/destroy/twitter.xml', Zend_Http_Client::POST, 'friendships.destroy.twitter.xml'
  496. ));
  497. $response = $twitter->friendship->destroy('twitter');
  498. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  499. }
  500. public function testBlockingCreate()
  501. {
  502. $twitter = new Zend_Service_Twitter;
  503. $twitter->setLocalHttpClient($this->_stubTwitter(
  504. 'blocks/create/twitter.xml', Zend_Http_Client::POST, 'blocks.create.twitter.xml'
  505. ));
  506. $response = $twitter->block->create('twitter');
  507. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  508. }
  509. public function testBlockingExistsReturnsTrueWhenBlockExists()
  510. {
  511. $twitter = new Zend_Service_Twitter;
  512. $twitter->setLocalHttpClient($this->_stubTwitter(
  513. 'blocks/exists/twitter.xml', Zend_Http_Client::GET, 'blocks.exists.twitter.xml'
  514. ));
  515. $this->assertTrue($twitter->block->exists('twitter'));
  516. }
  517. public function testBlockingBlocked()
  518. {
  519. $twitter = new Zend_Service_Twitter;
  520. $twitter->setLocalHttpClient($this->_stubTwitter(
  521. 'blocks/blocking.xml', Zend_Http_Client::GET, 'blocks.blocking.xml'
  522. ));
  523. $response = $twitter->block->blocking();
  524. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  525. }
  526. public function testBlockingBlockedReturnsIds()
  527. {
  528. $twitter = new Zend_Service_Twitter;
  529. $twitter->setLocalHttpClient($this->_stubTwitter(
  530. 'blocks/blocking/ids.xml', Zend_Http_Client::GET, 'blocks.blocking.ids.xml',
  531. array('page'=>1)
  532. ));
  533. $response = $twitter->block->blocking(1, true);
  534. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  535. $this->assertEquals('23836616', (string) $response->id);
  536. }
  537. public function testBlockingDestroy()
  538. {
  539. $twitter = new Zend_Service_Twitter;
  540. $twitter->setLocalHttpClient($this->_stubTwitter(
  541. 'blocks/destroy/twitter.xml', Zend_Http_Client::POST, 'blocks.destroy.twitter.xml'
  542. ));
  543. $response = $twitter->block->destroy('twitter');
  544. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  545. }
  546. public function testBlockingExistsReturnsFalseWhenBlockDoesNotExists()
  547. {
  548. $twitter = new Zend_Service_Twitter;
  549. $twitter->setLocalHttpClient($this->_stubTwitter(
  550. 'blocks/exists/padraicb.xml', Zend_Http_Client::GET, 'blocks.exists.padraicb.xml'
  551. ));
  552. $this->assertFalse($twitter->block->exists('padraicb'));
  553. }
  554. public function testBlockingExistsReturnsObjectWhenFlagPassed()
  555. {
  556. $twitter = new Zend_Service_Twitter;
  557. $twitter->setLocalHttpClient($this->_stubTwitter(
  558. 'blocks/exists/padraicb.xml', Zend_Http_Client::GET, 'blocks.exists.padraicb.xml'
  559. ));
  560. $response = $twitter->block->exists('padraicb', true);
  561. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  562. }
  563. /**
  564. * @group ZF-6284
  565. */
  566. public function testTwitterObjectsSoNotShareSameHttpClientToPreventConflictingAuthentication()
  567. {
  568. $twitter1 = new Zend_Service_Twitter(array('username'=>'zftestuser1'));
  569. $twitter2 = new Zend_Service_Twitter(array('username'=>'zftestuser2'));
  570. $this->assertFalse($twitter1->getLocalHttpClient() === $twitter2->getLocalHttpClient());
  571. }
  572. /**
  573. * @group ZF-10644
  574. */
  575. public function testStatusUserTimelineShouldHonorAllFlags()
  576. {
  577. $params = array(
  578. 'screen_name' => 'allzend',
  579. 'page' => 1,
  580. 'include_rts' => '1',
  581. 'trim_user' => '1',
  582. 'include_entities' => '1',
  583. );
  584. $twitter = new Zend_Service_Twitter();
  585. $twitter->setLocalHttpClient($this->_stubTwitter(
  586. 'statuses/user_timeline.xml', Zend_Http_Client::GET, 'user_timeline.twitter.xml',
  587. $params
  588. ));
  589. // Assertions are part of mocking
  590. $timeline = $twitter->statusUserTimeline($params);
  591. }
  592. /**
  593. * @group ZF-11014
  594. */
  595. public function testStatusFriendsTimelineShouldHonorAllFlags()
  596. {
  597. $params = array(
  598. 'page' => 3,
  599. 'include_rts' => '1',
  600. 'trim_user' => '1',
  601. 'include_entities' => '1',
  602. );
  603. $twitter = new Zend_Service_Twitter();
  604. $twitter->setLocalHttpClient($this->_stubTwitter(
  605. 'statuses/friends_timeline.xml', Zend_Http_Client::GET, 'statuses.friends_timeline.page.xml',
  606. $params
  607. ));
  608. // Assertions are part of mocking
  609. $timeline = $twitter->statusFriendsTimeline($params);
  610. }
  611. /**
  612. * @group ZF-11023
  613. */
  614. public function testConstructorPassedObjectZendConfig()
  615. {
  616. require_once 'Zend/Config.php';
  617. $config = new Zend_Config(array('username' => 'zf'));
  618. $twitter = new Zend_Service_Twitter($config);
  619. $this->assertEquals('zf', $twitter->getUsername());
  620. }
  621. }
  622. if (PHPUnit_MAIN_METHOD == 'Zend_Service_TwitterTest2::main') {
  623. Zend_Service_TwitterTest2::main();
  624. }