firehed / mocktrine
PHPUnit Doctrine mocking tools
Installs: 2 972
Dependents: 0
Suggesters: 0
Security: 0
Stars: 4
Watchers: 2
Forks: 0
Open Issues: 5
Requires
- php: ^7.4 || ^8.0
- doctrine/annotations: ^1.10
- doctrine/collections: ^1.6
- doctrine/orm: ^2.7
- doctrine/persistence: ^1.3 || ^2.0
- phpdocumentor/reflection-docblock: ^4.3 || ^5.0
- symfony/polyfill-php80: ^1.20
Requires (Dev)
- phpstan/phpstan: ^0.12
- phpstan/phpstan-phpunit: ^0.12.0
- phpunit/phpunit: ^9.3
- squizlabs/php_codesniffer: ^3.5
This package is auto-updated.
Last update: 2023-03-11 00:45:56 UTC
README
A Doctrine mocking library for testing
Usage
In your unit tests that need an Entity Manager, use a new \Firehed\Mocktrine\InMemoryEntityManager
. Done!
Any object with Doctrine's entity annotations (@Entity
, @Id
, @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.
Mapping support
As of version 0.5, any mapping driver supported by Doctrine can be used with this library.
The 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 the SimpleAnnotationReader
that's used via Setup::createAnnotationMetadataConfiguration
.
Supported features
The following methods on Doctrine's EntityManagerInterface
should all work as expected:
- find
- persist
- remove
- merge
- flush
- getRepository
- getCache (will always return
null
) - isOpen (will always return
true
)
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 matching(Criteria)
method.
The following methods are not supported at this time:
- clear
- detach
- refresh
- getClassMetadata
- getMetadataFactory
- initializeObject
- contains
- getConnection
- getExpressionBuilder
- beginTransaction
- transactional
- commit
- rollback
- createQuery
- createNamedQuery
- createNativeQuery
- createNamedNativeQuery
- getReference
- getPartialReference
- close
- copy
- lock
- getEventManager
- getConfiguration
- getUnitOfWork
- getHydrator
- newHydrator
- getProxyFactory
- getFilters
- isFiltersStateClean
- hasFilters