use Doctrine\Bundle\FixturesBundle\Fixture;use Doctrine\Persistence\ObjectManager;class TestFixtures extends Fixture{ public function load(ObjectManager $manager) { $entity = new MyEntity(); $manager->persist($entity); $manager->flush(); }}
If you need to exclude table from the data purge when fixtures are reloaded, you have to use the FixturesTrait::setExcludedDoctrineTables method before loading fixtures.
If you want to access particular fixture in a test, you can register a reference on it:
1
2
3
4
5
6
7
8
9
10
11
class TestFixtures extends Fixture{ public function load(ObjectManager $manager) { $user = new User('foobar@example.com'); $this->setReference('foobar-user', $user); $manager->persist($user); $manager->flush(); }}
Then get the reference in your test:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class AdminTest extends WebTestCase{ use FixturesTrait; public function testPages(): void { $fixtures = $this->loadFixtures([TestFixtures::class])->getReferenceRepository(); $client = static::createClient(); $user = $fixtures->getReference('foobar-user'); $client->request('GET', "/admin/user/{$user->getId()}"); static::assertResponseIsSuccessful(); }}
⚠️ While dumping database in cache is just copying the database file with sqlite, caching fixtures with mysql or mongodb will require dumping tools installed on the database server.