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
{
2010-05-06 12:25:53 +01:00
public function testConstructor ()
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>' );
$node = $dom -> getElementsByTagName ( 'div' ) -> item ( 0 );
2010-05-07 15:09:11 +01:00
try {
2010-05-06 12:25:53 +01:00
new Link ( $node );
$this -> fail ( '__construct() throws a \LogicException if the node is not an "a" tag' );
2010-05-07 15:09:11 +01:00
} catch ( \Exception $e ) {
2010-05-06 12:25:53 +01:00
$this -> assertInstanceOf ( '\LogicException' , $e , '__construct() throws a \LogicException if the node is not an "a" tag' );
}
2010-04-15 13:41:42 +01:00
}
2010-05-06 12:25:53 +01:00
public function testGetters ()
{
$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 );
$link = new Link ( $node );
2010-04-15 13:41:42 +01:00
2010-05-06 12:25:53 +01:00
$this -> assertEquals ( '/foo' , $link -> getUri (), '->getUri() returns the URI of the link' );
$this -> assertEquals ( $node , $link -> getNode (), '->getNode() returns the node associated with the link' );
$this -> assertEquals ( 'get' , $link -> getMethod (), '->getMethod() returns the method of the link' );
2010-04-15 13:41:42 +01:00
2010-05-06 12:25:53 +01:00
$link = new Link ( $node , 'post' );
$this -> assertEquals ( 'post' , $link -> getMethod (), '->getMethod() returns the method of the link' );
2010-04-15 13:41:42 +01:00
2010-05-06 12:25:53 +01:00
$link = new Link ( $node , 'get' , 'http://localhost' , '/bar/' );
$this -> assertEquals ( 'http://localhost/foo' , $link -> getUri (), '->getUri() returns the absolute URI of the link' );
$this -> assertEquals ( '/foo' , $link -> getUri ( false ), '->getUri() returns the relative URI of the link if false is the first argument' );
2010-04-15 13:41:42 +01:00
2010-05-06 12:25:53 +01:00
$dom = new \DOMDocument ();
$dom -> loadHTML ( '<html><a href="foo">foo</a></html>' );
$node = $dom -> getElementsByTagName ( 'a' ) -> item ( 0 );
2010-04-15 13:41:42 +01:00
2010-05-06 12:25:53 +01:00
$link = new Link ( $node , 'get' , 'http://localhost' , '/bar/' );
$this -> assertEquals ( 'http://localhost/bar/foo' , $link -> getUri (), '->getUri() returns the absolute URI of the link for relative hrefs' );
$this -> assertEquals ( '/bar/foo' , $link -> getUri ( false ), '->getUri() returns the relative URI of the link if false is the first argument' );
2010-07-06 16:39:11 +01:00
$dom = new \DOMDocument ();
$dom -> loadHTML ( '<html><a href="http://login.foo.com/foo">foo</a></html>' );
$node = $dom -> getElementsByTagName ( 'a' ) -> item ( 0 );
$link = new Link ( $node , 'get' , 'http://www.foo.com' );
$this -> assertEquals ( 'http://login.foo.com/foo' , $link -> getUri (), '->getUri() returns the absolute URI of the link, regardless of the context of the object' );
$link = new Link ( $node , 'get' );
$this -> assertEquals ( 'http://login.foo.com/foo' , $link -> getUri (), '->getUri() returns the absolute URI of the link, regardless of the context of the object' );
$link = new Link ( $node , 'get' , null , '/bar/' );
$this -> assertEquals ( 'http://login.foo.com/foo' , $link -> getUri (), '->getUri() returns the absolute URI of the link, regardless of the context of the object' );
$link = new Link ( $node , 'get' , 'http://www.foo.com' , '/bar/' );
$this -> assertEquals ( 'http://login.foo.com/foo' , $link -> getUri (), '->getUri() returns the absolute URI of the link, regardless of the context of the object' );
2010-09-02 19:47:15 +01:00
$dom = new \DOMDocument ();
$dom -> loadHTML ( '<html><a href="?get=param">foo</a></html>' );
$node = $dom -> getElementsByTagName ( 'a' ) -> item ( 0 );
$link = new Link ( $node , 'get' , 'http://www.foo.com' , '/foo/bar' );
$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' );
$link = new Link ( $node , 'get' , 'http://www.foo.com' , '/foo/bar' );
$this -> assertEquals ( '/foo/bar?get=param' , $link -> getUri ( false ), '->getUri() returns the relative URI of the link if false is the first argument' );
2011-01-13 01:05:26 +00:00
$dom = new \DOMDocument ();
$dom -> loadHTML ( '<html><a href="test.html">foo</a></html>' );
$node = $dom -> getElementsByTagName ( 'a' ) -> item ( 0 );
$link = new Link ( $node , 'get' , null , '/foo/bar' , 'http://www.foo.com/' );
$this -> assertEquals ( 'http://www.foo.com/test.html' , $link -> getUri ());
2010-05-06 12:25:53 +01:00
}
2010-04-15 13:41:42 +01:00
}