PageRenderTime 61ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Joomla/Twitter/Tests/FriendsTest.php

https://github.com/piotr-cz/joomla-framework
PHP | 1153 lines | 701 code | 173 blank | 279 comment | 26 complexity | ce54eaba4c3b5e6e0faac666a3288259 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  4. * @license GNU General Public License version 2 or later; see LICENSE
  5. */
  6. namespace Joomla\Twitter\Tests;
  7. use Joomla\Twitter\Friends;
  8. use \DomainException;
  9. use \RuntimeException;
  10. use \stdClass;
  11. require_once __DIR__ . '/case/TwitterTestCase.php';
  12. /**
  13. * Test class for Twitter Friends.
  14. *
  15. * @since 1.0
  16. */
  17. class FriendsTest extends TwitterTestCase
  18. {
  19. /**
  20. * @var string Sample JSON error message.
  21. * @since 12.3
  22. */
  23. protected $errorString = '{"error":"Generic error"}';
  24. /**
  25. * @var string Sample JSON string.
  26. * @since 1.0
  27. */
  28. protected $friendsRateLimit = '{"resources": {"friends": {
  29. "/friends/ids": {"remaining":15, "reset":"Mon Jun 25 17:20:53 +0000 2012"}
  30. }}}';
  31. /**
  32. * @var string Sample JSON string.
  33. * @since 1.0
  34. */
  35. protected $friendshipsRateLimit = '{"resources": {"friendships": {
  36. "/friendships/show": {"remaining":15, "reset":"Mon Jun 25 17:20:53 +0000 2012"},
  37. "/friendships/incoming": {"remaining":15, "reset":"Mon Jun 25 17:20:53 +0000 2012"},
  38. "/friendships/outgoing": {"remaining":15, "reset":"Mon Jun 25 17:20:53 +0000 2012"},
  39. "/friendships/create": {"remaining":15, "reset":"Mon Jun 25 17:20:53 +0000 2012"},
  40. "/friendships/lookup": {"remaining":15, "reset":"Mon Jun 25 17:20:53 +0000 2012"},
  41. "/friendships/no_retweets/ids": {"remaining":15, "reset":"Mon Jun 25 17:20:53 +0000 2012"}
  42. }}}';
  43. /**
  44. * @var string Sample JSON string.
  45. * @since 1.0
  46. */
  47. protected $followersRateLimit = '{"resources": {"followers": {
  48. "/followers/ids": {"remaining":15, "reset":"Mon Jun 25 17:20:53 +0000 2012"}
  49. }}}';
  50. /**
  51. * Sets up the fixture, for example, opens a network connection.
  52. * This method is called before a test is executed.
  53. *
  54. * @access protected
  55. *
  56. * @return void
  57. */
  58. protected function setUp()
  59. {
  60. parent::setUp();
  61. $this->object = new Friends($this->options, $this->client, $this->oauth);
  62. }
  63. /**
  64. * Provides test data for request format detection.
  65. *
  66. * @return array
  67. *
  68. * @since 1.0
  69. */
  70. public function seedUser()
  71. {
  72. // User ID or screen name
  73. return array(
  74. array(234654235457),
  75. array('testUser'),
  76. array(null)
  77. );
  78. }
  79. /**
  80. * Tests the getFriendIds method
  81. *
  82. * @param mixed $user Either an integer containing the user ID or a string containing the screen name.
  83. *
  84. * @return void
  85. *
  86. * @dataProvider seedUser
  87. * @since 1.0
  88. */
  89. public function testGetFriendIds($user)
  90. {
  91. $cursor = 123;
  92. $string_ids = true;
  93. $count = 5;
  94. $returnData = new stdClass;
  95. $returnData->code = 200;
  96. $returnData->body = $this->friendsRateLimit;
  97. $path = $this->object->fetchUrl('/application/rate_limit_status.json', array("resources" => "friends"));
  98. $this->client->expects($this->at(0))
  99. ->method('get')
  100. ->with($path)
  101. ->will($this->returnValue($returnData));
  102. $returnData = new stdClass;
  103. $returnData->code = 200;
  104. $returnData->body = $this->sampleString;
  105. // Set request parameters.
  106. if (is_numeric($user))
  107. {
  108. $data['user_id'] = $user;
  109. }
  110. elseif (is_string($user))
  111. {
  112. $data['screen_name'] = $user;
  113. }
  114. else
  115. {
  116. $this->setExpectedException('RuntimeException');
  117. $this->object->getFriendIds($user, $cursor, $string_ids, $count);
  118. }
  119. $data['cursor'] = $cursor;
  120. $data['stringify_ids'] = $string_ids;
  121. $data['count'] = $count;
  122. $path = $this->object->fetchUrl('/friends/ids.json', $data);
  123. $this->client->expects($this->at(1))
  124. ->method('get')
  125. ->with($path)
  126. ->will($this->returnValue($returnData));
  127. $this->assertThat(
  128. $this->object->getFriendIds($user, $cursor, $string_ids, $count),
  129. $this->equalTo(json_decode($this->sampleString))
  130. );
  131. }
  132. /**
  133. * Tests the getFriendIds method - failure
  134. *
  135. * @param mixed $user Either an integer containing the user ID or a string containing the screen name.
  136. *
  137. * @return void
  138. *
  139. * @dataProvider seedUser
  140. * @since 1.0
  141. * @expectedException DomainException
  142. */
  143. public function testGetFriendIdsFailure($user)
  144. {
  145. $cursor = 123;
  146. $string_ids = true;
  147. $count = 5;
  148. $returnData = new stdClass;
  149. $returnData->code = 200;
  150. $returnData->body = $this->friendsRateLimit;
  151. $path = $this->object->fetchUrl('/application/rate_limit_status.json', array("resources" => "friends"));
  152. $this->client->expects($this->at(0))
  153. ->method('get')
  154. ->with($path)
  155. ->will($this->returnValue($returnData));
  156. $returnData = new stdClass;
  157. $returnData->code = 500;
  158. $returnData->body = $this->errorString;
  159. // Set request parameters.
  160. if (is_numeric($user))
  161. {
  162. $data['user_id'] = $user;
  163. }
  164. elseif (is_string($user))
  165. {
  166. $data['screen_name'] = $user;
  167. }
  168. else
  169. {
  170. $this->setExpectedException('RuntimeException');
  171. $this->object->getFriendIds($user, $cursor, $string_ids, $count);
  172. }
  173. $data['cursor'] = $cursor;
  174. $data['stringify_ids'] = $string_ids;
  175. $data['count'] = $count;
  176. $path = $this->object->fetchUrl('/friends/ids.json', $data);
  177. $this->client->expects($this->at(1))
  178. ->method('get')
  179. ->with($path)
  180. ->will($this->returnValue($returnData));
  181. $this->object->getFriendIds($user, $cursor, $string_ids, $count);
  182. }
  183. /**
  184. * Provides test data for request format detection.
  185. *
  186. * @return array
  187. *
  188. * @since 1.0
  189. */
  190. public function seedFriendshipDetails()
  191. {
  192. // User IDs or screen names
  193. return array(
  194. array(234654235457, 2334657563),
  195. array(234654235457, 'userTest'),
  196. array('testUser', 2334657563),
  197. array('testUser', 'userTest'),
  198. array('testUser', null),
  199. array(null, 'userTest')
  200. );
  201. }
  202. /**
  203. * Tests the getFriendshipDetails method
  204. *
  205. * @param mixed $user_a Either an integer containing the user ID or a string containing the screen name of the first user.
  206. * @param mixed $user_b Either an integer containing the user ID or a string containing the screen name of the second user.
  207. *
  208. * @dataProvider seedFriendshipDetails
  209. * @return void
  210. *
  211. * @since 1.0
  212. */
  213. public function testGetFriendshipDetails($user_a, $user_b)
  214. {
  215. $returnData = new stdClass;
  216. $returnData->code = 200;
  217. $returnData->body = $this->friendshipsRateLimit;
  218. $path = $this->object->fetchUrl('/application/rate_limit_status.json', array("resources" => "friendships"));
  219. $this->client->expects($this->at(0))
  220. ->method('get')
  221. ->with($path)
  222. ->will($this->returnValue($returnData));
  223. $returnData = new stdClass;
  224. $returnData->code = 200;
  225. $returnData->body = $this->sampleString;
  226. // Set request parameters.
  227. if (is_numeric($user_a))
  228. {
  229. $data['source_id'] = $user_a;
  230. }
  231. elseif (is_string($user_a))
  232. {
  233. $data['source_screen_name'] = $user_a;
  234. }
  235. else
  236. {
  237. $this->setExpectedException('RuntimeException');
  238. $this->object->getFriendshipDetails($user_a, $user_b);
  239. }
  240. if (is_numeric($user_b))
  241. {
  242. $data['target_id'] = $user_b;
  243. }
  244. elseif (is_string($user_b))
  245. {
  246. $data['target_screen_name'] = $user_b;
  247. }
  248. else
  249. {
  250. $this->setExpectedException('RuntimeException');
  251. $this->object->getFriendshipDetails($user_a, $user_b);
  252. }
  253. $path = $this->object->fetchUrl('/friendships/show.json', $data);
  254. $this->client->expects($this->at(1))
  255. ->method('get')
  256. ->with($path)
  257. ->will($this->returnValue($returnData));
  258. $this->assertThat(
  259. $this->object->getFriendshipDetails($user_a, $user_b),
  260. $this->equalTo(json_decode($this->sampleString))
  261. );
  262. }
  263. /**
  264. * Tests the getFriendshipDetails method - failure
  265. *
  266. * @param mixed $user_a Either an integer containing the user ID or a string containing the screen name of the first user.
  267. * @param mixed $user_b Either an integer containing the user ID or a string containing the screen name of the second user.
  268. *
  269. * @dataProvider seedFriendshipDetails
  270. * @return void
  271. *
  272. * @since 1.0
  273. * @expectedException DomainException
  274. */
  275. public function testGetFriendshipDetailsFailure($user_a, $user_b)
  276. {
  277. $returnData = new stdClass;
  278. $returnData->code = 200;
  279. $returnData->body = $this->friendshipsRateLimit;
  280. $path = $this->object->fetchUrl('/application/rate_limit_status.json', array("resources" => "friendships"));
  281. $this->client->expects($this->at(0))
  282. ->method('get')
  283. ->with($path)
  284. ->will($this->returnValue($returnData));
  285. $returnData = new stdClass;
  286. $returnData->code = 500;
  287. $returnData->body = $this->errorString;
  288. // Set request parameters.
  289. if (is_numeric($user_a))
  290. {
  291. $data['source_id'] = $user_a;
  292. }
  293. elseif (is_string($user_a))
  294. {
  295. $data['source_screen_name'] = $user_a;
  296. }
  297. else
  298. {
  299. $this->setExpectedException('RuntimeException');
  300. $this->object->getFriendshipDetails($user_a, $user_b);
  301. }
  302. if (is_numeric($user_b))
  303. {
  304. $data['target_id'] = $user_b;
  305. }
  306. elseif (is_string($user_b))
  307. {
  308. $data['target_screen_name'] = $user_b;
  309. }
  310. else
  311. {
  312. $this->setExpectedException('RuntimeException');
  313. $this->object->getFriendshipDetails($user_a, $user_b);
  314. }
  315. $path = $this->object->fetchUrl('/friendships/show.json', $data);
  316. $this->client->expects($this->at(1))
  317. ->method('get')
  318. ->with($path)
  319. ->will($this->returnValue($returnData));
  320. $this->object->getFriendshipDetails($user_a, $user_b);
  321. }
  322. /**
  323. * Tests the getFollowerIds method
  324. *
  325. * @param mixed $user Either an integer containing the user ID or a string containing the screen name.
  326. *
  327. * @return void
  328. *
  329. * @dataProvider seedUser
  330. * @since 1.0
  331. */
  332. public function testGetFollowerIds($user)
  333. {
  334. $cursor = 123;
  335. $string_ids = true;
  336. $count = 5;
  337. $returnData = new stdClass;
  338. $returnData->code = 200;
  339. $returnData->body = $this->followersRateLimit;
  340. $path = $this->object->fetchUrl('/application/rate_limit_status.json', array("resources" => "followers"));
  341. $this->client->expects($this->at(0))
  342. ->method('get')
  343. ->with($path)
  344. ->will($this->returnValue($returnData));
  345. $returnData = new stdClass;
  346. $returnData->code = 200;
  347. $returnData->body = $this->sampleString;
  348. // Set request parameters.
  349. if (is_numeric($user))
  350. {
  351. $data['user_id'] = $user;
  352. }
  353. elseif (is_string($user))
  354. {
  355. $data['screen_name'] = $user;
  356. }
  357. else
  358. {
  359. $this->setExpectedException('RuntimeException');
  360. $this->object->getFollowerIds($user, $cursor, $string_ids, $count);
  361. }
  362. $data['cursor'] = $cursor;
  363. $data['stringify_ids'] = $string_ids;
  364. $data['count'] = $count;
  365. $path = $this->object->fetchUrl('/followers/ids.json', $data);
  366. $this->client->expects($this->at(1))
  367. ->method('get')
  368. ->with($path)
  369. ->will($this->returnValue($returnData));
  370. $this->assertThat(
  371. $this->object->getFollowerIds($user, $cursor, $string_ids, $count),
  372. $this->equalTo(json_decode($this->sampleString))
  373. );
  374. }
  375. /**
  376. * Tests the getFollowerIds method - failure
  377. *
  378. * @param mixed $user Either an integer containing the user ID or a string containing the screen name.
  379. *
  380. * @return void
  381. *
  382. * @dataProvider seedUser
  383. * @since 1.0
  384. * @expectedException DomainException
  385. */
  386. public function testGetFollowerIdsFailure($user)
  387. {
  388. $cursor = 123;
  389. $string_ids = true;
  390. $count = 5;
  391. $returnData = new stdClass;
  392. $returnData->code = 200;
  393. $returnData->body = $this->followersRateLimit;
  394. $path = $this->object->fetchUrl('/application/rate_limit_status.json', array("resources" => "followers"));
  395. $this->client->expects($this->at(0))
  396. ->method('get')
  397. ->with($path)
  398. ->will($this->returnValue($returnData));
  399. $returnData = new stdClass;
  400. $returnData->code = 500;
  401. $returnData->body = $this->errorString;
  402. // Set request parameters.
  403. if (is_numeric($user))
  404. {
  405. $data['user_id'] = $user;
  406. }
  407. elseif (is_string($user))
  408. {
  409. $data['screen_name'] = $user;
  410. }
  411. else
  412. {
  413. $this->setExpectedException('RuntimeException');
  414. $this->object->getFollowerIds($user, $cursor, $string_ids, $count);
  415. }
  416. $data['cursor'] = $cursor;
  417. $data['stringify_ids'] = $string_ids;
  418. $data['count'] = $count;
  419. $path = $this->object->fetchUrl('/followers/ids.json', $data);
  420. $this->client->expects($this->at(1))
  421. ->method('get')
  422. ->with($path)
  423. ->will($this->returnValue($returnData));
  424. $this->object->getFollowerIds($user, $cursor, $string_ids, $count);
  425. }
  426. /**
  427. * Tests the getFriendshipsIncoming method
  428. *
  429. * @return void
  430. *
  431. * @since 1.0
  432. */
  433. public function testGetFriendshipsIncoming()
  434. {
  435. $cursor = 1234;
  436. $string_ids = true;
  437. $returnData = new stdClass;
  438. $returnData->code = 200;
  439. $returnData->body = $this->friendshipsRateLimit;
  440. $path = $this->object->fetchUrl('/application/rate_limit_status.json', array("resources" => "friendships"));
  441. $this->client->expects($this->at(0))
  442. ->method('get')
  443. ->with($path)
  444. ->will($this->returnValue($returnData));
  445. $returnData = new stdClass;
  446. $returnData->code = 200;
  447. $returnData->body = $this->sampleString;
  448. $data['cursor'] = $cursor;
  449. $data['stringify_ids'] = $string_ids;
  450. $path = $this->object->fetchUrl('/friendships/incoming.json', $data);
  451. $this->client->expects($this->at(1))
  452. ->method('get')
  453. ->with($path)
  454. ->will($this->returnValue($returnData));
  455. $this->assertThat(
  456. $this->object->getFriendshipsIncoming($cursor, $string_ids),
  457. $this->equalTo(json_decode($this->sampleString))
  458. );
  459. }
  460. /**
  461. * Tests the getFriendshipsIncoming method - failure
  462. *
  463. * @return void
  464. *
  465. * @since 1.0
  466. * @expectedException DomainException
  467. */
  468. public function testGetFriendshipsIncomingFailure()
  469. {
  470. $cursor = 1243;
  471. $string_ids = true;
  472. $returnData = new stdClass;
  473. $returnData->code = 200;
  474. $returnData->body = $this->friendshipsRateLimit;
  475. $path = $this->object->fetchUrl('/application/rate_limit_status.json', array("resources" => "friendships"));
  476. $this->client->expects($this->at(0))
  477. ->method('get')
  478. ->with($path)
  479. ->will($this->returnValue($returnData));
  480. $returnData = new stdClass;
  481. $returnData->code = 500;
  482. $returnData->body = $this->errorString;
  483. $data['cursor'] = $cursor;
  484. $data['stringify_ids'] = $string_ids;
  485. $path = $this->object->fetchUrl('/friendships/incoming.json', $data);
  486. $this->client->expects($this->at(1))
  487. ->method('get')
  488. ->with($path)
  489. ->will($this->returnValue($returnData));
  490. $this->object->getFriendshipsIncoming($cursor, $string_ids);
  491. }
  492. /**
  493. * Tests the getFriendshipsOutgoing method
  494. *
  495. * @return void
  496. *
  497. * @since 1.0
  498. */
  499. public function testGetFriendshipsOutgoing()
  500. {
  501. $cursor = 12344;
  502. $string_ids = true;
  503. $returnData = new stdClass;
  504. $returnData->code = 200;
  505. $returnData->body = $this->friendshipsRateLimit;
  506. $path = $this->object->fetchUrl('/application/rate_limit_status.json', array("resources" => "friendships"));
  507. $this->client->expects($this->at(0))
  508. ->method('get')
  509. ->with($path)
  510. ->will($this->returnValue($returnData));
  511. $returnData = new stdClass;
  512. $returnData->code = 200;
  513. $returnData->body = $this->sampleString;
  514. $data['cursor'] = $cursor;
  515. $data['stringify_ids'] = $string_ids;
  516. $path = $this->object->fetchUrl('/friendships/outgoing.json', $data);
  517. $this->client->expects($this->at(1))
  518. ->method('get')
  519. ->with($path)
  520. ->will($this->returnValue($returnData));
  521. $this->assertThat(
  522. $this->object->getFriendshipsOutgoing($cursor, $string_ids),
  523. $this->equalTo(json_decode($this->sampleString))
  524. );
  525. }
  526. /**
  527. * Tests the getFriendshipsOutgoing method - failure
  528. *
  529. * @return void
  530. *
  531. * @since 1.0
  532. * @expectedException DomainException
  533. */
  534. public function testGetFriendshipsOutgoingFailure()
  535. {
  536. $cursor = 1234;
  537. $string_ids = true;
  538. $returnData = new stdClass;
  539. $returnData->code = 200;
  540. $returnData->body = $this->friendshipsRateLimit;
  541. $path = $this->object->fetchUrl('/application/rate_limit_status.json', array("resources" => "friendships"));
  542. $this->client->expects($this->at(0))
  543. ->method('get')
  544. ->with($path)
  545. ->will($this->returnValue($returnData));
  546. $returnData = new stdClass;
  547. $returnData->code = 500;
  548. $returnData->body = $this->errorString;
  549. $data['cursor'] = $cursor;
  550. $data['stringify_ids'] = $string_ids;
  551. $path = $this->object->fetchUrl('/friendships/outgoing.json', $data);
  552. $this->client->expects($this->at(1))
  553. ->method('get')
  554. ->with($path)
  555. ->will($this->returnValue($returnData));
  556. $this->object->getFriendshipsOutgoing($cursor, $string_ids);
  557. }
  558. /**
  559. * Provides test data for request format detection.
  560. *
  561. * @return array
  562. *
  563. * @since 1.0
  564. */
  565. public function seedFriendship()
  566. {
  567. // User ID or screen name
  568. return array(
  569. array('234654235457'),
  570. array('testUser'),
  571. array(null)
  572. );
  573. }
  574. /**
  575. * Tests the follow method
  576. *
  577. * @param mixed $user Either an integer containing the user ID or a string containing the screen name.
  578. *
  579. * @return void
  580. *
  581. * @dataProvider seedFriendship
  582. *
  583. * @since 1.0
  584. */
  585. public function testFollow($user)
  586. {
  587. $follow = true;
  588. $returnData = new stdClass;
  589. $returnData->code = 200;
  590. $returnData->body = $this->sampleString;
  591. // Set POST request parameters.
  592. if (is_numeric($user))
  593. {
  594. $data['user_id'] = $user;
  595. }
  596. elseif (is_string($user))
  597. {
  598. $data['screen_name'] = $user;
  599. }
  600. else
  601. {
  602. $this->setExpectedException('RuntimeException');
  603. $this->object->follow($user, $follow);
  604. }
  605. $data['follow'] = $follow;
  606. $this->client->expects($this->once())
  607. ->method('post')
  608. ->with('/friendships/create.json', $data)
  609. ->will($this->returnValue($returnData));
  610. $this->assertThat(
  611. $this->object->follow($user, $follow),
  612. $this->equalTo(json_decode($this->sampleString))
  613. );
  614. }
  615. /**
  616. * Tests the follow method - failure
  617. *
  618. * @param mixed $user Either an integer containing the user ID or a string containing the screen name.
  619. *
  620. * @return void
  621. *
  622. * @dataProvider seedFriendship
  623. *
  624. * @since 1.0
  625. * @expectedException DomainException
  626. */
  627. public function testFollowFailure($user)
  628. {
  629. $returnData = new stdClass;
  630. $returnData->code = 500;
  631. $returnData->body = $this->errorString;
  632. // Set POST request parameters.
  633. if (is_numeric($user))
  634. {
  635. $data['user_id'] = $user;
  636. }
  637. elseif (is_string($user))
  638. {
  639. $data['screen_name'] = $user;
  640. }
  641. else
  642. {
  643. $this->setExpectedException('RuntimeException');
  644. $this->object->follow($user);
  645. }
  646. $this->client->expects($this->once())
  647. ->method('post')
  648. ->with('/friendships/create.json', $data)
  649. ->will($this->returnValue($returnData));
  650. $this->object->follow($user);
  651. }
  652. /**
  653. * Tests the unfollow method
  654. *
  655. * @param mixed $user Either an integer containing the user ID or a string containing the screen name.
  656. *
  657. * @return void
  658. *
  659. * @dataProvider seedFriendship
  660. *
  661. * @since 1.0
  662. */
  663. public function testUnfollow($user)
  664. {
  665. $returnData = new stdClass;
  666. $returnData->code = 200;
  667. $returnData->body = $this->sampleString;
  668. // Set POST request parameters.
  669. if (is_numeric($user))
  670. {
  671. $data['user_id'] = $user;
  672. }
  673. elseif (is_string($user))
  674. {
  675. $data['screen_name'] = $user;
  676. }
  677. else
  678. {
  679. $this->setExpectedException('RuntimeException');
  680. $this->object->unfollow($user);
  681. }
  682. $this->client->expects($this->once())
  683. ->method('post')
  684. ->with('/friendships/destroy.json', $data)
  685. ->will($this->returnValue($returnData));
  686. $this->assertThat(
  687. $this->object->unfollow($user),
  688. $this->equalTo(json_decode($this->sampleString))
  689. );
  690. }
  691. /**
  692. * Tests the unfollow method - failure
  693. *
  694. * @param mixed $user Either an integer containing the user ID or a string containing the screen name.
  695. *
  696. * @return void
  697. *
  698. * @dataProvider seedFriendship
  699. *
  700. * @since 1.0
  701. * @expectedException DomainException
  702. */
  703. public function testUnfollowFailure($user)
  704. {
  705. $returnData = new stdClass;
  706. $returnData->code = 500;
  707. $returnData->body = $this->errorString;
  708. // Set POST request parameters.
  709. if (is_numeric($user))
  710. {
  711. $data['user_id'] = $user;
  712. }
  713. elseif (is_string($user))
  714. {
  715. $data['screen_name'] = $user;
  716. }
  717. else
  718. {
  719. $this->setExpectedException('RuntimeException');
  720. $this->object->unfollow($user);
  721. }
  722. $this->client->expects($this->once())
  723. ->method('post')
  724. ->with('/friendships/destroy.json', $data)
  725. ->will($this->returnValue($returnData));
  726. $this->object->unfollow($user);
  727. }
  728. /**
  729. * Provides test data for request format detection.
  730. *
  731. * @return array
  732. *
  733. * @since 1.0
  734. */
  735. public function seedFriendshipsLookup()
  736. {
  737. // User ID and screen name
  738. return array(
  739. array(null, '234654235457'),
  740. array(null, '234654235457,245864573437'),
  741. array('testUser', null),
  742. array('testUser', '234654235457'),
  743. array(null, null)
  744. );
  745. }
  746. /**
  747. * Tests the getFriendshipsLookup method
  748. *
  749. * @param string $screen_name A comma separated list of screen names, up to 100 are allowed in a single request.
  750. * @param string $id A comma separated list of user IDs, up to 100 are allowed in a single request.
  751. *
  752. * @return void
  753. *
  754. * @since 1.0
  755. * @dataProvider seedFriendshipsLookup
  756. */
  757. public function testGetFriendshipsLookup($screen_name, $id)
  758. {
  759. $returnData = new stdClass;
  760. $returnData->code = 200;
  761. $returnData->body = $this->friendshipsRateLimit;
  762. $path = $this->object->fetchUrl('/application/rate_limit_status.json', array("resources" => "friendships"));
  763. $this->client->expects($this->at(0))
  764. ->method('get')
  765. ->with($path)
  766. ->will($this->returnValue($returnData));
  767. $returnData = new stdClass;
  768. $returnData->code = 200;
  769. $returnData->body = $this->sampleString;
  770. if ($id)
  771. {
  772. $data['user_id'] = $id;
  773. }
  774. if ($screen_name)
  775. {
  776. $data['screen_name'] = $screen_name;
  777. }
  778. if ($id == null && $screen_name == null)
  779. {
  780. $this->setExpectedException('RuntimeException');
  781. $this->object->getFriendshipsLookup($screen_name, $id);
  782. }
  783. $path = $this->oauth->toUrl('/friendships/lookup.json', $data);
  784. $this->client->expects($this->at(1))
  785. ->method('get')
  786. ->with($path)
  787. ->will($this->returnValue($returnData));
  788. $this->assertThat(
  789. $this->object->getFriendshipsLookup($screen_name, $id),
  790. $this->equalTo(json_decode($this->sampleString))
  791. );
  792. }
  793. /**
  794. * Tests the getFriendshipsLookup method - failure
  795. *
  796. * @param string $screen_name A comma separated list of screen names, up to 100 are allowed in a single request.
  797. * @param string $id A comma separated list of user IDs, up to 100 are allowed in a single request.
  798. *
  799. * @return void
  800. *
  801. * @since 1.0
  802. * @dataProvider seedFriendshipsLookup
  803. * @expectedException DomainException
  804. */
  805. public function testGetFriendshipsLookupFailure($screen_name, $id)
  806. {
  807. $returnData = new stdClass;
  808. $returnData->code = 200;
  809. $returnData->body = $this->friendshipsRateLimit;
  810. $path = $this->object->fetchUrl('/application/rate_limit_status.json', array("resources" => "friendships"));
  811. $this->client->expects($this->at(0))
  812. ->method('get')
  813. ->with($path)
  814. ->will($this->returnValue($returnData));
  815. $returnData = new stdClass;
  816. $returnData->code = 500;
  817. $returnData->body = $this->errorString;
  818. if ($id)
  819. {
  820. $data['user_id'] = $id;
  821. }
  822. if ($screen_name)
  823. {
  824. $data['screen_name'] = $screen_name;
  825. }
  826. if ($id == null && $screen_name == null)
  827. {
  828. $this->setExpectedException('RuntimeException');
  829. $this->object->getFriendshipsLookup($screen_name, $id);
  830. }
  831. $path = $this->oauth->toUrl('/friendships/lookup.json', $data);
  832. $this->client->expects($this->at(1))
  833. ->method('get')
  834. ->with($path)
  835. ->will($this->returnValue($returnData));
  836. $this->object->getFriendshipsLookup($screen_name, $id);
  837. }
  838. /**
  839. * Tests the updateFriendship method
  840. *
  841. * @param mixed $user Either an integer containing the user ID or a string containing the screen name.
  842. *
  843. * @return void
  844. *
  845. * @dataProvider seedFriendship
  846. *
  847. * @since 1.0
  848. */
  849. public function testUpdateFriendship($user)
  850. {
  851. $device = true;
  852. $retweets = true;
  853. $returnData = new stdClass;
  854. $returnData->code = 200;
  855. $returnData->body = $this->sampleString;
  856. // Set POST request parameters.
  857. if (is_numeric($user))
  858. {
  859. $data['user_id'] = $user;
  860. }
  861. elseif (is_string($user))
  862. {
  863. $data['screen_name'] = $user;
  864. }
  865. else
  866. {
  867. $this->setExpectedException('RuntimeException');
  868. $this->object->updateFriendship($user, $device, $retweets);
  869. }
  870. $data['device'] = $device;
  871. $data['retweets'] = $retweets;
  872. $this->client->expects($this->once())
  873. ->method('post')
  874. ->with('/friendships/update.json', $data)
  875. ->will($this->returnValue($returnData));
  876. $this->assertThat(
  877. $this->object->updateFriendship($user, $device, $retweets),
  878. $this->equalTo(json_decode($this->sampleString))
  879. );
  880. }
  881. /**
  882. * Tests the updateFriendship method - failure
  883. *
  884. * @param mixed $user Either an integer containing the user ID or a string containing the screen name.
  885. *
  886. * @return void
  887. *
  888. * @dataProvider seedFriendship
  889. *
  890. * @since 1.0
  891. * @expectedException DomainException
  892. */
  893. public function testUpdateFriendshipFailure($user)
  894. {
  895. $returnData = new stdClass;
  896. $returnData->code = 500;
  897. $returnData->body = $this->errorString;
  898. // Set POST request parameters.
  899. if (is_numeric($user))
  900. {
  901. $data['user_id'] = $user;
  902. }
  903. elseif (is_string($user))
  904. {
  905. $data['screen_name'] = $user;
  906. }
  907. else
  908. {
  909. $this->setExpectedException('RuntimeException');
  910. $this->object->updateFriendship($user);
  911. }
  912. $this->client->expects($this->once())
  913. ->method('post')
  914. ->with('/friendships/update.json', $data)
  915. ->will($this->returnValue($returnData));
  916. $this->object->updateFriendship($user);
  917. }
  918. /**
  919. * Tests the getFriendshipNoRetweetIds method
  920. *
  921. * @return void
  922. *
  923. * @since 1.0
  924. */
  925. public function testGetFriendshipNoRetweetIds()
  926. {
  927. $string_ids = true;
  928. $returnData = new stdClass;
  929. $returnData->code = 200;
  930. $returnData->body = $this->friendshipsRateLimit;
  931. $path = $this->object->fetchUrl('/application/rate_limit_status.json', array("resources" => "friendships"));
  932. $this->client->expects($this->at(0))
  933. ->method('get')
  934. ->with($path)
  935. ->will($this->returnValue($returnData));
  936. $returnData = new stdClass;
  937. $returnData->code = 200;
  938. $returnData->body = $this->sampleString;
  939. $data['stringify_ids'] = $string_ids;
  940. $path = $this->object->fetchUrl('/friendships/no_retweets/ids.json', $data);
  941. $this->client->expects($this->at(1))
  942. ->method('get')
  943. ->with($path)
  944. ->will($this->returnValue($returnData));
  945. $this->assertThat(
  946. $this->object->getFriendshipNoRetweetIds($string_ids),
  947. $this->equalTo(json_decode($this->sampleString))
  948. );
  949. }
  950. /**
  951. * Tests the getFriendshipNoRetweetIds method - failure
  952. *
  953. * @return void
  954. *
  955. * @since 1.0
  956. * @expectedException DomainException
  957. */
  958. public function testGetFriendshipNoRetweetIdsFailure()
  959. {
  960. $string_ids = true;
  961. $returnData = new stdClass;
  962. $returnData->code = 200;
  963. $returnData->body = $this->friendshipsRateLimit;
  964. $path = $this->object->fetchUrl('/application/rate_limit_status.json', array("resources" => "friendships"));
  965. $this->client->expects($this->at(0))
  966. ->method('get')
  967. ->with($path)
  968. ->will($this->returnValue($returnData));
  969. $returnData = new stdClass;
  970. $returnData->code = 500;
  971. $returnData->body = $this->errorString;
  972. $data['stringify_ids'] = $string_ids;
  973. $path = $this->object->fetchUrl('/friendships/no_retweets/ids.json', $data);
  974. $this->client->expects($this->at(1))
  975. ->method('get')
  976. ->with($path)
  977. ->will($this->returnValue($returnData));
  978. $this->object->getFriendshipNoRetweetIds($string_ids);
  979. }
  980. }