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

ThreadTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 70
Duplicated Lines 37.14 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 5
dl 26
loc 70
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\Entity;
13
14
use Doctrine\Common\Collections\ArrayCollection;
15
use Sulu\Bundle\CommentBundle\Entity\CommentInterface;
16
use Sulu\Bundle\CommentBundle\Entity\Thread;
17
18
class ThreadTest extends \PHPUnit_Framework_TestCase
19
{
20
    public function testCommentCount()
21
    {
22
        $thread = new Thread('test', 1);
23
        $this->assertEquals(0, $thread->getCommentCount());
24
25
        $thread->increaseCommentCount();
26
        $this->assertEquals(1, $thread->getCommentCount());
27
28
        $thread->decreaseCommentCount();
29
        $this->assertEquals(0, $thread->getCommentCount());
30
31
        $thread->setCommentCount(5);
32
        $this->assertEquals(5, $thread->getCommentCount());
33
    }
34
35
    public function testAddPublishedComment()
36
    {
37
        $comment = $this->prophesize(CommentInterface::class);
38
        $thread = new Thread('test', 1);
39
40
        $comment->setThread($thread)->shouldBeCalled();
41
        $comment->isPublished()->willReturn(true);
42
43
        $thread->addComment($comment->reveal());
44
        $this->assertEquals($comment->reveal(), $thread->getComments()->first());
45
        $this->assertEquals(1, $thread->getCommentCount());
46
        $this->assertCount(1, $thread->getComments());
47
    }
48
49
    public function testAddUnpublishedComment()
50
    {
51
        $comment = $this->prophesize(CommentInterface::class);
52
        $thread = new Thread('test', 1);
53
54
        $comment->setThread($thread)->shouldBeCalled();
55
        $comment->isPublished()->willReturn(false);
56
57
        $thread->addComment($comment->reveal());
58
        $this->assertEquals($comment->reveal(), $thread->getComments()->first());
59
        $this->assertEquals(0, $thread->getCommentCount());
60
        $this->assertCount(1, $thread->getComments());
61
    }
62
63
    public function testRemovePublishedComment()
64
    {
65
        $comment = $this->prophesize(CommentInterface::class);
66
        $thread = new Thread('test', 1, new ArrayCollection([$comment->reveal()]), 1);
67
68
        $comment->isPublished()->willReturn(true);
69
70
        $thread->removeComment($comment->reveal());
71
        $this->assertEquals(0, $thread->getCommentCount());
72
        $this->assertCount(0, $thread->getComments());
73
    }
74
75
    public function testRemoveUnpublishedComment()
76
    {
77
        $comment1 = $this->prophesize(CommentInterface::class);
78
        $comment2 = $this->prophesize(CommentInterface::class);
79
        $thread = new Thread('test', 1, new ArrayCollection([$comment1->reveal(), $comment2->reveal()]), 1);
80
81
        $comment1->isPublished()->willReturn(false);
82
83
        $thread->removeComment($comment1->reveal());
84
        $this->assertEquals(1, $thread->getCommentCount());
85
        $this->assertCount(1, $thread->getComments());
86
    }
87
}
88