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 .
*/
2012-03-28 14:43:52 +01:00
namespace Symfony\Component\DomCrawler\Tests ;
2010-04-15 13:41:42 +01:00
2010-08-20 22:09:55 +01:00
use Symfony\Component\DomCrawler\Crawler ;
2010-04-15 13:41:42 +01:00
class CrawlerTest 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
$crawler = new Crawler ();
2012-04-12 09:09:52 +01:00
$this -> assertCount ( 0 , $crawler , '__construct() returns an empty crawler' );
2010-04-15 13:41:42 +01:00
2010-05-06 12:25:53 +01:00
$crawler = new Crawler ( new \DOMNode ());
2012-04-12 09:09:52 +01:00
$this -> assertCount ( 1 , $crawler , '__construct() takes a node as a first argument' );
2010-04-15 13:41:42 +01:00
}
2010-05-06 12:25:53 +01:00
/**
2010-08-20 22:09:55 +01:00
* @ covers Symfony\Component\DomCrawler\Crawler :: add
2010-05-06 12:25:53 +01:00
*/
public function testAdd ()
2010-04-15 13:41:42 +01:00
{
2010-05-06 12:25:53 +01:00
$crawler = new Crawler ();
$crawler -> add ( $this -> createDomDocument ());
2012-05-07 13:03:55 +01:00
$this -> assertEquals ( 'foo' , $crawler -> filterXPath ( '//div' ) -> attr ( 'class' ), '->add() adds nodes from a \DOMDocument' );
2010-05-06 12:25:53 +01:00
$crawler = new Crawler ();
$crawler -> add ( $this -> createNodeList ());
2012-05-07 13:03:55 +01:00
$this -> assertEquals ( 'foo' , $crawler -> filterXPath ( '//div' ) -> attr ( 'class' ), '->add() adds nodes from a \DOMNodeList' );
2010-05-06 12:25:53 +01:00
2010-05-07 15:09:11 +01:00
foreach ( $this -> createNodeList () as $node ) {
2010-05-06 12:25:53 +01:00
$list [] = $node ;
}
$crawler = new Crawler ();
$crawler -> add ( $list );
2012-05-07 13:03:55 +01:00
$this -> assertEquals ( 'foo' , $crawler -> filterXPath ( '//div' ) -> attr ( 'class' ), '->add() adds nodes from an array of nodes' );
2010-05-06 12:25:53 +01:00
$crawler = new Crawler ();
$crawler -> add ( $this -> createNodeList () -> item ( 0 ));
2012-05-07 13:03:55 +01:00
$this -> assertEquals ( 'foo' , $crawler -> filterXPath ( '//div' ) -> attr ( 'class' ), '->add() adds nodes from an \DOMNode' );
2010-06-27 18:44:04 +01:00
$crawler = new Crawler ();
$crawler -> add ( '<html><body>Foo</body></html>' );
2012-05-07 13:03:55 +01:00
$this -> assertEquals ( 'Foo' , $crawler -> filterXPath ( '//body' ) -> text (), '->add() adds nodes from a string' );
2010-04-15 13:41:42 +01:00
}
2013-05-03 23:53:35 +01:00
/**
* @ expectedException \InvalidArgumentException
*/
public function testAddInvalidNode ()
{
$crawler = new Crawler ();
$crawler -> add ( 1 );
}
2010-05-06 12:25:53 +01:00
/**
2010-08-20 22:09:55 +01:00
* @ covers Symfony\Component\DomCrawler\Crawler :: addHtmlContent
2010-05-06 12:25:53 +01:00
*/
public function testAddHtmlContent ()
2010-04-15 13:41:42 +01:00
{
2010-05-06 12:25:53 +01:00
$crawler = new Crawler ();
$crawler -> addHtmlContent ( '<html><div class="foo"></html>' , 'UTF-8' );
2012-05-07 13:03:55 +01:00
$this -> assertEquals ( 'foo' , $crawler -> filterXPath ( '//div' ) -> attr ( 'class' ), '->addHtmlContent() adds nodes from an HTML string' );
2011-05-07 15:15:13 +01:00
$crawler -> addHtmlContent ( '<html><head><base href="http://symfony.com"></head><a href="/contact"></a></html>' , 'UTF-8' );
2012-05-07 13:03:55 +01:00
$this -> assertEquals ( 'http://symfony.com' , $crawler -> filterXPath ( '//base' ) -> attr ( 'href' ), '->addHtmlContent() adds nodes from an HTML string' );
$this -> assertEquals ( 'http://symfony.com/contact' , $crawler -> filterXPath ( '//a' ) -> link () -> getUri (), '->addHtmlContent() adds nodes from an HTML string' );
2010-04-15 13:41:42 +01:00
}
2010-05-06 12:25:53 +01:00
2012-05-07 13:20:03 +01:00
/**
* @ covers Symfony\Component\DomCrawler\Crawler :: addHtmlContent
*/
public function testAddHtmlContentCharset ()
{
$crawler = new Crawler ();
$crawler -> addHtmlContent ( '<html><div class="foo">Tiếng Việt</html>' , 'UTF-8' );
$this -> assertEquals ( 'Tiếng Việt' , $crawler -> filterXPath ( '//div' ) -> text ());
}
2013-02-25 13:25:47 +00:00
/**
* @ covers Symfony\Component\DomCrawler\Crawler :: addHtmlContent
*/
public function testAddHtmlContentInvalidBaseTag ()
{
$crawler = new Crawler ( null , 'http://symfony.com' );
$crawler -> addHtmlContent ( '<html><head><base target="_top"></head><a href="/contact"></a></html>' , 'UTF-8' );
$this -> assertEquals ( 'http://symfony.com/contact' , current ( $crawler -> filterXPath ( '//a' ) -> links ()) -> getUri (), '->addHtmlContent() correctly handles a non-existent base tag href attribute' );
}
2012-09-25 10:03:23 +01:00
/**
* @ covers Symfony\Component\DomCrawler\Crawler :: addHtmlContent
*/
public function testAddHtmlContentUnsupportedCharset ()
{
$crawler = new Crawler ();
$crawler -> addHtmlContent ( file_get_contents ( __DIR__ . '/Fixtures/windows-1250.html' ), 'Windows-1250' );
$this -> assertEquals ( 'Žťčýů' , $crawler -> filterXPath ( '//p' ) -> text ());
}
2013-10-17 15:30:34 +01:00
/**
* @ covers Symfony\Component\DomCrawler\Crawler :: addHtmlContent
*/
public function testAddHtmlContentCharsetGbk ()
{
$crawler = new Crawler ();
//gbk encode of <html><p>中文</p></html>
$crawler -> addHtmlContent ( base64_decode ( 'PGh0bWw+PHA+1tDOxDwvcD48L2h0bWw+' ), 'gbk' );
$this -> assertEquals ( '中文' , $crawler -> filterXPath ( '//p' ) -> text ());
}
2011-09-28 08:53:50 +01:00
/**
* @ covers Symfony\Component\DomCrawler\Crawler :: addHtmlContent
*/
public function testAddHtmlContentWithErrors ()
{
libxml_use_internal_errors ( true );
$crawler = new Crawler ();
$crawler -> addHtmlContent ( <<< EOF
<! DOCTYPE html >
< html >
< head >
</ head >
< body >
< nav >< a href = " # " >< a href = " # " ></ nav >
</ body >
</ html >
EOF
, 'UTF-8' );
$errors = libxml_get_errors ();
2012-01-18 13:42:47 +00:00
$this -> assertCount ( 1 , $errors );
2011-09-28 08:53:50 +01:00
$this -> assertEquals ( " Tag nav invalid \n " , $errors [ 0 ] -> message );
libxml_clear_errors ();
libxml_use_internal_errors ( false );
}
2010-05-06 12:25:53 +01:00
/**
2010-08-20 22:09:55 +01:00
* @ covers Symfony\Component\DomCrawler\Crawler :: addXmlContent
2010-05-06 12:25:53 +01:00
*/
public function testAddXmlContent ()
2010-04-15 13:41:42 +01:00
{
2010-05-06 12:25:53 +01:00
$crawler = new Crawler ();
$crawler -> addXmlContent ( '<html><div class="foo"></div></html>' , 'UTF-8' );
2010-04-15 13:41:42 +01:00
2012-05-07 13:03:55 +01:00
$this -> assertEquals ( 'foo' , $crawler -> filterXPath ( '//div' ) -> attr ( 'class' ), '->addXmlContent() adds nodes from an XML string' );
2010-05-06 12:25:53 +01:00
}
2010-04-15 13:41:42 +01:00
2012-05-07 13:20:03 +01:00
/**
* @ covers Symfony\Component\DomCrawler\Crawler :: addXmlContent
*/
public function testAddXmlContentCharset ()
{
$crawler = new Crawler ();
$crawler -> addXmlContent ( '<html><div class="foo">Tiếng Việt</div></html>' , 'UTF-8' );
$this -> assertEquals ( 'Tiếng Việt' , $crawler -> filterXPath ( '//div' ) -> text ());
}
2011-09-28 08:53:50 +01:00
/**
* @ covers Symfony\Component\DomCrawler\Crawler :: addXmlContent
*/
public function testAddXmlContentWithErrors ()
{
libxml_use_internal_errors ( true );
$crawler = new Crawler ();
$crawler -> addXmlContent ( <<< EOF
<! DOCTYPE html >
< html >
< head >
</ head >
< body >
< nav >< a href = " # " >< a href = " # " ></ nav >
</ body >
</ html >
EOF
, 'UTF-8' );
$this -> assertTrue ( count ( libxml_get_errors ()) > 1 );
libxml_clear_errors ();
libxml_use_internal_errors ( false );
}
2010-05-06 12:25:53 +01:00
/**
2010-08-20 22:09:55 +01:00
* @ covers Symfony\Component\DomCrawler\Crawler :: addContent
2010-05-06 12:25:53 +01:00
*/
public function testAddContent ()
{
$crawler = new Crawler ();
$crawler -> addContent ( '<html><div class="foo"></html>' , 'text/html; charset=UTF-8' );
2012-05-07 13:03:55 +01:00
$this -> assertEquals ( 'foo' , $crawler -> filterXPath ( '//div' ) -> attr ( 'class' ), '->addContent() adds nodes from an HTML string' );
2010-04-15 13:41:42 +01:00
2011-08-10 10:41:07 +01:00
$crawler = new Crawler ();
$crawler -> addContent ( '<html><div class="foo"></html>' , 'text/html; charset=UTF-8; dir=RTL' );
2012-05-07 13:03:55 +01:00
$this -> assertEquals ( 'foo' , $crawler -> filterXPath ( '//div' ) -> attr ( 'class' ), '->addContent() adds nodes from an HTML string with extended content type' );
2011-08-10 10:41:07 +01:00
2010-05-06 12:25:53 +01:00
$crawler = new Crawler ();
$crawler -> addContent ( '<html><div class="foo"></html>' );
2012-05-07 13:03:55 +01:00
$this -> assertEquals ( 'foo' , $crawler -> filterXPath ( '//div' ) -> attr ( 'class' ), '->addContent() uses text/html as the default type' );
2010-04-15 13:41:42 +01:00
2010-05-06 12:25:53 +01:00
$crawler = new Crawler ();
$crawler -> addContent ( '<html><div class="foo"></div></html>' , 'text/xml; charset=UTF-8' );
2012-05-07 13:03:55 +01:00
$this -> assertEquals ( 'foo' , $crawler -> filterXPath ( '//div' ) -> attr ( 'class' ), '->addContent() adds nodes from an XML string' );
2010-04-15 13:41:42 +01:00
2010-05-06 12:25:53 +01:00
$crawler = new Crawler ();
$crawler -> addContent ( '<html><div class="foo"></div></html>' , 'text/xml' );
2012-05-07 13:03:55 +01:00
$this -> assertEquals ( 'foo' , $crawler -> filterXPath ( '//div' ) -> attr ( 'class' ), '->addContent() adds nodes from an XML string' );
2010-04-15 13:41:42 +01:00
2010-05-06 12:25:53 +01:00
$crawler = new Crawler ();
$crawler -> addContent ( 'foo bar' , 'text/plain' );
2012-04-12 09:09:52 +01:00
$this -> assertCount ( 0 , $crawler , '->addContent() does nothing if the type is not (x|ht)ml' );
2013-09-19 09:03:41 +01:00
$crawler = new Crawler ();
$crawler -> addContent ( '<html><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><span>中文</span></html>' );
$this -> assertEquals ( '中文' , $crawler -> filterXPath ( '//span' ) -> text (), '->addContent() guess wrong charset' );
2010-05-06 12:25:53 +01:00
}
2010-04-15 13:41:42 +01:00
2010-05-06 12:25:53 +01:00
/**
2010-08-20 22:09:55 +01:00
* @ covers Symfony\Component\DomCrawler\Crawler :: addDocument
2010-05-06 12:25:53 +01:00
*/
public function testAddDocument ()
{
$crawler = new Crawler ();
$crawler -> addDocument ( $this -> createDomDocument ());
2010-04-15 13:41:42 +01:00
2012-05-07 13:03:55 +01:00
$this -> assertEquals ( 'foo' , $crawler -> filterXPath ( '//div' ) -> attr ( 'class' ), '->addDocument() adds nodes from a \DOMDocument' );
2010-05-06 12:25:53 +01:00
}
2010-04-15 13:41:42 +01:00
2010-05-06 12:25:53 +01:00
/**
2010-08-20 22:09:55 +01:00
* @ covers Symfony\Component\DomCrawler\Crawler :: addNodeList
2010-05-06 12:25:53 +01:00
*/
public function testAddNodeList ()
{
$crawler = new Crawler ();
$crawler -> addNodeList ( $this -> createNodeList ());
2010-04-15 13:41:42 +01:00
2012-05-07 13:03:55 +01:00
$this -> assertEquals ( 'foo' , $crawler -> filterXPath ( '//div' ) -> attr ( 'class' ), '->addNodeList() adds nodes from a \DOMNodeList' );
2010-05-06 12:25:53 +01:00
}
2010-04-15 13:41:42 +01:00
2010-05-06 12:25:53 +01:00
/**
2010-08-20 22:09:55 +01:00
* @ covers Symfony\Component\DomCrawler\Crawler :: addNodes
2010-05-06 12:25:53 +01:00
*/
public function testAddNodes ()
{
2010-05-07 15:09:11 +01:00
foreach ( $this -> createNodeList () as $node ) {
2010-05-06 12:25:53 +01:00
$list [] = $node ;
}
2010-04-15 13:41:42 +01:00
2010-05-06 12:25:53 +01:00
$crawler = new Crawler ();
$crawler -> addNodes ( $list );
2010-04-15 13:41:42 +01:00
2012-05-07 13:03:55 +01:00
$this -> assertEquals ( 'foo' , $crawler -> filterXPath ( '//div' ) -> attr ( 'class' ), '->addNodes() adds nodes from an array of nodes' );
2010-05-06 12:25:53 +01:00
}
2010-04-15 13:41:42 +01:00
2010-05-06 12:25:53 +01:00
/**
2010-08-20 22:09:55 +01:00
* @ covers Symfony\Component\DomCrawler\Crawler :: addNode
2010-05-06 12:25:53 +01:00
*/
public function testAddNode ()
{
$crawler = new Crawler ();
$crawler -> addNode ( $this -> createNodeList () -> item ( 0 ));
2010-04-15 13:41:42 +01:00
2012-05-07 13:03:55 +01:00
$this -> assertEquals ( 'foo' , $crawler -> filterXPath ( '//div' ) -> attr ( 'class' ), '->addNode() adds nodes from an \DOMNode' );
2010-05-06 12:25:53 +01:00
}
2010-04-15 13:41:42 +01:00
2010-05-06 12:25:53 +01:00
public function testClear ()
{
$crawler = new Crawler ( new \DOMNode ());
$crawler -> clear ();
2012-04-12 09:09:52 +01:00
$this -> assertCount ( 0 , $crawler , '->clear() removes all the nodes from the crawler' );
2010-05-06 12:25:53 +01:00
}
2010-04-15 13:41:42 +01:00
2010-05-06 12:25:53 +01:00
public function testEq ()
{
2012-05-07 13:03:55 +01:00
$crawler = $this -> createTestCrawler () -> filterXPath ( '//li' );
2010-05-06 12:25:53 +01:00
$this -> assertNotSame ( $crawler , $crawler -> eq ( 0 ), '->eq() returns a new instance of a crawler' );
2010-08-20 22:09:55 +01:00
$this -> assertInstanceOf ( 'Symfony\\Component\\DomCrawler\\Crawler' , $crawler , '->eq() returns a new instance of a crawler' );
2010-04-15 13:41:42 +01:00
2010-05-06 12:25:53 +01:00
$this -> assertEquals ( 'Two' , $crawler -> eq ( 1 ) -> text (), '->eq() returns the nth node of the list' );
2012-01-18 13:42:47 +00:00
$this -> assertCount ( 0 , $crawler -> eq ( 100 ), '->eq() returns an empty crawler if the nth node does not exist' );
2010-05-06 12:25:53 +01:00
}
2010-04-15 13:41:42 +01:00
2010-05-06 12:25:53 +01:00
public function testEach ()
{
2013-04-22 05:49:51 +01:00
$data = $this -> createTestCrawler () -> filterXPath ( '//ul[1]/li' ) -> each ( function ( $node , $i ) {
2012-12-25 23:45:05 +00:00
return $i . '-' . $node -> text ();
2010-05-06 12:25:53 +01:00
});
2010-04-15 13:41:42 +01:00
2010-05-06 12:25:53 +01:00
$this -> assertEquals ( array ( '0-One' , '1-Two' , '2-Three' ), $data , '->each() executes an anonymous function on each node of the list' );
}
2010-04-15 13:41:42 +01:00
2010-05-06 12:25:53 +01:00
public function testReduce ()
2010-04-15 13:41:42 +01:00
{
2012-05-07 13:03:55 +01:00
$crawler = $this -> createTestCrawler () -> filterXPath ( '//ul[1]/li' );
2012-05-20 17:15:10 +01:00
$nodes = $crawler -> reduce ( function ( $node , $i ) {
2010-05-06 12:25:53 +01:00
return $i == 1 ? false : true ;
});
$this -> assertNotSame ( $nodes , $crawler , '->reduce() returns a new instance of a crawler' );
2010-08-20 22:09:55 +01:00
$this -> assertInstanceOf ( 'Symfony\\Component\\DomCrawler\\Crawler' , $nodes , '->reduce() returns a new instance of a crawler' );
2010-05-06 12:25:53 +01:00
2012-04-12 09:09:52 +01:00
$this -> assertCount ( 2 , $nodes , '->reduce() filters the nodes in the list' );
2010-04-15 13:41:42 +01:00
}
2010-05-06 12:25:53 +01:00
public function testAttr ()
2010-04-15 13:41:42 +01:00
{
2012-05-07 13:03:55 +01:00
$this -> assertEquals ( 'first' , $this -> createTestCrawler () -> filterXPath ( '//li' ) -> attr ( 'class' ), '->attr() returns the attribute of the first element of the node list' );
2010-05-06 12:25:53 +01:00
2010-05-07 15:09:11 +01:00
try {
2012-05-07 13:03:55 +01:00
$this -> createTestCrawler () -> filterXPath ( '//ol' ) -> attr ( 'class' );
2010-05-06 12:25:53 +01:00
$this -> fail ( '->attr() throws an \InvalidArgumentException if the node list is empty' );
2010-05-07 15:09:11 +01:00
} catch ( \InvalidArgumentException $e ) {
2010-05-06 12:25:53 +01:00
$this -> assertTrue ( true , '->attr() throws an \InvalidArgumentException if the node list is empty' );
}
2010-04-15 13:41:42 +01:00
}
2010-05-06 12:25:53 +01:00
public function testText ()
{
2012-05-07 13:03:55 +01:00
$this -> assertEquals ( 'One' , $this -> createTestCrawler () -> filterXPath ( '//li' ) -> text (), '->text() returns the node value of the first element of the node list' );
2010-05-06 12:25:53 +01:00
2010-05-07 15:09:11 +01:00
try {
2012-05-07 13:03:55 +01:00
$this -> createTestCrawler () -> filterXPath ( '//ol' ) -> text ();
2010-05-06 12:25:53 +01:00
$this -> fail ( '->text() throws an \InvalidArgumentException if the node list is empty' );
2010-05-07 15:09:11 +01:00
} catch ( \InvalidArgumentException $e ) {
2010-05-06 12:25:53 +01:00
$this -> assertTrue ( true , '->text() throws an \InvalidArgumentException if the node list is empty' );
}
}
2010-04-15 13:41:42 +01:00
2013-04-21 01:11:42 +01:00
public function testHtml ()
{
2013-05-10 16:43:46 +01:00
$this -> assertEquals ( '<img alt="Bar">' , $this -> createTestCrawler () -> filterXPath ( '//a[5]' ) -> html ());
$this -> assertEquals ( '<input type="text" value="TextValue" name="TextName"><input type="submit" value="FooValue" name="FooName" id="FooId"><input type="button" value="BarValue" name="BarName" id="BarId"><button value="ButtonValue" name="ButtonName" id="ButtonId"></button>'
2013-04-22 06:03:23 +01:00
, trim ( $this -> createTestCrawler () -> filterXPath ( '//form[@id="FooFormId"]' ) -> html ()));
2013-04-21 01:11:42 +01:00
try {
$this -> createTestCrawler () -> filterXPath ( '//ol' ) -> html ();
$this -> fail ( '->html() throws an \InvalidArgumentException if the node list is empty' );
} catch ( \InvalidArgumentException $e ) {
$this -> assertTrue ( true , '->html() throws an \InvalidArgumentException if the node list is empty' );
}
}
2010-05-06 12:25:53 +01:00
public function testExtract ()
{
2012-05-07 13:03:55 +01:00
$crawler = $this -> createTestCrawler () -> filterXPath ( '//ul[1]/li' );
2010-04-15 13:41:42 +01:00
2010-05-06 12:25:53 +01:00
$this -> assertEquals ( array ( 'One' , 'Two' , 'Three' ), $crawler -> extract ( '_text' ), '->extract() returns an array of extracted data from the node list' );
$this -> assertEquals ( array ( array ( 'One' , 'first' ), array ( 'Two' , '' ), array ( 'Three' , '' )), $crawler -> extract ( array ( '_text' , 'class' )), '->extract() returns an array of extracted data from the node list' );
2010-04-15 13:41:42 +01:00
2012-05-07 13:03:55 +01:00
$this -> assertEquals ( array (), $this -> createTestCrawler () -> filterXPath ( '//ol' ) -> extract ( '_text' ), '->extract() returns an empty array if the node list is empty' );
2010-05-06 12:25:53 +01:00
}
2010-04-15 13:41:42 +01:00
2010-05-06 12:25:53 +01:00
/**
2010-08-20 22:09:55 +01:00
* @ covers Symfony\Component\DomCrawler\Crawler :: filterXPath
2010-05-06 12:25:53 +01:00
*/
public function testFilterXPath ()
{
$crawler = $this -> createTestCrawler ();
$this -> assertNotSame ( $crawler , $crawler -> filterXPath ( '//li' ), '->filterXPath() returns a new instance of a crawler' );
2010-08-20 22:09:55 +01:00
$this -> assertInstanceOf ( 'Symfony\\Component\\DomCrawler\\Crawler' , $crawler , '->filterXPath() returns a new instance of a crawler' );
2010-04-15 13:41:42 +01:00
2012-05-07 13:03:55 +01:00
$crawler = $this -> createTestCrawler () -> filterXPath ( '//ul' );
2014-02-05 00:04:16 +00:00
2014-02-18 16:26:04 +00:00
$this -> assertCount ( 6 , $crawler -> filterXPath ( '//li' ), '->filterXPath() filters the node list with the XPath expression' );
2010-05-06 12:25:53 +01:00
}
2010-04-15 13:41:42 +01:00
2010-05-06 12:25:53 +01:00
/**
2010-08-20 22:09:55 +01:00
* @ covers Symfony\Component\DomCrawler\Crawler :: filter
2010-05-06 12:25:53 +01:00
*/
public function testFilter ()
2010-04-15 13:41:42 +01:00
{
2012-03-28 14:43:52 +01:00
if ( ! class_exists ( 'Symfony\Component\CssSelector\CssSelector' )) {
$this -> markTestSkipped ( 'The "CssSelector" component is not available' );
}
2010-05-06 12:25:53 +01:00
$crawler = $this -> createTestCrawler ();
$this -> assertNotSame ( $crawler , $crawler -> filter ( 'li' ), '->filter() returns a new instance of a crawler' );
2010-08-20 22:09:55 +01:00
$this -> assertInstanceOf ( 'Symfony\\Component\\DomCrawler\\Crawler' , $crawler , '->filter() returns a new instance of a crawler' );
2010-05-06 12:25:53 +01:00
$crawler = $this -> createTestCrawler () -> filter ( 'ul' );
2012-01-18 13:42:47 +00:00
$this -> assertCount ( 6 , $crawler -> filter ( 'li' ), '->filter() filters the node list with the CSS selector' );
2010-04-15 13:41:42 +01:00
}
2010-05-06 12:25:53 +01:00
public function testSelectLink ()
2010-04-15 13:41:42 +01:00
{
2010-05-06 12:25:53 +01:00
$crawler = $this -> createTestCrawler ();
$this -> assertNotSame ( $crawler , $crawler -> selectLink ( 'Foo' ), '->selectLink() returns a new instance of a crawler' );
2010-08-20 22:09:55 +01:00
$this -> assertInstanceOf ( 'Symfony\\Component\\DomCrawler\\Crawler' , $crawler , '->selectLink() returns a new instance of a crawler' );
2010-04-15 13:41:42 +01:00
2012-01-18 13:42:47 +00:00
$this -> assertCount ( 1 , $crawler -> selectLink ( 'Fabien\'s Foo' ), '->selectLink() selects links by the node values' );
$this -> assertCount ( 1 , $crawler -> selectLink ( 'Fabien\'s Bar' ), '->selectLink() selects links by the alt attribute of a clickable image' );
2010-04-15 13:41:42 +01:00
2012-01-18 13:42:47 +00:00
$this -> assertCount ( 2 , $crawler -> selectLink ( 'Fabien"s Foo' ), '->selectLink() selects links by the node values' );
$this -> assertCount ( 2 , $crawler -> selectLink ( 'Fabien"s Bar' ), '->selectLink() selects links by the alt attribute of a clickable image' );
2010-04-15 13:41:42 +01:00
2012-01-18 13:42:47 +00:00
$this -> assertCount ( 1 , $crawler -> selectLink ( '\' Fabien"s Foo' ), '->selectLink() selects links by the node values' );
$this -> assertCount ( 1 , $crawler -> selectLink ( '\' Fabien"s Bar' ), '->selectLink() selects links by the alt attribute of a clickable image' );
2010-04-15 13:41:42 +01:00
2012-01-18 13:42:47 +00:00
$this -> assertCount ( 4 , $crawler -> selectLink ( 'Foo' ), '->selectLink() selects links by the node values' );
$this -> assertCount ( 4 , $crawler -> selectLink ( 'Bar' ), '->selectLink() selects links by the node values' );
2010-05-06 12:25:53 +01:00
}
2010-04-15 13:41:42 +01:00
2010-05-06 12:25:53 +01:00
public function testSelectButton ()
{
$crawler = $this -> createTestCrawler ();
$this -> assertNotSame ( $crawler , $crawler -> selectButton ( 'FooValue' ), '->selectButton() returns a new instance of a crawler' );
2010-08-20 22:09:55 +01:00
$this -> assertInstanceOf ( 'Symfony\\Component\\DomCrawler\\Crawler' , $crawler , '->selectButton() returns a new instance of a crawler' );
2010-04-15 13:41:42 +01:00
2010-05-06 12:25:53 +01:00
$this -> assertEquals ( 1 , $crawler -> selectButton ( 'FooValue' ) -> count (), '->selectButton() selects buttons' );
$this -> assertEquals ( 1 , $crawler -> selectButton ( 'FooName' ) -> count (), '->selectButton() selects buttons' );
$this -> assertEquals ( 1 , $crawler -> selectButton ( 'FooId' ) -> count (), '->selectButton() selects buttons' );
2010-04-15 13:41:42 +01:00
2010-05-06 12:25:53 +01:00
$this -> assertEquals ( 1 , $crawler -> selectButton ( 'BarValue' ) -> count (), '->selectButton() selects buttons' );
$this -> assertEquals ( 1 , $crawler -> selectButton ( 'BarName' ) -> count (), '->selectButton() selects buttons' );
$this -> assertEquals ( 1 , $crawler -> selectButton ( 'BarId' ) -> count (), '->selectButton() selects buttons' );
2013-03-27 22:52:06 +00:00
$this -> assertEquals ( 1 , $crawler -> selectButton ( 'FooBarValue' ) -> count (), '->selectButton() selects buttons with form attribute too' );
$this -> assertEquals ( 1 , $crawler -> selectButton ( 'FooBarName' ) -> count (), '->selectButton() selects buttons with form attribute too' );
2010-05-06 12:25:53 +01:00
}
2010-04-15 13:41:42 +01:00
2010-05-06 12:25:53 +01:00
public function testLink ()
2010-04-15 13:41:42 +01:00
{
2011-04-23 15:25:21 +01:00
$crawler = $this -> createTestCrawler ( 'http://example.com/bar/' ) -> selectLink ( 'Foo' );
2010-08-20 22:09:55 +01:00
$this -> assertInstanceOf ( 'Symfony\\Component\\DomCrawler\\Link' , $crawler -> link (), '->link() returns a Link instance' );
2010-05-06 12:25:53 +01:00
2011-05-25 14:38:02 +01:00
$this -> assertEquals ( 'POST' , $crawler -> link ( 'post' ) -> getMethod (), '->link() takes a method as its argument' );
2010-05-06 12:25:53 +01:00
2010-09-02 19:47:15 +01:00
$crawler = $this -> createTestCrawler ( 'http://example.com/bar' ) -> selectLink ( 'GetLink' );
$this -> assertEquals ( 'http://example.com/bar?get=param' , $crawler -> link () -> getUri (), '->link() returns a Link instance' );
2010-05-07 15:09:11 +01:00
try {
2012-05-07 13:03:55 +01:00
$this -> createTestCrawler () -> filterXPath ( '//ol' ) -> link ();
2010-05-06 12:25:53 +01:00
$this -> fail ( '->link() throws an \InvalidArgumentException if the node list is empty' );
2010-05-07 15:09:11 +01:00
} catch ( \InvalidArgumentException $e ) {
2010-05-06 12:25:53 +01:00
$this -> assertTrue ( true , '->link() throws an \InvalidArgumentException if the node list is empty' );
}
2010-04-15 13:41:42 +01:00
}
2010-05-06 12:25:53 +01:00
public function testLinks ()
2010-04-15 13:41:42 +01:00
{
2011-04-23 15:25:21 +01:00
$crawler = $this -> createTestCrawler ( 'http://example.com/bar/' ) -> selectLink ( 'Foo' );
2011-01-02 22:08:16 +00:00
$this -> assertInternalType ( 'array' , $crawler -> links (), '->links() returns an array' );
2010-04-15 13:41:42 +01:00
2012-01-18 13:42:47 +00:00
$this -> assertCount ( 4 , $crawler -> links (), '->links() returns an array' );
2010-05-06 12:25:53 +01:00
$links = $crawler -> links ();
2010-08-20 22:09:55 +01:00
$this -> assertInstanceOf ( 'Symfony\\Component\\DomCrawler\\Link' , $links [ 0 ], '->links() returns an array of Link instances' );
2010-04-15 13:41:42 +01:00
2012-05-07 13:03:55 +01:00
$this -> assertEquals ( array (), $this -> createTestCrawler () -> filterXPath ( '//ol' ) -> links (), '->links() returns an empty array if the node selection is empty' );
2010-05-06 12:25:53 +01:00
}
2010-04-15 13:41:42 +01:00
2010-05-06 12:25:53 +01:00
public function testForm ()
2010-04-15 13:41:42 +01:00
{
2013-03-27 22:52:06 +00:00
$testCrawler = $this -> createTestCrawler ( 'http://example.com/bar/' );
$crawler = $testCrawler -> selectButton ( 'FooValue' );
$crawler2 = $testCrawler -> selectButton ( 'FooBarValue' );
2010-08-20 22:09:55 +01:00
$this -> assertInstanceOf ( 'Symfony\\Component\\DomCrawler\\Form' , $crawler -> form (), '->form() returns a Form instance' );
2013-03-27 22:52:06 +00:00
$this -> assertInstanceOf ( 'Symfony\\Component\\DomCrawler\\Form' , $crawler2 -> form (), '->form() returns a Form instance' );
$this -> assertEquals ( $crawler -> form () -> getFormNode () -> getAttribute ( 'id' ), $crawler2 -> form () -> getFormNode () -> getAttribute ( 'id' ), '->form() works on elements with form attribute' );
2010-05-06 12:25:53 +01:00
2013-03-27 22:52:06 +00:00
$this -> assertEquals ( array ( 'FooName' => 'FooBar' , 'TextName' => 'TextValue' , 'FooTextName' => 'FooTextValue' ), $crawler -> form ( array ( 'FooName' => 'FooBar' )) -> getValues (), '->form() takes an array of values to submit as its first argument' );
2013-09-25 10:22:53 +01:00
$this -> assertEquals ( array ( 'FooName' => 'FooValue' , 'TextName' => 'TextValue' , 'FooTextName' => 'FooTextValue' ), $crawler -> form () -> getValues (), '->getValues() returns correct form values' );
$this -> assertEquals ( array ( 'FooBarName' => 'FooBarValue' , 'TextName' => 'TextValue' , 'FooTextName' => 'FooTextValue' ), $crawler2 -> form () -> getValues (), '->getValues() returns correct form values' );
2010-05-06 12:25:53 +01:00
2010-05-07 15:09:11 +01:00
try {
2012-05-07 13:03:55 +01:00
$this -> createTestCrawler () -> filterXPath ( '//ol' ) -> form ();
2010-05-06 12:25:53 +01:00
$this -> fail ( '->form() throws an \InvalidArgumentException if the node list is empty' );
2010-05-07 15:09:11 +01:00
} catch ( \InvalidArgumentException $e ) {
2010-05-06 12:25:53 +01:00
$this -> assertTrue ( true , '->form() throws an \InvalidArgumentException if the node list is empty' );
}
2010-04-15 13:41:42 +01:00
}
2010-05-06 12:25:53 +01:00
public function testLast ()
2010-04-15 13:41:42 +01:00
{
2012-05-07 13:03:55 +01:00
$crawler = $this -> createTestCrawler () -> filterXPath ( '//ul[1]/li' );
2010-05-06 12:25:53 +01:00
$this -> assertNotSame ( $crawler , $crawler -> last (), '->last() returns a new instance of a crawler' );
2010-08-20 22:09:55 +01:00
$this -> assertInstanceOf ( 'Symfony\\Component\\DomCrawler\\Crawler' , $crawler , '->last() returns a new instance of a crawler' );
2010-05-06 12:25:53 +01:00
$this -> assertEquals ( 'Three' , $crawler -> last () -> text ());
2010-04-15 13:41:42 +01:00
}
2010-05-06 12:25:53 +01:00
public function testFirst ()
{
2012-05-07 13:03:55 +01:00
$crawler = $this -> createTestCrawler () -> filterXPath ( '//li' );
2010-05-06 12:25:53 +01:00
$this -> assertNotSame ( $crawler , $crawler -> first (), '->first() returns a new instance of a crawler' );
2010-08-20 22:09:55 +01:00
$this -> assertInstanceOf ( 'Symfony\\Component\\DomCrawler\\Crawler' , $crawler , '->first() returns a new instance of a crawler' );
2010-04-15 13:41:42 +01:00
2010-05-06 12:25:53 +01:00
$this -> assertEquals ( 'One' , $crawler -> first () -> text ());
}
2010-04-15 13:41:42 +01:00
2010-05-06 12:25:53 +01:00
public function testSiblings ()
2010-04-15 13:41:42 +01:00
{
2012-05-07 13:03:55 +01:00
$crawler = $this -> createTestCrawler () -> filterXPath ( '//li' ) -> eq ( 1 );
2010-05-06 12:25:53 +01:00
$this -> assertNotSame ( $crawler , $crawler -> siblings (), '->siblings() returns a new instance of a crawler' );
2010-08-20 22:09:55 +01:00
$this -> assertInstanceOf ( 'Symfony\\Component\\DomCrawler\\Crawler' , $crawler , '->siblings() returns a new instance of a crawler' );
2010-05-06 12:25:53 +01:00
$nodes = $crawler -> siblings ();
$this -> assertEquals ( 2 , $nodes -> count ());
$this -> assertEquals ( 'One' , $nodes -> eq ( 0 ) -> text ());
$this -> assertEquals ( 'Three' , $nodes -> eq ( 1 ) -> text ());
2012-05-07 13:03:55 +01:00
$nodes = $this -> createTestCrawler () -> filterXPath ( '//li' ) -> eq ( 0 ) -> siblings ();
2010-05-06 12:25:53 +01:00
$this -> assertEquals ( 2 , $nodes -> count ());
$this -> assertEquals ( 'Two' , $nodes -> eq ( 0 ) -> text ());
$this -> assertEquals ( 'Three' , $nodes -> eq ( 1 ) -> text ());
2010-05-07 15:09:11 +01:00
try {
2012-05-07 13:03:55 +01:00
$this -> createTestCrawler () -> filterXPath ( '//ol' ) -> siblings ();
2010-05-06 12:25:53 +01:00
$this -> fail ( '->siblings() throws an \InvalidArgumentException if the node list is empty' );
2010-05-07 15:09:11 +01:00
} catch ( \InvalidArgumentException $e ) {
2010-05-06 12:25:53 +01:00
$this -> assertTrue ( true , '->siblings() throws an \InvalidArgumentException if the node list is empty' );
}
2010-04-15 13:41:42 +01:00
}
2010-05-06 12:25:53 +01:00
public function testNextAll ()
2010-04-15 13:41:42 +01:00
{
2012-05-07 13:03:55 +01:00
$crawler = $this -> createTestCrawler () -> filterXPath ( '//li' ) -> eq ( 1 );
2010-05-06 12:25:53 +01:00
$this -> assertNotSame ( $crawler , $crawler -> nextAll (), '->nextAll() returns a new instance of a crawler' );
2010-08-20 22:09:55 +01:00
$this -> assertInstanceOf ( 'Symfony\\Component\\DomCrawler\\Crawler' , $crawler , '->nextAll() returns a new instance of a crawler' );
2010-05-06 12:25:53 +01:00
$nodes = $crawler -> nextAll ();
$this -> assertEquals ( 1 , $nodes -> count ());
$this -> assertEquals ( 'Three' , $nodes -> eq ( 0 ) -> text ());
2010-05-07 15:09:11 +01:00
try {
2012-05-07 13:03:55 +01:00
$this -> createTestCrawler () -> filterXPath ( '//ol' ) -> nextAll ();
2010-05-06 12:25:53 +01:00
$this -> fail ( '->nextAll() throws an \InvalidArgumentException if the node list is empty' );
2010-05-07 15:09:11 +01:00
} catch ( \InvalidArgumentException $e ) {
2010-05-06 12:25:53 +01:00
$this -> assertTrue ( true , '->nextAll() throws an \InvalidArgumentException if the node list is empty' );
}
2010-04-15 13:41:42 +01:00
}
2010-05-06 12:25:53 +01:00
public function testPreviousAll ()
2010-04-15 13:41:42 +01:00
{
2012-05-07 13:03:55 +01:00
$crawler = $this -> createTestCrawler () -> filterXPath ( '//li' ) -> eq ( 2 );
2010-05-06 12:25:53 +01:00
$this -> assertNotSame ( $crawler , $crawler -> previousAll (), '->previousAll() returns a new instance of a crawler' );
2010-08-20 22:09:55 +01:00
$this -> assertInstanceOf ( 'Symfony\\Component\\DomCrawler\\Crawler' , $crawler , '->previousAll() returns a new instance of a crawler' );
2010-05-06 12:25:53 +01:00
$nodes = $crawler -> previousAll ();
$this -> assertEquals ( 2 , $nodes -> count ());
$this -> assertEquals ( 'Two' , $nodes -> eq ( 0 ) -> text ());
2010-05-07 15:09:11 +01:00
try {
2012-05-07 13:03:55 +01:00
$this -> createTestCrawler () -> filterXPath ( '//ol' ) -> previousAll ();
2010-05-06 12:25:53 +01:00
$this -> fail ( '->previousAll() throws an \InvalidArgumentException if the node list is empty' );
2010-05-07 15:09:11 +01:00
} catch ( \InvalidArgumentException $e ) {
2010-05-06 12:25:53 +01:00
$this -> assertTrue ( true , '->previousAll() throws an \InvalidArgumentException if the node list is empty' );
}
2010-04-15 13:41:42 +01:00
}
2010-05-06 12:25:53 +01:00
public function testChildren ()
2010-04-15 13:41:42 +01:00
{
2012-05-07 13:03:55 +01:00
$crawler = $this -> createTestCrawler () -> filterXPath ( '//ul' );
2010-05-06 12:25:53 +01:00
$this -> assertNotSame ( $crawler , $crawler -> children (), '->children() returns a new instance of a crawler' );
2010-08-20 22:09:55 +01:00
$this -> assertInstanceOf ( 'Symfony\\Component\\DomCrawler\\Crawler' , $crawler , '->children() returns a new instance of a crawler' );
2010-05-06 12:25:53 +01:00
$nodes = $crawler -> children ();
$this -> assertEquals ( 3 , $nodes -> count ());
$this -> assertEquals ( 'One' , $nodes -> eq ( 0 ) -> text ());
$this -> assertEquals ( 'Two' , $nodes -> eq ( 1 ) -> text ());
$this -> assertEquals ( 'Three' , $nodes -> eq ( 2 ) -> text ());
2010-05-07 15:09:11 +01:00
try {
2012-05-07 13:03:55 +01:00
$this -> createTestCrawler () -> filterXPath ( '//ol' ) -> children ();
2010-05-06 12:25:53 +01:00
$this -> fail ( '->children() throws an \InvalidArgumentException if the node list is empty' );
2010-05-07 15:09:11 +01:00
} catch ( \InvalidArgumentException $e ) {
2010-05-06 12:25:53 +01:00
$this -> assertTrue ( true , '->children() throws an \InvalidArgumentException if the node list is empty' );
}
2013-05-16 01:06:15 +01:00
try {
$crawler = new Crawler ( '<p></p>' );
$crawler -> filter ( 'p' ) -> children ();
$this -> assertTrue ( true , '->children() does not trigger a notice if the node has no children' );
} catch ( \PHPUnit_Framework_Error_Notice $e ) {
$this -> fail ( '->children() does not trigger a notice if the node has no children' );
}
2010-04-15 13:41:42 +01:00
}
2010-05-06 12:25:53 +01:00
public function testParents ()
2010-04-15 13:41:42 +01:00
{
2012-05-07 13:03:55 +01:00
$crawler = $this -> createTestCrawler () -> filterXPath ( '//li[1]' );
2010-05-06 12:25:53 +01:00
$this -> assertNotSame ( $crawler , $crawler -> parents (), '->parents() returns a new instance of a crawler' );
2010-08-20 22:09:55 +01:00
$this -> assertInstanceOf ( 'Symfony\\Component\\DomCrawler\\Crawler' , $crawler , '->parents() returns a new instance of a crawler' );
2010-05-06 12:25:53 +01:00
$nodes = $crawler -> parents ();
$this -> assertEquals ( 3 , $nodes -> count ());
2012-05-07 13:03:55 +01:00
$nodes = $this -> createTestCrawler () -> filterXPath ( '//html' ) -> parents ();
2010-05-06 12:25:53 +01:00
$this -> assertEquals ( 0 , $nodes -> count ());
2010-05-07 15:09:11 +01:00
try {
2012-05-07 13:03:55 +01:00
$this -> createTestCrawler () -> filterXPath ( '//ol' ) -> parents ();
2010-05-06 12:25:53 +01:00
$this -> fail ( '->parents() throws an \InvalidArgumentException if the node list is empty' );
2010-05-07 15:09:11 +01:00
} catch ( \InvalidArgumentException $e ) {
2010-05-06 12:25:53 +01:00
$this -> assertTrue ( true , '->parents() throws an \InvalidArgumentException if the node list is empty' );
}
2010-04-15 13:41:42 +01:00
}
2010-05-06 12:25:53 +01:00
2013-04-20 13:52:51 +01:00
public function testBaseTag ()
{
$crawler = new Crawler ( '<html><base href="http://base.com"><a href="link"></a></html>' );
$this -> assertEquals ( 'http://base.com/link' , $crawler -> filterXPath ( '//a' ) -> link () -> getUri ());
$crawler = new Crawler ( '<html><base href="//base.com"><a href="link"></a></html>' , 'https://domain.com' );
2013-12-28 08:32:39 +00:00
$this -> assertEquals ( 'https://base.com/link' , $crawler -> filterXPath ( '//a' ) -> link () -> getUri (), '<base> tag can use a schema-less URL' );
2013-04-20 13:52:51 +01:00
$crawler = new Crawler ( '<html><base href="path/"><a href="link"></a></html>' , 'https://domain.com' );
$this -> assertEquals ( 'https://domain.com/path/link' , $crawler -> filterXPath ( '//a' ) -> link () -> getUri (), '<base> tag can set a path' );
}
2010-05-06 12:25:53 +01:00
public function createTestCrawler ( $uri = null )
2010-04-15 13:41:42 +01:00
{
2010-05-06 12:25:53 +01:00
$dom = new \DOMDocument ();
$dom -> loadHTML ( '
< html >
< body >
< a href = " foo " > Foo </ a >
< a href = " /foo " > Fabien\ ' s Foo </ a >
< a href = " /foo " > Fabien " s Foo</a>
< a href = " /foo " > \ ' Fabien " s Foo</a>
< a href = " /bar " >< img alt = " Bar " /></ a >
< a href = " /bar " >< img alt = " Fabien \ 's Bar " /></ a >
< a href = " /bar " >< img alt = " Fabien"s Bar " /></ a >
< a href = " /bar " >< img alt = " \ ' Fabien"s Bar " /></ a >
2010-09-02 19:47:15 +01:00
< a href = " ?get=param " > GetLink </ a >
2013-03-27 22:52:06 +00:00
< form action = " foo " id = " FooFormId " >
< input type = " text " value = " TextValue " name = " TextName " />
2010-05-06 12:25:53 +01:00
< input type = " submit " value = " FooValue " name = " FooName " id = " FooId " />
< input type = " button " value = " BarValue " name = " BarName " id = " BarId " />
< button value = " ButtonValue " name = " ButtonName " id = " ButtonId " />
</ form >
2013-03-27 22:52:06 +00:00
< input type = " submit " value = " FooBarValue " name = " FooBarName " form = " FooFormId " />
< input type = " text " value = " FooTextValue " name = " FooTextName " form = " FooFormId " />
2010-05-06 12:25:53 +01:00
< ul class = " first " >
< li class = " first " > One </ li >
< li > Two </ li >
< li > Three </ li >
</ ul >
< ul >
< li > One Bis </ li >
< li > Two Bis </ li >
< li > Three Bis </ li >
</ ul >
</ body >
</ html >
' );
return new Crawler ( $dom , $uri );
2010-04-15 13:41:42 +01:00
}
2010-05-06 12:25:53 +01:00
protected function createDomDocument ()
{
$dom = new \DOMDocument ();
$dom -> loadXML ( '<html><div class="foo"></div></html>' );
2010-04-15 13:41:42 +01:00
2010-05-06 12:25:53 +01:00
return $dom ;
}
2010-04-15 13:41:42 +01:00
2010-05-06 12:25:53 +01:00
protected function createNodeList ()
{
$dom = new \DOMDocument ();
$dom -> loadXML ( '<html><div class="foo"></div></html>' );
$domxpath = new \DOMXPath ( $dom );
2010-04-15 13:41:42 +01:00
2010-05-06 12:25:53 +01:00
return $domxpath -> query ( '//div' );
}
2010-04-15 13:41:42 +01:00
}