diff --git a/src/Codeception/Module/DataFactory.php b/src/Codeception/Module/DataFactory.php index c22eebe..4b8e67a 100644 --- a/src/Codeception/Module/DataFactory.php +++ b/src/Codeception/Module/DataFactory.php @@ -9,6 +9,7 @@ use Codeception\TestInterface; use League\FactoryMuffin\FactoryMuffin; use League\FactoryMuffin\Stores\RepositoryStore; +use League\FactoryMuffin\Stores\StoreInterface; /** * DataFactory allows you to easily generate and create test data using [**FactoryMuffin**](https://github.com/thephpleague/factory-muffin). @@ -119,6 +120,36 @@ * ```php * 'user' => 'entity|User' * ``` + * + * ### Custom store + * + * You can define a custom store for Factory Muffin using `customStore` parameter. It can be a simple class or a factory with `create` method. + * The instantiated object must implement `\League\FactoryMuffin\Stores\StoreInterface`. + * + * Store factory example: + * ```yaml + * modules: + * enabled: + * - DataFactory: + * customStore: \common\tests\store\MyCustomStoreFactory + * ``` + * + * ```php + * use League\FactoryMuffin\Stores\StoreInterface; + * + * class MyCustomStoreFactory + * { + * public function create(): StoreInterface + * { + * return CustomStore(); + * } + * } + * + * class CustomStore implements StoreInterface + * { + * // ... + * } + * ``` */ class DataFactory extends \Codeception\Module implements DependsOnModule, RequiresPackage { @@ -144,7 +175,7 @@ class DataFactory extends \Codeception\Module implements DependsOnModule, Requir */ public $factoryMuffin; - protected $config = ['factories' => null]; + protected $config = ['factories' => null, 'customStore' => null]; public function _requires() { @@ -174,6 +205,15 @@ public function _beforeSuite($settings = []) */ protected function getStore() { + if (!empty($this->config['customStore'])) { + $store = new $this->config['customStore']; + if (method_exists($store, 'create')) { + return $store->create(); + } + + return $store; + } + return $this->ormModule instanceof DataMapper ? new RepositoryStore($this->ormModule->_getEntityManager()) // for Doctrine : null;