createCrawler example

class NativeParserCrawlerTest extends AbstractCrawlerTestCase
{
    public static function getDoctype(): string
    {
        return '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
    }

    public function testAddHtmlContentWithErrors()
    {
        $internalErrors = libxml_use_internal_errors(true);

        $crawler = $this->createCrawler();
        $crawler->addHtmlContent(<<<'EOF' <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> </head> <body> <nav><a href="#"><a href="#"></nav> </body> </html> EOF
            , 'UTF-8');

        
abstract class AbstractCrawlerTestCase extends TestCase
{
    abstract public static function getDoctype(): string;

    protected function createCrawler($node = null, string $uri = null, string $baseHref = null, bool $useHtml5Parser = true)
    {
        return new Crawler($node$uri$baseHref$useHtml5Parser);
    }

    public function testConstructor()
    {
        $crawler = $this->createCrawler();
        $this->assertCount(0, $crawler, '__construct() returns an empty crawler');

        $doc = new \DOMDocument();
        $node = $doc->createElement('test');

        $crawler = $this->createCrawler($node);
        $this->assertCount(1, $crawler, '__construct() takes a node as a first argument');
    }

    public function testGetUri()
    {
        
class Html5ParserCrawlerTest extends AbstractCrawlerTestCase
{
    public static function getDoctype(): string
    {
        return '<!DOCTYPE html>';
    }

    public function testAddHtml5()
    {
        // Ensure a bug specific to the DOM extension is fixed (see https://github.com/symfony/symfony/issues/28596)         $crawler = $this->createCrawler();
        $crawler->add($this->getDoctype().'<html><body><h1><p>Foo</p></h1></body></html>');
        $this->assertEquals('Foo', $crawler->filterXPath('//h1')->text(), '->add() adds nodes from a string');
    }

    /** @dataProvider validHtml5Provider */
    public function testHtml5ParserParseContentStartingWithValidHeading(string $content)
    {
        $crawler = $this->createCrawler();
        $crawler->addHtmlContent($content);
        self::assertEquals(
            'Foo',
            
Home | Imprint | This part of the site doesn't use cookies.