TwitterTest.php 28 KB

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