How to write an AssertTags test in SimpleTest with regex?
-
02-07-2019 - |
Question
I wish to test a function that will generate lorem ipsum
text, but it does so within html tags. So I cant know in advance the textual content, but i know the html structure. That is what I want to test. And maybe that the length of the texts are within certain limits.
So what I am wondering is if the assertTags can do this in a way paraphrased bellow:
Result = "<p>Some text</p>";
Expected = array(
'<p' ,
'regex',
'/p'
);
assertTags(resutl, expected)
I am using SimpleTest with CakePHP, but I think it should be a general question.
Solution
$expected = array(
'<p',
'preg:/[A-Za-z\.\s\,]+/',
'/p'
);
OTHER TIPS
Extend the SimpleExpectation class and then use your new Expectation class in the assert statement
see: http://www.lastcraft.com/expectation_documentation.php#extending
the example given is for validating an IP address but should be applicable to your problem:
class ValidIp extends SimpleExpectation {
function test($ip) {
return (ip2long($ip) != -1);
}
function testMessage($ip) {
return "Address [$ip] should be a valid IP address";
}
}
then in your test
$this->assert(new ValidIp(),$server->getIp());