LinkTest.php 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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. }
  58. }