/** * Setup. */ public function setUp() { $this->commentRepository = $this->getMock('Elcodi\\Component\\Comment\\Repository\\CommentRepository', [], [], '', false); $this->voteManager = $this->getMock('Elcodi\\Component\\Comment\\Services\\VoteManager', [], [], '', false); $commentVotes = $this->getMock('Elcodi\\Component\\Comment\\Entity\\VotePackage', [], [], '', false); $commentVotes->expects($this->any())->method('getNbVotes')->will($this->returnValue(2)); $commentVotes->expects($this->any())->method('getNbUpVotes')->will($this->returnValue(1)); $commentVotes->expects($this->any())->method('getNbDownVotes')->will($this->returnValue(1)); $this->voteManager->expects($this->any())->method('getCommentVotes')->will($this->returnValue($commentVotes)); $this->voteManager->expects($this->any())->method('create')->will($this->returnValue(new Vote())); $this->commentCache = new CommentCache($this->commentRepository, $this->voteManager, 'key'); $this->commentCache->setCache($this->getMockForAbstractClass('Doctrine\\Common\\Cache\\CacheProvider'))->setEncoder($this->getMock('Elcodi\\Component\\Core\\Encoder\\Interfaces\\EncoderInterface')); }
/** * Build comments tree from doctrine given their source * * cost O(n) * * @param string $source Source of comments * @param string $context Context of comment * * @return Array Comments tree given the source */ private function buildCommentTree($source, $context) { $comments = $this->commentRepository->getAllCommentsSortedByParentAndIdAsc($source, $context); $commentTree = [0 => null, 'children' => []]; /** * @var CommentInterface $comment */ foreach ($comments as $comment) { $parentCommentId = 0; $commentId = $comment->getId(); if ($comment->getParent() instanceof CommentInterface) { $parentCommentId = $comment->getParent()->getId(); } if ($parentCommentId && !isset($commentTree[$parentCommentId])) { $commentTree[$parentCommentId] = ['entity' => null, 'children' => []]; } if (!isset($commentTree[$commentId])) { $commentTree[$commentId] = ['entity' => null, 'children' => []]; } $commentVotePackage = $this->voteManager->getCommentVotes($comment); $commentTree[$commentId]['entity'] = $this->createCommentStructure($comment, $commentVotePackage); $commentTree[$parentCommentId]['children'][] =& $commentTree[$commentId]; } return $commentTree[0]['children'] ?: []; }
/** * Test remove vote when no exists. */ public function testRemoveNonExistingVote() { $this->voteDirector->expects($this->any())->method('findOneBy')->will($this->returnValue(null)); $this->voteDirector->expects($this->never())->method('remove'); $this->voteManager->removeVote($this->comment, $this->authorToken, Vote::DOWN); }