2014-10-17 06:53:52 +02:00
title: How to use a FixtureFactory
# How to use a FixtureFactory
2017-07-03 03:22:12 +02:00
The [FixtureFactory ](api:SilverStripe\Dev\FixtureFactory ) is used to manually create data structures for use with tests. For more information on fixtures
2014-10-17 06:53:52 +02:00
see the [Fixtures ](../fixtures ) documentation.
In this how to we'll use a `FixtureFactory` and a custom blue print for giving us a shortcut for creating new objects
with information that we need.
2017-08-03 02:51:32 +02:00
```php
2017-10-27 04:38:27 +02:00
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Core\Injector\Injector;
2014-10-17 06:53:52 +02:00
2017-10-27 04:38:27 +02:00
class MyObjectTest extends SapphireTest
{
2014-10-17 06:53:52 +02:00
2017-10-27 04:38:27 +02:00
protected $factory;
2014-10-17 06:53:52 +02:00
2017-10-27 04:38:27 +02:00
function __construct() {
parent::__construct();
2014-10-17 06:53:52 +02:00
2017-10-27 04:38:27 +02:00
$factory = Injector::inst()->create('FixtureFactory');
2014-10-17 06:53:52 +02:00
2017-10-27 04:38:27 +02:00
// Defines a "blueprint" for new objects
$factory->define('MyObject', [
'MyProperty' => 'My Default Value'
]);
2014-10-17 06:53:52 +02:00
2017-10-27 04:38:27 +02:00
$this->factory = $factory;
}
2014-10-17 06:53:52 +02:00
2017-10-27 04:38:27 +02:00
function testSomething() {
$MyObjectObj = $this->factory->createObject(
'MyObject',
['MyOtherProperty' => 'My Custom Value']
);
2014-10-17 06:53:52 +02:00
2017-10-27 04:38:27 +02:00
echo $MyObjectObj->MyProperty;
// returns "My Default Value"
2017-08-03 05:35:09 +02:00
2017-10-27 04:38:27 +02:00
echo $myPageObj->MyOtherProperty;
// returns "My Custom Value"
}
}
2017-08-03 02:51:32 +02:00
```
2014-10-17 06:53:52 +02:00
## Related Documentation
* [Fixtures ](../fixtures )
## API Documentation
2017-07-03 03:22:12 +02:00
* [FixtureFactory ](api:SilverStripe\Dev\FixtureFactory )
* [FixtureBlueprint ](api:SilverStripe\Dev\FixtureBlueprint )