Testomatio - Test Management for Codeception
Access the database using Doctrine ORM.
When used with Symfony or Zend Framework 2, Doctrineâs Entity Manager is automatically retrieved from Service Locator.
Set up your functional.suite.yml
like this:
If you donât provide a depends
key, you need to specify a callback function to retrieve 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 cleanup
setting).
By doing this, tests will run much faster and will be isolated from each other.
To use the Doctrine Module in acceptance tests, set up your acceptance.suite.yml
like this:
You cannot use cleanup: true
in an acceptance test, since Codeception and your app (i.e. browser) are using two
different connections to the database, so Codeception canât wrap changes made by the app into a transaction.
Set the SQL statement that Doctrine fixtures (doctrine/data-fixtures
)
are using to purge the database tables:
For Symfony users, the recommended way to query for entities is not to use this moduleâs grab...()
methods, but rather
âinjectâ Doctrineâs repository:
Now you have access to all your familiar repository methods in your tests, e.g.:
em
- Entity ManagerCriteria
as query parametersEvery method that expects some query parameters (see...()
,
dontSee...()
, grab...()
) also accepts an instance of
\Doctrine\Common\Collections\Criteria
for more flexibility, e.g.:
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 the order of results:
Note that key is ignored, because actual field name is part of criteria and/or expression.
return void
Performs $em->clear():
param class-string
$entityparam array
$paramsreturn void
Flushes changes to database and performs findOneBy()
call for current repository.
return void
Performs $em->flush();
template
T of objectparam class-string<T>
$entityparam array
$params . For IS NULL
, use ['field' => null]
return list<T>
Selects entities from repository.
It builds a query based on an array of parameters. You can use entity associations to build complex queries. For Symfony users, itâs recommended to use the entityâs repository instead
Example:
template
T of objectversion
1.1param class-string<T>
$entityparam array
$params . For IS NULL
, use ['field' => null]
return T
Selects a single entity from repository.
It builds a query based on an array of parameters. You can use entity associations to build complex queries. For Symfony users, itâs recommended to use the entityâs repository instead
Example:
param class-string
$entityparam string
$fieldparam array
$paramsreturn mixed
Selects field value from repository.
It builds a query based on an array of parameters. You can use entity associations to build complex queries. For Symfony users, itâs recommended to use the entityâs repository instead
Example:
param class-string
$classNameparam array
$methodsreturn void
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.
Example:
This creates a stub class for Entity\User repository with redefined method findByUsername, which will always return the NULL value.
template
T of objectparam class-string<T>|T
$classNameOrInstanceparam array
$datareturn mixed
Persists a record into the repository.
This method creates an entity, and sets its properties directly (via reflection). Setters of the entity wonât be executed, but you can create almost any entity and save it to the 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 the primary key of the newly created entity. The primary key value is extracted using Reflection API. If the primary key is composite, an array of values is returned.
This method also accepts instances as first argument, which is useful when the 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 the entity has relations, they can be populated too. In case of OneToMany the following format is expected:
For ManyToOne the format is slightly different:
This works recursively, so you can create deep structures in a single call.
Note that $em->persist()
, $em->refresh()
, and $em->flush()
are called every time.
param class-string<FixtureInterface>|class-string<FixtureInterface>[]|list<FixtureInterface>
$fixturesparam bool
$appendthrows ModuleException
throws ModuleRequireException
return void
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:
This method requires doctrine/data-fixtures
to be installed.
return void
HOOK to be executed when config changes with _reconfigure
.
param object|object[]
$entitiesreturn void
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.
param class-string
$entityparam array
$paramsreturn void
Flushes changes to database, and executes a query with parameters defined in an array.
You can use entity associations to build complex queries.
Example:
Fails if record for given criteria can't be found,