Skip to content
Merged

3.0 #20

Show file tree
Hide file tree
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
1 change: 0 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
/tests export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/Robofile.php export-ignore
/*.md export-ignore
/*.yml export-ignore
15 changes: 9 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
{
"name": "codeception/module-datafactory",
"description": "DataFactory module for Codeception",
"keywords": [ "codeception" ],
"homepage": "https://codeception.com/",
"type": "library",
"license": "MIT",
"type": "library",
"keywords": [
"codeception"
],
"authors": [
{
"name": "Michael Bodnarchuk"
}
],
"minimum-stability": "dev",
"homepage": "https://codeception.com/",
"require": {
"php": "^8.0",
"codeception/codeception": "^5.0.0-alpha1",
"codeception/codeception": "^5.0.0-RC6",
"league/factory-muffin": "^3.3",
"league/factory-muffin-faker": "^2.3"
},
"minimum-stability": "RC",
"autoload": {
"classmap": [
"src/"
]
},
"config": {
"classmap-authoritative": true
"classmap-authoritative": true,
"sort-packages": true
}
}
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ A data factory module for Codeception.

## Requirements

* `PHP 7.4` or higher.
* `PHP 8.0` or higher.

## Installation

Expand Down
45 changes: 27 additions & 18 deletions src/Codeception/Module/DataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

namespace Codeception\Module;

use Codeception\Exception\ModuleException;
use Codeception\Lib\Interfaces\DataMapper;
use Codeception\Lib\Interfaces\DependsOnModule;
use Codeception\Lib\Interfaces\ORM;
use Codeception\Exception\ModuleException;
use Codeception\Lib\Interfaces\RequiresPackage;
use Codeception\Module;
use Codeception\TestInterface;
use League\FactoryMuffin\Definition;
use League\FactoryMuffin\Exceptions\DefinitionAlreadyDefinedException as FactoryMuffinDefinitionAlreadyDefinedException;
use League\FactoryMuffin\FactoryMuffin;
use League\FactoryMuffin\Stores\RepositoryStore;
use League\FactoryMuffin\Stores\StoreInterface;
Expand All @@ -27,10 +29,10 @@
* }
* ```
*
* Generation rules can be defined in a factories file.
* Generation rules can be defined in a factories file.
* Create a folder for factories files: `tests/_support/factories`.
*
* Create an ampty PHP file inside that folder `factories.php`.
* Create an empty PHP file inside that folder `factories.php`.
* Follow [FactoryMuffin documentation](https://github.com/thephpleague/factory-muffin) to set valid rules.
* Randomly generated data provided by [Faker](https://github.com/fzaninotto/Faker) library.
*
Expand Down Expand Up @@ -127,36 +129,36 @@
* ```
*
* ### 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
class DataFactory extends Module implements DependsOnModule, RequiresPackage
{
protected string $dependencyMessage = <<<EOF
ORM module (like Doctrine2) or Framework module with ActiveRecord support is required:
Expand All @@ -170,20 +172,25 @@ class DataFactory extends \Codeception\Module implements DependsOnModule, Requir

/**
* ORM module on which we we depend on.
*
* @var Module
*/
public ?ORM $ormModule = null;

public ?FactoryMuffin $factoryMuffin = null;

/**
* @var array
* @var array<string, mixed>
*/
protected $config = ['factories' => null, 'customStore' => null];
protected array $config = [
'factories' => null,
'customStore' => null,
];

public function _requires(): array
{
return [
FactoryMuffin::class => '"league/factory-muffin": "^3.0"',
FactoryMuffin::class => '"league/factory-muffin": "^3.3"',
];
}

Expand All @@ -193,8 +200,8 @@ public function _beforeSuite($settings = [])
$this->factoryMuffin = new FactoryMuffin($store);

if ($this->config['factories']) {
foreach ((array) $this->config['factories'] as $factoryPath) {
$realpath = realpath(codecept_root_dir().$factoryPath);
foreach ((array)$this->config['factories'] as $factoryPath) {
$realpath = realpath(codecept_root_dir() . $factoryPath);
if ($realpath === false) {
throw new ModuleException($this, 'The path to one of your factories is not correct. Please specify the directory relative to the codeception.yml file (ie. _support/factories).');
}
Expand Down Expand Up @@ -228,12 +235,12 @@ public function _inject(ORM $orm): void
public function _after(TestInterface $test)
{
$skipCleanup = array_key_exists('cleanup', $this->config) && $this->config['cleanup'] === false;
$cleanupOrmModule_Config = $this->ormModule->_getConfig('cleanup');
$cleanupOrmModuleConfig = $this->ormModule->_getConfig('cleanup');
if ($skipCleanup) {
return;
}

if ($cleanupOrmModule_Config) {
if ($cleanupOrmModuleConfig) {
return;
}

Expand All @@ -242,7 +249,9 @@ public function _after(TestInterface $test)

public function _depends(): array
{
return [ORM::class => $this->dependencyMessage];
return [
ORM::class => $this->dependencyMessage,
];
}

/**
Expand All @@ -268,7 +277,7 @@ public function onReconfigure($settings = [])
* ]);
* ```
*
* @throws \League\FactoryMuffin\Exceptions\DefinitionAlreadyDefinedException
* @throws FactoryMuffinDefinitionAlreadyDefinedException
*/
public function _define(string $model, array $fields): Definition
{
Expand Down