LinkTest.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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\Components\DomCrawler;
  11. use Symfony\Components\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. }
  47. }