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:

http://www.badprog.com/symfony-1-doctrine-set-up-a-new-database

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
<?php

include(dirname(__FILE__).'/unit.php');
$configuration = ProjectConfiguration::getApplicationConfiguration( 'frontend', 'test', true);

new sfDatabaseManager($configuration);
Doctrine_Core::loadData(sfConfig::get('sf_test_dir').'/fixtures');

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
<?php

include(dirname(__FILE__).'/unit.php');
$configuration = ProjectConfiguration::getApplicationConfiguration( 'frontend', 'test', true);

new sfDatabaseManager($configuration);
Doctrine_Core::loadData(sfConfig::get('sf_data_dir').'/fixtures');

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

Doctrine_Core::loadData(sfConfig::get('sf_test_dir').'/fixtures');

by

Doctrine_Core::loadData(sfConfig::get('sf_data_dir').'/fixtures');

You can see all these project directories in the file

lib/config/sfProjectConfiguration.class.php

Do not forget to add a test database configuration:

// config/databases.yml

...

test:
  doctrine:
    class: sfDoctrineDatabase
    param:
      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
<?php

include(dirname(__FILE__).'/../../bootstrap/Doctrine.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
BadprogHello:
  try_1:
    name: Try 1
    text:
      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:

1..1
# 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.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Refresh Type the characters you see in this picture. Type the characters you see in the picture; if you can't read them, submit the form and a new image will be generated. Not case sensitive.  Switch to audio verification.