2010-04-15 13:41:42 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2010-04-25 16:06:54 +01:00
|
|
|
* This file is part of the Symfony package.
|
2010-04-15 13:41:42 +01:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2010-04-15 13:41:42 +01:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
namespace Symfony\Tests\Component\DomCrawler;
|
2010-04-15 13:41:42 +01:00
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
use Symfony\Component\DomCrawler\Link;
|
2010-04-15 13:41:42 +01:00
|
|
|
|
|
|
|
class LinkTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
2011-04-23 15:25:21 +01:00
|
|
|
/**
|
|
|
|
* @expectedException \LogicException
|
|
|
|
*/
|
|
|
|
public function testConstructorWithANonATag()
|
2010-04-15 13:41:42 +01:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$dom = new \DOMDocument();
|
|
|
|
$dom->loadHTML('<html><div><div></html>');
|
|
|
|
|
2011-04-23 15:25:21 +01:00
|
|
|
new Link($dom->getElementsByTagName('div')->item(0), 'http://www.example.com/');
|
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
|
2011-04-23 15:25:21 +01:00
|
|
|
/**
|
|
|
|
* @expectedException \InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function testConstructorWithAnInvalidCurrentUri()
|
|
|
|
{
|
|
|
|
$dom = new \DOMDocument();
|
|
|
|
$dom->loadHTML('<html><a href="/foo">foo</a></html>');
|
|
|
|
|
|
|
|
new Link($dom->getElementsByTagName('a')->item(0), 'example.com');
|
2010-04-15 13:41:42 +01:00
|
|
|
}
|
|
|
|
|
2011-04-23 15:25:21 +01:00
|
|
|
public function testGetNode()
|
2010-05-06 12:25:53 +01:00
|
|
|
{
|
|
|
|
$dom = new \DOMDocument();
|
|
|
|
$dom->loadHTML('<html><a href="/foo">foo</a></html>');
|
2010-04-15 13:41:42 +01:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$node = $dom->getElementsByTagName('a')->item(0);
|
2011-04-23 15:25:21 +01:00
|
|
|
$link = new Link($node, 'http://example.com/');
|
2010-04-15 13:41:42 +01:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->assertEquals($node, $link->getNode(), '->getNode() returns the node associated with the link');
|
2011-04-23 15:25:21 +01:00
|
|
|
}
|
2010-04-15 13:41:42 +01:00
|
|
|
|
2011-04-23 15:25:21 +01:00
|
|
|
public function testGetMethod()
|
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$dom = new \DOMDocument();
|
2011-04-23 15:25:21 +01:00
|
|
|
$dom->loadHTML('<html><a href="/foo">foo</a></html>');
|
2010-07-06 16:39:11 +01:00
|
|
|
|
|
|
|
$node = $dom->getElementsByTagName('a')->item(0);
|
2011-04-23 15:25:21 +01:00
|
|
|
$link = new Link($node, 'http://example.com/');
|
2010-07-06 16:39:11 +01:00
|
|
|
|
2011-05-25 14:38:02 +01:00
|
|
|
$this->assertEquals('GET', $link->getMethod(), '->getMethod() returns the method of the link');
|
2010-07-06 16:39:11 +01:00
|
|
|
|
2011-04-23 15:25:21 +01:00
|
|
|
$link = new Link($node, 'http://example.com/', 'post');
|
2011-05-25 14:38:02 +01:00
|
|
|
$this->assertEquals('POST', $link->getMethod(), '->getMethod() returns the method of the link');
|
2011-04-23 15:25:21 +01:00
|
|
|
}
|
2010-07-06 16:39:11 +01:00
|
|
|
|
2011-04-23 15:25:21 +01:00
|
|
|
/**
|
|
|
|
* @dataProvider getGetUriTests
|
|
|
|
*/
|
|
|
|
public function testGetUri($url, $currentUri, $expected)
|
|
|
|
{
|
|
|
|
$dom = new \DOMDocument();
|
|
|
|
$dom->loadHTML(sprintf('<html><a href="%s">foo</a></html>', $url));
|
|
|
|
$link = new Link($dom->getElementsByTagName('a')->item(0), $currentUri);
|
2010-07-06 16:39:11 +01:00
|
|
|
|
2011-04-23 15:25:21 +01:00
|
|
|
$this->assertEquals($expected, $link->getUri());
|
|
|
|
}
|
2010-09-02 19:47:15 +01:00
|
|
|
|
2011-04-23 15:25:21 +01:00
|
|
|
public function getGetUriTests()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
array('/foo', 'http://localhost/bar/foo/', 'http://localhost/foo'),
|
|
|
|
array('/foo', 'http://localhost/bar/foo', 'http://localhost/foo'),
|
2011-11-10 14:38:28 +00:00
|
|
|
array('
|
|
|
|
/foo', 'http://localhost/bar/foo/', 'http://localhost/foo'),
|
|
|
|
array('/foo
|
|
|
|
', 'http://localhost/bar/foo', 'http://localhost/foo'),
|
2010-09-02 19:47:15 +01:00
|
|
|
|
2011-04-23 15:25:21 +01:00
|
|
|
array('foo', 'http://localhost/bar/foo/', 'http://localhost/bar/foo/foo'),
|
|
|
|
array('foo', 'http://localhost/bar/foo', 'http://localhost/bar/foo'),
|
2010-09-02 19:47:15 +01:00
|
|
|
|
2011-04-23 15:25:21 +01:00
|
|
|
array('', 'http://localhost/bar/', 'http://localhost/bar/'),
|
|
|
|
array('#', 'http://localhost/bar/', 'http://localhost/bar/#'),
|
2011-06-23 07:18:40 +01:00
|
|
|
array('#bar', 'http://localhost/bar/#foo', 'http://localhost/bar/#bar'),
|
2011-04-23 15:25:21 +01:00
|
|
|
array('?a=b', 'http://localhost/bar/', 'http://localhost/bar/?a=b'),
|
2011-01-13 01:05:26 +00:00
|
|
|
|
2011-04-23 15:25:21 +01:00
|
|
|
array('http://login.foo.com/foo', 'http://localhost/bar/', 'http://login.foo.com/foo'),
|
2011-06-22 20:42:59 +01:00
|
|
|
|
|
|
|
array('?foo=2', 'http://localhost?foo=1', 'http://localhost?foo=2'),
|
|
|
|
array('?foo=2', 'http://localhost/?foo=1', 'http://localhost/?foo=2'),
|
|
|
|
array('?foo=2', 'http://localhost/bar?foo=1', 'http://localhost/bar?foo=2'),
|
|
|
|
array('?foo=2', 'http://localhost/bar/?foo=1', 'http://localhost/bar/?foo=2'),
|
2011-06-23 07:18:40 +01:00
|
|
|
array('?bar=2', 'http://localhost?foo=1', 'http://localhost?bar=2'),
|
2011-04-23 15:25:21 +01:00
|
|
|
);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2010-04-15 13:41:42 +01:00
|
|
|
}
|