-
Notifications
You must be signed in to change notification settings - Fork 5
The Basics
Phactory is a lightweight, extensible library for creating test data. Inspired by Machinist, Phactory works by defining blueprints from which objects can be created.
Here's what a basic Phactory looks like:
<?php
class UserPhactory
{
public function blueprint()
{
return array(
'name' => 'Example User',
'email' => 'user#{sn}@example.org',
);
}
}If we then wanted to create an instance of a user object we would do this:
<?php
$user = Phactory::user();
echo $user->name; // "Example User"
echo $user->email; // "[email protected]"Pretty simple no?
There are a few basic conventions to adhere to. Phactories should be named '{name}Phactory', and all Phactories must implement the blueprint method.
What if you need a slightly different type of object, say an admin user for example?
<?php
class UserPhactory
{
public function blueprint()
{
return array(
'name' => 'Example User',
'email' => 'user#{sn}@example.org',
);
}
public function admin()
{
return array(
'admin' => true,
);
}
}Now if we call this:
<?php
$user = Phactory::user('admin');
echo $user->name; // "Example User"
echo $user->email; // "[email protected]"
echo $user->admin ? 'an admin' : 'not an admin'; // 'an admin'we see an admin user has been created. Not only that but it has inherited all of the properties from the base blueprint.
So all of this is great, but sometimes you've got a test that needs something a little bit special, for these cases you can override the blueprint properties without needing to modify our Phactory:
<?php
$user = Phactory::user(array(
'name' => 'Fronzel Neekburm',
));
echo $user->name; // "Fronzel Neekburm"
echo $user->email; // "[email protected]"Naturally you can also combine variations and overrides like so:
<?php
$user = Phactory::user('admin', array(
'email' => '[email protected]',
));
echo $user->name; // "Example User"
echo $user->email; // "[email protected]"
echo $user->admin ? 'an admin' : 'not an admin'; // 'an admin'