Conditions | 1 |
Paths | 1 |
Total Lines | 73 |
Code Lines | 43 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
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 |
||
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 | } |
||
209 | } |