public function testGetAddRemoveMention()
 {
     $this->assertInstanceOf(Collection::class, $this->comment->getMentions());
     $this->assertEmpty($this->comment->getMentions());
     $mention1 = new Mention();
     $mention1->setUserId(73);
     $mention2 = new Mention();
     $mention2->setUserId(89);
     $this->comment->addMention($mention1);
     $this->comment->addMention($mention2);
     $this->assertCount(2, $this->comment->getMentions());
     $this->comment->removeMention($mention1);
     $this->assertCount(1, $this->comment->getMentions());
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $defaultEntityManager = $this->getContainer()->get('doctrine.orm.default_entity_manager');
     $legacyEntityManager = $this->getContainer()->get('doctrine.orm.legacy_entity_manager');
     $legacyCommentsRepository = $legacyEntityManager->getRepository('LegacyBundle:Comment');
     $legacyParameterRepository = $legacyEntityManager->getRepository('LegacyBundle:Parameter');
     $authorRepository = $defaultEntityManager->getRepository('AppBundle:Author');
     $mentionRepository = $defaultEntityManager->getRepository('AppBundle:Mention');
     $legacyComments = $legacyCommentsRepository->findAll();
     $progress = new ProgressBar($output, count($legacyComments));
     $progress->start();
     $progress->setFormat(' %current%/%max% [%bar%] %percent:3s%% %message%');
     $progress->setMessage('Copying legacy data');
     foreach ($legacyComments as $legacyComment) {
         $userId = $legacyComment->getUser()->getId();
         $author = $authorRepository->findOneBy(['userId' => $userId]);
         if (null === $author) {
             $author = new Author();
             $author->setUserId($legacyComment->getUser()->getId());
             $author->setUsername($legacyComment->getUser()->getUsername());
             $avatarParameter = $legacyParameterRepository->findOneBy(['userId' => $legacyComment->getUser()->getId(), 'parameterId' => 4]);
             if (null !== $avatarParameter) {
                 $author->setAvatarFilename($avatarParameter->getValue());
             }
             $defaultEntityManager->persist($author);
             $defaultEntityManager->flush($author);
         }
         $comment = new Comment();
         $comment->setId($legacyComment->getId());
         $comment->setPostId($legacyComment->getPostId());
         $comment->setContent($legacyComment->getContent());
         $comment->setCreatedAt($legacyComment->getCreatedAt());
         $comment->setActive($legacyComment->getActive());
         $comment->setAuthor($author);
         foreach ($legacyComment->getMentions() as $legacyMention) {
             $mention = $mentionRepository->findOneBy(['userId' => $legacyMention->getUser()->getId()]);
             if (null === $mention) {
                 $mention = new Mention();
                 $mention->setUserId($legacyMention->getUser()->getId());
                 $mention->setUsername($legacyMention->getUser()->getUsername());
                 $defaultEntityManager->persist($mention);
                 $defaultEntityManager->flush($mention);
             }
             $comment->addMention($mention);
         }
         $defaultEntityManager->persist($comment);
         $progress->advance();
     }
     $defaultEntityManager->flush();
     $progress->setMessage('Data has been copied');
     $progress->finish();
     $output->writeln('');
 }