LinkTest.php 4.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. public function testConstructor()
  15. {
  16. $dom = new \DOMDocument();
  17. $dom->loadHTML('<html><div><div></html>');
  18. $node = $dom->getElementsByTagName('div')->item(0);
  19. try {
  20. new Link($node);
  21. $this->fail('__construct() throws a \LogicException if the node is not an "a" tag');
  22. } catch (\Exception $e) {
  23. $this->assertInstanceOf('\LogicException', $e, '__construct() throws a \LogicException if the node is not an "a" tag');
  24. }
  25. }
  26. public function testGetters()
  27. {
  28. $dom = new \DOMDocument();
  29. $dom->loadHTML('<html><a href="/foo">foo</a></html>');
  30. $node = $dom->getElementsByTagName('a')->item(0);
  31. $link = new Link($node);
  32. $this->assertEquals('/foo', $link->getUri(), '->getUri() returns the URI of the link');
  33. $this->assertEquals($node, $link->getNode(), '->getNode() returns the node associated with the link');
  34. $this->assertEquals('get', $link->getMethod(), '->getMethod() returns the method of the link');
  35. $link = new Link($node, 'post');
  36. $this->assertEquals('post', $link->getMethod(), '->getMethod() returns the method of the link');
  37. $link = new Link($node, 'get', 'http://localhost', '/bar/');
  38. $this->assertEquals('http://localhost/foo', $link->getUri(), '->getUri() returns the absolute URI of the link');
  39. $this->assertEquals('/foo', $link->getUri(false), '->getUri() returns the relative URI of the link if false is the first argument');
  40. $dom = new \DOMDocument();
  41. $dom->loadHTML('<html><a href="foo">foo</a></html>');
  42. $node = $dom->getElementsByTagName('a')->item(0);
  43. $link = new Link($node, 'get', 'http://localhost', '/bar/');
  44. $this->assertEquals('http://localhost/bar/foo', $link->getUri(), '->getUri() returns the absolute URI of the link for relative hrefs');
  45. $this->assertEquals('/bar/foo', $link->getUri(false), '->getUri() returns the relative URI of the link if false is the first argument');
  46. $dom = new \DOMDocument();
  47. $dom->loadHTML('<html><a href="http://login.foo.com/foo">foo</a></html>');
  48. $node = $dom->getElementsByTagName('a')->item(0);
  49. $link = new Link($node, 'get', 'http://www.foo.com');
  50. $this->assertEquals('http://login.foo.com/foo', $link->getUri(), '->getUri() returns the absolute URI of the link, regardless of the context of the object');
  51. $link = new Link($node, 'get');
  52. $this->assertEquals('http://login.foo.com/foo', $link->getUri(), '->getUri() returns the absolute URI of the link, regardless of the context of the object');
  53. $link = new Link($node, 'get', null, '/bar/');
  54. $this->assertEquals('http://login.foo.com/foo', $link->getUri(), '->getUri() returns the absolute URI of the link, regardless of the context of the object');
  55. $link = new Link($node, 'get','http://www.foo.com','/bar/');
  56. $this->assertEquals('http://login.foo.com/foo', $link->getUri(), '->getUri() returns the absolute URI of the link, regardless of the context of the object');
  57. $dom = new \DOMDocument();
  58. $dom->loadHTML('<html><a href="?get=param">foo</a></html>');
  59. $node = $dom->getElementsByTagName('a')->item(0);
  60. $link = new Link($node, 'get', 'http://www.foo.com', '/foo/bar');
  61. $this->assertEquals('http://www.foo.com/foo/bar?get=param', $link->getUri(), '->getUri() returns the absolute URI of the link, regardless of the context of the object');
  62. $link = new Link($node, 'get', 'http://www.foo.com', '/foo/bar');
  63. $this->assertEquals('/foo/bar?get=param', $link->getUri(false), '->getUri() returns the relative URI of the link if false is the first argument');
  64. $dom = new \DOMDocument();
  65. $dom->loadHTML('<html><a href="test.html">foo</a></html>');
  66. $node = $dom->getElementsByTagName('a')->item(0);
  67. $link = new Link($node, 'get', null, '/foo/bar', 'http://www.foo.com/');
  68. $this->assertEquals('http://www.foo.com/test.html', $link->getUri());
  69. }
  70. }