sfPropelTestPlugin plugin
The sfPropelTestPlugin is a symfony plugin for quickly creating unit tests that require database interaction.
Please Note this has been superceded by sfModelTestPlugin
Features
- Uses a separate test database
- Automatically reloads test data before each group of tests
- Easy to install and use
Installation
- Use symfony cli
symfony plugin-install http://plugins.symfony-project.com/sfPropelTestPlugin
- or uncompress the archive
cd /path/to/your/symfony/project/plugins/
tar -xzf /path/to/sfPropelTestPlugin-1.0.tgz
Setup
- Create a database for testing. To load the schema, just use your existing sql
mysql -u username -p myApp_test < data/sql/lib.model.schema.sql
- Add the test database to databases.yml
all:
propel:
class: sfPropelDatabase
param:
phptype: mysql
host: localhost
database: myApp
username: myUser
password: myPasswd
test:
propel:
class: sfPropelDatabase
param:
phptype: mysql
host: localhost
database: myApp_test
username: myUser
password: myPasswd
- Create one or more YAML data files for testing, and put them in tests/fixtures. These files are just like the test data files you might put in data/fixtures, except they will be reloaded into the test database at the beginning of every test method. (see "Usage")
- Create your unit tests! You will need to include the following two lines at the top of your unit tests, (replacing 'myApp' with, of course, the name of your app):
define('SF_APP', 'myApp');
include(dirname(__FILE__).'/../../plugins/sfPropelTestPlugin/bootstrap/propel-unit.php');
Usage
Unit tests that use this plugin are almost identical in form and function to those in Ruby on Rails. A unit test file will look like this:
define('SF_APP', 'myApp');
include(dirname(__FILE__).'/../../plugins/sfPropelTestPlugin/bootstrap/propel-unit.php');
class myUnitTest extends sfPropelTest
{
public function test_user()
{
$user = new User();
$user->setFirstName('Joe');
$user->setLastName('Smith');
$user->setUsername('bobbyjoe');
$user->save();
$joe = UserPeer::getBy(UserPeer::USERNAME, 'bobbyjoe');
$this->ok($joe, 'Joe exists!');
}
public function test_dataDelete()
{
$joe = UserPeer::getBy(UserPeer::USERNAME, 'bobbyjoe');
$this->ok(!$joe, 'Joe no longer exists.');
}
}
$test = new myUnitTest();
$test->execute();
Each unit test is a class that extends sfPropelTest (which itself extends lime_test). If you are familiar with Rails, the above should look familiar. If not, here is the quick summary: For every test case, you write a class; every class method that begins with "test_" gets called in turn; before each test method is called, the data is reloaded and the setup() method is called (if you defined one); after each test method is called, the teardown() method is called (if you defined one). All the test functions available from line are methods of your class, so call them with $this.
Attachments
- sfPropelTestPlugin-0.9.0.tgz (3.8 kB) - added by Rob.Rosenbaum on 09/13/07 14:08:57.