LinkTest.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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\Tests\Component\DomCrawler;
  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('?foo=2', 'http://localhost?foo=1', 'http://localhost?foo=2'),
  77. array('?foo=2', 'http://localhost/?foo=1', 'http://localhost/?foo=2'),
  78. array('?foo=2', 'http://localhost/bar?foo=1', 'http://localhost/bar?foo=2'),
  79. array('?foo=2', 'http://localhost/bar/?foo=1', 'http://localhost/bar/?foo=2'),
  80. array('?bar=2', 'http://localhost?foo=1', 'http://localhost?bar=2'),
  81. );
  82. }
  83. }