codeception/module-doctrine
Doctrine module for Codeception to integrate ORM/DBAL in your tests. Provides helpers for fetching and asserting entities, managing the EntityManager, and cleaning up the database between runs, enabling fast, reliable functional and acceptance testing.
This package, Codeception's Doctrine Module, enables seamless interaction with Doctrine ORM entities in Codeception tests. To get started:
composer require codeception/module-doctrine
codeception.yml under modules:
modules:
enabled:
- Doctrine:
dsn: 'sqlite:///:memory:' # Replace with your DB connection
entityManager: '@doctrine.orm.entity_manager' # Symfony DI container key (if applicable)
$this->haveInDatabase('App\Entity\User', ['id' => 1, 'name' => 'John']);
$user = $this->grabFromDatabase('App\Entity\User', ['id' => 1]);
$this->seeRecord('App\Entity\User', ['name' => 'John']);
Entity Management:
haveInDatabase() to seed test data before tests.seeRecord()/dontSeeRecord() for assertions.deleteFromDatabase() to clean up after tests.Lazy Loading & Performance:
Dependency Injection:
entityManager via DI container (e.g., @doctrine.orm.entity_manager). The module will use this instead of creating its own connection.Complex Queries:
grabFromDatabase() with DQL or Criteria API for advanced queries:
$users = $this->grabFromDatabase('App\Entity\User', 'u WHERE u.age > :age', ['age' => 18]);
db.connection config key to specify your Laravel DB connection:
modules:
Doctrine:
db: 'mysql'
entityManager: null # Laravel uses its own EM
Db module for transactions:
modules:
enabled:
- Db
- Doctrine
Then wrap tests in transactions to avoid DB pollution.3.2.x if stuck on PHP 8.1.Reflection*::setAccessible() calls, which may affect custom Doctrine extensions relying on private property access. Test thoroughly if you extend Doctrine.Lazy Loading Issues:
Proxy __load()), ensure your Symfony kernel is bootstrapped or use warmup() in tests:
$this->getModule('Doctrine')->getEntityManager()->getConnection()->getWrappedConnection()->getNativeConnection()->close();
Doctrine:
useLazyObjects: true
Deprecation Warnings:
doctrine/collections:2.4, suppress warnings by updating to 2.5+ or configure the module to ignore them:
Doctrine:
ignoreDeprecations: true
Object ID Fixes:
$this->seeRecord('App\Entity\User', ['customId' => 'abc123']);
$this->getModule('Doctrine')->setRepository('App\Entity\User', CustomUserRepository::class);
postPersist) via the module’s getEntityManager():
$em = $this->getModule('Doctrine')->getEntityManager();
$em->getEventManager()->addEventSubscriber(new CustomSubscriber());
buildQuery() in a custom module.fetchAll() for bulk operations if needed:
$users = $this->grabFromDatabase('App\Entity\User', [], [], ['fetch' => 'EAGER']);
Doctrine:
useLazyObjects: true
How can I help you explore Laravel packages today?