Giter VIP home page Giter VIP logo

yii2-arfixture's Introduction

Yii2 Active Record Fixture

Latest Stable Version Total Downloads Latest Unstable Version License

Library to load data fixutres using using the methods defined by yii\db\ActiveRecord and show progress log.

Installation

The preferred way to install this extension is through composer.

Either run

composer require --prefer-dist "roaresearch/yii2-arfixture:*"

or add

"roaresearch/yii2-arfixture": "*"

to the require section of your composer.json file.

Differences

yii\test\Fixture and yii\test\ActiveFixture load data to the tables using the name of the table and the yii\db\Schema::insert() method which means all the methods defined in the model such as validations, events, behaviors and even table prefix are ignored.

roaresearch\yii2\arfixutre\ARFixture loads fixtures by creating a model using the $modelClass property and then passes by all the workflow of saving the data using ActiveRecord considering scenarios, exceptions, events, safe attributes, validation errors and showing detailed information to the user of the procedure.

\ ARFixture ActiveFixture & Fixture
How is the data saved? ActiveRecord::save() Schema::insert()
Works will all the db's Without modification Have to change classes
Support to change scenario ๐Ÿ‘ ๐Ÿ‘Ž
Support ActiveRecord events ๐Ÿ‘ ๐Ÿ‘Ž
Support Behaviors ๐Ÿ‘ ๐Ÿ‘Ž
Shows details on each row ๐Ÿ‘ ๐Ÿ‘Ž
Check validated attributes ๐Ÿ‘ ๐Ÿ‘Ž
Shows how many tests passed ๐Ÿ‘ ๐Ÿ‘Ž
Shows how many tests failed ๐Ÿ‘ ๐Ÿ‘Ž
Support silent mode ๐Ÿ‘ ๐Ÿ‘Ž
Exception handling ๐Ÿ‘ ๐Ÿ‘Ž

Usage

ARFixture

class UserFixture extends ARFixture
{
    public $modelClass = User::class;
}

If [[$dataFile]] is not defined then it will seek on the data/ subfolder the file with the same name as this class except for the Fixture keyword at the end. Example: UserFixture will return the file data/User.php or an empty array when the file can not found.

The data must follow this structure

return [
    // record with no explicit alias, will only show the key number on the log.
    [
        'username' => 'faryshta',
        'name' => 'Angel',
        'lastname' => 'Guevara',
        'email' => '[email protected]',
    ],

    // record with explicit alias, will be shown on the log
    'duplicated' => [
        'username' => 'faryshta',
        'name' => 'Angel',
        'lastname' => 'Guevara',
        'email' => '[email protected]',

        // optional, will apply the scenario before loading the models.
        'scenario' => 'api-save',

        // optional, will check the validation errors.
        'attributeErrors' => [
            // will check that username has this exact validation error.
            'username' => 'Username already in use',
            // will check that email has any validation error
            'email',
            // the other attributes are not expected to have a validation error.
        ],
    ]    
];

Each row can contain the following special options:

  • attributeErrors: array list of expected validation errors in the format

    'attributeErrors' => [
        'attribute1', // will check that it contains any error.
        'attribute2' => 'Error Message' // This has to be the error found.
    ]

    Warning: If this option is defined the record won't be saved even if it passes all the validations.

  • scenario: string to be used as scenario for the model to handle the methods Model::load() and Model::validate(), if not defined the $scenarioDefault will be used

Documentation

TODO

License

The BSD License (BSD). Please see License File for more information.

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.