If you use Codeception installed using composer, install this module with the following command:
Alternatively, you can enable
Doctrine2 module in suite configuration file and run
This module was bundled with Codeception 2 and 3, but since version 4 it is necessary to install it separately.
Some modules are bundled with PHAR files.
Warning. Using PHAR file and composer in the same project can cause unexpected errors.
Access the database using Doctrine2 ORM.
When used with Zend Framework 2 or Symfony2, Doctrine’s Entity Manager is automatically retrieved from Service Locator.
Set up your
functional.suite.yml like this:
If you don’t use Symfony or Zend Framework, you need to specify a callback function to retrieve the Entity Manager:
This will use static method of
MyDb::createEntityManager() to establish the Entity Manager.
By default, the module will wrap everything into a transaction for each test and roll it back afterwards
(this is controlled by the
By doing this, tests will run much faster and will be isolated from each other.
To use the Doctrine2 Module in acceptance tests, set up your
acceptance.suite.yml like this:
php $I->seeInRepository(‘User’, [ ‘name’ => ‘John’, Criteria::create()->where( Criteria::expr()->endsWith(‘email’, ‘@domain.com’) ), ]); ```
If criteria is just a
->where(...) construct, you can pass just expression without criteria wrapper:
Criteria can be used not only to filter data, but also to change order of results:
Note that key is ignored, because actual field name is part of criteria and/or expression.
Flushes changes to database and performs
findOneBy() call for current repository.
Selects entities from repository. It builds query based on array of parameters. You can use entity associations to build complex queries.
param array$params. For
IS NULL, use
Selects a single entity from repository. It builds query based on array of parameters. You can use entity associations to build complex queries.
param array$params. For
IS NULL, use
Selects field value from repository. It builds query based on array of parameters. You can use entity associations to build complex queries.
Mocks the repository.
With this action you can redefine any method of any repository. Please, note: this fake repositories will be accessible through entity manager till the end of test.
This creates a stub class for Entity\User repository with redefined method findByUsername, which will always return the NULL value.
Persists record into repository. This method creates an entity, and sets its properties directly (via reflection). Setters of entity won’t be executed, but you can create almost any entity and save it to database. If the entity has a constructor, for optional parameters the default value will be used and for non-optional parameters the given fields (with a matching name) will be passed when calling the constructor before the properties get set directly (via reflection).
Returns primary key of newly created entity. Primary key value is extracted using Reflection API. If primary key is composite, array of values is returned.
This method also accepts instances as first argument, which is useful when entity constructor has some arguments:
Alternatively, constructor arguments can be passed by name. Given User constructor signature is
__constructor($arg), the example above could be rewritten like this:
If entity has relations, they can be populated too. In case of OneToMany the following format ie expected:
For ManyToOne format is slightly different:
This works recursively, so you can create deep structures in a single call.
Note that both
$em->persist(...), $em->refresh(…), and
$em->flush() are called every time.
Loads fixtures. Fixture can be specified as a fully qualified class name, an instance, or an array of class names/instances.
By default fixtures are loaded in ‘append’ mode. To replace all
data in database, use
false as second parameter:
Note: this method requires
doctrine/data-fixtures package to be installed.
param bool$append @throws ModuleException @throws ModuleRequireException
HOOK to be executed when config changes with
This method is deprecated in favor of
haveInRepository(). It’s functionality is exactly the same.
Performs $em->refresh() on every passed entity:
This can useful in acceptance tests where entity can become invalid due to external (relative to entity manager used in tests) changes.
Flushes changes to database, and executes a query with parameters defined in an array. You can use entity associations to build complex queries.
Fails if record for given criteria can't be found,