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.

120 lines
4.7 KiB

  1. <?php
  2. use Symfony\Component\HttpFoundation\Request;
  3. use Symfony\Component\HttpFoundation\Response;
  4. class ParseTest extends PHPUnit_Framework_TestCase {
  5. private $http;
  6. public function setUp() {
  7. $this->client = new Parse();
  8. $this->client->http = new p3k\HTTPTest(dirname(__FILE__).'/data/');
  9. }
  10. private function parse($params) {
  11. $request = new Request($params);
  12. $response = new Response();
  13. return $this->client->parse($request, $response);
  14. }
  15. public function testMissingURL() {
  16. $response = $this->parse([]);
  17. $body = $response->getContent();
  18. $this->assertEquals(400, $response->getStatusCode());
  19. $data = json_decode($body);
  20. $this->assertObjectHasAttribute('error', $data);
  21. $this->assertEquals('missing_url', $data->error);
  22. }
  23. public function testInvalidURL() {
  24. $url = 'ftp://example.com/foo';
  25. $response = $this->parse(['url' => $url]);
  26. $body = $response->getContent();
  27. $this->assertEquals(400, $response->getStatusCode());
  28. $data = json_decode($body);
  29. $this->assertObjectHasAttribute('error', $data);
  30. $this->assertEquals('invalid_url', $data->error);
  31. }
  32. public function testTargetNotFound() {
  33. $url = 'http://source.example.com/baseictest';
  34. $response = $this->parse(['url' => $url, 'target' => 'http://example.net']);
  35. $body = $response->getContent();
  36. $this->assertEquals(400, $response->getStatusCode());
  37. $data = json_decode($body);
  38. $this->assertObjectHasAttribute('error', $data);
  39. $this->assertEquals('no_link_found', $data->error);
  40. }
  41. public function testTargetFound() {
  42. $url = 'http://source.example.com/basictest';
  43. $response = $this->parse(['url' => $url, 'target' => 'http://target.example.com']);
  44. $body = $response->getContent();
  45. $this->assertEquals(200, $response->getStatusCode());
  46. $data = json_decode($body);
  47. $this->assertObjectNotHasAttribute('error', $data);
  48. $this->assertObjectNotHasAttribute('error', $data);
  49. }
  50. public function testHTMLContent() {
  51. $url = 'http://source.example.com/html-content';
  52. $response = $this->parse(['url' => $url]);
  53. $body = $response->getContent();
  54. $this->assertEquals(200, $response->getStatusCode());
  55. $data = json_decode($body);
  56. $this->assertObjectNotHasAttribute('name', $data->data);
  57. $this->assertEquals('This page has a link to target.example.com and some formatted text.', $data->data->content->text);
  58. $this->assertEquals('This page has a link to <a href="http://target.example.com">target.example.com</a> and some <b>formatted text</b>.', $data->data->content->html);
  59. }
  60. public function testTextContent() {
  61. $url = 'http://source.example.com/text-content';
  62. $response = $this->parse(['url' => $url]);
  63. $body = $response->getContent();
  64. $this->assertEquals(200, $response->getStatusCode());
  65. $data = json_decode($body);
  66. $this->assertObjectNotHasAttribute('name', $data->data);
  67. $this->assertEquals('This page has a link to target.example.com and some formatted text but is in a p-content element so is plaintext.', $data->data->content->text);
  68. }
  69. public function testContentWithPrefixedName() {
  70. $url = 'http://source.example.com/content-with-prefixed-name';
  71. $response = $this->parse(['url' => $url]);
  72. $body = $response->getContent();
  73. $this->assertEquals(200, $response->getStatusCode());
  74. $data = json_decode($body);
  75. $this->assertObjectNotHasAttribute('name', $data->data);
  76. $this->assertEquals('This page has a link to target.example.com and some formatted text.', $data->data->content->text);
  77. $this->assertEquals('This page has a link to <a href="http://target.example.com">target.example.com</a> and some <b>formatted text</b>.', $data->data->content->html);
  78. }
  79. public function testContentWithDistinctName() {
  80. $url = 'http://source.example.com/content-with-distinct-name';
  81. $response = $this->parse(['url' => $url]);
  82. $body = $response->getContent();
  83. $this->assertEquals(200, $response->getStatusCode());
  84. $data = json_decode($body);
  85. $this->assertEquals('Hello World', $data->data->name);
  86. $this->assertEquals('This page has a link to target.example.com and some formatted text.', $data->data->content->text);
  87. $this->assertEquals('This page has a link to <a href="http://target.example.com">target.example.com</a> and some <b>formatted text</b>.', $data->data->content->html);
  88. }
  89. public function testNameWithNoContent() {
  90. $url = 'http://source.example.com/name-no-content';
  91. $response = $this->parse(['url' => $url]);
  92. $body = $response->getContent();
  93. $this->assertEquals(200, $response->getStatusCode());
  94. $data = json_decode($body);
  95. $this->assertEquals('Hello World', $data->data->name);
  96. $this->assertObjectNotHasAttribute('content', $data->data);
  97. }
  98. }