You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
6.5 KiB

  1. <?php
  2. use Symfony\Component\HttpFoundation\Request;
  3. use Symfony\Component\HttpFoundation\Response;
  4. class TwitterTest extends PHPUnit_Framework_TestCase {
  5. public function setUp() {
  6. $this->client = new Parse();
  7. $this->client->mc = null;
  8. }
  9. private function parse($params) {
  10. $request = new Request($params);
  11. $response = new Response();
  12. $result = $this->client->parse($request, $response);
  13. $body = $result->getContent();
  14. $this->assertEquals(200, $result->getStatusCode());
  15. return json_decode($body, true);
  16. }
  17. private function loadTweet($id) {
  18. $url = 'https://twitter.com/_/status/'.$id;
  19. $json = file_get_contents(dirname(__FILE__).'/data/api.twitter.com/'.$id.'.json');
  20. return [$url, $json];
  21. }
  22. public function testBasicProfileInfo() {
  23. list($url, $json) = $this->loadTweet('818912506496229376');
  24. $data = $this->parse(['url' => $url, 'json' => $json]);
  25. $this->assertEquals('entry', $data['data']['type']);
  26. $this->assertEquals('aaronpk dev', $data['data']['author']['name']);
  27. $this->assertEquals('pkdev', $data['data']['author']['nickname']);
  28. $this->assertEquals('https://aaronparecki.com/', $data['data']['author']['url']);
  29. $this->assertEquals('Portland, OR', $data['data']['author']['location']);
  30. $this->assertEquals('Dev account for testing Twitter things. Follow me here: https://twitter.com/aaronpk', $data['data']['author']['bio']);
  31. $this->assertEquals('https://pbs.twimg.com/profile_images/638125135904436224/qd_d94Qn_normal.jpg', $data['data']['author']['photo']);
  32. }
  33. public function testProfileWithNonExpandedURL() {
  34. list($url, $json) = $this->loadTweet('791704641046052864');
  35. $data = $this->parse(['url' => $url, 'json' => $json]);
  36. $this->assertEquals('http://agiletortoise.com', $data['data']['author']['url']);
  37. }
  38. public function testBasicTestStuff() {
  39. list($url, $json) = $this->loadTweet('818913630569664512');
  40. $data = $this->parse(['url' => $url, 'json' => $json]);
  41. $this->assertEquals('entry', $data['data']['type']);
  42. $this->assertEquals('A tweet with a URL https://indieweb.org/ #and #some #hashtags', $data['data']['content']['text']);
  43. $this->assertContains('and', $data['data']['category']);
  44. $this->assertContains('some', $data['data']['category']);
  45. $this->assertContains('hashtags', $data['data']['category']);
  46. // Published date should be set to the timezone of the user
  47. $this->assertEquals('2017-01-10T12:13:18-08:00', $data['data']['published']);
  48. }
  49. public function testPositiveTimezone() {
  50. list($url, $json) = $this->loadTweet('719914707566649344');
  51. $data = $this->parse(['url' => $url, 'json' => $json]);
  52. $this->assertEquals("2016-04-12T16:46:56+01:00", $data['data']['published']);
  53. }
  54. public function testTweetWithEmoji() {
  55. list($url, $json) = $this->loadTweet('818943244553699328');
  56. $data = $this->parse(['url' => $url, 'json' => $json]);
  57. $this->assertEquals('entry', $data['data']['type']);
  58. $this->assertEquals('Here 🎉 have an emoji', $data['data']['content']['text']);
  59. }
  60. public function testHTMLEscaping() {
  61. list($url, $json) = $this->loadTweet('818928092383166465');
  62. $data = $this->parse(['url' => $url, 'json' => $json]);
  63. $this->assertEquals('entry', $data['data']['type']);
  64. $this->assertEquals('Double escaping &amp; & amp', $data['data']['content']['text']);
  65. }
  66. public function testTweetWithPhoto() {
  67. list($url, $json) = $this->loadTweet('818912506496229376');
  68. $data = $this->parse(['url' => $url, 'json' => $json]);
  69. $this->assertEquals('entry', $data['data']['type']);
  70. $this->assertEquals('Tweet with a photo and a location', $data['data']['content']['text']);
  71. $this->assertEquals('https://pbs.twimg.com/media/C11cfRJUoAI26h9.jpg', $data['data']['photo'][0]);
  72. }
  73. public function testTweetWithTwoPhotos() {
  74. list($url, $json) = $this->loadTweet('818935308813103104');
  75. $data = $this->parse(['url' => $url, 'json' => $json]);
  76. $this->assertEquals('entry', $data['data']['type']);
  77. $this->assertEquals('Two photos', $data['data']['content']['text']);
  78. $this->assertContains('https://pbs.twimg.com/media/C11xS1wUcAAeaKF.jpg', $data['data']['photo']);
  79. $this->assertContains('https://pbs.twimg.com/media/C11wtndUoAE1WfE.jpg', $data['data']['photo']);
  80. }
  81. public function testTweetWithVideo() {
  82. list($url, $json) = $this->loadTweet('818913178260160512');
  83. $data = $this->parse(['url' => $url, 'json' => $json]);
  84. $this->assertEquals('entry', $data['data']['type']);
  85. $this->assertEquals('Tweet with a video', $data['data']['content']['text']);
  86. $this->assertEquals('https://video.twimg.com/ext_tw_video/818913089248595970/pr/vid/1280x720/qP-sDx-Q0Hs-ckVv.mp4', $data['data']['video'][0]);
  87. }
  88. public function testTweetWithLocation() {
  89. list($url, $json) = $this->loadTweet('818912506496229376');
  90. $data = $this->parse(['url' => $url, 'json' => $json]);
  91. $this->assertEquals('entry', $data['data']['type']);
  92. $this->assertEquals('Tweet with a photo and a location', $data['data']['content']['text']);
  93. $this->assertEquals('https://api.twitter.com/1.1/geo/id/ac88a4f17a51c7fc.json', $data['data']['location']);
  94. $location = $data['refs']['https://api.twitter.com/1.1/geo/id/ac88a4f17a51c7fc.json'];
  95. $this->assertEquals('adr', $location['type']);
  96. $this->assertEquals('Portland', $location['locality']);
  97. $this->assertEquals('United States', $location['country-name']);
  98. $this->assertEquals('Portland, OR', $location['name']);
  99. }
  100. public function testRetweet() {
  101. list($url, $json) = $this->loadTweet('818913351623245824');
  102. $data = $this->parse(['url' => $url, 'json' => $json]);
  103. $this->assertEquals('entry', $data['data']['type']);
  104. $this->assertArrayNotHasKey('content', $data['data']);
  105. $repostOf = 'https://twitter.com/aaronpk/status/817414679131660288';
  106. $this->assertEquals($repostOf, $data['data']['repost-of']);
  107. $tweet = $data['refs'][$repostOf];
  108. $this->assertEquals('Yeah that\'s me http://xkcd.com/1782/', $tweet['content']['text']);
  109. }
  110. public function testQuotedTweet() {
  111. list($url, $json) = $this->loadTweet('818913488609251331');
  112. $data = $this->parse(['url' => $url, 'json' => $json]);
  113. $this->assertEquals('entry', $data['data']['type']);
  114. $this->assertEquals('Quoted tweet with a #hashtag https://twitter.com/aaronpk/status/817414679131660288', $data['data']['content']['text']);
  115. $tweet = $data['refs']['https://twitter.com/aaronpk/status/817414679131660288'];
  116. $this->assertEquals('Yeah that\'s me http://xkcd.com/1782/', $tweet['content']['text']);
  117. }
  118. }