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.

143 lines
6.0 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 testTweetWithEmoji() {
  45. list($url, $json) = $this->loadTweet('818943244553699328');
  46. $data = $this->parse(['url' => $url, 'json' => $json]);
  47. $this->assertEquals('entry', $data['data']['type']);
  48. $this->assertEquals('Here 🎉 have an emoji', $data['data']['content']['text']);
  49. }
  50. public function testHTMLEscaping() {
  51. list($url, $json) = $this->loadTweet('818928092383166465');
  52. $data = $this->parse(['url' => $url, 'json' => $json]);
  53. $this->assertEquals('entry', $data['data']['type']);
  54. $this->assertEquals('Double escaping &amp; & amp', $data['data']['content']['text']);
  55. }
  56. public function testTweetWithPhoto() {
  57. list($url, $json) = $this->loadTweet('818912506496229376');
  58. $data = $this->parse(['url' => $url, 'json' => $json]);
  59. $this->assertEquals('entry', $data['data']['type']);
  60. $this->assertEquals('Tweet with a photo and a location', $data['data']['content']['text']);
  61. $this->assertEquals('https://pbs.twimg.com/media/C11cfRJUoAI26h9.jpg', $data['data']['photo'][0]);
  62. }
  63. public function testTweetWithTwoPhotos() {
  64. list($url, $json) = $this->loadTweet('818935308813103104');
  65. $data = $this->parse(['url' => $url, 'json' => $json]);
  66. $this->assertEquals('entry', $data['data']['type']);
  67. $this->assertEquals('Two photos', $data['data']['content']['text']);
  68. $this->assertContains('https://pbs.twimg.com/media/C11xS1wUcAAeaKF.jpg', $data['data']['photo']);
  69. $this->assertContains('https://pbs.twimg.com/media/C11wtndUoAE1WfE.jpg', $data['data']['photo']);
  70. }
  71. public function testTweetWithVideo() {
  72. list($url, $json) = $this->loadTweet('818913178260160512');
  73. $data = $this->parse(['url' => $url, 'json' => $json]);
  74. $this->assertEquals('entry', $data['data']['type']);
  75. $this->assertEquals('Tweet with a video', $data['data']['content']['text']);
  76. $this->assertEquals('https://video.twimg.com/ext_tw_video/818913089248595970/pr/vid/1280x720/qP-sDx-Q0Hs-ckVv.mp4', $data['data']['video'][0]);
  77. }
  78. public function testTweetWithLocation() {
  79. list($url, $json) = $this->loadTweet('818912506496229376');
  80. $data = $this->parse(['url' => $url, 'json' => $json]);
  81. $this->assertEquals('entry', $data['data']['type']);
  82. $this->assertEquals('Tweet with a photo and a location', $data['data']['content']['text']);
  83. $this->assertEquals('https://api.twitter.com/1.1/geo/id/ac88a4f17a51c7fc.json', $data['data']['location']);
  84. $location = $data['refs']['https://api.twitter.com/1.1/geo/id/ac88a4f17a51c7fc.json'];
  85. $this->assertEquals('adr', $location['type']);
  86. $this->assertEquals('Portland', $location['locality']);
  87. $this->assertEquals('United States', $location['country-name']);
  88. $this->assertEquals('Portland, OR', $location['name']);
  89. }
  90. public function testRetweet() {
  91. list($url, $json) = $this->loadTweet('818913351623245824');
  92. $data = $this->parse(['url' => $url, 'json' => $json]);
  93. $this->assertEquals('entry', $data['data']['type']);
  94. $this->assertArrayNotHasKey('content', $data['data']);
  95. $repostOf = 'https://twitter.com/aaronpk/status/817414679131660288';
  96. $this->assertEquals($repostOf, $data['data']['repost-of']);
  97. $tweet = $data['refs'][$repostOf];
  98. $this->assertEquals('Yeah that\'s me http://xkcd.com/1782/', $tweet['content']['text']);
  99. }
  100. public function testQuotedTweet() {
  101. list($url, $json) = $this->loadTweet('818913488609251331');
  102. $data = $this->parse(['url' => $url, 'json' => $json]);
  103. $this->assertEquals('entry', $data['data']['type']);
  104. $this->assertEquals('Quoted tweet with a #hashtag https://twitter.com/aaronpk/status/817414679131660288', $data['data']['content']['text']);
  105. $tweet = $data['refs']['https://twitter.com/aaronpk/status/817414679131660288'];
  106. $this->assertEquals('Yeah that\'s me http://xkcd.com/1782/', $tweet['content']['text']);
  107. }
  108. }