Symfony 1 - Test Unit - With Doctrine and a database

In this test unit tutorial of the Symfony 1 framework, we will take examples to show how it works with Doctrine and a database.

First of all, you have to set up a database to test and create some fixtures.
If you have not done it yet, take a look at this tutorial:

Then we can create a special database for tests.

For this new test database, we can either copy/paste all fixture files from /data/fixtures/* in /test/fixtures/* or use directly the fixtures of the /data/fixtures/*.

The difference is that you can separate the classic fixtures from the test ones.
But, the backward is that you have to copy/paste all fixtures modified each time you want ot test your fixtures.

For the first option, we have to create a new Doctrine file:

// test/bootstrap/Doctrine.php

$configuration = ProjectConfiguration::getApplicationConfiguration( 'frontend', 'test', true);

new sfDatabaseManager($configuration);

And of course, copy/paste all fixtures from data/fixtures to test/fixtures.

If you prefer to use directly fixtures from data/fixtures, create this file instead of the last one:

// test/bootstrap/Doctrine.php

$configuration = ProjectConfiguration::getApplicationConfiguration( 'frontend', 'test', true);

new sfDatabaseManager($configuration);

You can note the difference, indeed, we changed the line:




You can see all these project directories in the file


Do not forget to add a test database configuration:

// config/databases.yml


    class: sfDoctrineDatabase
      dsn: 'mysql:host=localhost;dbname=badprog_test'
      username: root
      password: b


Let's now create the class test by naming it with the table you want to test.
Note that Symfony has an integrated  test unit framework named lime.
In our example, we want to test the BadprogHello table:

// test/unit/model/BadprogHelloTest.php


$t = new lime_test(1);
$text = Doctrine_Core::getTable('BadprogHello')->createQuery()->fetchOne();

$t->comment('Do you have a text?');
$t->is($text->getText(), 'Hello World!');

Of course, we suppose that your hello.yml fixture is like this:

// data/fixtures/hello.yml
    name: Try 1
      Hello World!

In the code above, name and text are columns of the badprog_hello table in our database.
You use only the text column in our test.
You can of course test the name too.

In a command line, let's try this:

$ php symfony test:unit BadprogHello

This code above will delete all the tables of the database test and repopulate them as the fixtures say.
Why? Because it calls the code you specified, before, in the test/bootstrap/Doctrine.php file, more specially the Doctrine_Core::loadData().

The result if you have the correct matching:

# Do you have a text?
ok 1
# Looks like everything went fine.

That was your first test unit with Symfony 1, Doctrine, lime and a database.
Well done! kiss

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Enter the characters shown in the image.