0

I have a template class that grabs HTML and basically returns html to the caller. How do I test the caller using PHP Unit? Do I just assertTrue(is_string(call_function))? It seems like a stupid test, and I thought I may be testing it improperly.

Strawberry
  • 66,024
  • 56
  • 149
  • 197

1 Answers1

0

Is the returned HTML supposed to be well-formed? If so you could validate it.

And/or if there is always supposed to be a certain node, or string of text, present you could check for its existence. Using strpos, regexes, or a proper DOM parser.

This StackOverflow question gives you some ideas for ways to parse and query your HTML: How do you parse and process HTML/XML in PHP?

More generally, the way I usually approach how to test a function that returns a string is to use:

$html=call_function();
$this->assertEquals("dummy",$html);

Then it fails, but tells me the correct output, so I paste that in:

$html=call_function();
$expected=<<<EOD
<html>
...
</html>
EOD;
$this->assertEquals($expected,$html);

If it fails again I then study the differences between the two correct answers I have. If this is a good unit test should they really even be different? Do I want to use a mock object to replace some uncontrollable aspect of the system? (E.g. if the HTML it is returning is google search results, then maybe I want a mock object to simulate calling google, but always return exactly the same search results page.)

If the only differences are timestamps I might use regexes to hunt-and-destroy them, to give me a string that should always be the same, e.g.

$html=preg_replace('/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/','[TIMESTAMP]',$html);

ADDITION If the HTML string is very big, one alternative is to use md5() to reduce it to a short string. This will still warn you when something breaks, but the (big) downside is when it breaks you won't know where. If you are concerned about that then it is better to use the DOM approach (or its poor cousin, regexes) to just cherry-pick a few key parts of the HTML to test.

Community
  • 1
  • 1
Darren Cook
  • 27,837
  • 13
  • 117
  • 217
  • I mean, testing for a HUGE chunk of HTML is weird. Let's say for a template engine, if I use the template engine to call for different templates and test those calls. It would be weird to wrap a huge chunk of html in PHP just for the sake of expected result testing? – Strawberry Jul 22 '12 at 17:27
  • @Strawberry Unless you have a sadistic boss, testing is not a punishment, or something you do for the sake of it :-) You put a test around code that might conceivably break when you work on its code in the future. Template engine output can rely on so many things that I think the full output is often useful to test. You can keep `$expected` in an external file and load it with `file_get_contents`, if source code ugliness is the only concern. – Darren Cook Jul 22 '12 at 22:27
  • @Strawberry I updated my answer with an `md5` suggestion for very large files, but as noted there, I don't recommend it. – Darren Cook Jul 22 '12 at 22:31