-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME
102 lines (52 loc) · 2.28 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# 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](http://github.com/thoughtbot/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