本文整理汇总了PHP中Symfony\Component\DomCrawler\Crawler::addDocument方法的典型用法代码示例。如果您正苦于以下问题:PHP Crawler::addDocument方法的具体用法?PHP Crawler::addDocument怎么用?PHP Crawler::addDocument使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Symfony\Component\DomCrawler\Crawler
的用法示例。
在下文中一共展示了Crawler::addDocument方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: assertSelectEquals
/**
* 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');
}
}
}
}
示例2: execute
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: testAddDocument
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: testAddDocument
/**
* @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: getXmlContent
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;
}