|
13 | 13 | use HeimrichHannot\QnaBundle\Gateway\QnaVoteGateway; |
14 | 14 | use HeimrichHannot\QnaBundle\Service\FrontendMemberProvider; |
15 | 15 | use HeimrichHannot\QnaBundle\Service\QuestionAnswerService; |
| 16 | +use HeimrichHannot\QnaBundle\Service\QuestionService; |
16 | 17 | use HeimrichHannot\QnaBundle\Service\VoteService; |
17 | 18 | use PHPUnit\Framework\Attributes\DataProvider; |
18 | 19 | use PHPUnit\Framework\TestCase; |
@@ -50,6 +51,57 @@ protected function tearDown(): void |
50 | 51 | $this->connection->close(); |
51 | 52 | } |
52 | 53 |
|
| 54 | + public function testNewQuestionIncludesAuthorVoteAndCannotBeVotedTwice(): void |
| 55 | + { |
| 56 | + $question = $this->questionService(new QnaVoteGateway($this->connection))->create($this->sessionId, 'Automatically voted question'); |
| 57 | + |
| 58 | + try { |
| 59 | + $votes = new QnaVoteGateway($this->connection); |
| 60 | + self::assertSame(1, $votes->getState($question->id, 2147483647)->voteCount); |
| 61 | + self::assertTrue($votes->getState($question->id, 2147483647)->hasVoted); |
| 62 | + self::assertFalse($votes->getState($question->id, 2147483646)->hasVoted); |
| 63 | + self::assertSame(1, $this->voteService()->vote($question->id, $this->sessionId)->voteCount); |
| 64 | + self::assertSame(2, $this->voteService(2147483646)->vote($question->id, $this->sessionId)->voteCount); |
| 65 | + } finally { |
| 66 | + $this->connection->delete('tl_qna_vote', ['pid' => $question->id]); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + public function testFailedAuthorVoteRollsBackQuestion(): void |
| 71 | + { |
| 72 | + $votes = $this->createMock(QnaVoteGateway::class); |
| 73 | + $votes->expects(self::once())->method('create')->willThrowException(new \RuntimeException('Vote insert failed')); |
| 74 | + |
| 75 | + try { |
| 76 | + $this->questionService($votes)->create($this->sessionId, 'Must be rolled back'); |
| 77 | + self::fail('Expected vote failure.'); |
| 78 | + } catch (\RuntimeException $exception) { |
| 79 | + self::assertSame('Vote insert failed', $exception->getMessage()); |
| 80 | + } |
| 81 | + |
| 82 | + self::assertFalse($this->connection->isTransactionActive()); |
| 83 | + self::assertCount(1, (new QnaQuestionGateway($this->connection))->findForStage($this->sessionId)); |
| 84 | + } |
| 85 | + |
| 86 | + private function questionService(QnaVoteGateway $votes): QuestionService |
| 87 | + { |
| 88 | + $member = $this->createStub(FrontendUser::class); |
| 89 | + $member->method('__get')->willReturn(2147483647); |
| 90 | + $security = $this->createStub(Security::class); |
| 91 | + $security->method('getUser')->willReturn($member); |
| 92 | + |
| 93 | + return new QuestionService( |
| 94 | + new QnaSessionGateway($this->connection), |
| 95 | + new QnaQuestionGateway($this->connection), |
| 96 | + new FrontendMemberProvider($security), |
| 97 | + new MockClock('@150'), |
| 98 | + 500, |
| 99 | + 20, |
| 100 | + $votes, |
| 101 | + $this->connection, |
| 102 | + ); |
| 103 | + } |
| 104 | + |
53 | 105 | public function testMarkUndoPreservesVotesAndRestoresVoting(): void |
54 | 106 | { |
55 | 107 | $questions = new QnaQuestionGateway($this->connection); |
|
0 commit comments