Answer the question
In order to leave comments, you need to log in
TDD - how to test HTML output? Regulars?!?
Inspired by "I don't write unit tests because..." are excuses . I decided to try using TDD again.
There is a certain MVC web application. Let's assume that models and controllers are covered by tests. And here is how to test views, templates, layouts, the result of which is a direct output to stdin. It is clear that the output can be intercepted through ob_*, but how to test HTML code in general (native PHP templates)?
I tried this:
On the main page there should be a link to user/new (registration). Wrote a test (PHPUnit):
function testRegisterLinkForAnonymousPresent()<br>
{<br>
$app = new App();<br>
$this->expectOutputRegex("#<a.+href='/user/new'.*>.+</a>#");<br>
$app->run();<br>
}
function testRegisterFormPresentAndValid()<br>
{<br>
$app = new App();<br>
$request = new Request('/user/new');<br>
$app->setRequest($request);<br>
$this->expectOutputRegex("#<form.+action='/user'.+method='POST'.*></form>#");<br>
$app->run();<br>
}
<form action='/user'bmethod='POST'><br>
</form>
function testRegisterLinkForAnonymousPresent()<br>
{<br>
ob_start();<br>
$app = new App();<br>
$app->run();<br>
$result = ob_get_contents();<br>
ob_end_clean();<br><br>
$this->assertSelectEquals('a[href="/user/new"]', 'Register', 1, $result);<br>
}
Answer the question
In order to leave comments, you need to log in
DOM methods, CSS/XPath selectors. Regulars are not suitable for parsing html, this is not news for a long time.
you can use selenium he can do it.
When I used ZendFramework, I used the Zend_Dom_Query component,
it can also be used separately from ZF, it supports xpath and css queries, this is very convenient regarding regular expressions.
framework.zend.com/manual/en/zend.dom.query.html
To the maximum, separate the layout from the code, preferably using some kind of template engine instead of native-php.
Then the formation of the html code based on the input data will correspond to the simplest logic, which is not particularly necessary to test.
To test the formation of these same input data.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question