Generating Test Data with Doctrine Fixtures
Generating Test Data with Doctrine Fixtures
~15 Min. Lesezeit Zuletzt aktualisiert am August 8, 2026
Until now, all test data was created MANUALLY via curl – tedious, and gone AGAIN after every doctrine:database:drop. Doctrine fixtures solve this REPRODUCIBLY.
Installing the fixtures bundle
docker compose exec php composer require --dev orm-fixturesCreates src/DataFixtures/ AND registers the console command doctrine:fixtures:load – the api-platform distribution does NOT ship with Alice/Foundry by default, the lean orm-fixtures is entirely sufficient for our purposes.
Creating a fixtures class
<?php
declare(strict_types=1);
namespace App\DataFixtures;
use App\Entity\Project;
use App\Entity\Tag;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;
final class AppFixtures extends Fixture
{
public function load(ObjectManager $manager): void
{
$urgentTag = new Tag();
$urgentTag->setName('Urgent');
$manager->persist($urgentTag);
$internalTag = new Tag();
$internalTag->setName('Internal');
$manager->persist($internalTag);
$project = new Project();
$project->setName('Website Relaunch');
$project->setDescription('Complete redesign of the online store.');
$manager->persist($project);
$manager->flush();
}
}EXACTLY the same Fixture base class and the same load() method as in the Symfony course (chapter 23) – ObjectManager wraps the Doctrine EntityManager and works identically.
Loading fixtures
docker compose exec php bin/console doctrine:fixtures:load --no-interactionAchtung: doctrine:fixtures:load EMPTIES ALL tables by default BEFORE loading (--append prevents that) – NEVER run this against a production database without --append.
Verifying the fixtures through the API
curl -k https://localhost/api/tags{
"@context": "/api/contexts/Tag",
"@id": "/api/tags",
"@type": "hydra:Collection",
"hydra:member": [
{"@id": "/api/tags/1", "@type": "Tag", "id": 1, "name": "Urgent"},
{"@id": "/api/tags/2", "@type": "Tag", "id": 2, "name": "Internal"}
],
"hydra:totalItems": 2
}The SAME two tags from AppFixtures, EXACTLY as defined – retrievable through the READ-ONLY endpoint from chapter 12, even though POST for Tag STILL doesn't exist.
Tipp: bin/console doctrine:fixtures:load is now part of the STANDARD workflow after every doctrine:database:drop/doctrine:migrations:migrate cycle – so EVERY local development environment starts with the SAME baseline data.