EventFellows's avatar

Unit test rendered html output NOT the source html

I'd like to check a page against unwanted/ forgotten html parts as part of phpunit tests in an automated fashion.

Use case is the following:

  • There can be a lot of dd() and var_dump() during development if something fails and things need to be debugged in a manual way
  • all of these should be removed before the next release, of course
  • sometimes one might forget one of the places where it had been used resulting in rendering some weird output for the user

I want to use phpunit to automatically detect these mistakes. E.g. the letters for <div> should never be rendered in html output, If they are something is broken. The letters int( should never be rendered, if it is there might be a forgotten var_dump of an integer...

When I do something like this:

$this->visit($route)
    ->seePageIs($route)
    ->dontSee('<div>');

This does not work because phpunit checks the pages' soures and a <div> can well be within the that code.

Is there a way to limit the phpunit tests to only check what would be rendered? (I do know that there is no browser running by default in phpunit)

0 likes
2 replies
ohffs's avatar

I guess the easiest way (off the top of my head) might be to have a single test that re-cursed through your app/ directory (or whatever other ones you wanted) and did a file_get_contents() for each .php file and then assert it doesn't contain 'bad' debug left-overs like var_dump/dd( etc. Maybe something like :

$appDir = new RecursiveDirectoryIterator('app/');
$iterator = new RecursiveIteratorIterator($appDir);
$matches = new RegexIterator($iterator, '/^.+\.php$/i', RecursiveRegexIterator::GET_MATCH);
foreach ($matches as $phpFile) {
  $contents = file_get_contents($phpFile);
  $this->assertFalse(preg_match('/(var_dump|dd\()/s', $contents));
}

Something like that anyway :-)

Please or to participate in this conversation.