mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-09-18 23:46:21 +02:00
6888901468
* First cut * Temporarily disable composer.json for netlify build * POC * New recursive directory query, various refinements * Fix flexbox * new styled components plugin * Apply frontmatter delimiters * Mobile styles, animation * Search * Redesign, clean up * Nuke the cache, try again * fix file casing * Remove production env file * ID headers * Move app to new repo * Add frontmatter universally * Hide children changelogs * Add how to title * New callout tags * Revert inline code block change * Replace note callouts * Fix icons * Repalce images * Fix icon * Fix image links * Use proper SQL icon
1.5 KiB
1.5 KiB
title | summary | icon |
---|---|---|
How to use a FixtureFactory | Provide context to your tests with database fixtures | industry |
How to use a FixtureFactory
The FixtureFactory is used to manually create data structures for use with tests. For more information on fixtures see the 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.
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Core\Injector\Injector;
class MyObjectTest extends SapphireTest
{
protected $factory;
function __construct() {
parent::__construct();
$factory = Injector::inst()->create('FixtureFactory');
// Defines a "blueprint" for new objects
$factory->define('MyObject', [
'MyProperty' => 'My Default Value'
]);
$this->factory = $factory;
}
function testSomething() {
$MyObjectObj = $this->factory->createObject(
'MyObject',
['MyOtherProperty' => 'My Custom Value']
);
echo $MyObjectObj->MyProperty;
// returns "My Default Value"
echo $myPageObj->MyOtherProperty;
// returns "My Custom Value"
}
}