PHPUnit Doctrine mocking tools
A Doctrine mocking library for testing
In your unit tests that need an Entity Manager, use a
new \Firehed\Mocktrine\InMemoryEntityManager. Done!
Any object with Doctrine's entity annotations (
@Column, etc) should work without modification.
This library aims to provide as much type information as possible, so that static analysis tools (such as PHPStan) work well without additional plugins.
As of version 0.5, any mapping driver supported by Doctrine can be used with this library.
InMemoryEntityManager accepts the driver as an optional parameter.
- $em = new Mocktrine\InMemoryEntityManager(); + $em = new Mocktrine\InMemoryEntityManager( + \Doctrine\ORM\Mapping\Driver\AttributeDriver(['src/Model']), + );
You can also grab the value directly from your Doctrine config:
$config = Setup::createAnnotationMetadataDriverConfiguration(...) $driver = $config->getMetadataDriverImpl(); $em = new Mocktrine\InMemoryEntityManager($driver)
If a driver is not provided, it will default to either
AnnotationReader that's used via
The former will be preferred, but the class has been removed in
doctrine/annotations:2.0; if your local dependencies allow that version then the latter will be used.
It is RECOMMENDED to always explicitly provide a driver, as that best matches Doctrine's own setup behavior. Future versions of this library may make this required.
The following methods on Doctrine's
EntityManagerInterface should all work as expected:
- getCache (will always return
- isOpen (will always return
All methods on the
ObjectRepository (for various findBy operations) should also work.
ObjectRepository also implements the
Selectable interface (as
EntityRepository does, which is the returned type from
EntityManager), so it's also possible to use the
The following methods are not supported at this time: