LinkTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 Symfony\Component\DomCrawler\Link;
  12. class LinkTest extends \PHPUnit_Framework_TestCase
  13. {
  14. /**
  15. * @expectedException \LogicException
  16. */
  17. public function testConstructorWithANonATag()
  18. {
  19. $dom = new \DOMDocument();
  20. $dom->loadHTML('<html><div><div></html>');
  21. new Link($dom->getElementsByTagName('div')->item(0), 'http://www.example.com/');
  22. }
  23. /**
  24. * @expectedException \InvalidArgumentException
  25. */
  26. public function testConstructorWithAnInvalidCurrentUri()
  27. {
  28. $dom = new \DOMDocument();
  29. $dom->loadHTML('<html><a href="/foo">foo</a></html>');
  30. new Link($dom->getElementsByTagName('a')->item(0), 'example.com');
  31. }
  32. public function testGetNode()
  33. {
  34. $dom = new \DOMDocument();
  35. $dom->loadHTML('<html><a href="/foo">foo</a></html>');
  36. $node = $dom->getElementsByTagName('a')->item(0);
  37. $link = new Link($node, 'http://example.com/');
  38. $this->assertEquals($node, $link->getNode(), '->getNode() returns the node associated with the link');
  39. }
  40. public function testGetMethod()
  41. {
  42. $dom = new \DOMDocument();
  43. $dom->loadHTML('<html><a href="/foo">foo</a></html>');
  44. $node = $dom->getElementsByTagName('a')->item(0);
  45. $link = new Link($node, 'http://example.com/');
  46. $this->assertEquals('GET', $link->getMethod(), '->getMethod() returns the method of the link');
  47. $link = new Link($node, 'http://example.com/', 'post');
  48. $this->assertEquals('POST', $link->getMethod(), '->getMethod() returns the method of the link');
  49. }
  50. /**
  51. * @dataProvider getGetUriTests
  52. */
  53. public function testGetUri($url, $currentUri, $expected)
  54. {
  55. $dom = new \DOMDocument();
  56. $dom->loadHTML(sprintf('<html><a href="%s">foo</a></html>', $url));
  57. $link = new Link($dom->getElementsByTagName('a')->item(0), $currentUri);
  58. $this->assertEquals($expected, $link->getUri());
  59. }
  60. public function getGetUriTests()
  61. {
  62. return array(
  63. array('/foo', 'http://localhost/bar/foo/', 'http://localhost/foo'),
  64. array('/foo', 'http://localhost/bar/foo', 'http://localhost/foo'),
  65. array('
  66. /foo', 'http://localhost/bar/foo/', 'http://localhost/foo'),
  67. array('/foo
  68. ', 'http://localhost/bar/foo', 'http://localhost/foo'),
  69. array('foo', 'http://localhost/bar/foo/', 'http://localhost/bar/foo/foo'),
  70. array('foo', 'http://localhost/bar/foo', 'http://localhost/bar/foo'),
  71. array('', 'http://localhost/bar/', 'http://localhost/bar/'),
  72. array('#', 'http://localhost/bar/', 'http://localhost/bar/#'),
  73. array('#bar', 'http://localhost/bar/#foo', 'http://localhost/bar/#bar'),
  74. array('?a=b', 'http://localhost/bar/', 'http://localhost/bar/?a=b'),
  75. array('http://login.foo.com/foo', 'http://localhost/bar/', 'http://login.foo.com/foo'),
  76. array('https://login.foo.com/foo', 'https://localhost/bar/', 'https://login.foo.com/foo'),
  77. array('mailto:foo@bar.com', 'http://localhost/foo', 'mailto:foo@bar.com'),
  78. // tests schema relative URL (issue #7169)
  79. array('//login.foo.com/foo', 'http://localhost/bar/', 'http://login.foo.com/foo'),
  80. array('//login.foo.com/foo', 'https://localhost/bar/', 'https://login.foo.com/foo'),
  81. array('?foo=2', 'http://localhost?foo=1', 'http://localhost?foo=2'),
  82. array('?foo=2', 'http://localhost/?foo=1', 'http://localhost/?foo=2'),
  83. array('?foo=2', 'http://localhost/bar?foo=1', 'http://localhost/bar?foo=2'),
  84. array('?foo=2', 'http://localhost/bar/?foo=1', 'http://localhost/bar/?foo=2'),
  85. array('?bar=2', 'http://localhost?foo=1', 'http://localhost?bar=2'),
  86. array('foo', 'http://login.foo.com/bar/baz?/query/string', 'http://login.foo.com/bar/foo'),
  87. array('.', 'http://localhost/foo/bar/baz', 'http://localhost/foo/bar/'),
  88. array('./', 'http://localhost/foo/bar/baz', 'http://localhost/foo/bar/'),
  89. array('./foo', 'http://localhost/foo/bar/baz', 'http://localhost/foo/bar/foo'),
  90. array('..', 'http://localhost/foo/bar/baz', 'http://localhost/foo/'),
  91. array('../', 'http://localhost/foo/bar/baz', 'http://localhost/foo/'),
  92. array('../foo', 'http://localhost/foo/bar/baz', 'http://localhost/foo/foo'),
  93. array('../..', 'http://localhost/foo/bar/baz', 'http://localhost/'),
  94. array('../../', 'http://localhost/foo/bar/baz', 'http://localhost/'),
  95. array('../../foo', 'http://localhost/foo/bar/baz', 'http://localhost/foo'),
  96. array('../../foo', 'http://localhost/bar/foo/', 'http://localhost/foo'),
  97. array('../bar/../../foo', 'http://localhost/bar/foo/', 'http://localhost/foo'),
  98. array('../bar/./../../foo', 'http://localhost/bar/foo/', 'http://localhost/foo'),
  99. array('../../', 'http://localhost/', 'http://localhost/'),
  100. array('../../', 'http://localhost', 'http://localhost/'),
  101. array('/foo', 'file:///', 'file:///foo'),
  102. array('/foo', 'file:///bar/baz', 'file:///foo'),
  103. array('foo', 'file:///', 'file:///foo'),
  104. array('foo', 'file:///bar/baz', 'file:///bar/foo'),
  105. );
  106. }
  107. }