symfony testing cheat sheet


Load symfony helper


Clear doctrine

Clears the first level cache (identityMap) of the models. This method ensures that records are reloaded from the db.


Clear result cache



Functional tests

Print out html source code:


Note: our extension now allows $->dumpDie() instead.


Good explanation of CSS selectors: 

Combining "not" and a list of strings - so this means check if there is neither of the strings "error,Error or ERROR" in the <body> tag.

  ->checkResponseElement('body', '!/error|Error|ERROR/')

Testing sort order: The first element is positon 0!

  ->checkResponseElement('div.ullwiki_header > div > h3 > a', 'My new test subject, updated', array('position' => 0))
  ->checkResponseElement('div.ullwiki_header > div > h3 > a', 'Testdoc', array('position' => 1))
  ->checkResponseElement('div.ullwiki_header > div > h3 > a', 'Another Testdoc', array('position' => 2))

It's currently not possible to mix id selectors with attribute selectors.


Doesn't work:


Does work:


Check Select-Box

    ->checkElement('select[id="fields_ull_project_id"] > option[value="1"][selected="selected"]', true)

Set Form Fields


->setField('fields[username]', 'superuser'))


->setField('fields[ReadGroups]', 30))

Select multiple:

->setField('fields[ReadGroups]', array(30,31))


Click on a specific, ambiguous link

  ->click('Edit', array(), array('position' => 2))

Click via css selector (recommended)


Do post request by hand with array syntax:

Example: <input name='login[username]'>

  ->post('/ullUser/login', array('login' => array('username' => 'admin', 'password' => 'admin')))

Testing tables / lists

Testing tables can be tedious because very long and unreadable css selectors are necessary. Bad example:

->checkResponseElement('table#ull_time_edit_list > tbody > tr + tr + tr + tr > td + td + td > span', '5:00')

For this purpose we created the ullDomGridSelector:

We use it in ullright by adding a getter with the ullDomGridSelector to ullTestBrowser.class.php
For examples see ullTestBrowser::getDgsUllVentoryList() and ullTestBrowser::getDgsUllVentoryEdit().

For examples of usage in functional tests see ullVentoryEditTest

Mimic complex test output

Use case: Create expected output for a complex Widget like ullWidgetDateWrite:

In the widget:


This output can be directly copied into the functional test.

Unit tests

Testing HTML in unit Tests (e.g. for widgets)

$w = new sfWidgetFormReCaptcha(array('public_key' => $PUBLIC_KEY));
$dom = new DomDocument('1.0', 'utf-8');
$c = new sfDomCssSelector($dom);
$t->is(count($c->matchSingle('script[src^="http://"]')->getNodes()), 1, '->render() uses the HTTP ReCaptcha URL by default'); 

Set user language


Set "logged in" user

$instance->getUser()->setAttribute('user_id', $userId);