From 1ba8c265c2c58e44e4054c47ac9a60f5dc13fb2b Mon Sep 17 00:00:00 2001 From: Gintautas Miselis Date: Thu, 10 Oct 2019 11:54:44 +0300 Subject: [PATCH] Added package files --- .gitattributes | 7 ++ .gitignore | 3 + .travis.yml | 21 ++++++ LICENSE | 21 ++++++ Robofile.php | 21 ++++++ codeception.yml | 7 ++ composer.json | 30 +++++++++ documentation.md | 171 +++++++++++++++++++++++++++++++++++++++++++++++ readme.md | 13 ++++ 9 files changed, 294 insertions(+) create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 .travis.yml create mode 100644 LICENSE create mode 100644 Robofile.php create mode 100644 codeception.yml create mode 100644 composer.json create mode 100644 documentation.md create mode 100644 readme.md diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..87f3679 --- /dev/null +++ b/.gitattributes @@ -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 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cb42441 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/.idea/ +/vendor/ +/composer.lock \ No newline at end of file diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..08cc594 --- /dev/null +++ b/.travis.yml @@ -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 diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..cc43c4a --- /dev/null +++ b/LICENSE @@ -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. diff --git a/Robofile.php b/Robofile.php new file mode 100644 index 0000000..2b7e6b6 --- /dev/null +++ b/Robofile.php @@ -0,0 +1,21 @@ + 

Module reference is taken from the source code. Help us to improve documentation. Edit module reference
'; + $documentationFile = 'documentation.md'; + $this->generateDocumentationForClass($className, $documentationFile, $sourceMessage); + } +} \ No newline at end of file diff --git a/codeception.yml b/codeception.yml new file mode 100644 index 0000000..779d777 --- /dev/null +++ b/codeception.yml @@ -0,0 +1,7 @@ +paths: + tests: tests + output: tests/_output + data: tests/_data + support: tests/_support + envs: tests/_envs +actor_suffix: Tester diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..e3d263d --- /dev/null +++ b/composer.json @@ -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 + } +} diff --git a/documentation.md b/documentation.md new file mode 100644 index 0000000..1ab4cae --- /dev/null +++ b/documentation.md @@ -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 +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 +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 + +

 

Module reference is taken from the source code. Help us to improve documentation. Edit module reference
diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..0763d61 --- /dev/null +++ b/readme.md @@ -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 + +Look at documentation.md file