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