zsDoctrineRecordBuilderPlugin - 0.1.0

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

You are currently browsing
the website for symfony 1

Visit the Symfony2 website


« Back to the Plugins Home

Signin


Forgot your password?
Create an account

Tools

Stats

advanced search
Information Readme Releases Changelog Contribute
Show source

About

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

Version 0.1

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.

TODO

Add tests covering simple and advanced usages for ::stub(), ::attributes(), and ::create()

Usage

In file test/bootstrap/unit.php

require_once __DIR__ . '/../fixtures/factories.php';

In file test/fixtures/factories.php

sfProjectConfiguration::getActive()->loadHelpers('zsDoctrineRecordBuilder');

Defining factories

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';
});

Invoking factories

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

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();
});

And

That's all