Conditions | 1 |
Paths | 1 |
Total Lines | 73 |
Code Lines | 43 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
32 | public function shouldCreateFetchAndDeleteCommentsForPost() |
||
33 | { |
||
34 | //given: there was a Post Created |
||
35 | $postId = $this->createAndUpdatePost(); |
||
36 | |||
37 | //and: someone authored a Comment for that Post |
||
38 | $command = new CreateCommentCommand($postId, 'Parent Author', 'Parent Body', null); |
||
39 | |||
40 | //and: comment is Created |
||
41 | $parentCommentId = $this->createComment($command)->getId(); |
||
42 | |||
43 | //and: someone authored a Child Comment for that Post and Parent Comment |
||
44 | $command = new CreateCommentCommand($postId, 'Author', 'Body', $parentCommentId); |
||
45 | |||
46 | //and: Child Comment is Created |
||
47 | $commentId = $this->createComment($command)->getId(); |
||
48 | |||
49 | //when: list of Comments for that Post is fetched |
||
50 | $comments = $this->findCommentsForPost(new FindCommentsByPostIdQuery($postId)); |
||
51 | |||
52 | //then: the Post Comments are fetched correctly |
||
53 | self::assertNotNull($comments); |
||
54 | self::assertCount(2, $comments); |
||
55 | self::assertTrue(isset($comments[0])); |
||
56 | self::assertTrue(isset($comments[1])); |
||
57 | |||
58 | self::assertEquals($parentCommentId, $comments[0]->getId()); |
||
59 | self::assertEquals('Parent Author', $comments[0]->getAuthor()); |
||
60 | self::assertEquals('Parent Body', $comments[0]->getBody()); |
||
61 | self::assertNotNull($comments[0]->getCreatedAt()); |
||
62 | self::assertNull($comments[0]->getParentId()); |
||
63 | |||
64 | self::assertEquals($commentId, $comments[1]->getId()); |
||
65 | self::assertEquals('Author', $comments[1]->getAuthor()); |
||
66 | self::assertEquals('Body', $comments[1]->getBody()); |
||
67 | self::assertNotNull($comments[1]->getCreatedAt()); |
||
68 | self::assertEquals($parentCommentId, $comments[1]->getParentId()); |
||
69 | |||
70 | //when: list of Comments for that Post is fetched |
||
71 | $comments = $this->findLatestComments(new FindLatestCommentsQuery(1)); |
||
72 | |||
73 | //then: the Post Comments are fetched correctly |
||
74 | self::assertNotNull($comments); |
||
75 | self::assertCount(2, $comments->getData()); |
||
76 | self::assertEquals(2, $comments->getCount()); |
||
77 | self::assertTrue(isset($comments->getData()[0])); |
||
78 | self::assertTrue(isset($comments->getData()[1])); |
||
79 | |||
80 | $parentComment = $this->convert($comments->getData()[1], FindLatestCommentsQueryResponse::class); |
||
81 | $comment = $this->convert($comments->getData()[0], FindLatestCommentsQueryResponse::class); |
||
82 | |||
83 | self::assertEquals($parentCommentId, $parentComment->getId()); |
||
84 | self::assertEquals('Parent Author', $parentComment->getAuthor()); |
||
85 | self::assertEquals('Parent Body', $parentComment->getBody()); |
||
86 | self::assertNotNull($parentComment->getCreatedAt()); |
||
87 | self::assertNull($parentComment->getParentId()); |
||
88 | |||
89 | self::assertEquals($commentId, $comment->getId()); |
||
90 | self::assertEquals('Author', $comment->getAuthor()); |
||
91 | self::assertEquals('Body', $comment->getBody()); |
||
92 | self::assertNotNull($comment->getCreatedAt()); |
||
93 | self::assertEquals($parentCommentId, $comment->getParentId()); |
||
94 | |||
95 | //when: the Post was Deleted |
||
96 | $this->deletePost($postId); |
||
97 | |||
98 | //and: the list of Comments was fetched |
||
99 | $comments = $this->findLatestComments(new FindLatestCommentsQuery(1)); |
||
100 | |||
101 | //then: there were no Comments left |
||
102 | self::assertNotNull($comments); |
||
103 | self::assertCount(0, $comments->getData()); |
||
104 | self::assertEquals(0, $comments->getCount()); |
||
105 | } |
||
231 | } |