rnr/laravel-alice

Wrapper of alice fixture generator for laravel
3,420 7
Install
composer require rnr/laravel-alice
Latest Version:0.9.1
Laravel:5
Last Updated:Nov 27, 2017
Links: GitHub  ·  Packagist
Maintainer: rnr

LaravelAlice

Package version Build Status

A Laravel package to manage fixtures with nelmio/alice.

Installation

This is installable via Composer as rnr/laravel-alice

composer install --dev rnr/laravel-alice

Basic usage

Create yml fixture file as it is described nelmio/alice

Nelmio\Entity\User:
    user{1..10}:
        username: '<username()>'
        fullname: '<firstName()> <lastName()>'
        birthDate: '<date()>'
        email: '<email()>'
        favoriteNumber: '50%? <numberBetween(1, 200)>'

Nelmio\Entity\Group:
    group1:
        name: Admins
        owner: '@user1'
        members: '<numberBetween(1, 10)>x @user*'
        created: '<dateTimeBetween("-200 days", "now")>'
        updated: '<dateTimeBetween($created, "now")>'

Class FixturelLoader has one significant method load to load models. That method receive one or array of files with data to load. You can load this fixture to database with next code:

<?php
namespace Rnr\Tests\Alice;

use Orchestra\Testbench\TestCase as ParentTestCase;
use Rnr\Alice\FixturesLoader;
use Nelmio\Entity\User;
use Nelmio\Entity\Group;

class TestCase extends ParentTestCase
{
    /** @var FixturesLoader */
    protected $fixturesLoader;

    protected function setUp()
    {
        parent::setUp();

        $this->fixturesLoader = $this->app->make(FixturesLoader::class);
    }
    
    public function testLoadingFixtures() {
        $objects = $this->fixturesLoader->load('fixture.yml');
        
        $users = User::all();
        
        $this->assertEquals(array_map($objects, function ($model) {
            return $model->getKey();
        }), $users->modelKeys());
    }
}

It loads data for next models:

<?php
namespace Nelmio\Entity;

use Illuminate\Database\Eloquent\Model;

class User extends Model  {
    protected $table = 'users';
}

class Group extends Model {
    protected $table = 'groups';
    
    public function owner() {
        return $this->belnogsTo(User::class);
    }
}

Restrictions

You can use id to specify related models in relationships, but these models should be already create in database.

Extracting fixtures from database

If you add GenerateFixtureCommand to your console kernel you can export data to yml from existing database. This class add new command db:generate-fixture to artisan. This command extract fixtures from database. Command takes array of models with relations in specific format:

php artisan db:generate-fixture \
    'Nelmio\Entity\User(relations:relation1,realation2.subrelation)=1,2,3-5,17,20-25' \
    'Nelmio\Entity\Group(relations:hasOne)=*' > /path/to/fixture.yml

Related Packages

doctrine/dbal

Powerful PHP database abstraction layer (DBAL) with many features for database s...

641,106,524 9,700
laravel/framework

The Laravel Framework.

593,882,149 34,959
laravel/tinker

Powerful REPL for the Laravel framework.

502,645,077 7,441
laravel/serializable-closure

Laravel Serializable Closure provides an easy and secure way to serialize closur...

418,291,778 609
nunomaduro/collision

Cli error handling for console/command-line PHP applications.

394,964,158 4,660

Version History

Version Released Laravel License
0.9.1 ~5.3
0.9 ~5.3
0.8.5.3 ~5.1
0.8.5.2 ~5.1
0.8.5.1 ~5.1