symfony testing cheat sheet

Generic

Load symfony helper

sfContext::getInstance()->getConfiguration()->loadHelpers('I18N');

Clear doctrine

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

$t->clearTables();

Clear result cache

$t->clearDoctrineResultCache();

 

Functional tests

Print out html source code:

var_dump($b->getResponse()->getContent());

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

CheckResponseElement:

Good explanation of CSS selectors: http://css.maxdesign.com.au/selectutorial/index.htm 

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.

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

Testing sort order: The first element is positon 0!

$b
  ->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.

@see: http://trac.symfony-project.org/ticket/6691

Doesn't work:

$t->with('response')->checkElement('#my_field[type="hidden"][value="foobar"]');

Does work:

$t->with('response')->checkElement('input[id="my_field"][type="hidden"][value="foobar"]');

Check Select-Box

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

Set Form Fields

Input:

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

Select:

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

Select multiple:

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

 

Click on a specific, ambiguous link

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

Click via css selector (recommended)

->click('input[name="submit|action_slug=save_show"]')  

Do post request by hand with array syntax:

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

$b
  ->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:

http://trac.ullright.org/browser/trunk/plugins/ullCorePlugin/lib/tools/ullDomGridSelector.class.php

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:

var_dump(ullCoreTools::escapeSingleQuotes($return));

This output can be directly copied into the functional test.

Unit tests

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

$t->diag('->render()');
$w = new sfWidgetFormReCaptcha(array('public_key' => $PUBLIC_KEY));
$dom = new DomDocument('1.0', 'utf-8');
$dom->loadHTML($w->render('captcha'));
$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

$instance->getUser()->setCulture("de");

Set "logged in" user

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