matching number in PHP simpletest webtestcase assert

68 Views Asked by At

I am writing a PHP simpletest webtestcase. The following assert does not work.

$this->assertPattern('"serverTime":\d+');

for the page response

Blah Bla "serverTime":1447147314 ,Bla Bla

However when I test the regex in an online tool it works. I need to assert that the response contains a string "serverTime":<10digitnumber>

1

There are 1 best solutions below

0
Jens A. Koch On

The function syntax is assertPattern($regexp, $x).

That means: fail, unless the regex $regexp matches $x.

The regexp you are looking for is: #\"serverTime\":(\d+){10}#.

The whole function for SimpleTest:

    public function testPattern()
    {
        $string = 'Blah Bla "serverTime":1447147314 ,Bla Bla';

        $this->assertPattern('#\"serverTime\":(\d+){10}#', $string);
    }