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.

150 lines
6.3 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 testBasicTestStuff() {
  34. list($url, $json) = $this->loadTweet('818913630569664512');
  35. $data = $this->parse(['url' => $url, 'json' => $json]);
  36. $this->assertEquals('entry', $data['data']['type']);
  37. $this->assertEquals('A tweet with a URL https://indieweb.org/ #and #some #hashtags', $data['data']['content']['text']);
  38. $this->assertContains('and', $data['data']['category']);
  39. $this->assertContains('some', $data['data']['category']);
  40. $this->assertContains('hashtags', $data['data']['category']);
  41. // Published date should be set to the timezone of the user
  42. $this->assertEquals('2017-01-10T12:13:18-08:00', $data['data']['published']);
  43. }
  44. public function testPositiveTimezone() {
  45. list($url, $json) = $this->loadTweet('719914707566649344');
  46. $data = $this->parse(['url' => $url, 'json' => $json]);
  47. $this->assertEquals("2016-04-12T16:46:56+01:00", $data['data']['published']);
  48. }
  49. public function testTweetWithEmoji() {
  50. list($url, $json) = $this->loadTweet('818943244553699328');
  51. $data = $this->parse(['url' => $url, 'json' => $json]);
  52. $this->assertEquals('entry', $data['data']['type']);
  53. $this->assertEquals('Here 🎉 have an emoji', $data['data']['content']['text']);
  54. }
  55. public function testHTMLEscaping() {
  56. list($url, $json) = $this->loadTweet('818928092383166465');
  57. $data = $this->parse(['url' => $url, 'json' => $json]);
  58. $this->assertEquals('entry', $data['data']['type']);
  59. $this->assertEquals('Double escaping &amp; & amp', $data['data']['content']['text']);
  60. }
  61. public function testTweetWithPhoto() {
  62. list($url, $json) = $this->loadTweet('818912506496229376');
  63. $data = $this->parse(['url' => $url, 'json' => $json]);
  64. $this->assertEquals('entry', $data['data']['type']);
  65. $this->assertEquals('Tweet with a photo and a location', $data['data']['content']['text']);
  66. $this->assertEquals('https://pbs.twimg.com/media/C11cfRJUoAI26h9.jpg', $data['data']['photo'][0]);
  67. }
  68. public function testTweetWithTwoPhotos() {
  69. list($url, $json) = $this->loadTweet('818935308813103104');
  70. $data = $this->parse(['url' => $url, 'json' => $json]);
  71. $this->assertEquals('entry', $data['data']['type']);
  72. $this->assertEquals('Two photos', $data['data']['content']['text']);
  73. $this->assertContains('https://pbs.twimg.com/media/C11xS1wUcAAeaKF.jpg', $data['data']['photo']);
  74. $this->assertContains('https://pbs.twimg.com/media/C11wtndUoAE1WfE.jpg', $data['data']['photo']);
  75. }
  76. public function testTweetWithVideo() {
  77. list($url, $json) = $this->loadTweet('818913178260160512');
  78. $data = $this->parse(['url' => $url, 'json' => $json]);
  79. $this->assertEquals('entry', $data['data']['type']);
  80. $this->assertEquals('Tweet with a video', $data['data']['content']['text']);
  81. $this->assertEquals('https://video.twimg.com/ext_tw_video/818913089248595970/pr/vid/1280x720/qP-sDx-Q0Hs-ckVv.mp4', $data['data']['video'][0]);
  82. }
  83. public function testTweetWithLocation() {
  84. list($url, $json) = $this->loadTweet('818912506496229376');
  85. $data = $this->parse(['url' => $url, 'json' => $json]);
  86. $this->assertEquals('entry', $data['data']['type']);
  87. $this->assertEquals('Tweet with a photo and a location', $data['data']['content']['text']);
  88. $this->assertEquals('https://api.twitter.com/1.1/geo/id/ac88a4f17a51c7fc.json', $data['data']['location']);
  89. $location = $data['refs']['https://api.twitter.com/1.1/geo/id/ac88a4f17a51c7fc.json'];
  90. $this->assertEquals('adr', $location['type']);
  91. $this->assertEquals('Portland', $location['locality']);
  92. $this->assertEquals('United States', $location['country-name']);
  93. $this->assertEquals('Portland, OR', $location['name']);
  94. }
  95. public function testRetweet() {
  96. list($url, $json) = $this->loadTweet('818913351623245824');
  97. $data = $this->parse(['url' => $url, 'json' => $json]);
  98. $this->assertEquals('entry', $data['data']['type']);
  99. $this->assertArrayNotHasKey('content', $data['data']);
  100. $repostOf = 'https://twitter.com/aaronpk/status/817414679131660288';
  101. $this->assertEquals($repostOf, $data['data']['repost-of']);
  102. $tweet = $data['refs'][$repostOf];
  103. $this->assertEquals('Yeah that\'s me http://xkcd.com/1782/', $tweet['content']['text']);
  104. }
  105. public function testQuotedTweet() {
  106. list($url, $json) = $this->loadTweet('818913488609251331');
  107. $data = $this->parse(['url' => $url, 'json' => $json]);
  108. $this->assertEquals('entry', $data['data']['type']);
  109. $this->assertEquals('Quoted tweet with a #hashtag https://twitter.com/aaronpk/status/817414679131660288', $data['data']['content']['text']);
  110. $tweet = $data['refs']['https://twitter.com/aaronpk/status/817414679131660288'];
  111. $this->assertEquals('Yeah that\'s me http://xkcd.com/1782/', $tweet['content']['text']);
  112. }
  113. }