I use the PHP Codeception framework to work with integration tests in the PHP app. Having mongodb as a main db, I use https://codeception.com/docs/modules/Doctrine2 extension to loadFixtures in the test.
It looks like this:
class TestExampleCest
{
public function setUp(IntegrationTester $i): void
{
$i->loadFixtures(TestFixtures::class);
}
....
}
class TestFixtures extends Fixture
{
public function load(ObjectManager $manager): void
{
$object = new Ob();
$manager->persist($object);
$manager->flush();
}
}
Fixtures work perfectly, when I run the command to load them manually:
php bin/console --env=test doctrine:mongodb:fixtures:load
Some basic fixtures I load before all tests and some on runtime in particular tests. Unfortunately, loading on runtime in the setUp method doesn't work. I am getting:
[ModuleException] Doctrine2: Fixtures could not be loaded, got Doctrine\Persistence\Mapping\MappingException: The class 'App\Ob' was not found in the chain configured namespaces
Can anybody help? Maybe it's a problem with mappings for tests.. I can't figure out it.
Solution -> workaround
The problem can be solved by adding a custom function to load fixture and skipping using a plugin. I wrote a loadFixture function in the Integration class helper:
public function loadFixture(object $fixture): void
{
$container = $this->getModule('Symfony')->_getContainer();
$documentManager = $container->get('doctrine_mongodb.odm.default_document_manager');
$loader = new Loader();
$loader->addFixture($fixture);
$executor = new MongoDBExecutor(
$documentManager,
new MongoDBPurger()
);
$executor->execute($loader->getFixtures(), true);
}