This may be a dumb question, but do you make unit tests for the HTML output of your PHP functions/scripts?
I try to keep my HTML and my PHP separate - i.e. HTML includes
You can use PHPUnit. It has Output testing.
http://www.phpunit.de/manual/3.0/en/testcase-extensions.html
There is an extension for PHPUnit that does html validation here: https://github.com/xvoland/html-validate
One very simple way to do this is with output buffering.
eg
ob_start();
function_which_produces_some_output();
$this->assertEquals( ob_get_clean(), '<p>Expected Output Here</p>');
In some cases (such as CakePHP Helpers), the purpose of a class or function is to generate consistent HTML for you. In such cases, it's important to test that the expected properties of the generated unit of HTML are correct for given inputs. The question is definitely valid in that context.
PHPUnit provides an assertTag() function for this purpose.
However to echo the others; it's important to stress that unit testing should be done on the smallest possible components of your project, and not entire rendered web pages. There are other tools (Selenium for example) that are designed for ensuring that those individual components are integrated together properly.
I found the SimpleTest framework to be very useful, usually i use it for integration-tests and PhpUnit for unit-tests. They spare me a lot of manually submitted formulars, which i would do otherwise over and over again.
It became my habit to follow this points, when doing such integrations tests:
$webTestCase->assertText('...');
or $webTestCase->assertPattern('/.../');
.With some tiny helper functions, you can gain a lot of robustness. The following function will open a page and checks if the page was opened successfully without warnings. Since there is no compiler for PHP that can give out warnings at design time, you can at least make sure that your code will not produce errors or warnings.
public static function openPageWithNoWarnings($webTestCase, $page, $landingPage = null)
{
// check that page can be opened successfully
$webTestCase->assertTrue($webTestCase->get($page));
// check that there are no PHP warnings
$webTestCase->assertNoPattern('/(warning:|error:)/i', 'PHP error or warning on page!');
// check if landed on expected page (maybe a redirect)
if (!empty($landingPage))
{
$url = $webTestCase->getUrl();
$file = basename(parse_url($url, PHP_URL_PATH));
$webTestCase->assertEqual($page, $file,
sprintf('Expected page "%s", got page "%s".', page, $file));
}
}
Such tests will give you not much of work, you can start with very light tests, but they give you instantly feedback if something fails, with only one mouse click.
A lot of these links are either dead or old. Take a look at http://phpfui.com/PHPFUI/HTMLUnitTester It validated HTML and CSS against w3.org standards. I wrote it because I could not find any recent and currently supported PHP library to do it. Other solutions were based on an old version of PHPUnit or not compatible with modern PHP code.
It uses the W3C HTML validator service which I would recommend running locally. It is all explained in the readme file in configuration. The beauty of the library is that it does not do the actual validation, but calls the W3C.org code, so it will always be up to date if you keep your local install current.
Hope this helps.