LiipFunctionalTestBundle icon indicating copy to clipboard operation
LiipFunctionalTestBundle copied to clipboard

loginAs results later into duplicate entry

Open develth opened this issue 8 years ago • 3 comments

Hey there,

When i create my Client via loginAs and a Reference it will later, when i want to edit this user, into a duplicate entry.

FYI: I´m using FOSUserBundle.

What i do:

User Create:

$userManager = $this->container->get('fos_user.user_manager');
$user = new User();
$user->setUsername("myname");
$user->setEmail("[email protected]");
$user->setPlainPassword("testpass");
$user->setEnabled(true);
$userManager->updateCanonicalFields($user);
$userManager->updatePassword($user);
$this->addReference('user-default', $user);
$userManager->updateUser($user);

Fixtures Load (LiipFunctionalTestBundle)

$em = $this->getContainer()->get('doctrine')->getManager();
if (!isset($metadatas)) {
  $metadatas = $em->getMetadataFactory()->getAllMetadata();
}
$schemaTool = new SchemaTool($em);
$schemaTool->dropDatabase();
if (!empty($metadatas)) {
  $schemaTool->createSchema($metadatas);
}
$fixturesClasses = array(
  LoadUserData::class
);
$this->fixtures = $this->loadFixtures($fixturesClasses)->getReferenceRepository();

Client Create:

$currentUser = $this->fixtures->getReference("user-default");
$this->loginAs($currentUser, $fireWall);
$this->client = $this->makeClient();

User Update in Controller:

$user = $this->get('security.token_storage')->getToken()->getUser();
if ($user instanceof User) {
        $user->setlastActionAt(new \DateTime());
        $this->em->persist($user);
        $this->em->flush(); // results in a Integrity constraint violation: 1062 Duplicate entry
}

What i recognized is that the getEntityState in the UnitOfWork results in $assume, because the $this->entityStates is empty.

What i´m doing wrong? Or is it a bug?

Thanks, Thomas

develth avatar Sep 20 '17 13:09 develth

When you perform the same things by yourself (i.e. by going to app_dev.php/…), do you have the same error?

alexislefebvre avatar Sep 20 '17 14:09 alexislefebvre

What do you exactly mean with "same things"? Just the "User Update in Controller"? That works.

develth avatar Sep 20 '17 14:09 develth

By the way: if i do a makeClient with username and password credentials, everything is working fine.

develth avatar Sep 20 '17 14:09 develth