Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion src/Codeception/Module/DataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -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
{
Expand All @@ -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()
{
Expand Down Expand Up @@ -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;
Expand Down