Magento 2 Experten — Hyvä Theme, Tailwind CSS & SEO aus einer Hand ›

Test Database and Fixtures for Tests

Test Database and Fixtures for Tests

~15 Min. Lesezeit Zuletzt aktualisiert am August 8, 2026

The test from chapter 43 (findOneBy(['email' => 'anna@example.com'])) assumes THIS user exists – to wrap up block 7, let's set up a CLEAN, ISOLATED test database with reproducible test data.

Why a DEDICATED database just for tests?

Achtung: Tests running against your ACTUAL development database are DANGEROUS: they change/delete real data, AND test results depend on that database's RANDOM current state, instead of being REPRODUCIBLE. A SEPARATE test database, reset to a KNOWN state before EVERY test run, is essential.

Configuring the test environment

.env.test
DATABASE_URL="postgresql://symfony:symfony@127.0.0.1:5432/aufgaben_manager_test?serverVersion=16&charset=utf8"

EXACTLY the pattern from chapter 4: .env.test overrides DATABASE_URL ONLY for the test environment – WebTestCase from chapter 43 automatically boots the application with APP_ENV=test, so it AUTOMATICALLY uses this separate database.

Creating and migrating the test database

php bin/console doctrine:database:create --env=test
php bin/console doctrine:migrations:migrate --env=test --no-interaction

--env=test EXPLICITLY overrides the default environment – EXACTLY the same migrations from chapter 20, applied to the SEPARATE test database.

Reusing the fixtures from chapter 25 for tests

php bin/console doctrine:fixtures:load --env=test --no-interaction

EXACTLY the same fixture classes from block 4 – NO separate test fixtures needed, as long as the core fixtures (like the user "Anna Schmidt" with a fixed anna@example.com address from chapter 25) stay referenceable for tests.

Resetting the database before EVERY test run

For tests to stay INDEPENDENT of each other and REPRODUCIBLE, the test database should be rebuilt before EVERY full test run – a simple script for that:

bin/reset-test-db.sh
#!/bin/bash
set -e

php bin/console doctrine:database:drop --env=test --force --if-exists
php bin/console doctrine:database:create --env=test
php bin/console doctrine:migrations:migrate --env=test --no-interaction
php bin/console doctrine:fixtures:load --env=test --no-interaction
chmod +x bin/reset-test-db.sh
./bin/reset-test-db.sh && php bin/phpunit

A faster alternative: transaction rollback per test

Fully rebuilding the database is slow with LARGE test suites – a more advanced approach (e.g. with the dama/doctrine-test-bundle package) starts a database TRANSACTION before EVERY individual test and rolls it back (ROLLBACK) at the end – CONSIDERABLY faster than rebuilding the whole database every time, but its own, more advanced setup that goes beyond this introduction's scope.

Referencing Anna reliably in tests

// In tests, instead of the email address as a 'magic string':
use App\DataFixtures\UserFixtures;

// Better: reference constants from the fixture classes (chapter 25) directly,
// instead of repeating the email address in several places.
// (Requires a small extension of UserFixtures with a public email constant.)

Tipp: With that, block 7 (console commands & testing) is FULLY complete – from custom commands (chapters 39-41) through unit tests (chapter 42) to functional tests with a clean test database (chapters 43-44). Block 8, the FINAL block of this course, covers caching, performance, and deployment – the last steps to make aufgaben-manager production-ready.