I have a problem during testing a symfony App with phpunit. I created a service and it works well but when I execute the service in my tests doctrine throw me an exception
A new entity was found through the relationship 'PointOfSale#country' that was not configured to cascade persist operations for entity
In my service I do this:
$pointOfSale->setCountry($this->getCountry($name));
private function getCountry(string $name): Country
{
$country = $this->entityManager->getRepository(Country::class)->findOneBy(['alpha3Code' => $name]);
return $country;
}
I don't understand why this entity is not managed by UnitOfWorks. Indeed when I do the following code I have an exception only during test
$country = $this->entityManager->getRepository(Country::class)->findOneBy(['alpha3Code' => $name]);
$this->entityManager->refresh($country);
[ERROR] Entity Namespace\Country@000000001bfea0ed00000000602457d6 is not managed. An entity is
managed if its fetched from the database or registered as new through EntityManager#persist
How can I fix this ?
findOneBy
if I do$this->entityManager->getUnitOfWork()->isInIdentityMap($country)
it returns true but it returns false in tests – al37350