Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dmirogin/fakemodel
Make, create and store models in database. Another way to work with fixtures in Yii2.
https://github.com/dmirogin/fakemodel
factory fixtures yii2-extension
Last synced: 3 months ago
JSON representation
Make, create and store models in database. Another way to work with fixtures in Yii2.
- Host: GitHub
- URL: https://github.com/dmirogin/fakemodel
- Owner: dmirogin
- License: mit
- Archived: true
- Created: 2017-12-17T19:23:09.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2023-08-31T10:27:45.000Z (over 1 year ago)
- Last Synced: 2024-09-18T22:33:00.461Z (4 months ago)
- Topics: factory, fixtures, yii2-extension
- Language: PHP
- Homepage:
- Size: 59.6 KB
- Stars: 29
- Watchers: 4
- Forks: 5
- Open Issues: 1
-
Metadata Files:
- Readme: Readme.md
- License: LICENSE
Awesome Lists containing this project
README
[![Build Status](https://travis-ci.org/dmirogin/fakemodel.svg?branch=master)](https://travis-ci.org/dmirogin/fakemodel)
[![Latest Stable Version](https://poser.pugx.org/dmirogin/fakemodel/v/stable)](https://packagist.org/packages/dmirogin/fakemodel)
[![GitHub license](https://img.shields.io/github/license/dmirogin/fakemodel.svg)](https://github.com/dmirogin/fakemodel/blob/master/LICENSE)This package helps you to manage faked models.
Make, create and store in database.
This factory is another way to work with fixtures and inspired by factories in laravel.### Requirements
- PHP 7.0 +### Instalation
```php
composer require dmirogin/fakemodel
```### How to use
1. Add component to your application configuration
```php
'factory' => [
'class' => \dmirogin\fakemodel\ModelFactory::class,
'resolvers' => [
[
'class' => \dmirogin\fakemodel\resolvers\FakerResolver::class,
'definitions' => [
\app\models\MyModel::class => function (\Faker\Generator $faker) {
return [
'id' => $faker->numberBetween(1, 100),
'username' => $faker->userName,
'password' => $faker->password
];
}
]
]
]
],
```
2. Now you can do:
```php
Yii::$app->factory->setModel(\app\models\MyModel::class)->make();
```### Function in base TestCase
In your base TestCase class you can create simple function:
```php
/**
* Create model factory
*
* @param string $model
* @param int $amount
* @return \dmirogin\fakemodel\ModelFactory
*/
protected function factory(string $model, int $amount = 1): \dmirogin\fakemodel\ModelFactory
{
/** @var \dmirogin\fakemodel\ModelFactory $factory */
$factory = Yii::$app->factory;
return $factory->setModel($model)->setAmount($amount);
}
```and call just by:
```php
$this->factory(\app\models\MyModel::class)->make();
```### Enhanced example
```php
'factory' => [
'class' => \dmirogin\fakemodel\ModelFactory::class,
'resolvers' => [
[
'class' => \dmirogin\fakemodel\resolvers\FakerResolver::class,
'definitions' => [
\app\models\MyModel::class => function (\Faker\Generator $faker) {
return [
'id' => $faker->numberBetween(1, 100),
'username' => $faker->userName,
'password' => $faker->password
];
}
]
],
[
'class' => \dmirogin\fakemodel\resolvers\StatesResolver::class,
'definitions' => [
\app\models\MyModel::class => [
'admin' => [
'id' => 1
]
]
]
]
]
],Yii::$app->factory->setModel(\app\models\MyModel::class)->states(['admin'])->setAmount(5)->make();
```
See more in [WIKI](https://github.com/dmirogin/fakemodel/wiki).