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);
 }
 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());
 }
 public function setUp()
 {
     $this->avatarUploadPathResolver = m::mock(UploadPathResolver::class);
     $this->avatarUploadPathResolver->shouldReceive('getUploadPath')->withArgs(['test.jpg'])->andReturn('/avatars/test.jpg');
     $this->commentMapper = new CommentMapper($this->avatarUploadPathResolver);
     $createdAt = m::mock(\DateTime::class);
     $createdAt->shouldReceive('format')->andReturn('2016-01-25 10:25:00');
     $author = m::mock(Author::class);
     $author->shouldReceive('getUserId')->andReturn(123);
     $author->shouldReceive('getUsername')->andReturn('test_user');
     $author->shouldReceive('getAvatarFilename')->andReturn('test.jpg');
     $mappedMentions = m::mock(ArrayCollection::class);
     $mappedMentions->shouldReceive('toArray')->andReturn(['tester']);
     $mentions = m::mock(ArrayCollection::class);
     $mentions->shouldReceive('map')->andReturn($mappedMentions);
     $this->comment = m::mock(Comment::class);
     $this->comment->shouldReceive('getId')->andReturn(5);
     $this->comment->shouldReceive('getPostId')->andReturn(40);
     $this->comment->shouldReceive('getCreatedAt')->andReturn($createdAt);
     $this->comment->shouldReceive('getContent')->andReturn('test @tester foo bar');
     $this->comment->shouldReceive('getAuthor')->andReturn($author);
     $this->comment->shouldReceive('getMentions')->andReturn($mentions);
 }