|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Tests\Modules\Comments\Unit; |
|
4
|
|
|
|
|
5
|
|
|
use App\Modules\Comments\Api\Command\CreateCommentCommand; |
|
6
|
|
|
use App\Modules\Comments\Api\Event\Inbound\PostCreatedCommentsIEvent; |
|
7
|
|
|
use App\Modules\Comments\Api\Query\FindCommentsByPostIdQuery; |
|
8
|
|
|
use App\Modules\Comments\Domain\Event\Outbound\CommentCreatedOEvent; |
|
9
|
|
|
use App\Modules\Comments\Domain\Event\Outbound\CommentsCountUpdatedOEvent; |
|
10
|
|
|
use App\Tests\TestUtils\Events\InMemoryEventPublisher; |
|
11
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
|
12
|
|
|
use Symfony\Component\Uid\Ulid; |
|
13
|
|
|
|
|
14
|
|
|
class CommentsCreationSpec extends CommentsSpec |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @test |
|
19
|
|
|
*/ |
|
20
|
|
|
public function shouldCreateCommentForExistingPost() |
|
21
|
|
|
{ |
|
22
|
|
|
//given: there was a Post Created |
|
23
|
|
|
$postId = $this->createPost(); |
|
24
|
|
|
|
|
25
|
|
|
//and: someone authored a Comment for that Post |
|
26
|
|
|
$command = new CreateCommentCommand($postId, 'Author', 'Body', null); |
|
27
|
|
|
|
|
28
|
|
|
//when: comment is Created |
|
29
|
|
|
$commentId = $this->commentsApi->createComment($command)->getId(); |
|
30
|
|
|
|
|
31
|
|
|
//then: Comment was Created correctly |
|
32
|
|
|
self::assertNotNull($commentId); |
|
33
|
|
|
|
|
34
|
|
|
//and: Comment can be found by Post Id |
|
35
|
|
|
$comments = $this->commentsApi->findCommentsForPost(new FindCommentsByPostIdQuery($postId)); |
|
36
|
|
|
self::assertNotNull($comments); |
|
37
|
|
|
self::assertCount(1, $comments); |
|
38
|
|
|
self::assertTrue(isset($comments[0])); |
|
39
|
|
|
self::assertEquals($commentId, $comments[0]->getId()); |
|
40
|
|
|
self::assertEquals('Author', $comments[0]->getAuthor()); |
|
41
|
|
|
self::assertEquals('Body', $comments[0]->getBody()); |
|
42
|
|
|
self::assertNotNull($comments[0]->getCreatedAt()); |
|
43
|
|
|
self::assertNull($comments[0]->getParentId()); |
|
44
|
|
|
|
|
45
|
|
|
//and: CommentCreated Event was Published |
|
46
|
|
|
$events = InMemoryEventPublisher::get(CommentCreatedOEvent::class); |
|
47
|
|
|
self::assertCount(1, $events); |
|
48
|
|
|
self::assertTrue(isset($events[0])); |
|
49
|
|
|
InMemoryEventPublisher::assertEventData([ |
|
50
|
|
|
'postId' => $postId, |
|
51
|
|
|
], $events[0]); |
|
52
|
|
|
$commentData = json_decode($events[0]->getData()['comment'], true); |
|
53
|
|
|
self::assertEquals('Author', $commentData['author']); |
|
54
|
|
|
self::assertEquals('Body', $commentData['body']); |
|
55
|
|
|
|
|
56
|
|
|
//and: CommentCountUpdated Event was Published |
|
57
|
|
|
$events = InMemoryEventPublisher::get(CommentsCountUpdatedOEvent::class); |
|
58
|
|
|
self::assertCount(1, $events); |
|
59
|
|
|
self::assertTrue(isset($events[0])); |
|
60
|
|
|
InMemoryEventPublisher::assertEventData([ |
|
61
|
|
|
'postId' => $postId, |
|
62
|
|
|
'commentsCount' => 1, |
|
63
|
|
|
], $events[0]); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @test |
|
68
|
|
|
*/ |
|
69
|
|
|
public function shouldCreateCommentForExistingPostWithParent() |
|
70
|
|
|
{ |
|
71
|
|
|
//given: there was a Post Created |
|
72
|
|
|
$postId = $this->createPost(); |
|
73
|
|
|
|
|
74
|
|
|
//and: someone authored a Comment for that Post |
|
75
|
|
|
$command = new CreateCommentCommand($postId, 'Parent Author', 'Parent Body', null); |
|
76
|
|
|
|
|
77
|
|
|
//and: comment is Created |
|
78
|
|
|
$parentCommentId = $this->commentsApi->createComment($command)->getId(); |
|
79
|
|
|
|
|
80
|
|
|
//and: someone authored a Child Comment for that Post and Parent Comment |
|
81
|
|
|
$command = new CreateCommentCommand($postId, 'Author', 'Body', $parentCommentId); |
|
82
|
|
|
|
|
83
|
|
|
//when: Child Comment is Created |
|
84
|
|
|
$commentId = $this->commentsApi->createComment($command)->getId(); |
|
85
|
|
|
|
|
86
|
|
|
//then: Child Comment was Created correctly |
|
87
|
|
|
self::assertNotNull($commentId); |
|
88
|
|
|
|
|
89
|
|
|
//and: Child Comment can be found by Post Id |
|
90
|
|
|
$comments = $this->commentsApi->findCommentsForPost(new FindCommentsByPostIdQuery($postId)); |
|
91
|
|
|
self::assertNotNull($comments); |
|
92
|
|
|
self::assertCount(2, $comments); |
|
93
|
|
|
self::assertTrue(isset($comments[1])); |
|
94
|
|
|
self::assertEquals($commentId, $comments[1]->getId()); |
|
95
|
|
|
self::assertEquals('Author', $comments[1]->getAuthor()); |
|
96
|
|
|
self::assertEquals('Body', $comments[1]->getBody()); |
|
97
|
|
|
self::assertNotNull($comments[1]->getCreatedAt()); |
|
98
|
|
|
self::assertEquals($parentCommentId, $comments[1]->getParentId()); |
|
99
|
|
|
|
|
100
|
|
|
//and: CommentCreated Event was Published |
|
101
|
|
|
$events = InMemoryEventPublisher::get(CommentCreatedOEvent::class); |
|
102
|
|
|
self::assertCount(2, $events); |
|
103
|
|
|
self::assertTrue(isset($events[1])); |
|
104
|
|
|
InMemoryEventPublisher::assertEventData([ |
|
105
|
|
|
'postId' => $postId, |
|
106
|
|
|
], $events[1]); |
|
107
|
|
|
$commentData = json_decode($events[1]->getData()['comment'], true); |
|
108
|
|
|
self::assertEquals('Author', $commentData['author']); |
|
109
|
|
|
self::assertEquals('Body', $commentData['body']); |
|
110
|
|
|
self::assertEquals($parentCommentId, new Ulid($commentData['parentId'])); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @test |
|
115
|
|
|
*/ |
|
116
|
|
|
public function shouldNotCreateCommentForNotExistingPost() |
|
117
|
|
|
{ |
|
118
|
|
|
|
|
119
|
|
|
//given: someone authored a Comment for not existing Post |
|
120
|
|
|
$command = new CreateCommentCommand(new Ulid(), 'Author', 'Body', null); |
|
121
|
|
|
|
|
122
|
|
|
//expect: |
|
123
|
|
|
$this->expectException(BadRequestHttpException::class); |
|
124
|
|
|
|
|
125
|
|
|
//when: comment is Created |
|
126
|
|
|
$this->commentsApi->createComment($command); |
|
127
|
|
|
|
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
protected function createPost(): Ulid |
|
131
|
|
|
{ |
|
132
|
|
|
$event = new PostCreatedCommentsIEvent($this->getInboundEvent("Comments/PostCreatedCommentsIEvent")); |
|
133
|
|
|
$this->commentsApi->onPostCreated($event); |
|
134
|
|
|
$headers = $this->commentsApi->findPostHeaders(); |
|
135
|
|
|
self::assertNotNull($headers); |
|
136
|
|
|
self::assertCount(1, $headers); |
|
137
|
|
|
self::assertTrue(isset($headers[0])); |
|
138
|
|
|
return $headers[0]->getId(); |
|
139
|
|
|
} |
|
140
|
|
|
} |