Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPUnit assertTrue if regex text found?

I'm using PHPUnit and trying to check if text exists on a page. The assertRegExp works but using the if statement I get the error Failed asserting that null is true.

I understand that $test is returning null, but I don't know how to have it return 1 or 0 or true/false if the text exists? Any help's appreciated thanks.

        $element = $this->byCssSelector('body')->text();
        $test = $this->assertRegExp('/find this text/i',$element);

        if($this->assertTrue($test)){
            echo 'text found';
        }
        else{
            echo 'not found';
        }
like image 708
Anagio Avatar asked Sep 26 '13 23:09

Anagio


2 Answers

Use this method in newer phpunit versions:

$this->assertMatchesRegularExpression('/PATTERN/', $yourString);
like image 109
Aboozar Ghaffari Avatar answered Oct 19 '22 03:10

Aboozar Ghaffari


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"
like image 31
hek2mgl Avatar answered Oct 19 '22 02:10

hek2mgl