LinkTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. /**
  61. * @dataProvider getGetUriTests
  62. */
  63. public function testGetUriOnArea($url, $currentUri, $expected)
  64. {
  65. $dom = new \DOMDocument();
  66. $dom->loadHTML(sprintf('<html><map><area href="%s" /></map></html>', $url));
  67. $link = new Link($dom->getElementsByTagName('area')->item(0), $currentUri);
  68. $this->assertEquals($expected, $link->getUri());
  69. }
  70. /**
  71. * @dataProvider getGetUriTests
  72. */
  73. public function testGetUriOnLink($url, $currentUri, $expected)
  74. {
  75. $dom = new \DOMDocument();
  76. $dom->loadHTML(sprintf('<html><head><link href="%s" /></head></html>', $url));
  77. $link = new Link($dom->getElementsByTagName('link')->item(0), $currentUri);
  78. $this->assertEquals($expected, $link->getUri());
  79. }
  80. public function getGetUriTests()
  81. {
  82. return array(
  83. array('/foo', 'http://localhost/bar/foo/', 'http://localhost/foo'),
  84. array('/foo', 'http://localhost/bar/foo', 'http://localhost/foo'),
  85. array('
  86. /foo', 'http://localhost/bar/foo/', 'http://localhost/foo'),
  87. array('/foo
  88. ', 'http://localhost/bar/foo', 'http://localhost/foo'),
  89. array('foo', 'http://localhost/bar/foo/', 'http://localhost/bar/foo/foo'),
  90. array('foo', 'http://localhost/bar/foo', 'http://localhost/bar/foo'),
  91. array('', 'http://localhost/bar/', 'http://localhost/bar/'),
  92. array('#', 'http://localhost/bar/', 'http://localhost/bar/#'),
  93. array('#bar', 'http://localhost/bar?a=b', 'http://localhost/bar?a=b#bar'),
  94. array('#bar', 'http://localhost/bar/#foo', 'http://localhost/bar/#bar'),
  95. array('?a=b', 'http://localhost/bar#foo', 'http://localhost/bar?a=b'),
  96. array('?a=b', 'http://localhost/bar/', 'http://localhost/bar/?a=b'),
  97. array('http://login.foo.com/foo', 'http://localhost/bar/', 'http://login.foo.com/foo'),
  98. array('https://login.foo.com/foo', 'https://localhost/bar/', 'https://login.foo.com/foo'),
  99. array('mailto:foo@bar.com', 'http://localhost/foo', 'mailto:foo@bar.com'),
  100. // tests schema relative URL (issue #7169)
  101. array('//login.foo.com/foo', 'http://localhost/bar/', 'http://login.foo.com/foo'),
  102. array('//login.foo.com/foo', 'https://localhost/bar/', 'https://login.foo.com/foo'),
  103. array('?foo=2', 'http://localhost?foo=1', 'http://localhost?foo=2'),
  104. array('?foo=2', 'http://localhost/?foo=1', 'http://localhost/?foo=2'),
  105. array('?foo=2', 'http://localhost/bar?foo=1', 'http://localhost/bar?foo=2'),
  106. array('?foo=2', 'http://localhost/bar/?foo=1', 'http://localhost/bar/?foo=2'),
  107. array('?bar=2', 'http://localhost?foo=1', 'http://localhost?bar=2'),
  108. array('foo', 'http://login.foo.com/bar/baz?/query/string', 'http://login.foo.com/bar/foo'),
  109. array('.', 'http://localhost/foo/bar/baz', 'http://localhost/foo/bar/'),
  110. array('./', 'http://localhost/foo/bar/baz', 'http://localhost/foo/bar/'),
  111. array('./foo', 'http://localhost/foo/bar/baz', 'http://localhost/foo/bar/foo'),
  112. array('..', 'http://localhost/foo/bar/baz', 'http://localhost/foo/'),
  113. array('../', 'http://localhost/foo/bar/baz', 'http://localhost/foo/'),
  114. array('../foo', 'http://localhost/foo/bar/baz', 'http://localhost/foo/foo'),
  115. array('../..', 'http://localhost/foo/bar/baz', 'http://localhost/'),
  116. array('../../', 'http://localhost/foo/bar/baz', 'http://localhost/'),
  117. array('../../foo', 'http://localhost/foo/bar/baz', 'http://localhost/foo'),
  118. array('../../foo', 'http://localhost/bar/foo/', 'http://localhost/foo'),
  119. array('../bar/../../foo', 'http://localhost/bar/foo/', 'http://localhost/foo'),
  120. array('../bar/./../../foo', 'http://localhost/bar/foo/', 'http://localhost/foo'),
  121. array('../../', 'http://localhost/', 'http://localhost/'),
  122. array('../../', 'http://localhost', 'http://localhost/'),
  123. array('/foo', 'http://localhost?bar=1', 'http://localhost/foo'),
  124. array('/foo', 'http://localhost#bar', 'http://localhost/foo'),
  125. array('/foo', 'file:///', 'file:///foo'),
  126. array('/foo', 'file:///bar/baz', 'file:///foo'),
  127. array('foo', 'file:///', 'file:///foo'),
  128. array('foo', 'file:///bar/baz', 'file:///bar/foo'),
  129. );
  130. }
  131. }