zsDoctrineRecordBuilderPlugin is a symfony 1.4 plugin, for php > 5.3.x and Doctrine 1.2, which help you to build, create and stub records for testing purpose. It's strongly influenced by thoughtbot's' factory_girl
zsDoctrineRecordBuilderPlugin is in an experimental state, but the code is extremely simple and straightforward and it's quite well test covered. So, feel free to fork and contribute.
Add tests covering simple and advanced usages for ::stub(), ::attributes(), and ::create()
In file test/bootstrap/unit.php
require_once __DIR__ . '/../fixtures/factories.php';
In file test/fixtures/factories.php
sfProjectConfiguration::getActive()->loadHelpers('zsDoctrineRecordBuilder');
Here, we define a builder named 'User'. The model is guessed from the name:
zs_add_builder('User', function($user) {
$user->firstname = 'Stéphane';
$user->lastname = 'Richard';
});
We also can specify the model explicitly:
zs_add_builder(array('name' => 'Stéphane', 'model' => 'User', function($user) {
$user->firstname = 'Stéphane';
$user->lastname = 'Richard';
});
A builder can inherit from another:
zs_add_builder(array('name' => 'Stéphane', 'extends' => 'User', function($user) {
$user->firstname = 'Stéphane';
$user->lastname = 'Richard';
});
To build an object with a given factory:
$record = zs_get_builder('Stéphane')->build(); //The record is not yet persisted
$record->doSomething();
$record->save();
If you want a persisted object:
$record = zs_get_builder('Stéphane')->create();
To get an array representation of the record:
$attributes = zs_get_builder('Stéphane')->attributes();
if you just need a stub, you can retrieve an stdClass instance stubbed out:
$stub = zs_get_builder('Stéphane')->stub();
Dealing with relations is extremely straightforward:
zs_add_builder('Group', function($group) {
$group->name = 'Admin';
});
zs_add_builder('User', function($user) {
$user->firstname = 'Stéphane';
$user->lastname = 'Richard';
$user->groups[] = zs_get_builder('Group')->build();
});
That's all