rnr / laravel-alice
Wrapper of alice fixture generator for laravel
Installs: 3 416
Dependents: 0
Suggesters: 0
Security: 0
Stars: 7
Watchers: 3
Forks: 3
Open Issues: 1
Requires
- laravel/framework: ~5.3
- nelmio/alice: ~3.1
Requires (Dev)
- orchestra/database: ~3.3.0@dev
- orchestra/testbench: ~3.3
- phpunit/phpunit: ~5.5
This package is not auto-updated.
Last update: 2024-11-23 20:53:31 UTC
README
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