php - How to refresh entity using entity manager in Symfony tests?

I'm testing a form in my website but I came across this issue which is mainly caused by the entity manager.
This is the error
- IndexBundle\Tests\Controller\CompanyControllerTest::testCompany Doctrine\ORM\ORMInvalidArgumentException: Entity IndexBundle\Entity\[email protected] is not managed. An entity is managed if its fetched from the database or registered as new through EntityManager#persist
This is my test
<?php
class CompanyControllerTest extends \DataFixturesTestCase
{
/**
* @var Client|null
*/
protected $client = null;
/**
* @var $session
*/
protected $session;
/**
* {@inheritDoc}
*/
public function setUp(): void
{
parent::setUp();
$this->client = static::createClient(array(
'PHP_AUTH_USER' => '[email protected]',
'PHP_AUTH_PW' => 'test',
));
}
public function testCompany()
{
$this->logIn();
$this->testProfile();
}
protected function testProfile(){
$userTest = $this->entityManager->getRepository(User::class)->findOneBy(['username'=>'[email protected]']);
$crawler = $this->client->request('GET', '/company/profile?edit=user&lang=en');
$formNode= $crawler->selectButton('Continue В»');
$form= $formNode->form(array(
'user[title]'=> 'Mr',
'user[name]'=> 'the boss',
'user[surnames]'=> 'the boss surname',
'user[birthdate][day]'=> 1,
'user[birthdate][month]'=> 3,
'user[birthdate][year]'=> 1990,
'user[birthPlace]'=> 'valencia',
));
$this->client->submit($form);
$this->entityManager->refresh($userTest);
$this->assertSame('[email protected]', $userTest->getContactEmail()) ;
}
private function logIn()
{
$session = $this->client->getContainer()->get('session');
$firewall = 'secured_area';
$token = new UsernamePasswordToken('company', 'null', $firewall, array('ROLE_COMPANY_SUPER_ADMIN'));
$session->set('_security_'.$firewall, serialize($token));
$session->save();
$cookie = new Cookie($session->getName(), $session->getId());
$this->client->getCookieJar()->set($cookie);
}
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: script cache:clear returned with error code 255
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.