matthiasnoback / doctrine-orm-test-service-provider
Service provider for tests in need of a Doctrine entity manager
Installs: 63 968
Dependents: 4
Suggesters: 0
Security: 0
Stars: 1
Watchers: 3
Forks: 1
Open Issues: 0
Requires
- php: ^7.0 || ^8.0
- doctrine/orm: ^2.5
- matthiasnoback/doctrine-dbal-test-service-provider: ^3.0
- matthiasnoback/phpunit-test-service-container: ^3.0
- phpunit/phpunit: ^6.0 || ^7.0 || ^8.0 || ^9.0
README
This library contains a service provider to be used with a service container for PHPUnit tests.
Usage
Use the trait Noback\PHPUnitTestServiceContainer\PHPUnit\TestCaseWithEntityManager
in your test class. You then
need to implement the getEntityDirectories()
which should return an array of the directories containing the entities that should be loaded.
For each test method a connection to an SQLite database will be available. Also the schema for the given entities will be created automatically.
<?php use PHPUnit\Framework\TestCase; use Noback\PHPUnitTestServiceContainer\PHPUnit\TestCaseWithEntityManager; class StorageTest extends TestCase { use TestCaseWithEntityManager; protected function getEntityDirectories(): array { return array( __DIR__ . '/Entity' ); } /** * @test */ public function it_persists_an_entity() { $user = new User(); $user->setName('Matthias'); $this->getEntityManager()->persist($user); $this->getEntityManager()->flush(); } }
Of course, you would usually inject the entity manager into some object which is the subject-under-test.
To register Doctrine event listeners/subscribers, get the EventManager
instance by calling
$this->getEventManager()
. To get the database Connection
object, call $this->getConnection()
.