LinkTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\DomCrawler\Tests;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\DomCrawler\Link;
  13. class LinkTest extends TestCase
  14. {
  15. public function testConstructorWithANonATag()
  16. {
  17. $this->expectException('LogicException');
  18. $dom = new \DOMDocument();
  19. $dom->loadHTML('<html><div><div></html>');
  20. new Link($dom->getElementsByTagName('div')->item(0), 'http://www.example.com/');
  21. }
  22. public function testConstructorWithAnInvalidCurrentUri()
  23. {
  24. $this->expectException('InvalidArgumentException');
  25. $dom = new \DOMDocument();
  26. $dom->loadHTML('<html><a href="/foo">foo</a></html>');
  27. new Link($dom->getElementsByTagName('a')->item(0), 'example.com');
  28. }
  29. public function testGetNode()
  30. {
  31. $dom = new \DOMDocument();
  32. $dom->loadHTML('<html><a href="/foo">foo</a></html>');
  33. $node = $dom->getElementsByTagName('a')->item(0);
  34. $link = new Link($node, 'http://example.com/');
  35. $this->assertEquals($node, $link->getNode(), '->getNode() returns the node associated with the link');
  36. }
  37. public function testGetMethod()
  38. {
  39. $dom = new \DOMDocument();
  40. $dom->loadHTML('<html><a href="/foo">foo</a></html>');
  41. $node = $dom->getElementsByTagName('a')->item(0);
  42. $link = new Link($node, 'http://example.com/');
  43. $this->assertEquals('GET', $link->getMethod(), '->getMethod() returns the method of the link');
  44. $link = new Link($node, 'http://example.com/', 'post');
  45. $this->assertEquals('POST', $link->getMethod(), '->getMethod() returns the method of the link');
  46. }
  47. /**
  48. * @dataProvider getGetUriTests
  49. */
  50. public function testGetUri($url, $currentUri, $expected)
  51. {
  52. $dom = new \DOMDocument();
  53. $dom->loadHTML(sprintf('<html><a href="%s">foo</a></html>', $url));
  54. $link = new Link($dom->getElementsByTagName('a')->item(0), $currentUri);
  55. $this->assertEquals($expected, $link->getUri());
  56. }
  57. /**
  58. * @dataProvider getGetUriTests
  59. */
  60. public function testGetUriOnArea($url, $currentUri, $expected)
  61. {
  62. $dom = new \DOMDocument();
  63. $dom->loadHTML(sprintf('<html><map><area href="%s" /></map></html>', $url));
  64. $link = new Link($dom->getElementsByTagName('area')->item(0), $currentUri);
  65. $this->assertEquals($expected, $link->getUri());
  66. }
  67. /**
  68. * @dataProvider getGetUriTests
  69. */
  70. public function testGetUriOnLink($url, $currentUri, $expected)
  71. {
  72. $dom = new \DOMDocument();
  73. $dom->loadHTML(sprintf('<html><head><link href="%s" /></head></html>', $url));
  74. $link = new Link($dom->getElementsByTagName('link')->item(0), $currentUri);
  75. $this->assertEquals($expected, $link->getUri());
  76. }
  77. public function getGetUriTests()
  78. {
  79. return [
  80. ['/foo', 'http://localhost/bar/foo/', 'http://localhost/foo'],
  81. ['/foo', 'http://localhost/bar/foo', 'http://localhost/foo'],
  82. ['
  83. /foo', 'http://localhost/bar/foo/', 'http://localhost/foo'],
  84. ['/foo
  85. ', 'http://localhost/bar/foo', 'http://localhost/foo'],
  86. ['foo', 'http://localhost/bar/foo/', 'http://localhost/bar/foo/foo'],
  87. ['foo', 'http://localhost/bar/foo', 'http://localhost/bar/foo'],
  88. ['', 'http://localhost/bar/', 'http://localhost/bar/'],
  89. ['#', 'http://localhost/bar/', 'http://localhost/bar/#'],
  90. ['#bar', 'http://localhost/bar?a=b', 'http://localhost/bar?a=b#bar'],
  91. ['#bar', 'http://localhost/bar/#foo', 'http://localhost/bar/#bar'],
  92. ['?a=b', 'http://localhost/bar#foo', 'http://localhost/bar?a=b'],
  93. ['?a=b', 'http://localhost/bar/', 'http://localhost/bar/?a=b'],
  94. ['http://login.foo.com/foo', 'http://localhost/bar/', 'http://login.foo.com/foo'],
  95. ['https://login.foo.com/foo', 'https://localhost/bar/', 'https://login.foo.com/foo'],
  96. ['mailto:foo@bar.com', 'http://localhost/foo', 'mailto:foo@bar.com'],
  97. // tests schema relative URL (issue #7169)
  98. ['//login.foo.com/foo', 'http://localhost/bar/', 'http://login.foo.com/foo'],
  99. ['//login.foo.com/foo', 'https://localhost/bar/', 'https://login.foo.com/foo'],
  100. ['?foo=2', 'http://localhost?foo=1', 'http://localhost?foo=2'],
  101. ['?foo=2', 'http://localhost/?foo=1', 'http://localhost/?foo=2'],
  102. ['?foo=2', 'http://localhost/bar?foo=1', 'http://localhost/bar?foo=2'],
  103. ['?foo=2', 'http://localhost/bar/?foo=1', 'http://localhost/bar/?foo=2'],
  104. ['?bar=2', 'http://localhost?foo=1', 'http://localhost?bar=2'],
  105. ['foo', 'http://login.foo.com/bar/baz?/query/string', 'http://login.foo.com/bar/foo'],
  106. ['.', 'http://localhost/foo/bar/baz', 'http://localhost/foo/bar/'],
  107. ['./', 'http://localhost/foo/bar/baz', 'http://localhost/foo/bar/'],
  108. ['./foo', 'http://localhost/foo/bar/baz', 'http://localhost/foo/bar/foo'],
  109. ['..', 'http://localhost/foo/bar/baz', 'http://localhost/foo/'],
  110. ['../', 'http://localhost/foo/bar/baz', 'http://localhost/foo/'],
  111. ['../foo', 'http://localhost/foo/bar/baz', 'http://localhost/foo/foo'],
  112. ['../..', 'http://localhost/foo/bar/baz', 'http://localhost/'],
  113. ['../../', 'http://localhost/foo/bar/baz', 'http://localhost/'],
  114. ['../../foo', 'http://localhost/foo/bar/baz', 'http://localhost/foo'],
  115. ['../../foo', 'http://localhost/bar/foo/', 'http://localhost/foo'],
  116. ['../bar/../../foo', 'http://localhost/bar/foo/', 'http://localhost/foo'],
  117. ['../bar/./../../foo', 'http://localhost/bar/foo/', 'http://localhost/foo'],
  118. ['../../', 'http://localhost/', 'http://localhost/'],
  119. ['../../', 'http://localhost', 'http://localhost/'],
  120. ['/foo', 'http://localhost?bar=1', 'http://localhost/foo'],
  121. ['/foo', 'http://localhost#bar', 'http://localhost/foo'],
  122. ['/foo', 'file:///', 'file:///foo'],
  123. ['/foo', 'file:///bar/baz', 'file:///foo'],
  124. ['foo', 'file:///', 'file:///foo'],
  125. ['foo', 'file:///bar/baz', 'file:///bar/foo'],
  126. ];
  127. }
  128. }