Cancelled
Pull Request — master (#19)
by Wachter
05:00 queued 21s
created

CommentManagerTest   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 407
Duplicated Lines 59.46 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 11
dl 242
loc 407
rs 10
c 0
b 0
f 0

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/*
4
 * This file is part of Sulu.
5
 *
6
 * (c) MASSIVE ART WebServices GmbH
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Sulu\Bundle\CommentBundle\Tests\Unit\Manager;
13
14
use Prophecy\Argument;
15
use Sulu\Bundle\CommentBundle\Entity\CommentInterface;
16
use Sulu\Bundle\CommentBundle\Entity\CommentRepositoryInterface;
17
use Sulu\Bundle\CommentBundle\Entity\ThreadInterface;
18
use Sulu\Bundle\CommentBundle\Entity\ThreadRepositoryInterface;
19
use Sulu\Bundle\CommentBundle\Events\CommentEvent;
20
use Sulu\Bundle\CommentBundle\Events\Events;
21
use Sulu\Bundle\CommentBundle\Events\ThreadEvent;
22
use Sulu\Bundle\CommentBundle\Manager\CommentManager;
23
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
24
25
class CommentManagerTest extends \PHPUnit_Framework_TestCase
26
{
27
    /**
28
     * @var ThreadRepositoryInterface
29
     */
30
    private $threadRepository;
31
32
    /**
33
     * @var CommentRepositoryInterface
34
     */
35
    private $commentRepository;
36
37
    /**
38
     * @var EventDispatcherInterface
39
     */
40
    private $dispatcher;
41
42
    /**
43
     * @var CommentManager
44
     */
45
    private $commentManager;
46
47
    /**
48
     * @var ThreadInterface
49
     */
50
    private $thread;
51
52
    /**
53
     * @var CommentInterface
54
     */
55
    private $comment;
56
57
    protected function setUp()
58
    {
59
        $this->threadRepository = $this->prophesize(ThreadRepositoryInterface::class);
60
        $this->commentRepository = $this->prophesize(CommentRepositoryInterface::class);
61
        $this->dispatcher = $this->prophesize(EventDispatcherInterface::class);
62
63
        $this->thread = $this->prophesize(ThreadInterface::class);
64
        $this->comment = $this->prophesize(CommentInterface::class);
65
66
        $this->commentManager = new CommentManager(
67
            $this->threadRepository->reveal(),
68
            $this->commentRepository->reveal(),
69
            $this->dispatcher->reveal()
70
        );
71
    }
72
73
    public function testFindComments($type = 'article', $entityId = '123-123-123')
74
    {
75
        $this->commentRepository->findComments($type, $entityId, 1, null)->shouldBeCalled()->willReturn([]);
76
77
        $comments = $this->commentManager->findComments($type, $entityId);
78
        $this->assertEquals([], $comments);
79
    }
80
81
    public function testAddComment($type = 'article', $entityId = '123-123-123')
82
    {
83
        $this->threadRepository->findThread($type, $entityId)->willReturn($this->thread->reveal());
84
85
        $commentRepository = $this->commentRepository;
86
        $comment = $this->comment;
87
        $dispatcher = $this->dispatcher;
88
        $thread = $this->thread;
89
90
        $this->dispatcher->dispatch(Events::PRE_PERSIST_EVENT, Argument::type(CommentEvent::class))
91
            ->shouldBeCalledTimes(1)
92
            ->will(
93
                function () use ($commentRepository, $comment, $dispatcher, $thread) {
94
                    $thread->addComment($comment->reveal())->willReturn($thread->reveal());
95
                    $commentRepository->persist($comment->reveal())
96
                        ->shouldBeCalledTimes(1)
97
                        ->will(
98
                            function () use ($dispatcher, $thread) {
99
                                $dispatcher->dispatch(
100
                                    Events::POST_PERSIST_EVENT,
101
                                    Argument::type(CommentEvent::class)
102
                                )->shouldBeCalledTimes(1);
103
104
                                return $thread->reveal();
105
                            }
106
                        );
107
                }
108
            );
109
110
        $thread->setTitle(Argument::any())->shouldNotBeCalled();
111
        $thread = $this->commentManager->addComment($type, $entityId, $this->comment->reveal());
112
113
        $this->assertEquals($this->thread->reveal(), $thread);
114
    }
115
116
    public function testAddCommentWithThreadTitle($type = 'article', $entityId = '123-123-123')
117
    {
118
        $this->threadRepository->findThread($type, $entityId)->willReturn($this->thread->reveal());
119
120
        $commentRepository = $this->commentRepository;
121
        $comment = $this->comment;
122
        $dispatcher = $this->dispatcher;
123
        $thread = $this->thread;
124
125
        $this->dispatcher->dispatch(Events::PRE_PERSIST_EVENT, Argument::type(CommentEvent::class))
126
            ->shouldBeCalledTimes(1)
127
            ->will(
128
                function () use ($commentRepository, $comment, $dispatcher, $thread) {
129
                    $thread->addComment($comment->reveal())->willReturn($thread->reveal());
130
                    $commentRepository->persist($comment->reveal())
131
                        ->shouldBeCalledTimes(1)
132
                        ->will(
133
                            function () use ($dispatcher, $thread) {
134
                                $dispatcher->dispatch(
135
                                    Events::POST_PERSIST_EVENT,
136
                                    Argument::type(CommentEvent::class)
137
                                )->shouldBeCalledTimes(1);
138
139
                                return $thread->reveal();
140
                            }
141
                        );
142
                }
143
            );
144
145
        $thread->setTitle('Test')->shouldBeCalled();
146
        $thread = $this->commentManager->addComment($type, $entityId, $this->comment->reveal(), 'Test');
147
148
        $this->assertEquals($this->thread->reveal(), $thread);
149
    }
150
151
    public function testUpdate()
152
    {
153
        $comment = $this->comment;
154
        $comment->getThread()->willReturn($this->thread->reveal());
155
156
        $this->dispatcher->dispatch(
157
            Events::PRE_UPDATE_EVENT,
158
            Argument::that(
159
                function (CommentEvent $event) use ($comment) {
160
                    return $event->getComment() === $comment->reveal();
161
                }
162
            )
163
        )->shouldBeCalled();
164
165
        $this->assertEquals($comment->reveal(), $this->commentManager->update($comment->reveal()));
166
    }
167
168
    public function testUpdateThread()
169
    {
170
        $thread = $this->thread;
171
172
        $this->dispatcher->dispatch(
173
            Events::THREAD_PRE_UPDATE_EVENT,
174
            Argument::that(
175
                function (ThreadEvent $event) use ($thread) {
176
                    return $event->getThread() === $thread->reveal();
177
                }
178
            )
179
        )->shouldBeCalled();
180
181
        $this->assertEquals($thread->reveal(), $this->commentManager->updateThread($thread->reveal()));
182
    }
183
184
    public function testDelete()
185
    {
186
        $dispatcher = $this->dispatcher;
187
188
        $comments = [
189
            $this->prophesize(CommentInterface::class),
190
            $this->prophesize(CommentInterface::class),
191
            $this->prophesize(CommentInterface::class),
192
        ];
193
        $commentsReveal = [];
194
195
        $this->thread->getType()->willReturn('Test');
196
        $this->thread->getEntityId()->willReturn('123-123-123');
197
198
        foreach ($comments as $comment) {
199
            $comment->getThread()->willReturn($this->thread->reveal());
200
            $this->thread->removeComment($comment->reveal())->shouldBeCalled();
201
            $this->commentRepository->delete($comment->reveal())->shouldBeCalled();
202
203
            $this->dispatcher->dispatch(
204
                Events::PRE_DELETE_EVENT,
205
                Argument::that(
206
                    function (CommentEvent $event) use ($comment) {
207
                        return $event->getComment() === $comment->reveal();
208
                    }
209
                )
210
            )->will(
211
                function () use ($comment, $dispatcher) {
212
                    $dispatcher->dispatch(
213
                        Events::POST_DELETE_EVENT,
214
                        Argument::that(
215
                            function (CommentEvent $event) use ($comment) {
216
                                return $event->getComment() === $comment->reveal();
217
                            }
218
                        )
219
                    )->shouldBeCalledTimes(1);
220
                }
221
            )->shouldBeCalledTimes(1);
222
223
            $commentsReveal[] = $comment->reveal();
224
        }
225
226
        $this->commentRepository->findCommentsByIds([1, 2, 3])->willReturn($commentsReveal);
227
228
        $this->commentManager->delete([1, 2, 3]);
229
    }
230
231
    public function testDeleteOne()
232
    {
233
        $dispatcher = $this->dispatcher;
234
235
        $comments = [
236
            $this->prophesize(CommentInterface::class),
237
        ];
238
        $commentsReveal = [];
239
240
        $this->thread->getType()->willReturn('Test');
241
        $this->thread->getEntityId()->willReturn('123-123-123');
242
243
        foreach ($comments as $comment) {
244
            $comment->getThread()->willReturn($this->thread->reveal());
245
            $this->thread->removeComment($comment->reveal())->shouldBeCalled();
246
            $this->commentRepository->delete($comment->reveal())->shouldBeCalled();
247
248
            $this->dispatcher->dispatch(
249
                Events::PRE_DELETE_EVENT,
250
                Argument::that(
251
                    function (CommentEvent $event) use ($comment) {
252
                        return $event->getComment() === $comment->reveal();
253
                    }
254
                )
255
            )->will(
256
                function () use ($comment, $dispatcher) {
257
                    $dispatcher->dispatch(
258
                        Events::POST_DELETE_EVENT,
259
                        Argument::that(
260
                            function (CommentEvent $event) use ($comment) {
261
                                return $event->getComment() === $comment->reveal();
262
                            }
263
                        )
264
                    )->shouldBeCalledTimes(1);
265
                }
266
            )->shouldBeCalledTimes(1);
267
268
            $commentsReveal[] = $comment->reveal();
269
        }
270
271
        $this->commentRepository->findCommentsByIds([1])->willReturn($commentsReveal);
272
273
        $this->commentManager->delete(1);
274
    }
275
276
    public function testDeleteThread()
277
    {
278
        $dispatcher = $this->dispatcher;
279
280
        $threads = [
281
            $this->prophesize(ThreadInterface::class),
282
            $this->prophesize(ThreadInterface::class),
283
            $this->prophesize(ThreadInterface::class),
284
        ];
285
        $threadsReveal = [];
286
287
        foreach ($threads as $thread) {
288
            $this->threadRepository->delete($thread->reveal())->shouldBeCalled();
289
290
            $this->dispatcher->dispatch(
291
                Events::THREAD_PRE_DELETE_EVENT,
292
                Argument::that(
293
                    function (ThreadEvent $event) use ($thread) {
294
                        return $event->getThread() === $thread->reveal();
295
                    }
296
                )
297
            )->will(
298
                function () use ($thread, $dispatcher) {
299
                    $dispatcher->dispatch(
300
                        Events::THREAD_POST_DELETE_EVENT,
301
                        Argument::that(
302
                            function (ThreadEvent $event) use ($thread) {
303
                                return $event->getThread() === $thread->reveal();
304
                            }
305
                        )
306
                    )->shouldBeCalledTimes(1);
307
                }
308
            )->shouldBeCalledTimes(1);
309
310
            $threadsReveal[] = $thread->reveal();
311
        }
312
313
        $this->threadRepository->findThreadsByIds([1, 2, 3])->willReturn($threadsReveal);
314
315
        $this->commentManager->deleteThreads([1, 2, 3]);
316
    }
317
318
    public function testDeleteThreadOne()
319
    {
320
        $dispatcher = $this->dispatcher;
321
322
        $threads = [
323
            $this->prophesize(ThreadInterface::class),
324
        ];
325
        $threadsReveal = [];
326
327
        foreach ($threads as $thread) {
328
            $this->threadRepository->delete($thread->reveal())->shouldBeCalled();
329
330
            $this->dispatcher->dispatch(
331
                Events::THREAD_PRE_DELETE_EVENT,
332
                Argument::that(
333
                    function (ThreadEvent $event) use ($thread) {
334
                        return $event->getThread() === $thread->reveal();
335
                    }
336
                )
337
            )->will(
338
                function () use ($thread, $dispatcher) {
339
                    $dispatcher->dispatch(
340
                        Events::THREAD_POST_DELETE_EVENT,
341
                        Argument::that(
342
                            function (ThreadEvent $event) use ($thread) {
343
                                return $event->getThread() === $thread->reveal();
344
                            }
345
                        )
346
                    )->shouldBeCalledTimes(1);
347
                }
348
            )->shouldBeCalledTimes(1);
349
350
            $threadsReveal[] = $thread->reveal();
351
        }
352
353
        $this->threadRepository->findThreadsByIds([1])->willReturn($threadsReveal);
354
355
        $this->commentManager->deleteThreads(1);
356
    }
357
358
    public function testPublish()
359
    {
360
        $this->comment->isPublished()->willReturn(false);
361
        $this->comment->publish()->shouldBeCalled();
362
        $this->comment->getThread()->willReturn($this->thread->reveal());
363
364
        $comment = $this->comment;
365
        $this->dispatcher->dispatch(
366
            Events::PUBLISH_EVENT,
367
            Argument::that(
368
                function (CommentEvent $event) use ($comment) {
369
                    return $event->getComment() === $comment->reveal();
370
                }
371
            )
372
        )->shouldBeCalledTimes(1);
373
374
        $this->assertEquals($this->comment->reveal(), $this->commentManager->publish($this->comment->reveal()));
375
    }
376
377
    public function testPublishIsAlreadyPublished()
378
    {
379
        $this->comment->isPublished()->willReturn(true);
380
        $this->comment->publish()->shouldNotBeCalled();
381
382
        $comment = $this->comment;
383
        $this->dispatcher->dispatch(
384
            Events::PUBLISH_EVENT,
385
            Argument::that(
386
                function (CommentEvent $event) use ($comment) {
387
                    return $event->getComment() === $comment->reveal();
388
                }
389
            )
390
        )->shouldNotBeCalled();
391
392
        $this->assertEquals($this->comment->reveal(), $this->commentManager->publish($this->comment->reveal()));
393
    }
394
395
    public function testUnpublish()
396
    {
397
        $this->comment->isPublished()->willReturn(true);
398
        $this->comment->unpublish()->shouldBeCalled();
399
        $this->comment->getThread()->willReturn($this->thread->reveal());
400
401
        $comment = $this->comment;
402
        $this->dispatcher->dispatch(
403
            Events::UNPUBLISH_EVENT,
404
            Argument::that(
405
                function (CommentEvent $event) use ($comment) {
406
                    return $event->getComment() === $comment->reveal();
407
                }
408
            )
409
        )->shouldBeCalledTimes(1);
410
411
        $this->assertEquals($this->comment->reveal(), $this->commentManager->unpublish($this->comment->reveal()));
412
    }
413
414
    public function testUnpublishIsAlreadyUnpublished()
415
    {
416
        $this->comment->isPublished()->willReturn(false);
417
        $this->comment->unpublish()->shouldNotBeCalled();
418
419
        $comment = $this->comment;
420
        $this->dispatcher->dispatch(
421
            Events::UNPUBLISH_EVENT,
422
            Argument::that(
423
                function (CommentEvent $event) use ($comment) {
424
                    return $event->getComment() === $comment->reveal();
425
                }
426
            )
427
        )->shouldNotBeCalled();
428
429
        $this->assertEquals($this->comment->reveal(), $this->commentManager->unpublish($this->comment->reveal()));
430
    }
431
}
432