How to configure symfony unit tests to use an sqlite memory database
February 9th, 2010Since Bernard Schussecks presentation at “symfony Day Cologne”, talking about lime and using sqlite memory tables for unit tests, I was wondering HOW TO DO IT
Well, there was not much talk about the how… of course this must be known to everybody – such an easy task. (Find yourself some <irony> tags to put around this).
Well, at least I did not have a clue how to do it and did not find too much information on it in the web…
So, spending some more time with testing and some rare tutorials on it, I finally made it work!!
And for you and me and everybod else, I’ll note it down for reference…
Assumptions/Prerequisits:
- We are using symfony and doctrine
- In test/fixtures we have a/some yml files with test data
First, we need to set up the test connection to use sqlite:MEMORY:
In your config/database.yml add
test:
sqlitetest:
class: sfDoctrineDatabase
param:
classname: DebugPDO
dsn: 'sqlite::memory:'
(yes, ::)
Now, lets assume (accoding to the Jobeet tutorial Day 8 ) we have a
test/bootstrap/Doctrine.php
Now, the important part is to not only LOAD the data but first create the tables:
include(dirname(__FILE__).'/unit.php');
$configuration = ProjectConfiguration::getApplicationConfiguration( 'frontend', 'test', true);
new sfDatabaseManager($configuration);
Doctrine::createTablesFromModels(dirname(__FILE__).'/../../lib/model');
Doctrine::loadData(sfConfig::get('sf_test_dir').'/fixtures');
Now, in our test files we can simply call
include(dirname(__FILE__).'/../../bootstrap/Doctrine.php'); $t = new lime_test(4); //change it with the number of tests you have
everytime
Took me quite some time to figure it out – really simpe – if you know how…
