* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\DomCrawler\Tests; use Symfony\Component\DomCrawler\Crawler; class CrawlerTest extends \PHPUnit_Framework_TestCase { public function testConstructor() { $crawler = new Crawler(); $this->assertCount(0, $crawler, '__construct() returns an empty crawler'); $crawler = new Crawler(new \DOMNode()); $this->assertCount(1, $crawler, '__construct() takes a node as a first argument'); } /** * @covers Symfony\Component\DomCrawler\Crawler::add */ public function testAdd() { $crawler = new Crawler(); $crawler->add($this->createDomDocument()); $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->add() adds nodes from a \DOMDocument'); $crawler = new Crawler(); $crawler->add($this->createNodeList()); $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->add() adds nodes from a \DOMNodeList'); foreach ($this->createNodeList() as $node) { $list[] = $node; } $crawler = new Crawler(); $crawler->add($list); $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->add() adds nodes from an array of nodes'); $crawler = new Crawler(); $crawler->add($this->createNodeList()->item(0)); $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->add() adds nodes from an \DOMNode'); $crawler = new Crawler(); $crawler->add('Foo'); $this->assertEquals('Foo', $crawler->filterXPath('//body')->text(), '->add() adds nodes from a string'); } /** * @covers Symfony\Component\DomCrawler\Crawler::addHtmlContent */ public function testAddHtmlContent() { $crawler = new Crawler(); $crawler->addHtmlContent('
', 'UTF-8'); $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addHtmlContent() adds nodes from an HTML string'); $crawler->addHtmlContent('', 'UTF-8'); $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'); } /** * @covers Symfony\Component\DomCrawler\Crawler::addHtmlContent */ public function testAddHtmlContentCharset() { $crawler = new Crawler(); $crawler->addHtmlContent('
Tiếng Việt', 'UTF-8'); $this->assertEquals('Tiếng Việt', $crawler->filterXPath('//div')->text()); } /** * @covers Symfony\Component\DomCrawler\Crawler::addHtmlContent */ public function testAddHtmlContentInvalidBaseTag() { $crawler = new Crawler(null, 'http://symfony.com'); $crawler->addHtmlContent('', 'UTF-8'); $this->assertEquals('http://symfony.com/contact', current($crawler->filterXPath('//a')->links())->getUri(), '->addHtmlContent() correctly handles a non-existent base tag href attribute'); } /** * @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()); } /** * @covers Symfony\Component\DomCrawler\Crawler::addHtmlContent */ public function testAddHtmlContentWithErrors() { libxml_use_internal_errors(true); $crawler = new Crawler(); $crawler->addHtmlContent(<< EOF , 'UTF-8'); $errors = libxml_get_errors(); $this->assertCount(1, $errors); $this->assertEquals("Tag nav invalid\n", $errors[0]->message); libxml_clear_errors(); libxml_use_internal_errors(false); } /** * @covers Symfony\Component\DomCrawler\Crawler::addXmlContent */ public function testAddXmlContent() { $crawler = new Crawler(); $crawler->addXmlContent('
', 'UTF-8'); $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addXmlContent() adds nodes from an XML string'); } /** * @covers Symfony\Component\DomCrawler\Crawler::addXmlContent */ public function testAddXmlContentCharset() { $crawler = new Crawler(); $crawler->addXmlContent('
Tiếng Việt
', 'UTF-8'); $this->assertEquals('Tiếng Việt', $crawler->filterXPath('//div')->text()); } /** * @covers Symfony\Component\DomCrawler\Crawler::addXmlContent */ public function testAddXmlContentWithErrors() { libxml_use_internal_errors(true); $crawler = new Crawler(); $crawler->addXmlContent(<<
EOF , 'UTF-8'); $this->assertTrue(count(libxml_get_errors()) > 1); libxml_clear_errors(); libxml_use_internal_errors(false); } /** * @covers Symfony\Component\DomCrawler\Crawler::addContent */ public function testAddContent() { $crawler = new Crawler(); $crawler->addContent('
', 'text/html; charset=UTF-8'); $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addContent() adds nodes from an HTML string'); $crawler = new Crawler(); $crawler->addContent('
', 'text/html; charset=UTF-8; dir=RTL'); $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addContent() adds nodes from an HTML string with extended content type'); $crawler = new Crawler(); $crawler->addContent('
'); $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addContent() uses text/html as the default type'); $crawler = new Crawler(); $crawler->addContent('
', 'text/xml; charset=UTF-8'); $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addContent() adds nodes from an XML string'); $crawler = new Crawler(); $crawler->addContent('
', 'text/xml'); $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addContent() adds nodes from an XML string'); $crawler = new Crawler(); $crawler->addContent('foo bar', 'text/plain'); $this->assertCount(0, $crawler, '->addContent() does nothing if the type is not (x|ht)ml'); } /** * @covers Symfony\Component\DomCrawler\Crawler::addDocument */ public function testAddDocument() { $crawler = new Crawler(); $crawler->addDocument($this->createDomDocument()); $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addDocument() adds nodes from a \DOMDocument'); } /** * @covers Symfony\Component\DomCrawler\Crawler::addNodeList */ public function testAddNodeList() { $crawler = new Crawler(); $crawler->addNodeList($this->createNodeList()); $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addNodeList() adds nodes from a \DOMNodeList'); } /** * @covers Symfony\Component\DomCrawler\Crawler::addNodes */ public function testAddNodes() { foreach ($this->createNodeList() as $node) { $list[] = $node; } $crawler = new Crawler(); $crawler->addNodes($list); $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addNodes() adds nodes from an array of nodes'); } /** * @covers Symfony\Component\DomCrawler\Crawler::addNode */ public function testAddNode() { $crawler = new Crawler(); $crawler->addNode($this->createNodeList()->item(0)); $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addNode() adds nodes from an \DOMNode'); } public function testClear() { $crawler = new Crawler(new \DOMNode()); $crawler->clear(); $this->assertCount(0, $crawler, '->clear() removes all the nodes from the crawler'); } public function testEq() { $crawler = $this->createTestCrawler()->filterXPath('//li'); $this->assertNotSame($crawler, $crawler->eq(0), '->eq() returns a new instance of a crawler'); $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->eq() returns a new instance of a crawler'); $this->assertEquals('Two', $crawler->eq(1)->text(), '->eq() returns the nth node of the list'); $this->assertCount(0, $crawler->eq(100), '->eq() returns an empty crawler if the nth node does not exist'); } public function testEach() { $data = $this->createTestCrawler()->filterXPath('//ul[1]/li')->each(function ($node, $i) { return $i.'-'.$node->text(); }); $this->assertEquals(array('0-One', '1-Two', '2-Three'), $data, '->each() executes an anonymous function on each node of the list'); } public function testReduce() { $crawler = $this->createTestCrawler()->filterXPath('//ul[1]/li'); $nodes = $crawler->reduce(function ($node, $i) { return $i == 1 ? false : true; }); $this->assertNotSame($nodes, $crawler, '->reduce() returns a new instance of a crawler'); $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $nodes, '->reduce() returns a new instance of a crawler'); $this->assertCount(2, $nodes, '->reduce() filters the nodes in the list'); } public function testAttr() { $this->assertEquals('first', $this->createTestCrawler()->filterXPath('//li')->attr('class'), '->attr() returns the attribute of the first element of the node list'); try { $this->createTestCrawler()->filterXPath('//ol')->attr('class'); $this->fail('->attr() throws an \InvalidArgumentException if the node list is empty'); } catch (\InvalidArgumentException $e) { $this->assertTrue(true, '->attr() throws an \InvalidArgumentException if the node list is empty'); } } public function testText() { $this->assertEquals('One', $this->createTestCrawler()->filterXPath('//li')->text(), '->text() returns the node value of the first element of the node list'); try { $this->createTestCrawler()->filterXPath('//ol')->text(); $this->fail('->text() throws an \InvalidArgumentException if the node list is empty'); } catch (\InvalidArgumentException $e) { $this->assertTrue(true, '->text() throws an \InvalidArgumentException if the node list is empty'); } } public function testHtml() { $this->assertEquals('Bar', $this->createTestCrawler()->filterXPath('//a[5]')->html()); $this->assertEquals('