dmirogin/fakemodel

This package is abandoned and no longer maintained. No replacement package was suggested.

Test helper for Yii2 projects that make and persist models

Installs: 267

Dependents: 0

Suggesters: 0

Security: 0

Stars: 29

Watchers: 4

Forks: 5

Open Issues: 1

Type:yii2-extension

v0.1.0 2017-12-17 20:26 UTC

This package is not auto-updated.

Last update: 2024-04-10 09:00:38 UTC


README

Build Status Latest Stable Version GitHub 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

composer require dmirogin/fakemodel

How to use

  1. Add component to your application configuration

    '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:

Yii::$app->factory->setModel(\app\models\MyModel::class)->make();

Function in base TestCase

In your base TestCase class you can create simple function:

/**
 * 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:

$this->factory(\app\models\MyModel::class)->make();

Enhanced example

'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.