4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\DomCrawler\Tests;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\DomCrawler\Link;
17 class LinkTest extends TestCase
20 * @expectedException \LogicException
22 public function testConstructorWithANonATag()
24 $dom = new \DOMDocument();
25 $dom->loadHTML('<html><div><div></html>');
27 new Link($dom->getElementsByTagName('div')->item(0), 'http://www.example.com/');
31 * @expectedException \InvalidArgumentException
33 public function testConstructorWithAnInvalidCurrentUri()
35 $dom = new \DOMDocument();
36 $dom->loadHTML('<html><a href="/foo">foo</a></html>');
38 new Link($dom->getElementsByTagName('a')->item(0), 'example.com');
41 public function testGetNode()
43 $dom = new \DOMDocument();
44 $dom->loadHTML('<html><a href="/foo">foo</a></html>');
46 $node = $dom->getElementsByTagName('a')->item(0);
47 $link = new Link($node, 'http://example.com/');
49 $this->assertEquals($node, $link->getNode(), '->getNode() returns the node associated with the link');
52 public function testGetMethod()
54 $dom = new \DOMDocument();
55 $dom->loadHTML('<html><a href="/foo">foo</a></html>');
57 $node = $dom->getElementsByTagName('a')->item(0);
58 $link = new Link($node, 'http://example.com/');
60 $this->assertEquals('GET', $link->getMethod(), '->getMethod() returns the method of the link');
62 $link = new Link($node, 'http://example.com/', 'post');
63 $this->assertEquals('POST', $link->getMethod(), '->getMethod() returns the method of the link');
67 * @dataProvider getGetUriTests
69 public function testGetUri($url, $currentUri, $expected)
71 $dom = new \DOMDocument();
72 $dom->loadHTML(sprintf('<html><a href="%s">foo</a></html>', $url));
73 $link = new Link($dom->getElementsByTagName('a')->item(0), $currentUri);
75 $this->assertEquals($expected, $link->getUri());
79 * @dataProvider getGetUriTests
81 public function testGetUriOnArea($url, $currentUri, $expected)
83 $dom = new \DOMDocument();
84 $dom->loadHTML(sprintf('<html><map><area href="%s" /></map></html>', $url));
85 $link = new Link($dom->getElementsByTagName('area')->item(0), $currentUri);
87 $this->assertEquals($expected, $link->getUri());
91 * @dataProvider getGetUriTests
93 public function testGetUriOnLink($url, $currentUri, $expected)
95 $dom = new \DOMDocument();
96 $dom->loadHTML(sprintf('<html><head><link href="%s" /></head></html>', $url));
97 $link = new Link($dom->getElementsByTagName('link')->item(0), $currentUri);
99 $this->assertEquals($expected, $link->getUri());
102 public function getGetUriTests()
105 array('/foo', 'http://localhost/bar/foo/', 'http://localhost/foo'),
106 array('/foo', 'http://localhost/bar/foo', 'http://localhost/foo'),
108 /foo', 'http://localhost/bar/foo/', 'http://localhost/foo'),
110 ', 'http://localhost/bar/foo', 'http://localhost/foo'),
112 array('foo', 'http://localhost/bar/foo/', 'http://localhost/bar/foo/foo'),
113 array('foo', 'http://localhost/bar/foo', 'http://localhost/bar/foo'),
115 array('', 'http://localhost/bar/', 'http://localhost/bar/'),
116 array('#', 'http://localhost/bar/', 'http://localhost/bar/#'),
117 array('#bar', 'http://localhost/bar?a=b', 'http://localhost/bar?a=b#bar'),
118 array('#bar', 'http://localhost/bar/#foo', 'http://localhost/bar/#bar'),
119 array('?a=b', 'http://localhost/bar#foo', 'http://localhost/bar?a=b'),
120 array('?a=b', 'http://localhost/bar/', 'http://localhost/bar/?a=b'),
122 array('http://login.foo.com/foo', 'http://localhost/bar/', 'http://login.foo.com/foo'),
123 array('https://login.foo.com/foo', 'https://localhost/bar/', 'https://login.foo.com/foo'),
124 array('mailto:foo@bar.com', 'http://localhost/foo', 'mailto:foo@bar.com'),
126 // tests schema relative URL (issue #7169)
127 array('//login.foo.com/foo', 'http://localhost/bar/', 'http://login.foo.com/foo'),
128 array('//login.foo.com/foo', 'https://localhost/bar/', 'https://login.foo.com/foo'),
130 array('?foo=2', 'http://localhost?foo=1', 'http://localhost?foo=2'),
131 array('?foo=2', 'http://localhost/?foo=1', 'http://localhost/?foo=2'),
132 array('?foo=2', 'http://localhost/bar?foo=1', 'http://localhost/bar?foo=2'),
133 array('?foo=2', 'http://localhost/bar/?foo=1', 'http://localhost/bar/?foo=2'),
134 array('?bar=2', 'http://localhost?foo=1', 'http://localhost?bar=2'),
136 array('foo', 'http://login.foo.com/bar/baz?/query/string', 'http://login.foo.com/bar/foo'),
138 array('.', 'http://localhost/foo/bar/baz', 'http://localhost/foo/bar/'),
139 array('./', 'http://localhost/foo/bar/baz', 'http://localhost/foo/bar/'),
140 array('./foo', 'http://localhost/foo/bar/baz', 'http://localhost/foo/bar/foo'),
141 array('..', 'http://localhost/foo/bar/baz', 'http://localhost/foo/'),
142 array('../', 'http://localhost/foo/bar/baz', 'http://localhost/foo/'),
143 array('../foo', 'http://localhost/foo/bar/baz', 'http://localhost/foo/foo'),
144 array('../..', 'http://localhost/foo/bar/baz', 'http://localhost/'),
145 array('../../', 'http://localhost/foo/bar/baz', 'http://localhost/'),
146 array('../../foo', 'http://localhost/foo/bar/baz', 'http://localhost/foo'),
147 array('../../foo', 'http://localhost/bar/foo/', 'http://localhost/foo'),
148 array('../bar/../../foo', 'http://localhost/bar/foo/', 'http://localhost/foo'),
149 array('../bar/./../../foo', 'http://localhost/bar/foo/', 'http://localhost/foo'),
150 array('../../', 'http://localhost/', 'http://localhost/'),
151 array('../../', 'http://localhost', 'http://localhost/'),
153 array('/foo', 'http://localhost?bar=1', 'http://localhost/foo'),
154 array('/foo', 'http://localhost#bar', 'http://localhost/foo'),
155 array('/foo', 'file:///', 'file:///foo'),
156 array('/foo', 'file:///bar/baz', 'file:///foo'),
157 array('foo', 'file:///', 'file:///foo'),
158 array('foo', 'file:///bar/baz', 'file:///bar/foo'),