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);

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.

makes
  • 6,438
  • 3
  • 40
  • 58
Plootor
  • 61
  • 5

1 Answers1

1

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);
...
David Harkness
  • 35,992
  • 10
  • 112
  • 134
  • Thank you David for responce but your fix doesn't help, the problem is the – Plootor Nov 14 '11 at 08:35
  • @SergiuGolban Ah, we had to use `loadXML()` instead of `loadHTML()`. See my update. – David Harkness Nov 14 '11 at 21:10
  • Thank you David till the end I found the cause; thirst of all html should be a valid one, becouse I use strict Doctype; in case it isn't a valid one you can't use facebook tag inside; and I just delete it with regex; but the best solution here for me is Selenium, it is more easy – Plootor Dec 01 '11 at 08:56