Installation
composer require aatis/fixtures
Add the bundle to your config/bundles.php (Symfony) or register the service provider in config/app.php (Laravel via bridge if applicable):
// Laravel (if using Symfony bridge)
$provider = new Aatis\Fixtures\FixturesServiceProvider();
$this->registerServiceProvider($provider);
Basic Fixture Definition
Create a fixture file (e.g., database/fixtures/users.yml):
App\Models\User:
- { name: 'John Doe', email: 'john@example.com' }
- { name: 'Jane Smith', email: 'jane@example.com' }
First Load Run the loader via Artisan (if CLI integration is set up):
php artisan fixtures:load
(Note: CLI integration may require custom setup; see "Implementation Patterns" for alternatives.)
database/fixtures/ (configurable). Organize files by model or domain.app/Console/Kernel.php for custom commands.App\Models\Post in database/fixtures/posts.yml:
App\Models\Post:
- { title: 'First Post', content: 'Hello World' }
- { title: 'Second Post', content: 'Fixtures are great' }
php artisan fixtures:load --path=database/fixtures/posts.yml
(Verify CLI command exists; may need manual instantiation of FixturesLoader.)Fixtures as Code
database/fixtures/).fixtures/staging/, fixtures/prod/).Dynamic Fixture Loading
Load fixtures conditionally in routes/web.php or service providers:
use Aatis\Fixtures\FixturesLoader;
public function boot()
{
$loader = new FixturesLoader();
$loader->load('database/fixtures/users.yml');
}
Fixtures + Factories Combine with Laravel’s factories for hybrid testing:
// Fixture (database/fixtures/users.yml)
App\Models\User:
- { name: 'Factory User', email: 'factory@example.com', password: '$2y...' }
// Factory (DatabaseSeeder.php)
User::factory()->create(['email' => 'factory@example.com']);
Laravel-Specific Adaptations
Artisan facade:
use Illuminate\Support\Facades\Artisan;
Artisan::call('fixtures:load', [
'path' => 'database/fixtures/users.yml',
]);
FixturesLoader to support Laravel’s Eloquent models natively.Environment-Aware Loading
Use Laravel’s .env to control fixture loading:
if (env('APP_ENV') === 'testing') {
$loader->load('database/fixtures/testing/*.yml');
}
Fixtures + Migrations
Load fixtures after migrations in DatabaseSeeder.php:
public function run()
{
Artisan::call('migrate:fresh');
$loader = new FixturesLoader();
$loader->load('database/fixtures/');
}
Referencing Fixtures Use YAML anchors/aliases for relationships:
App\Models\User:
- &user1 { name: 'Alice', email: 'alice@example.com' }
App\Models\Post:
- { title: 'Welcome', user: *user1 }
No Native Laravel Support
FixturesLoader.App\Models\User vs. Symfony’s App\Entity\User).FixturesLoader.CLI Command Missing
fixtures:load command may not exist. Register it in AppServiceProvider:
public function boot()
{
if ($this->app->runningInConsole()) {
$this->commands([
new \Aatis\Fixtures\Console\LoadFixturesCommand(),
]);
}
}
YAML Parsing Quirks
php artisan fixtures:validate database/fixtures/users.yml
Foreign Key Constraints
onDuplicateKeyUpdate in MySQL or transactions:
DB::beginTransaction();
try {
$loader->load('fixtures/');
DB::commit();
} catch (\Exception $e) {
DB::rollBack();
throw $e;
}
Enable Debug Mode
Set FixturesLoader debug mode:
$loader = new FixturesLoader();
$loader->setDebug(true); // Logs SQL and errors
Check Loaded Data Inspect the database or use Tinker:
php artisan tinker
>>> App\Models\User::all();
Validate Fixtures Run a dry load to check syntax:
$loader->load('fixtures/users.yml', true); // Dry run
Custom Fixture Formats
Extend Aatis\Fixtures\Loader\LoaderInterface to support JSON/XML:
class JsonFixturesLoader implements LoaderInterface
{
public function load(string $path): void
{
$data = json_decode(file_get_contents($path), true);
// Custom logic...
}
}
Pre/Post-Load Hooks Subscribe to events (if the package supports them) or wrap the loader:
$loader = new FixturesLoader();
$loader->setPreLoadCallback(function () {
// Reset auto-increment IDs
DB::statement('ALTER TABLE users AUTO_INCREMENT = 1');
});
Fixture Factories Dynamically generate fixtures from models:
$loader->addFactory(new class implements FixtureFactory {
public function create(): array
{
return [
'name' => 'Dynamic User',
'email' => 'dynamic@example.com',
];
}
});
Default Path
Override the default fixtures path in a config file (e.g., config/fixtures.php):
return [
'paths' => [
database_path('fixtures'),
database_path('seeds/fixtures'), // Custom path
],
];
Model Mapping If models aren’t autodetected, configure mappings:
# config/fixtures.php
'mappings' => [
'App\Models\User' => 'users',
'App\Models\Post' => 'posts',
],
Environment Variables
Use .env to toggle fixture loading:
FIxtures_ENABLED=true
if (env('Fixtures_ENABLED') === 'true') {
$loader->load('fixtures/');
}
How can I help you explore Laravel packages today?