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 |
||
| 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 |