TwitterTest.php 28 KB

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