TwitterTest.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  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. $response = $this->twitter->status->friendsTimeline( array('id' => 'zftestuser1') );
  329. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  330. $httpClient = $this->twitter->getLocalHttpClient();
  331. $httpRequest = $httpClient->getLastRequest();
  332. $httpResponse = $httpClient->getLastResponse();
  333. $this->assertTrue($httpResponse->isSuccessful(), $httpResponse->getStatus() . ': ' . var_export($httpRequest, 1) . '\n' . $httpResponse->getHeadersAsString());
  334. $this->assertTrue(isset($response->status));
  335. }
  336. /**
  337. * @return void
  338. */
  339. public function testFriendsTimelineWithPageReturnsResults()
  340. {
  341. /* @var $response Zend_Rest_Client_Result */
  342. $response = $this->twitter->status->friendsTimeline( array('id' => 'zftestuser1', 'page' => '2') );
  343. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  344. $httpClient = $this->twitter->getLocalHttpClient();
  345. $httpRequest = $httpClient->getLastRequest();
  346. $httpResponse = $httpClient->getLastResponse();
  347. $this->assertTrue($httpResponse->isSuccessful(), $httpResponse->getStatus() . ': ' . var_export($httpRequest, 1) . '\n' . $httpResponse->getHeadersAsString());
  348. $this->assertTrue(isset($response->status));
  349. }
  350. /**
  351. * @return void
  352. */
  353. public function testFriendsTimelineWithCountReturnsResults()
  354. {
  355. /* @var $response Zend_Rest_Client_Result */
  356. $response = $this->twitter->status->friendsTimeline( array('id' => 'zftestuser1', 'count' => '2') );
  357. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  358. $httpClient = $this->twitter->getLocalHttpClient();
  359. $httpRequest = $httpClient->getLastRequest();
  360. $httpResponse = $httpClient->getLastResponse();
  361. $this->assertTrue($httpResponse->isSuccessful(), $httpResponse->getStatus() . ': ' . var_export($httpRequest, 1) . '\n' . $httpResponse->getHeadersAsString());
  362. $this->assertTrue(isset($response->status));
  363. $this->assertEquals(2, count($response->status), $httpResponse->getStatus() . ': ' . var_export($httpRequest, 1) . '\n' . $httpResponse->getHeadersAsString());
  364. }
  365. /**
  366. * @return void
  367. */
  368. public function testUserTimelineStatusWithPageAndTwoTweetsReturnsResults()
  369. {
  370. /* @var $response Zend_Rest_Client_Result */
  371. $response = $this->twitter->status->userTimeline( array('id' => 'zftestuser1', 'count' => 2) );
  372. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  373. $httpClient = $this->twitter->getLocalHttpClient();
  374. $httpRequest = $httpClient->getLastRequest();
  375. $httpResponse = $httpClient->getLastResponse();
  376. $raw_response = $httpResponse->getHeadersAsString() . $httpResponse->getBody();
  377. $this->assertTrue($httpResponse->isSuccessful(), $httpResponse->getStatus() . ': ' . var_export($httpRequest, 1) . '\n' . $httpResponse->getHeadersAsString());
  378. $this->assertTrue(isset($response->status));
  379. $this->assertEquals(2, count($response->status), $httpResponse->getStatus() . ': ' . var_export($httpRequest, 1) . '\n' . $httpResponse->getHeadersAsString());
  380. }
  381. public function testUserTimelineStatusShouldReturnFortyResults()
  382. {
  383. /* @var $response Zend_Rest_Client_Result */
  384. $response = $this->twitter->status->userTimeline( array('id' => 'zftestuser1', 'count' => 40) );
  385. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  386. $httpClient = $this->twitter->getLocalHttpClient();
  387. $httpRequest = $httpClient->getLastRequest();
  388. $httpResponse = $httpClient->getLastResponse();
  389. $this->assertTrue($httpResponse->isSuccessful(), $httpResponse->getStatus() . ': ' . var_export($httpRequest, 1) . '\n' . $httpResponse->getHeadersAsString());
  390. $this->assertTrue(isset($response->status));
  391. $this->assertEquals(40, count($response->status));
  392. }
  393. /**
  394. * @return void
  395. */
  396. public function testPostStatusUpdateReturnsResponse()
  397. {
  398. /* @var $response Zend_Rest_Client_Result */
  399. $response = $this->twitter->status->update( 'Test Message - ' . rand() );
  400. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  401. $httpClient = $this->twitter->getLocalHttpClient();
  402. $httpRequest = $httpClient->getLastRequest();
  403. $httpResponse = $httpClient->getLastResponse();
  404. $this->assertTrue($httpResponse->isSuccessful(), $httpResponse->getStatus() . ': ' . var_export($httpRequest, 1) . '\n' . $httpResponse->getHeadersAsString());
  405. $this->assertTrue(isset($response->status));
  406. }
  407. /**
  408. * $return void
  409. */
  410. public function testPostStatusUpdateToLongShouldThrowException()
  411. {
  412. try {
  413. $response = $this->twitter->status->update( 'Test Message - ' . str_repeat(' Hello ', 140) );
  414. $this->fail('Trying to post a status with > 140 character should throw exception');
  415. } catch (Exception $e) {
  416. }
  417. }
  418. public function testPostStatusUpdateUTF8ShouldNotThrowException()
  419. {
  420. try {
  421. $response = $this->twitter->status->update( str_repeat('M�r', 46) . 'M�' );
  422. } catch (Exception $e) {
  423. $this->fail('Trying to post a utf8 string of 140 chars should not throw exception');
  424. }
  425. }
  426. /**
  427. * $return void
  428. */
  429. public function testPostStatusUpdateEmptyShouldThrowException()
  430. {
  431. try {
  432. $response = $this->twitter->status->update('');
  433. $this->fail('Trying to post an empty status should throw exception');
  434. } catch (Exception $e) {
  435. }
  436. }
  437. /**
  438. * @return void
  439. */
  440. public function testShowStatusReturnsResponse()
  441. {
  442. $response = $this->twitter->status->publicTimeline();
  443. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  444. $status_id = $response->toValue($response->status->id);
  445. $this->assertType('numeric', $status_id);
  446. $response2 = $this->twitter->status->show($status_id);
  447. $this->assertTrue($response2 instanceof Zend_Rest_Client_Result);
  448. $httpClient = $this->twitter->getLocalHttpClient();
  449. $httpRequest = $httpClient->getLastRequest();
  450. $httpResponse = $httpClient->getLastResponse();
  451. $this->assertTrue($httpResponse->isSuccessful(), $httpResponse->getStatus() . ': ' . var_export($httpRequest, 1) . '\n' . $httpResponse->getHeadersAsString());
  452. $this->assertTrue(isset($response->status));
  453. }
  454. /**
  455. * @return void
  456. */
  457. public function testCreateFavoriteStatusReturnsResponse()
  458. {
  459. /* @var $response Zend_Rest_Client_Result */
  460. $response = $this->twitter->status->userTimeline();
  461. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  462. $update_id = $response->toValue($response->status->id);
  463. $this->assertType('numeric', $update_id);
  464. $response2 = $this->twitter->favorite->create($update_id);
  465. $this->assertTrue($response2 instanceof Zend_Rest_Client_Result);
  466. $httpClient = $this->twitter->getLocalHttpClient();
  467. $httpRequest = $httpClient->getLastRequest();
  468. $httpResponse = $httpClient->getLastResponse();
  469. $this->assertTrue($httpResponse->isSuccessful(), $httpResponse->getStatus() . ': ' . var_export($httpRequest, 1) . '\n' . $httpResponse->getHeadersAsString());
  470. $this->assertTrue(isset($response->status));
  471. }
  472. /**
  473. * @return void
  474. */
  475. public function testFavoriteFavoriesReturnsResponse()
  476. {
  477. $response = $this->twitter->favorite->favorites();
  478. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  479. $httpClient = $this->twitter->getLocalHttpClient();
  480. $httpRequest = $httpClient->getLastRequest();
  481. $httpResponse = $httpClient->getLastResponse();
  482. $this->assertTrue($httpResponse->isSuccessful(), $httpResponse->getStatus() . ': ' . var_export($httpRequest, 1) . '\n' . $httpResponse->getHeadersAsString());
  483. $this->assertTrue(isset($response->status));
  484. }
  485. public function testDestroyFavoriteReturnsResponse()
  486. {
  487. $response = $this->twitter->favorite->favorites();
  488. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  489. $update_id = $response->toValue($response->status->id);
  490. $this->assertType('numeric', $update_id);
  491. $response2 = $this->twitter->favorite->destroy($update_id);
  492. $this->assertTrue($response2 instanceof Zend_Rest_Client_Result);
  493. $httpClient = $this->twitter->getLocalHttpClient();
  494. $httpRequest = $httpClient->getLastRequest();
  495. $httpResponse = $httpClient->getLastResponse();
  496. $this->assertTrue($httpResponse->isSuccessful(), $httpResponse->getStatus() . ': ' . var_export($httpRequest, 1) . '\n' . $httpResponse->getHeadersAsString());
  497. $this->assertTrue(isset($response->status));
  498. }
  499. public function testStatusDestroyReturnsResult()
  500. {
  501. /* @var $response Zend_Rest_Client_Result */
  502. $response = $this->twitter->status->userTimeline();
  503. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  504. $update_id = $response->toValue($response->status->id);
  505. $this->assertType('numeric', $update_id);
  506. $response2 = $this->twitter->status->destroy($update_id);
  507. $this->assertTrue($response2 instanceof Zend_Rest_Client_Result);
  508. $httpClient = $this->twitter->getLocalHttpClient();
  509. $httpRequest = $httpClient->getLastRequest();
  510. $httpResponse = $httpClient->getLastResponse();
  511. $this->assertTrue($httpResponse->isSuccessful(), $httpResponse->getStatus() . ': ' . var_export($httpRequest, 1) . '\n' . $httpResponse->getHeadersAsString());
  512. $this->assertTrue(isset($response->status));
  513. }
  514. public function testUserFriendsReturnsResults()
  515. {
  516. $response = $this->twitter->user->friends();
  517. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  518. $httpClient = $this->twitter->getLocalHttpClient();
  519. $httpRequest = $httpClient->getLastRequest();
  520. $httpResponse = $httpClient->getLastResponse();
  521. $this->assertTrue($httpResponse->isSuccessful(), $httpResponse->getStatus() . ': ' . var_export($httpRequest, 1) . '\n' . $httpResponse->getHeadersAsString());
  522. $this->assertTrue(isset($response->status));
  523. }
  524. public function testUserFolloersReturnsResults()
  525. {
  526. $response = $this->twitter->user->followers(array('id' =>'zftestuser1'));
  527. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  528. $httpClient = $this->twitter->getLocalHttpClient();
  529. $httpRequest = $httpClient->getLastRequest();
  530. $httpResponse = $httpClient->getLastResponse();
  531. $this->assertTrue($httpResponse->isSuccessful(), $httpResponse->getStatus() . ': ' . var_export($httpRequest, 1) . '\n' . $httpResponse->getHeadersAsString());
  532. $this->assertTrue(isset($response->status));
  533. }
  534. public function testUserFriendsSpecificUserReturnsResults()
  535. {
  536. $response = $this->twitter->user->friends(array('id' =>'zftestuser1'));
  537. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  538. $httpClient = $this->twitter->getLocalHttpClient();
  539. $httpRequest = $httpClient->getLastRequest();
  540. $httpResponse = $httpClient->getLastResponse();
  541. $this->assertTrue($httpResponse->isSuccessful(), $httpResponse->getStatus() . ': ' . var_export($httpRequest, 1) . '\n' . $httpResponse->getHeadersAsString());
  542. $this->assertTrue(isset($response->status), $httpResponse->getStatus() . ': ' . var_export($httpRequest, 1) . '\n' . $httpResponse->getHeadersAsString());
  543. return $response;
  544. }
  545. public function testUserShowReturnsResults()
  546. {
  547. $userInfo = $this->testUserFriendsSpecificUserReturnsResults();
  548. $userId = $userInfo->toValue($userInfo->user->id);
  549. $response = $this->twitter->user->show($userId);
  550. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  551. $this->assertEquals($userInfo->toValue($userInfo->user->name), $response->toValue($response->name));
  552. $this->assertEquals($userId, $response->toValue($response->id));
  553. }
  554. public function testStatusRepliesReturnsResults()
  555. {
  556. $response = $this->twitter->status->replies(array('page' => 1, 'since_id' => 10000, 'invalid_option' => 'doh'));
  557. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  558. $httpClient = $this->twitter->getLocalHttpClient();
  559. $httpRequest = $httpClient->getLastRequest();
  560. $httpResponse = $httpClient->getLastResponse();
  561. $this->assertTrue($httpResponse->isSuccessful(), $httpResponse->getStatus() . ': ' . var_export($httpRequest, 1) . '\n' . $httpResponse->getHeadersAsString());
  562. }
  563. /**
  564. * @return void
  565. */
  566. public function testFriendshipDestory()
  567. {
  568. $response = $this->twitter->friendship->destroy('zftestuser1');
  569. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  570. $httpClient = $this->twitter->getLocalHttpClient();
  571. $httpRequest = $httpClient->getLastRequest();
  572. $httpResponse = $httpClient->getLastResponse();
  573. $this->assertTrue($httpResponse->isSuccessful(), $httpResponse->getStatus() . ': ' . var_export($httpRequest, 1) . '\n' . $httpResponse->getHeadersAsString());
  574. }
  575. /**
  576. * Insert Test Data
  577. *
  578. */
  579. protected function insertTestTwitterData()
  580. {
  581. $twitter = new Zend_Service_Twitter('zftestuser1','zftestuser1');
  582. // create 10 new entries
  583. for($x=0; $x<10; $x++) {
  584. $twitter->status->update( 'Test Message - ' . $x);
  585. }
  586. $twitter->account->endSession();
  587. }
  588. /**
  589. * @issue ZF-6284
  590. */
  591. public function testTwitterObjectsSoNotShareSameHttpClientToPreventConflictingAuthentication()
  592. {
  593. $twitter1 = new Zend_Service_Twitter('zftestuser1','zftestuser1');
  594. $twitter2 = new Zend_Service_Twitter('zftestuser2','zftestuser2');
  595. $this->assertFalse($twitter1->getLocalHttpClient() === $twitter2->getLocalHttpClient());
  596. }
  597. }
  598. if (PHPUnit_MAIN_METHOD == 'Zend_Service_TwitterTest::main') {
  599. Zend_Service_TwitterTest::main();
  600. }