Functional tests are very important to test your application. Once defined, you just have to runs them to make sure there are no side effects when you add new features.
Now, in real life, it is always time consuming to write functionnal test, or just to remember all the options from the tester class to the request parameters for each page, most of us, just skip this important step. (not to mention that project manager can just simply say : “we have to time to code all the tests, bla bla bla …, client wants this project done for last week…. “)
Last day I introduced the swToolboxPlugin which provides a handy tool to send email. It also contains a specific filter which just records user’s interactions in a symfony Functional Test format.
So mainly you navigate accross your application, submit your forms and click on links, and boom … you have a functional test almost ready to use.
- Install swToolboxPlugin
- svn version at http://svn.symfony-project.com/plugins/swToolboxPlugin/sf1.2/trunk/
- there is not package version for now
clear your cache
Edit the filters.yml file and add these configuration lines after the rendering filter
- Make sure the debug panel is enabled
A functional test menu should appear on the right side of the debug panel. When you click on the menu, you will get two options :
- Reset : reset the content of the current functional test
- Activate : activate the filter to record action.
when you are ready to create a functional test, just activate the filter, perform your actions on the website. When you are done reopen the menu and copy the generated code to a new test file.
include(dirname(__FILE__).'/../../bootstrap/functional.php'); $browser = new sfTestFunctional(new sfBrowser()); $test = $browser->test(); $conn = Doctrine::getConnectionByTableName('sw_blog_posts'); // I changed this to match the test $conn->beginTransaction(); $browser ->call('/en/articles/2008/03/13/using-soap-php-with-ntlm-authentication', 'get', array()) ->with('request')->begin() ->isParameter('module', 'swBlogPosts') ->isParameter('action', 'view') ->end() ->with('response')->begin() ->isStatusCode(200) ->end() ; $browser ->call('/en/articles/add-comment/1', 'post', array ( 'sw_blog_comment' => array ( 'name' => 'thomas', 'email' => '', 'url' => '', 'message' => 'my message', ), 'module' => 'swBlogPosts', 'action' => 'addComment', 'sf_culture' => 'en', 'id' => '1', )) ->with('request')->begin() ->isParameter('module', 'swBlogPosts') ->isParameter('action', 'addComment') ->end() ; $conn->rollback();
if you run the test you should have :
You are free now to customize the generated code to add more test. In this case I might add this following code
$form = $browser->getContext()->getActionStack()->getLastEntry()->getActionInstance()->getVar('form'); $comment = $form->getObject(); $test->is($comment->getArticleId() == 1, 'Check if the comment belong to the correct article');
As you can see it is quite easy to create complete functional test, just use your application. Please keep in mind, this tools just generated basic test from navigation, you are strongly advice to spend more time to check database relation.
Note : The “generated test” uses doctrine database connection to make a transaction and rollback the change at this end, so your database is not mess up.