Search code examples
phpjsonweb-servicessymfony-1.4functional-testing

Functional tests in Symfony: how can I get matching to span on several lines?


I am building some functional tests for a json api in Symfony.

Using the sfTestFunctional object to test my result, I would try to validate the following response:

{
    "result": true,
    "content": [
           "one",
           "two"
    ]
}

with something like:

$browser = new sfTestFunctional(new sfBrowser());

$browser->
    get('/hi')->
    with('response')->
    begin()->
    isStatusCode(200)->
    matches('/result\"\: true/')->
    matches('/one.*two/m')->
end()

Now This is what I get:

ok 1 - status code is 200
ok 2 - response content matches regex /result\\: true/"
not ok 3 - response content matches regex /one.*two/m

Surely, I am doing something wrong. Any hint ?


Solution

  • The regex fails.

    You should use the flag s for dotall (PCRE_DOTALL) which includes newlines.

    If this modifier is set, a dot metacharacter in the pattern matches all characters, including newlines. Without it, newlines are excluded.

    So:

    $browser->
        get('/hi')->
        with('response')->
        begin()->
        isStatusCode(200)->
        matches('/result\"\: true/')->
        matches('/one.*two/sm')->
    end()
    

    Otherwise you can make two differents tests:

    $browser->
        get('/hi')->
        with('response')->
        begin()->
        isStatusCode(200)->
        matches('/result\"\: true/')->
        matches('/\"one\"')->
        matches('/\"two\"')->
    end()