phpmob/data-fixture
Lightweight PHP package for defining and loading data fixtures to seed databases during development and testing. Helps generate consistent sample records, manage fixture sets, and reset data quickly for repeatable test runs and local environments.
composer require phpmob/data-fixture
UserFixture):
use Phpmob\DataFixture\Fixture\AbstractFixture;
class UserFixture extends AbstractFixture
{
public function load()
{
return [
'users' => [
[
'name' => 'John Doe',
'email' => 'john@example.com',
],
[
'name' => 'Jane Smith',
'email' => 'jane@example.com',
],
],
];
}
}
php artisan fixture:load UserFixture
(Note: If no fixture:load command exists, check for fixture:run or create a custom Artisan command.)Use this to seed a local development database with test data before running Laravel’s migrate:fresh or db:seed. Example workflow:
php artisan migrate:fresh --seed
php artisan fixture:load UserFixture PostFixture
/database/fixtures/
├── auth/
│ ├── UserFixture.php
│ └── RoleFixture.php
├── blog/
│ ├── PostFixture.php
│ └── CommentFixture.php
└── FixtureLoader.php # Custom loader (optional)
class PostFixture extends AbstractFixture
{
protected $userFixture;
public function __construct(UserFixture $userFixture)
{
$this->userFixture = $userFixture;
}
public function load()
{
$this->userFixture->load(); // Load users first
return [
'posts' => [
['title' => 'First Post', 'user_id' => 1],
],
];
}
}
Batch Loading:
php artisan fixture:load UserFixture PostFixture --batch
(If --batch isn’t supported, chain fixtures manually in a custom command.)
Conditional Loading:
class EnvironmentAwareFixture extends AbstractFixture
{
public function load()
{
if (app()->environment('local')) {
return $this->getLocalData();
}
return $this->getStagingData();
}
}
Integration with Laravel Seeder:
Extend Laravel’s DatabaseSeeder to use phpmob/data-fixture:
class DatabaseSeeder extends Seeder
{
public function run()
{
$loader = new \Phpmob\DataFixture\FixtureLoader();
$loader->load([new UserFixture(), new PostFixture()]);
}
}
public function test_user_creation()
{
$loader = new FixtureLoader();
$loader->load([new UserFixture()]);
$this->assertDatabaseHas('users', ['email' => 'john@example.com']);
}
trait CleansFixtures
{
protected function tearDown(): void
{
Artisan::call('fixture:reset');
parent::tearDown();
}
}
No Built-in Artisan Command:
The package lacks a default fixture:load command. Workaround:
Create a custom command:
php artisan make:command FixtureLoadCommand
// app/Console/Commands/FixtureLoadCommand.php
public function handle()
{
$fixtures = $this->argument('fixtures');
$loader = new \Phpmob\DataFixture\FixtureLoader();
$loader->load(array_map(function ($class) {
return new $class();
}, explode(',', $fixtures)));
}
Register it in app/Console/Kernel.php:
protected $commands = [
\App\Console\Commands\FixtureLoadCommand::class,
];
No Transaction Support: Fixtures are loaded directly to the DB. Tip: Wrap loading in a transaction for safety:
DB::transaction(function () {
$loader->load([new UserFixture()]);
});
Outdated Package: Last release in 2018. Mitigations:
Verify Fixture Data:
Add a dump() in load() to inspect payloads:
public function load()
{
$data = [
'users' => [...]
];
dump($data); // Debug output
return $data;
}
Check for Duplicate Keys:
Ensure fixture data doesn’t conflict with existing records. Use upsert or ignore flags:
public function load()
{
return [
'users' => [
['email' => 'john@example.com', 'ignore' => true], // Skip if exists
],
];
}
Custom Fixture Loaders:
Override Phpmob\DataFixture\FixtureLoader to add logic (e.g., logging, pre-processing):
class CustomLoader extends FixtureLoader
{
public function load(array $fixtures)
{
Log::info('Loading fixtures: ' . implode(', ', array_keys($fixtures)));
parent::load($fixtures);
}
}
Dynamic Fixture Data: Fetch data from APIs or config:
class ApiUserFixture extends AbstractFixture
{
public function load()
{
$users = Http::get('https://api.example.com/users')->json();
return ['users' => $users];
}
}
Integration with Laravel Events: Trigger events after fixture loading:
event(new FixturesLoaded($fixtures));
Listen in EventServiceProvider:
protected $listen = [
FixturesLoaded::class => [
HandleFixturePostLoad::class,
],
];
How can I help you explore Laravel packages today?