PHPUnit assertTrue if regex text found? PHPUnit assertTrue if regex text found? php php

PHPUnit assertTrue if regex text found?


assertRegExp() will return nothing. If the assertion fails - meaning the text was not found - then the following code won't get executed:

 $this->assertRegExp('/find this text/i',$element); // following code will not get executed if the text was not found // and the test will get marked as "failed"


PHPUnit is not designed to return value from assertions. Assertions by definition are meant to break the flow when they fail.

If you need to do something like this, why do you use PHPUnit at all? Use preg_match:

 $test = preg_match('/find this text/i', $element); if($test) {        echo 'text found'; } else {        echo 'text not found'; }


Use this method in newer phpunit versions:

$this->assertMatchesRegularExpression('/PATTERN/', $yourString);