mercredi 14 avril 2021

Refresh a test object with entitymanager symfony issue

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

  1. IndexBundle\Tests\Controller\CompanyControllerTest::testCompany Doctrine\ORM\ORMInvalidArgumentException: Entity IndexBundle\Entity\User@000000007e2ef25700000000462028ec 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' => 'company@example.com',
            'PHP_AUTH_PW' => 'test',
        ));

    }

    public function testCompany()
    {
        $this->logIn();
        $this->testProfile();


    }
    protected function testProfile(){
        $userTest = $this->entityManager->getRepository(User::class)->findOneBy(['username'=>'company@example.com']);

        $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('newEmail@email.com', $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);
    }
}

Aucun commentaire:

Enregistrer un commentaire