Giter Site home page Giter Site logo

fakemodel's Introduction

Yii Framework

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.1 +

Instalation

composer require dmirogin/yii2-js-urlmanager

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(\app\models\MyModel::class)->setAmount(1);
}

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.