Download the PHP package chustilla/cakephp-model-factory without Composer
On this page you can find all versions of the php package chustilla/cakephp-model-factory. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download chustilla/cakephp-model-factory
More information about chustilla/cakephp-model-factory
Files in chustilla/cakephp-model-factory
Package cakephp-model-factory
Short Description An easy for rapid creation of models for the purpose of testing, inspired by Laravel Model Factory.
License MIT
Homepage https://github.com/chustilla/cakephp-model-factory
Informations about the package cakephp-model-factory
cakephp-model-factory
An easy for rapid creation of models for the purpose of testing, inspired by Laravel Model Factory.
Requirements
- PHP >=7.2
Installation
Via Composer
Usage
Define factories
Through model definition you will tell to the factory how to populate the entity with data.
An example of model definition would be...
That is an unusual model definition because it would create all entity instances with the same data. Usually you need compute data or use Faker library for fake data, in that case you can use a Closure for defining model attributes
Load model definitions
For creating models from its definitions you need to load those definitions. A good place for loading the models definitions could be the tests bootstrap file, by specifying the models definitions directory path.
Creating models
For an easy model creation the library provides the factory() helper
That will create a full populate entity and save it in the datasource defined for testing. If you don't need to persist the data you can use the make() method
You may also create an array of may models by passing the number of models to the helper
Override model definition data
By passing an array of attributes to create() or make() functions you can override the data filled by the model definition
States
By states, you can define discrete modifications to the model attributes.
As for definitions you can define state attributes by a Closure or a simple array
Apply states during the model building in the bellow way. States combination is allowed.
Deleting models
For ensuring tests isolation is very important to remove the models stored by each test. The library provides a trait which will do it for you.
License
The MIT License (MIT). Please see License File for more information.
All versions of cakephp-model-factory with dependencies
symfony/finder Version >=4.0
cakephp/cakephp Version ~3.7