I'm using Doctrine DBAL and want to test (with PHPUnit) my repositories using an in-memory sqlite database. Since it is in-memory I need to run the migrations before the tests.
In Laravel you can do this easily by including the RefreshDatabase trait in your test class.
Before switching to the Doctrine DBAL I was using the Doctrine ORM and was able to set up the database from my tests as such:
self::$entityManager = EntityManagerFactory::getEntityManager();
$metadatas = self::$entityManager->getMetadataFactory()->getAllMetadata();
$schemaTool = new SchemaTool(self::$entityManager);
$schemaTool->updateSchema($metadatas);
I put this in some base DoctrineORMRepositoryTestCase
class so that each individual repository test class inherited from it and the database was always setup before the tests ran.
I haven't found a way to do this with the Doctrine DBAL. I've tried running
exec('/path/to/vendor/bin/doctrine-migrations migrate --no-interaction')
from my test class and I get the message ++ 1 sql queries
in the console which sounds like it has successfully migrated (I currently only have 1 migration class), but then all of my tests fail with the message no such table...
.
How can I run my Doctrine DBAL migrations from my PHPUnit tests?