feffel / felfactory
Requires
- php: >=7.2
- doctrine/annotations: ^1.6
- doctrine/lexer: ^1.0
- fzaninotto/faker: ^1.8
- mustangostang/spyc: ^0.6.2
- symfony/dotenv: ^4.3
Requires (Dev)
- mockery/mockery: ^1.2
- phpunit/phpunit: >=7.0
- psy/psysh: ^0.9.9
- squizlabs/php_codesniffer: ^3.0
- symfony/var-dumper: ^4.2
- vimeo/psalm: ^3.2
This package is auto-updated.
Last update: 2024-11-21 00:26:47 UTC
README
felfactory is a library that generates objects full of fake data for you. Whether you need to randomize test data, bootstrap your database, fill-in your persistence to stress test it, or anonymize data taken from a production service.
Powered by Faker's data generators.
Table of Contents
Installation
via composer
composer require feffel/felfactory
Basic Usage
Pass a class name to your factory instance, it will create and fill properties with the appropriate type of data.
class Person { private $firstName; private $lastName; private $address; } $factory = new \felfactory\Factory(); $person = $factory->generate(Person::class); var_dump($person); // class Person#2407 (3) { // private $firstName => string(6) "Breana" // private $lastName => string(7) "Okuneva" // private $address => string(43) "37382 Chanel Point Steuberchester, AR 83395" // }
Configuration
Configuration uses enviornment variables, add these to your environment or add a .env
file to your root project dir.
Model Definitions
You can provide model definitions to customize the generation of a model, the definition does not have to contain all of the properties, the factory will still guess the missing properties.
- Generate accepts a faker generator property [eg: firstName, lastName, phoneNumber, ...]
- Value accepts any php value and passes it down to property [eg: "string value", 15, null, ...]
- ObjectOf accepts a FQCN and generates an object of this type [eg: namespace\models\Person , Person::class, ...]
- ManyOf accepts any of the previous definitions as it's first parameter and generates an array of it bound by the inclusive range provided by it's second and third parameter.
Annotation definition
use felfactory\Annotation as FCT; class AnnotatedModel { /** * @FCT\Generate("firstName") */ public $firstName; /** * @FCT\Value("""felfel""") */ protected $lastName; /** * @FCT\ObjectOf(AddressModel::class) */ public $address; /** * @FCT\ManyOf(@FCT\Generate("phoneNumber"), 1, 3) */ public $phoneNos; }
Php defintion
return [ AnnotatedModel::class => [ 'firstName' => "generate('firstName')", 'lastName' => "value('\"felfel\"')", 'address' => "class(felfactory\models\AddressModel)", 'phoneNos' => "many(generate('phoneNumber'), 1, 3)" ], ];
How it works
The factory does not use the class's original constructor, nor the provided setters if any. All of the initiation process is handled by reflections.
The factory looks for the @var
annotation on a property to determine it's type, if a definition is found for the property it will be used, otherwise it will be guessed based on type and name of the property.
Objects
If a property is found to be an object it will trigger another factory call to generate it, and the name will be ignored. Interfaces and abstract types will not be generated automatically and will be set to null.
Scalar types and Non-annotated properties
Scalar types will be guessed based on the name of the property first, if it doesn't match any of the supported data generators, it's generated based on type.
Contributing
Please see CONTRIBUTING and CODE_OF_CONDUCT for details.
Credits
License
The MIT License (MIT). Please see License File for more information.