From 92901650b3459235f6d3a89a09595bb6383272ec Mon Sep 17 00:00:00 2001 From: Denis Markovkin Date: Wed, 23 Dec 2020 13:57:36 +0300 Subject: [PATCH 1/2] Support for custom store class --- src/Codeception/Module/DataFactory.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Codeception/Module/DataFactory.php b/src/Codeception/Module/DataFactory.php index c22eebe..4e89d97 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). @@ -144,7 +145,7 @@ class DataFactory extends \Codeception\Module implements DependsOnModule, Requir */ public $factoryMuffin; - protected $config = ['factories' => null]; + protected $config = ['factories' => null, 'storeClass' => null]; public function _requires() { @@ -174,6 +175,10 @@ public function _beforeSuite($settings = []) */ protected function getStore() { + if (!empty($this->config['storeClass'])) { + return new $this->config['storeClass']; + } + return $this->ormModule instanceof DataMapper ? new RepositoryStore($this->ormModule->_getEntityManager()) // for Doctrine : null; From b27a8e63e4249a22cf1699c7cd91f339405e2254 Mon Sep 17 00:00:00 2001 From: Denis Markovkin Date: Wed, 23 Dec 2020 18:29:28 +0300 Subject: [PATCH 2/2] Ability to use factory create method. Updated docs. --- src/Codeception/Module/DataFactory.php | 41 ++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/src/Codeception/Module/DataFactory.php b/src/Codeception/Module/DataFactory.php index 4e89d97..4b8e67a 100644 --- a/src/Codeception/Module/DataFactory.php +++ b/src/Codeception/Module/DataFactory.php @@ -120,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 { @@ -145,7 +175,7 @@ class DataFactory extends \Codeception\Module implements DependsOnModule, Requir */ public $factoryMuffin; - protected $config = ['factories' => null, 'storeClass' => null]; + protected $config = ['factories' => null, 'customStore' => null]; public function _requires() { @@ -175,8 +205,13 @@ public function _beforeSuite($settings = []) */ protected function getStore() { - if (!empty($this->config['storeClass'])) { - return new $this->config['storeClass']; + 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