Skip to content

Commit

Permalink
Added package files
Browse files Browse the repository at this point in the history
  • Loading branch information
Naktibalda committed Oct 10, 2019
1 parent d18d0b3 commit 1ba8c26
Show file tree
Hide file tree
Showing 9 changed files with 294 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Exclude files that don't need to be present in packages (so they're not downloaded by Composer)
/tests export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/Robofile.php export-ignore
/*.md export-ignore
/*.yml export-ignore
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/.idea/
/vendor/
/composer.lock
21 changes: 21 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
language: php

php:
- 5.6
- 7.0
- 7.1
- 7.2
- 7.3

# faster builds on new travis setup not using sudo
sudo: false

install:
# - '[[ -z "$CI_USER_TOKEN" ]] || composer config github-oauth.github.com ${CI_USER_TOKEN};'
# - travis_retry composer self-update && composer --version
# - travis_retry composer update --prefer-dist --no-interaction

script:
# This module has no tests yet
- composer validate --strict
- php -l src/Codeception/Module/DataFactory.php
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2011 Michael Bodnarchuk and contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
21 changes: 21 additions & 0 deletions Robofile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

require __DIR__ . '/vendor/autoload.php';

use Codeception\Module\DataFactory;
use Codeception\Util\DocumentationHelpers;

class RoboFile extends \Robo\Tasks
{
use DocumentationHelpers;

public function buildDocs()
{
$className = DataFactory::class;
$classPath = str_replace('\\', '/', $className);
$source = "https://github.com/Codeception/module-datafactory/tree/master/src/$classPath.php";
$sourceMessage = '<p>&nbsp;</p><div class="alert alert-warning">Module reference is taken from the source code. <a href="' . $source . '">Help us to improve documentation. Edit module reference</a></div>';
$documentationFile = 'documentation.md';
$this->generateDocumentationForClass($className, $documentationFile, $sourceMessage);
}
}
7 changes: 7 additions & 0 deletions codeception.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
paths:
tests: tests
output: tests/_output
data: tests/_data
support: tests/_support
envs: tests/_envs
actor_suffix: Tester
30 changes: 30 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name":"codeception/module-datafactory",
"description":"DataFactory module for Codeception",
"keywords":["codeception"],
"homepage":"http://codeception.com/",
"type":"library",
"license":"MIT",
"authors":[
{
"name":"Michael Bodnarchuk"
}
],
"minimum-stability": "RC",

"require": {
"php": ">=5.6.0 <8.0",
"codeception/codeception": "4.0.x-dev | ^4.0",
"league/factory-muffin": "^3.0",
"league/factory-muffin-faker": "^2.1"
},
"require-dev": {
"codeception/util-robohelpers": "dev-master"
},
"autoload":{
"classmap": ["src/"]
},
"config": {
"classmap-authoritative": true
}
}
171 changes: 171 additions & 0 deletions documentation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
# DataFactory


DataFactory allows you to easily generate and create test data using [**FactoryMuffin**](https://github.com/thephpleague/factory-muffin).
DataFactory uses an ORM of your application to define, save and cleanup data. Thus, should be used with ORM or Framework modules.

This module requires packages installed:

```json
{
"league/factory-muffin": "^3.0",
}
```

Generation rules can be defined in a factories file. You will need to create `factories.php` (it is recommended to store it in `_support` dir)
Follow [FactoryMuffin documentation](https://github.com/thephpleague/factory-muffin) to set valid rules.
Random data provided by [Faker](https://github.com/fzaninotto/Faker) library.

```php
<?php
use League\FactoryMuffin\Faker\Facade as Faker;

$fm->define(User::class)->setDefinitions([
'name' => Faker::name(),

// generate email
'email' => Faker::email(),
'body' => Faker::text(),

// generate a profile and return its Id
'profile_id' => 'factory|Profile'
]);
```

Configure this module to load factory definitions from a directory.
You should also specify a module with an ORM as a dependency.

```yaml
modules:
enabled:
- Yii2:
configFile: path/to/config.php
- DataFactory:
factories: tests/_support/factories
depends: Yii2
```
(you can also use Laravel5 and Phalcon).
In this example factories are loaded from `tests/_support/factories` directory. Please note that this directory is relative from the codeception.yml file (so for Yii2 it would be codeception/_support/factories).
You should create this directory manually and create PHP files in it with factories definitions following [official documentation](https://github.com/thephpleague/factory-muffin#usage).

In cases you want to use data from database inside your factory definitions you can define them in Helper.
For instance, if you use Doctrine, this allows you to access `EntityManager` inside a definition.

To proceed you should create Factories helper via `generate:helper` command and enable it:

```
modules:
enabled:
- DataFactory:
depends: Doctrine2
- \Helper\Factories
```

In this case you can define factories from a Helper class with `_define` method.

```php
<?php
public function _beforeSuite()
{
$factory = $this->getModule('DataFactory');
// let us get EntityManager from Doctrine
$em = $this->getModule('Doctrine2')->_getEntityManager();
$factory->_define(User::class, [
// generate random user name
// use League\FactoryMuffin\Faker\Facade as Faker;
'name' => Faker::name(),
// get real company from database
'company' => $em->getRepository(Company::class)->find(),
// let's generate a profile for each created user
// receive an entity and set it via `setProfile` method
// UserProfile factory should be defined as well
'profile' => 'entity|'.UserProfile::class
]);
}
```

Factory Definitions are described in official [Factory Muffin Documentation](https://github.com/thephpleague/factory-muffin)

### Related Models Generators

If your module relies on other model you can generate them both.
To create a related module you can use either `factory` or `entity` prefix, depending on ORM you use.

In case your ORM expects an Id of a related record (Eloquent) to be set use `factory` prefix:

```php
'user_id' => 'factory|User'
```

In case your ORM expects a related record itself (Doctrine) then you should use `entity` prefix:

```php
'user' => 'entity|User'
```

## Actions

### have

Generates and saves a record,.

```php
$I->have('User'); // creates user
$I->have('User', ['is_active' => true]); // creates active user
```

Returns an instance of created user.

* `param string` $name
* `param array` $extraAttrs

* `return` object


### haveMultiple

Generates and saves a record multiple times.

```php
$I->haveMultiple('User', 10); // create 10 users
$I->haveMultiple('User', 10, ['is_active' => true]); // create 10 active users
```

* `param string` $name
* `param int` $times
* `param array` $extraAttrs

* `return` \object[]


### make

Generates a record instance.

This does not save it in the database. Use `have` for that.

```php
$user = $I->make('User'); // return User instance
$activeUser = $I->make('User', ['is_active' => true]); // return active user instance
```

Returns an instance of created user without creating a record in database.

* `param string` $name
* `param array` $extraAttrs

* `return` object


### onReconfigure

@throws ModuleException

<p>&nbsp;</p><div class="alert alert-warning">Module reference is taken from the source code. <a href="https://github.com/Codeception/module-datafactory/tree/master/src/Codeception/Module/DataFactory.php">Help us to improve documentation. Edit module reference</a></div>
13 changes: 13 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Data Factory module for Codeception

[![Build Status](https://travis-ci.org/Codeception/module-datafactory.svg?branch=master)](https://travis-ci.org/Codeception/module-datafactory)

## Installation

```
composer require --dev "codeception/module-datafactory"
```

## Documentation

<a href="documentation.md">Look at documentation.md file</a>

0 comments on commit 1ba8c26

Please sign in to comment.