symfony testing cheat sheet

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()

Click on a specific, ambiguous link

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

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

 

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');