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(''); }
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()); }
/** * @param ConfirmedMentionDto[] $mentionsDto * * @return Mention[] */ public function map(array $mentionsDto) : array { $mentions = []; foreach ($mentionsDto as $mentionDto) { $mention = $this->mentionRepository->getByUserId($mentionDto->userId); if (null === $mention) { $mention = new Mention(); $mention->setUserId($mentionDto->userId); $mention->setUsername($mentionDto->username); } $mentions[] = $mention; } return $mentions; }
public function testCreate() { $this->dtoToEntityMapper->shouldReceive('transform')->once()->andReturn($this->comment); $this->commentRepository->shouldReceive('add')->once()->withArgs([$this->comment]); $this->eventDispatcher->shouldReceive('dispatch'); $this->entityToDtoMapper->shouldReceive('transform')->once()->withArgs([$this->comment])->andReturn($this->commentDto); $this->mention->shouldReceive('getUsername')->andReturn('mentioned_user'); $this->comment->shouldReceive('getAuthorsUsername')->andReturn('test'); $this->comment->shouldReceive('getAuthorsAvatarFilename')->andReturn('test.jpg'); $this->comment->shouldReceive('getPostId')->andReturn(123); $this->comment->shouldReceive('getMentions')->andReturn(new ArrayCollection([$this->mention])); $createdCommentDto = $this->createCommentHandler->create($this->commentDto); $this->assertInstanceOf(CommentDto::class, $createdCommentDto); }
public function testGetSetUsername() { $this->mention->setUsername('test'); $this->assertEquals('test', $this->mention->getUsername()); }