Test Setup Failed
Push — main ( abdcb9...2e5f68 )
by Slawomir
04:37
created

CommentsIT   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 183
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 83
c 1
b 0
f 0
dl 0
loc 183
rs 10
wmc 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A createAndUpdatePost() 0 15 1
A getCommentsApi() 0 3 1
A deletePost() 0 7 1
A shouldBaselineCommentsForPost() 0 30 1
A shouldRenameUserForPost() 0 17 1
A shouldCreateFetchAndDeleteCommentsForPost() 0 73 1
A setupKernel() 0 9 1
1
<?php
2
3
namespace App\Tests\Modules\Comments\Integration;
4
5
use App\Modules\Comments\Api\Command\BaselineCommentsCommand;
6
use App\Modules\Comments\Api\Command\CreateCommentCommand;
7
use App\Modules\Comments\Api\CommentsApiInterface;
8
use App\Modules\Comments\Api\Event\Inbound\PostCreatedCommentsIEvent;
9
use App\Modules\Comments\Api\Event\Inbound\PostDeletedCommentsIEvent;
10
use App\Modules\Comments\Api\Event\Inbound\PostUpdatedCommentsIEvent;
11
use App\Modules\Comments\Api\Event\Inbound\UserRenamedCommentsIEvent;
12
use App\Modules\Comments\Api\Query\FindCommentsByPostIdQuery;
13
use App\Modules\Comments\Api\Query\FindCommentsPostHeadersQuery;
14
use App\Modules\Comments\Api\Query\FindLatestCommentsQuery;
15
use App\Modules\Comments\Api\Query\Response\FindLatestCommentsQueryResponse;
16
use App\Modules\Comments\Domain\Event\Outbound\CommentsBaselinedOEvent;
17
use App\Tests\Modules\Comments\Integration\Http\CommentsHttpTrait;
18
use App\Tests\TestUtils\Contracts\ApplicationEventContractLoader;
19
use App\Tests\TestUtils\Events\InMemoryEventPublisher;
20
use App\Tests\TestUtils\IntegrationTest;
21
use Symfony\Bundle\FrameworkBundle\Console\Application;
22
use Symfony\Component\Console\Tester\CommandTester;
23
use Symfony\Component\Uid\Ulid;
24
25
class CommentsIT extends IntegrationTest
26
{
27
    use CommentsHttpTrait;
28
    use ApplicationEventContractLoader;
29
30
    /**
31
     * @test
32
     */
33
    public function shouldCreateFetchAndDeleteCommentsForPost()
34
    {
35
        //given: there was a Post Created
36
        $postId = $this->createAndUpdatePost();
37
38
        //and: someone authored a Comment for that Post
39
        $command = new CreateCommentCommand($postId, 'Parent Author', 'Parent Body', null);
40
41
        //and: comment is Created
42
        $parentCommentId = $this->createComment($command)->getId();
43
44
        //and: someone authored a Child Comment for that Post and Parent Comment
45
        $command = new CreateCommentCommand($postId, 'Author', 'Body', $parentCommentId);
46
47
        //and: Child Comment is Created
48
        $commentId = $this->createComment($command)->getId();
49
50
        //when: list of Comments for that Post is fetched
51
        $comments = $this->findCommentsForPost(new FindCommentsByPostIdQuery($postId));
52
53
        //then: the Post Comments are fetched correctly
54
        self::assertNotNull($comments);
55
        self::assertCount(2, $comments);
56
        self::assertTrue(isset($comments[0]));
57
        self::assertTrue(isset($comments[1]));
58
59
        self::assertEquals($parentCommentId, $comments[0]->getId());
60
        self::assertEquals('Parent Author', $comments[0]->getAuthor());
61
        self::assertEquals('Parent Body', $comments[0]->getBody());
62
        self::assertNotNull($comments[0]->getCreatedAt());
63
        self::assertNull($comments[0]->getParentId());
64
65
        self::assertEquals($commentId, $comments[1]->getId());
66
        self::assertEquals('Author', $comments[1]->getAuthor());
67
        self::assertEquals('Body', $comments[1]->getBody());
68
        self::assertNotNull($comments[1]->getCreatedAt());
69
        self::assertEquals($parentCommentId, $comments[1]->getParentId());
70
71
        //when: list of Comments for that Post is fetched
72
        $comments = $this->findLatestComments(new FindLatestCommentsQuery(1));
73
74
        //then: the Post Comments are fetched correctly
75
        self::assertNotNull($comments);
76
        self::assertCount(2, $comments->getData());
77
        self::assertEquals(2, $comments->getCount());
78
        self::assertTrue(isset($comments->getData()[0]));
79
        self::assertTrue(isset($comments->getData()[1]));
80
81
        $parentComment = $this->convert($comments->getData()[0], FindLatestCommentsQueryResponse::class);
82
        $comment = $this->convert($comments->getData()[1], FindLatestCommentsQueryResponse::class);
83
84
        self::assertEquals($parentCommentId, $parentComment->getId());
85
        self::assertEquals('Parent Author', $parentComment->getAuthor());
86
        self::assertEquals('Parent Body', $parentComment->getBody());
87
        self::assertNotNull($parentComment->getCreatedAt());
88
        self::assertNull($parentComment->getParentId());
89
90
        self::assertEquals($commentId, $comment->getId());
91
        self::assertEquals('Author', $comment->getAuthor());
92
        self::assertEquals('Body', $comment->getBody());
93
        self::assertNotNull($comment->getCreatedAt());
94
        self::assertEquals($parentCommentId, $comment->getParentId());
95
96
        //when: the Post was Deleted
97
        $this->deletePost($postId);
98
99
        //and: the list of Comments was fetched
100
        $comments = $this->findLatestComments(new FindLatestCommentsQuery(1));
101
102
        //then: there were no Comments left
103
        self::assertNotNull($comments);
104
        self::assertCount(0, $comments->getData());
105
        self::assertEquals(0, $comments->getCount());
106
    }
107
108
    /**
109
     * @test
110
     */
111
    public function shouldBaselineCommentsForPost()
112
    {
113
        //given: there was a Post Created
114
        $postId = $this->createAndUpdatePost();
115
116
        //and: someone authored a Comment for that Post
117
        $command = new CreateCommentCommand($postId, 'Parent Author', 'Parent Body', null);
118
119
        //and: comment is Created
120
        $parentCommentId = $this->createComment($command)->getId();
121
122
        //and: someone authored a Child Comment for that Post and Parent Comment
123
        $command = new CreateCommentCommand($postId, 'Author', 'Body', $parentCommentId);
124
125
        //and: Child Comment is Created
126
        $this->createComment($command)->getId();
127
128
        //when: Comments are baselined
129
        $application = $this->setupKernel();
130
        $command = $application->find('app:comments:baseline');
131
        $commandTester = new CommandTester($command);
132
        $commandTester->execute([]);
133
        $output = $commandTester->getDisplay();
134
135
        //then: Comments were baselined correctly
136
        self::assertStringStartsWith('Successfully base-lined all Comments', $output);
137
        $events = InMemoryEventPublisher::get(CommentsBaselinedOEvent::class);
138
        self::assertCount(1, $events);
139
        self::assertEquals($postId, $events[0]->getData()['postId']);
140
        self::assertCount(2, json_decode($events[0]->getData()['comments']));
141
    }
142
143
    /**
144
     * @test
145
     */
146
    public function shouldRenameUserForPost()
147
    {
148
        //given: there was a Post Created
149
        $postId = $this->createAndUpdatePost();
0 ignored issues
show
Unused Code introduced by
The assignment to $postId is dead and can be removed.
Loading history...
150
151
        //and: the Header was to be updated
152
        $event = new UserRenamedCommentsIEvent($this->getInboundEvent("Comments/UserRenamedCommentsIEvent"));
153
154
        //when: the Event was published
155
        $this->getCommentsApi()->onUserRenamed($event);
156
157
        //then: Post Header was created - no Exception was thrown
158
        $headers = $this->getCommentsApi()->findPostHeaders(new FindCommentsPostHeadersQuery());
159
        self::assertNotNull($headers);
160
        self::assertCount(1, $headers);
161
        self::assertTrue(isset($headers[0]));
162
        self::assertEquals($event->getNewLogin(), $headers[0]->getCreatedByName());
163
    }
164
165
    private function createAndUpdatePost(): Ulid
166
    {
167
        //given: there was a PostCreatedIEvent to be published
168
        $event = new PostCreatedCommentsIEvent($this->getInboundEvent("Comments/PostCreatedCommentsIEvent"));
169
170
        //and: the Event was already published
171
        $this->getCommentsApi()->onPostCreated($event);
172
173
        //and: the Header was to be updated
174
        $event = new PostUpdatedCommentsIEvent($this->getInboundEvent("Comments/PostUpdatedCommentsIEvent"));
175
176
        //and: the Event was published
177
        $this->getCommentsApi()->onPostUpdated($event);
178
179
        return $event->getId();
180
    }
181
182
    private function deletePost(Ulid $postId): void
0 ignored issues
show
Unused Code introduced by
The parameter $postId is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

182
    private function deletePost(/** @scrutinizer ignore-unused */ Ulid $postId): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
183
    {
184
        //and: the Header was to be updated
185
        $event = new PostDeletedCommentsIEvent($this->getInboundEvent("Comments/PostDeletedCommentsIEvent"));
186
187
        //and: the Event was published
188
        $this->getCommentsApi()->onPostDeleted($event);
189
    }
190
191
    private function getCommentsApi(): CommentsApiInterface
192
    {
193
        return $this->getContainer()->get(CommentsApiInterface::class);
1 ignored issue
show
Bug Best Practice introduced by
The expression return $this->getContain...ntsApiInterface::class) could return the type null which is incompatible with the type-hinted return App\Modules\Comments\Api\CommentsApiInterface. Consider adding an additional type-check to rule them out.
Loading history...
194
    }
195
196
    /**
197
     * @return Application
198
     */
199
    protected function setupKernel(): Application
200
    {
201
        $kernel = static::createKernel();
202
        $kernel->boot();
203
        $application = new Application($kernel);
204
        $application->add(new BaselineCommentsCommand(
205
            $this->getContainer()->get(CommentsApiInterface::class)
1 ignored issue
show
Bug introduced by
It seems like $this->getContainer()->g...ntsApiInterface::class) can also be of type null; however, parameter $commentsApi of App\Modules\Comments\Api...sCommand::__construct() does only seem to accept App\Modules\Comments\Api\CommentsApiInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

205
            /** @scrutinizer ignore-type */ $this->getContainer()->get(CommentsApiInterface::class)
Loading history...
206
        ));
207
        return $application;
208
    }
209
}