TwitterTest.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  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-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. if (!defined('PHPUnit_MAIN_METHOD')) {
  23. define('PHPUnit_MAIN_METHOD', 'Zend_Service_TwitterTest::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-2009 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_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. /**
  57. * Sets up the fixture, for example, open a network connection.
  58. * This method is called before a test is executed.
  59. *
  60. * @return void
  61. */
  62. protected function setUp()
  63. {
  64. if (!defined('TESTS_ZEND_SERVICE_TWITTER_ONLINE_ENABLED')
  65. || !constant('TESTS_ZEND_SERVICE_TWITTER_ONLINE_ENABLED')
  66. ) {
  67. $this->markTestSkipped('Twitter tests are not enabled');
  68. return;
  69. }
  70. Zend_Service_Abstract::getHttpClient()->setAdapter('Zend_Http_Client_Adapter_Socket');
  71. $this->twitter = new Zend_Service_Twitter(
  72. TESTS_ZEND_SERVICE_TWITTER_USER,
  73. TESTS_ZEND_SERVICE_TWITTER_PASS
  74. );
  75. }
  76. /**
  77. * @issue ZF-7781
  78. */
  79. public function testValidationOfScreenNames_NoError()
  80. {
  81. $response = $this->twitter->status->userTimeline(array('screen_name'=>'Abc123_Abc123_Abc123'));
  82. }
  83. /**
  84. * @issue ZF-7781
  85. */
  86. public function testValidationOfScreenNames_InvalidChar()
  87. {
  88. $this->setExpectedException('Zend_Service_Twitter_Exception');
  89. $response = $this->twitter->status->userTimeline(array('screen_name'=>'abc.def'));
  90. }
  91. /**
  92. * @issue ZF-7781
  93. */
  94. public function testValidationOfScreenNames_InvalidLength()
  95. {
  96. $this->setExpectedException('Zend_Service_Twitter_Exception');
  97. $response = $this->twitter->status->userTimeline(array('screen_name'=>'abcdef_abc123_abc123x'));
  98. }
  99. /**
  100. * @issue ZF-7781
  101. */
  102. public function testStatusUserTimelineConstructsExpectedGetUriAndOmitsInvalidParams()
  103. {
  104. $client = new Zend_Http_Client;
  105. $client->setAdapter(new Zend_Http_Client_Adapter_Test);
  106. Zend_Service_Twitter::setHttpClient($client);
  107. $twitter = new Zend_Service_Twitter(
  108. TESTS_ZEND_SERVICE_TWITTER_USER,
  109. TESTS_ZEND_SERVICE_TWITTER_PASS
  110. );
  111. try {
  112. $twitter->status->userTimeline(array(
  113. 'id' => '123',
  114. 'since' => '+2 days', /* invalid param since Apr 2009 */
  115. 'page' => '1',
  116. 'count' => '123',
  117. 'user_id' => '123',
  118. 'since_id' => '123',
  119. 'max_id' => '123',
  120. 'screen_name'=>'abcdef'
  121. ));
  122. } catch (Zend_Rest_Client_Result_Exception $e) {
  123. // ignores empty response complaint from Zend_Rest
  124. }
  125. $this->assertContains(
  126. 'GET /statuses/user_timeline/123.xml?page=1&count=123&user_id=123&since_id=123&max_id=123&screen_name=abcdef',
  127. $twitter->getLocalHttpClient()->getLastRequest()
  128. );
  129. }
  130. /**
  131. * @return void
  132. */
  133. public function testConstructorShouldSetUsernameAndPassword()
  134. {
  135. $this->assertEquals(TESTS_ZEND_SERVICE_TWITTER_USER, $this->twitter->getUsername());
  136. $this->assertEquals(TESTS_ZEND_SERVICE_TWITTER_PASS, $this->twitter->getPassword());
  137. }
  138. /**
  139. * @return void
  140. */
  141. public function testConstructorShouldAllowUsernamePasswordAsArray()
  142. {
  143. $userInfo = array('username' => 'foo', 'password' => 'bar');
  144. $twit = new Zend_Service_Twitter($userInfo);
  145. $this->assertEquals('foo', $twit->getUsername());
  146. $this->assertEquals('bar', $twit->getPassword());
  147. }
  148. /**
  149. * @return void
  150. */
  151. public function testUsernameAccessorsShouldAllowSettingAndRetrievingUsername()
  152. {
  153. $this->twitter->setUsername('foo');
  154. $this->assertEquals('foo', $this->twitter->getUsername());
  155. }
  156. /**
  157. * @return void
  158. */
  159. public function testPasswordAccessorsShouldAllowSettingAndRetrievingPassword()
  160. {
  161. $this->twitter->setPassword('foo');
  162. $this->assertEquals('foo', $this->twitter->getPassword());
  163. }
  164. /**
  165. * @return void
  166. */
  167. public function testOverloadingGetShouldReturnObjectInstanceWithValidMethodType()
  168. {
  169. try {
  170. $return = $this->twitter->status;
  171. $this->assertSame($this->twitter, $return);
  172. } catch (Exception $e) {
  173. $this->fail('Property overloading with a valid method type should not throw an exception');
  174. }
  175. }
  176. /**
  177. * @return void
  178. */
  179. public function testOverloadingGetShouldthrowExceptionWithInvalidMethodType()
  180. {
  181. try {
  182. $return = $this->twitter->foo;
  183. $this->fail('Property overloading with an invalid method type should throw an exception');
  184. } catch (Exception $e) {
  185. }
  186. }
  187. /**
  188. * @return void
  189. */
  190. public function testOverloadingGetShouldthrowExceptionWithInvalidFunction()
  191. {
  192. try {
  193. $return = $this->twitter->foo();
  194. $this->fail('Property overloading with an invalid function should throw an exception');
  195. } catch (Exception $e) {
  196. }
  197. }
  198. /**
  199. * @return void
  200. */
  201. public function testMethodProxyingDoesNotThrowExceptionsWithValidMethods()
  202. {
  203. try {
  204. $this->twitter->status->publicTimeline();
  205. } catch (Exception $e) {
  206. $this->fail('Method proxying should not throw an exception with valid methods; exception: ' . $e->getMessage());
  207. }
  208. }
  209. /**
  210. * @return void
  211. */
  212. public function testMethodProxyingThrowExceptionsWithInvalidMethods()
  213. {
  214. try {
  215. $this->twitter->status->foo();
  216. $this->fail('Method proxying should throw an exception with invalid methods');
  217. } catch (Exception $e) {
  218. }
  219. }
  220. /**
  221. * @return void
  222. */
  223. public function testVerifiedCredentials()
  224. {
  225. $response = $this->twitter->account->verifyCredentials();
  226. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  227. $httpClient = $this->twitter->getLocalHttpClient();
  228. $httpRequest = $httpClient->getLastRequest();
  229. $httpResponse = $httpClient->getLastResponse();
  230. $this->assertTrue($httpResponse->isSuccessful(), $httpResponse->getStatus() . ': ' . var_export($httpRequest, 1) . '\n' . $httpResponse->getHeadersAsString());
  231. }
  232. /**
  233. * @return void
  234. */
  235. public function testPublicTimelineStatusReturnsResults()
  236. {
  237. $response = $this->twitter->status->publicTimeline();
  238. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  239. $httpClient = $this->twitter->getLocalHttpClient();
  240. $httpRequest = $httpClient->getLastRequest();
  241. $httpResponse = $httpClient->getLastResponse();
  242. $this->assertTrue($httpResponse->isSuccessful(), $httpResponse->getStatus() . ': ' . var_export($httpRequest, 1) . '\n' . $httpResponse->getHeadersAsString());
  243. $this->assertTrue(isset($response->status));
  244. }
  245. /**
  246. * @return void
  247. */
  248. public function testUsersFeaturedStatusReturnsResults()
  249. {
  250. $response = $this->twitter->user->featured();
  251. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  252. $httpClient = $this->twitter->getLocalHttpClient();
  253. $httpRequest = $httpClient->getLastRequest();
  254. $httpResponse = $httpClient->getLastResponse();
  255. $this->assertTrue($httpResponse->isSuccessful(), $httpResponse->getStatus() . ': ' . var_export($httpRequest, 1) . '\n' . $httpResponse->getHeadersAsString());
  256. $this->assertTrue(isset($response->status));
  257. }
  258. public function testRateLimitStatusReturnsResults()
  259. {
  260. /* @var $response Zend_Rest_Client_Result */
  261. $response = $this->twitter->account->rateLimitStatus();
  262. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  263. $httpClient = $this->twitter->getLocalHttpClient();
  264. $httpRequest = $httpClient->getLastRequest();
  265. $httpResponse = $httpClient->getLastResponse();
  266. $this->assertTrue($httpResponse->isSuccessful(), $httpResponse->getStatus() . ': ' . var_export($httpRequest, 1) . '\n' . $httpResponse->getHeadersAsString());
  267. }
  268. public function testRateLimitStatusHasHitsLeft()
  269. {
  270. /* @var $response Zend_Rest_Client_Result */
  271. $response = $this->twitter->account->rateLimitStatus();
  272. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  273. $remaining_hits = $response->toValue($response->{'remaining-hits'});
  274. $this->assertType('numeric', $remaining_hits);
  275. $this->assertGreaterThan(0, $remaining_hits);
  276. }
  277. /**
  278. * @return void
  279. */
  280. public function testAccountEndSession()
  281. {
  282. $response = $this->twitter->account->endSession();
  283. $this->assertTrue($response);
  284. }
  285. /**
  286. * @return void
  287. */
  288. public function testFriendshipCreate()
  289. {
  290. $response = $this->twitter->friendship->create('zftestuser1');
  291. $httpClient = $this->twitter->getLocalHttpClient();
  292. $httpResponse = $httpClient->getLastResponse();
  293. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  294. }
  295. /**
  296. * @return void
  297. */
  298. public function testFriendshipExists()
  299. {
  300. /* @var $response Zend_Rest_Client_Result */
  301. $response = $this->twitter->friendship->exists('zftestuser1');
  302. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  303. $httpClient = $this->twitter->getLocalHttpClient();
  304. $httpRequest = $httpClient->getLastRequest();
  305. $httpResponse = $httpClient->getLastResponse();
  306. $this->assertTrue($httpResponse->isSuccessful(), $httpResponse->getStatus() . ': ' . var_export($httpRequest, 1) . '\n' . $httpResponse->getHeadersAsString());
  307. }
  308. /**
  309. * @return void
  310. */
  311. public function testFriendsTimelineWithInvalidParamReturnsResults()
  312. {
  313. /* @var $response Zend_Rest_Client_Result */
  314. $response = $this->twitter->status->friendsTimeline( array('foo' => 'bar') );
  315. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  316. $httpClient = $this->twitter->getLocalHttpClient();
  317. $httpRequest = $httpClient->getLastRequest();
  318. $httpResponse = $httpClient->getLastResponse();
  319. $this->assertTrue($httpResponse->isSuccessful(), $httpResponse->getStatus() . ': ' . var_export($httpRequest, 1) . '\n' . $httpResponse->getHeadersAsString());
  320. $this->assertTrue(isset($response->status));
  321. }
  322. /**
  323. * @return void
  324. */
  325. public function testFriendsTimelineStatusWithFriendSpecifiedReturnsResults()
  326. {
  327. /* @var $response Zend_Rest_Client_Result */
  328. $this->insertTestTwitterData();
  329. $response = $this->twitter->status->friendsTimeline( array('id' => 'zftestuser1') );
  330. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  331. $httpClient = $this->twitter->getLocalHttpClient();
  332. $httpRequest = $httpClient->getLastRequest();
  333. $httpResponse = $httpClient->getLastResponse();
  334. $this->assertTrue($httpResponse->isSuccessful(), $httpResponse->getStatus() . ': ' . var_export($httpRequest, 1) . '\n' . $httpResponse->getHeadersAsString());
  335. $this->assertTrue(isset($response->status));
  336. }
  337. /**
  338. * @return void
  339. */
  340. public function testFriendsTimelineWithPageReturnsResults()
  341. {
  342. /* @var $response Zend_Rest_Client_Result */
  343. $response = $this->twitter->status->friendsTimeline( array('id' => 'zftestuser1', 'page' => '2') );
  344. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  345. $httpClient = $this->twitter->getLocalHttpClient();
  346. $httpRequest = $httpClient->getLastRequest();
  347. $httpResponse = $httpClient->getLastResponse();
  348. $this->assertTrue($httpResponse->isSuccessful(), $httpResponse->getStatus() . ': ' . var_export($httpRequest, 1) . '\n' . $httpResponse->getHeadersAsString());
  349. $this->assertTrue(isset($response->status));
  350. }
  351. /**
  352. * @return void
  353. */
  354. public function testFriendsTimelineWithCountReturnsResults()
  355. {
  356. /* @var $response Zend_Rest_Client_Result */
  357. $response = $this->twitter->status->friendsTimeline( array('id' => 'zftestuser1', 'count' => '2') );
  358. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  359. $httpClient = $this->twitter->getLocalHttpClient();
  360. $httpRequest = $httpClient->getLastRequest();
  361. $httpResponse = $httpClient->getLastResponse();
  362. $this->assertTrue($httpResponse->isSuccessful(), $httpResponse->getStatus() . ': ' . var_export($httpRequest, 1) . '\n' . $httpResponse->getHeadersAsString());
  363. $this->assertTrue(isset($response->status));
  364. $this->assertEquals(2, count($response->status), $httpResponse->getStatus() . ': ' . var_export($httpRequest, 1) . '\n' . $httpResponse->getHeadersAsString());
  365. }
  366. /**
  367. * @return void
  368. */
  369. public function testUserTimelineStatusWithPageAndTwoTweetsReturnsResults()
  370. {
  371. /* @var $response Zend_Rest_Client_Result */
  372. $response = $this->twitter->status->userTimeline( array('id' => 'zftestuser1', 'count' => 2) );
  373. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  374. $httpClient = $this->twitter->getLocalHttpClient();
  375. $httpRequest = $httpClient->getLastRequest();
  376. $httpResponse = $httpClient->getLastResponse();
  377. $raw_response = $httpResponse->getHeadersAsString() . $httpResponse->getBody();
  378. $this->assertTrue($httpResponse->isSuccessful(), $httpResponse->getStatus() . ': ' . var_export($httpRequest, 1) . '\n' . $httpResponse->getHeadersAsString());
  379. $this->assertTrue(isset($response->status));
  380. $this->assertEquals(2, count($response->status), $httpResponse->getStatus() . ': ' . var_export($httpRequest, 1) . '\n' . $httpResponse->getHeadersAsString());
  381. }
  382. public function testUserTimelineStatusShouldReturnFortyResults()
  383. {
  384. /* @var $response Zend_Rest_Client_Result */
  385. $response = $this->twitter->status->userTimeline( array('id' => 'zftestuser1', 'count' => 40) );
  386. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  387. $httpClient = $this->twitter->getLocalHttpClient();
  388. $httpRequest = $httpClient->getLastRequest();
  389. $httpResponse = $httpClient->getLastResponse();
  390. $this->assertTrue($httpResponse->isSuccessful(), $httpResponse->getStatus() . ': ' . var_export($httpRequest, 1) . '\n' . $httpResponse->getHeadersAsString());
  391. $this->assertTrue(isset($response->status));
  392. $this->assertEquals(40, count($response->status));
  393. }
  394. /**
  395. * @return void
  396. */
  397. public function testPostStatusUpdateReturnsResponse()
  398. {
  399. /* @var $response Zend_Rest_Client_Result */
  400. $response = $this->twitter->status->update( 'Test Message - ' . rand() );
  401. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  402. $httpClient = $this->twitter->getLocalHttpClient();
  403. $httpRequest = $httpClient->getLastRequest();
  404. $httpResponse = $httpClient->getLastResponse();
  405. $this->assertTrue($httpResponse->isSuccessful(), $httpResponse->getStatus() . ': ' . var_export($httpRequest, 1) . '\n' . $httpResponse->getHeadersAsString());
  406. $this->assertTrue(isset($response->status));
  407. }
  408. /**
  409. * $return void
  410. */
  411. public function testPostStatusUpdateToLongShouldThrowException()
  412. {
  413. try {
  414. $response = $this->twitter->status->update( 'Test Message - ' . str_repeat(' Hello ', 140) );
  415. $this->fail('Trying to post a status with > 140 character should throw exception');
  416. } catch (Exception $e) {
  417. }
  418. }
  419. public function testPostStatusUpdateUTF8ShouldNotThrowException()
  420. {
  421. try {
  422. $response = $this->twitter->status->update( str_repeat('M�r', 46) . 'M�' );
  423. } catch (Exception $e) {
  424. $this->fail('Trying to post a utf8 string of 140 chars should not throw exception');
  425. }
  426. }
  427. /**
  428. * $return void
  429. */
  430. public function testPostStatusUpdateEmptyShouldThrowException()
  431. {
  432. try {
  433. $response = $this->twitter->status->update('');
  434. $this->fail('Trying to post an empty status should throw exception');
  435. } catch (Exception $e) {
  436. }
  437. }
  438. /**
  439. * @return void
  440. */
  441. public function testShowStatusReturnsResponse()
  442. {
  443. $response = $this->twitter->status->publicTimeline();
  444. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  445. $status_id = $response->toValue($response->status->id);
  446. $this->assertType('numeric', $status_id);
  447. $response2 = $this->twitter->status->show($status_id);
  448. $this->assertTrue($response2 instanceof Zend_Rest_Client_Result);
  449. $httpClient = $this->twitter->getLocalHttpClient();
  450. $httpRequest = $httpClient->getLastRequest();
  451. $httpResponse = $httpClient->getLastResponse();
  452. $this->assertTrue($httpResponse->isSuccessful(), $httpResponse->getStatus() . ': ' . var_export($httpRequest, 1) . '\n' . $httpResponse->getHeadersAsString());
  453. $this->assertTrue(isset($response->status));
  454. }
  455. /**
  456. * @return void
  457. */
  458. public function testCreateFavoriteStatusReturnsResponse()
  459. {
  460. /* @var $response Zend_Rest_Client_Result */
  461. $response = $this->twitter->status->userTimeline();
  462. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  463. $update_id = $response->toValue($response->status->id);
  464. $this->assertType('numeric', $update_id);
  465. $response2 = $this->twitter->favorite->create($update_id);
  466. $this->assertTrue($response2 instanceof Zend_Rest_Client_Result);
  467. $httpClient = $this->twitter->getLocalHttpClient();
  468. $httpRequest = $httpClient->getLastRequest();
  469. $httpResponse = $httpClient->getLastResponse();
  470. $this->assertTrue($httpResponse->isSuccessful(), $httpResponse->getStatus() . ': ' . var_export($httpRequest, 1) . '\n' . $httpResponse->getHeadersAsString());
  471. $this->assertTrue(isset($response->status));
  472. }
  473. /**
  474. * @return void
  475. */
  476. public function testFavoriteFavoriesReturnsResponse()
  477. {
  478. $response = $this->twitter->favorite->favorites();
  479. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  480. $httpClient = $this->twitter->getLocalHttpClient();
  481. $httpRequest = $httpClient->getLastRequest();
  482. $httpResponse = $httpClient->getLastResponse();
  483. $this->assertTrue($httpResponse->isSuccessful(), $httpResponse->getStatus() . ': ' . var_export($httpRequest, 1) . '\n' . $httpResponse->getHeadersAsString());
  484. $this->assertTrue(isset($response->status));
  485. }
  486. public function testDestroyFavoriteReturnsResponse()
  487. {
  488. $response = $this->twitter->favorite->favorites();
  489. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  490. $update_id = $response->toValue($response->status->id);
  491. $this->assertType('numeric', $update_id);
  492. $response2 = $this->twitter->favorite->destroy($update_id);
  493. $this->assertTrue($response2 instanceof Zend_Rest_Client_Result);
  494. $httpClient = $this->twitter->getLocalHttpClient();
  495. $httpRequest = $httpClient->getLastRequest();
  496. $httpResponse = $httpClient->getLastResponse();
  497. $this->assertTrue($httpResponse->isSuccessful(), $httpResponse->getStatus() . ': ' . var_export($httpRequest, 1) . '\n' . $httpResponse->getHeadersAsString());
  498. $this->assertTrue(isset($response->status));
  499. }
  500. public function testStatusDestroyReturnsResult()
  501. {
  502. /* @var $response Zend_Rest_Client_Result */
  503. $response = $this->twitter->status->userTimeline();
  504. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  505. $update_id = $response->toValue($response->status->id);
  506. $this->assertType('numeric', $update_id);
  507. $response2 = $this->twitter->status->destroy($update_id);
  508. $this->assertTrue($response2 instanceof Zend_Rest_Client_Result);
  509. $httpClient = $this->twitter->getLocalHttpClient();
  510. $httpRequest = $httpClient->getLastRequest();
  511. $httpResponse = $httpClient->getLastResponse();
  512. $this->assertTrue($httpResponse->isSuccessful(), $httpResponse->getStatus() . ': ' . var_export($httpRequest, 1) . '\n' . $httpResponse->getHeadersAsString());
  513. $this->assertTrue(isset($response->status));
  514. }
  515. public function testUserFriendsReturnsResults()
  516. {
  517. $response = $this->twitter->user->friends();
  518. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  519. $httpClient = $this->twitter->getLocalHttpClient();
  520. $httpRequest = $httpClient->getLastRequest();
  521. $httpResponse = $httpClient->getLastResponse();
  522. $this->assertTrue($httpResponse->isSuccessful(), $httpResponse->getStatus() . ': ' . var_export($httpRequest, 1) . '\n' . $httpResponse->getHeadersAsString());
  523. $this->assertTrue(isset($response->status));
  524. }
  525. public function testUserFolloersReturnsResults()
  526. {
  527. $response = $this->twitter->user->followers(array('id' =>'zftestuser1'));
  528. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  529. $httpClient = $this->twitter->getLocalHttpClient();
  530. $httpRequest = $httpClient->getLastRequest();
  531. $httpResponse = $httpClient->getLastResponse();
  532. $this->assertTrue($httpResponse->isSuccessful(), $httpResponse->getStatus() . ': ' . var_export($httpRequest, 1) . '\n' . $httpResponse->getHeadersAsString());
  533. $this->assertTrue(isset($response->status));
  534. }
  535. public function testUserFriendsSpecificUserReturnsResults()
  536. {
  537. $response = $this->twitter->user->friends(array('id' =>'ZendRssFeed'));
  538. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  539. $httpClient = $this->twitter->getLocalHttpClient();
  540. $httpRequest = $httpClient->getLastRequest();
  541. $httpResponse = $httpClient->getLastResponse();
  542. $this->assertTrue($httpResponse->isSuccessful(), $httpResponse->getStatus() . ': ' . var_export($httpRequest, 1) . '\n' . $httpResponse->getHeadersAsString());
  543. $this->assertTrue(isset($response->status), $httpResponse->getStatus() . ': ' . var_export($httpRequest, 1) . '\n' . $httpResponse->getHeadersAsString());
  544. return $response;
  545. }
  546. public function testUserShowByIdReturnsResults()
  547. {
  548. $userInfo = $this->testUserFriendsSpecificUserReturnsResults();
  549. $userId = $userInfo->toValue($userInfo->user->id);
  550. $response = $this->twitter->user->show($userId);
  551. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  552. $this->assertEquals($userInfo->toValue($userInfo->user->name), $response->toValue($response->name));
  553. $this->assertEquals($userId, $response->toValue($response->id));
  554. }
  555. public function testUserShowByNameReturnsResults()
  556. {
  557. $response = $this->twitter->user->show('zftestuser1');
  558. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  559. $this->assertEquals('zftestuser1', $response->toValue($response->screen_name));
  560. }
  561. public function testStatusRepliesReturnsResults()
  562. {
  563. $response = $this->twitter->status->replies(array('page' => 1, 'since_id' => 10000, 'invalid_option' => 'doh'));
  564. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  565. $httpClient = $this->twitter->getLocalHttpClient();
  566. $httpRequest = $httpClient->getLastRequest();
  567. $httpResponse = $httpClient->getLastResponse();
  568. $this->assertTrue($httpResponse->isSuccessful(), $httpResponse->getStatus() . ': ' . var_export($httpRequest, 1) . '\n' . $httpResponse->getHeadersAsString());
  569. }
  570. /**
  571. * @return void
  572. */
  573. public function testFriendshipDestory()
  574. {
  575. $response = $this->twitter->friendship->destroy('zftestuser1');
  576. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  577. $httpClient = $this->twitter->getLocalHttpClient();
  578. $httpRequest = $httpClient->getLastRequest();
  579. $httpResponse = $httpClient->getLastResponse();
  580. $this->assertTrue($httpResponse->isSuccessful(), $httpResponse->getStatus() . ': ' . var_export($httpRequest, 1) . '\n' . $httpResponse->getHeadersAsString());
  581. }
  582. /**
  583. * Insert Test Data
  584. *
  585. */
  586. protected function insertTestTwitterData()
  587. {
  588. $twitter = new Zend_Service_Twitter('zftestuser1','zftestuser1');
  589. // create 10 new entries
  590. for($x=0; $x<10; $x++) {
  591. $twitter->status->update( 'Test Message - ' . $x);
  592. }
  593. $twitter->account->endSession();
  594. }
  595. /**
  596. * @issue ZF-6284
  597. */
  598. public function testTwitterObjectsSoNotShareSameHttpClientToPreventConflictingAuthentication()
  599. {
  600. $twitter1 = new Zend_Service_Twitter('zftestuser1','zftestuser1');
  601. $twitter2 = new Zend_Service_Twitter('zftestuser2','zftestuser2');
  602. $this->assertFalse($twitter1->getLocalHttpClient() === $twitter2->getLocalHttpClient());
  603. }
  604. }
  605. if (PHPUnit_MAIN_METHOD == 'Zend_Service_TwitterTest::main') {
  606. Zend_Service_TwitterTest::main();
  607. }