phpunit 与 Zend_Dom_Query

发布于 2024-12-15 13:42:57 字数 930 浏览 1 评论 0原文

$domDoc = new DOMDocument();
$domDoc->loadHTML($docSrc);
$xpath  = new DOMXPath($domDoc);
$nodeList = $xpath->query("//a[@class='active' and @href='/advanced-2-0.html']");
$this->assertTrue($nodeList->length == 1);

如果 $docSrc 是这样的,则此代码效果很好:

$docSrc = '<div><a class="active" href = "/advanced-2-0.html']"></div>';

但是当我像这样将 HTML 分配给 $docSrc 时:

$this->dispatch($_SERVER['REQUEST_URI']);
$html = $this->getResponse()->getBody();

它不起作用。我打印 $html ,里面一切正常;我无法解释它,因为在这两种情况下它都是一个字符串。

我收到错误:

AccountControllerTest::testIfAllow DOMDocument::loadHTML(): 实体中未定义命名空间前缀 fb,第 54 行

我也尝试过:

$domDoc->loadHTML("$this->getResponse()->getBody()");

没有出现错误,但结果为空。

$domDoc = new DOMDocument();
$domDoc->loadHTML($docSrc);
$xpath  = new DOMXPath($domDoc);
$nodeList = $xpath->query("//a[@class='active' and @href='/advanced-2-0.html']");
$this->assertTrue($nodeList->length == 1);

This code works great if $docSrc is something like this:

$docSrc = '<div><a class="active" href = "/advanced-2-0.html']"></div>';

but when I assign HTML to $docSrc like this:

$this->dispatch($_SERVER['REQUEST_URI']);
$html = $this->getResponse()->getBody();

it doesn't work. I print $html and everything is ok inside; I can't explain it because in both cases it's a string.

I get the error:

AccountControllerTest::testIfAllow
DOMDocument::loadHTML(): Namespace prefix fb is not defined in Entity, line: 54

I also tried:

$domDoc->loadHTML("$this->getResponse()->getBody()");

Errors don't appear, but the result is empty.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒绊 2024-12-22 13:42:57

您必须在输出中使用 FBML,例如 用于“Like”按钮。您可以将输出包装在声明命名空间的 元素中。

更新:现在我正在查看我们的代码,我们还必须切换到 loadXML() 而不是 loadHTML() 并设置 libxml 的 <代码>恢复标志:

$html = '<html xmlns:fb="http://www.facebook.com/2008/fbml">'
        . $this->getResponse()->getBody()
        . '</html>';
$domDoc = new DOMDocument();
$domDoc->recover = true; // attempt to handle non-well-formed documents
$domDoc->loadXML($html);
...

You must be using FBML in your output, e.g. <fb:like ...> for a Like button. You can possibly wrap the output in an <html> element that declares the namespace.

Update: Now that I am looking at our code, we also had to switch to loadXML() instead of loadHTML() and set libxml's recover flag:

$html = '<html xmlns:fb="http://www.facebook.com/2008/fbml">'
        . $this->getResponse()->getBody()
        . '</html>';
$domDoc = new DOMDocument();
$domDoc->recover = true; // attempt to handle non-well-formed documents
$domDoc->loadXML($html);
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文