addDocument() публичный Метод

Adds a \DOMDocument to the list of nodes.
public addDocument ( DOMDocument $dom )
$dom DOMDocument A \DOMDocument instance
 /**
  * assertSelectEquals("#binder .name", "Chuck", true,  $xml);  // any?
  * assertSelectEquals("#binder .name", "Chuck", false, $xml);  // none?
  *
  * @param array                 $selector
  * @param string                $content
  * @param integer|boolean|array $count
  * @param mixed                 $actual
  * @param string                $message
  * @param boolean               $isHtml
  * @since Method available since Release 1.0.0
  *
  * @throws PHPUnit_Framework_Exception
  */
 public static function assertSelectEquals($selector, $content, $count, $actual, $message = '', $isHtml = true)
 {
     $crawler = new Crawler();
     if ($actual instanceof DOMDocument) {
         $crawler->addDocument($actual);
     } else {
         if ($isHtml) {
             $crawler->addHtmlContent($actual);
         } else {
             $crawler->addXmlContent($actual);
         }
     }
     $crawler = $crawler->filter($selector);
     if (is_string($content)) {
         $crawler = $crawler->reduce(function (Crawler $node, $i) use($content) {
             if ($content === '') {
                 return $node->text() === '';
             }
             if (preg_match('/^regexp\\s*:\\s*(.*)/i', $content, $matches)) {
                 return (bool) preg_match($matches[1], $node->text());
             }
             return strstr($node->text(), $content) !== false;
         });
     }
     $found = count($crawler);
     if (is_numeric($count)) {
         self::assertEquals($count, $found, $message);
     } else {
         if (is_bool($count)) {
             $found = $found > 0;
             if ($count) {
                 self::assertTrue($found, $message);
             } else {
                 self::assertFalse($found, $message);
             }
         } else {
             if (is_array($count) && (isset($count['>']) || isset($count['<']) || isset($count['>=']) || isset($count['<=']))) {
                 if (isset($count['>'])) {
                     self::assertTrue($found > $count['>'], $message);
                 }
                 if (isset($count['>='])) {
                     self::assertTrue($found >= $count['>='], $message);
                 }
                 if (isset($count['<'])) {
                     self::assertTrue($found < $count['<'], $message);
                 }
                 if (isset($count['<='])) {
                     self::assertTrue($found <= $count['<='], $message);
                 }
             } else {
                 throw new PHPUnit_Framework_Exception('Invalid count format');
             }
         }
     }
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     /** @var FormatterHelper $formatter */
     $formatter = $this->getHelper('formatter');
     $crawlerXml = new Crawler(file_get_contents(__DIR__ . '/test.xml'));
     $output->writeln($formatter->formatBlock('Sibling test', 'question', true));
     $crawlerXml->filter('node')->siblings()->each(function (Crawler $node) use($output) {
         $output->writeln(sprintf('<info>%s</info>', $node->text()));
     });
     $output->writeln($formatter->formatBlock('Test adding content', 'question', true));
     $crawler = new Crawler('<root><node id="1" /><node id="2" /></root>');
     $document = new \DOMDocument();
     $document->loadXML('<root><node id="3" /><node id="4" /></root>');
     $crawler->addDocument($document);
     $crawler->filter('node')->each(function (Crawler $node) use($output) {
         $output->writeln($node->nodeName() . ' ' . $node->attr('id'));
     });
 }
Пример #3
0
 public function testAddDocument()
 {
     $crawler = new Crawler();
     $crawler->addDocument($this->createDomDocument());
     $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addDocument() adds nodes from a \\DOMDocument');
 }
Пример #4
0
 /**
  * @covers Symfony\Component\DomCrawler\Crawler::addDocument
  */
 public function testAddDocument()
 {
     if (!class_exists('Symfony\\Component\\CssSelector\\CssSelector')) {
         $this->markTestSkipped('The "CssSelector" component is not available');
     }
     $crawler = new Crawler();
     $crawler->addDocument($this->createDomDocument());
     $this->assertEquals('foo', $crawler->filter('div')->attr('class'), '->addDocument() adds nodes from a \\DOMDocument');
 }
Пример #5
0
 public function getXmlContent($filename, $parentTag)
 {
     $opts = array('http' => array('header' => 'Accept-Charset: UTF-8, *;q=0'));
     $context = stream_context_create($opts);
     $document = new \DOMDocument();
     $fileData = file_get_contents($filename, false, $context);
     $document->loadXML($fileData);
     $crawler = new Crawler();
     $crawler->addDocument($document);
     $content = $crawler->filter($parentTag)->children();
     ini_set('MAX_EXECUTION_TIME', -1);
     return $content;
 }