TestPost::postConstructor()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 69

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 69
rs 8.6763
c 0
b 0
f 0
cc 1
nc 1
nop 0

How to fix   Long Method   

Long Method

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:

1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: davis
5
 * Date: 7/23/17
6
 * Time: 7:25 PM
7
 */
8
9
namespace DavisPeixoto\BlogCore\Tests\Entity;
10
11
use DateTime;
12
use DavisPeixoto\BlogCore\Entity\Author;
13
use DavisPeixoto\BlogCore\Entity\Post;
14
use DavisPeixoto\BlogCore\Entity\Tag;
15
use PHPUnit\Framework\TestCase;
16
use Ramsey\Uuid\Uuid;
17
18
class TestPost extends TestCase
19
{
20
    /**
21
     * @var Post $post
22
     */
23
    private $post;
24
25
    public function setUp()
26
    {
27
        $this->post = new Post('A Post', 'Lorem ipsum', new Author(
28
            'Davis',
29
            '[email protected]',
30
            'Some string',
31
            null,
32
            new DateTime()
33
        ), null, [], null);
34
    }
35
36
    /**
37
     * @param string|null $uuid
38
     * @param string $title
39
     * @param string $body
40
     * @param Author $author
41
     * @param Tag[] $tags
42
     * @param DateTime|null $publishDate
43
     * @param string $expected
44
     * @param string $message
45
     * @dataProvider postConstructor
46
     */
47
    public function testConstructor($uuid, $title, $body, $author, $tags, $publishDate, $expected, $message)
48
    {
49
        $post = new Post($title, $body, $author, $uuid, $tags, $publishDate);
50
        $this->assertInstanceOf($expected, $post, $message);
51
    }
52
53
    /**
54
     * @param DateTime|null $date
55
     * @param string $expected
56
     * @param string $message
57
     * @dataProvider publishDateProvider
58
     */
59
    public function testPublishDates($date, $expected, $message)
60
    {
61
        $this->post->setPublishDate($date);
62
        $this->assertEquals($expected, $this->post->getPublishDate(), $message);
63
    }
64
65
    /**
66
     * @param Tag[] $tags
67
     * @param Tag $tag
68
     * @param string $expected
69
     * @param string $message
70
     * @dataProvider addTagProvider
71
     */
72
    public function testAddTag($tags, $tag, $expected, $message)
73
    {
74
        $this->post->setTags($tags);
75
        $this->post->addTag($tag);
76
        $this->assertEquals($expected, $this->post->getTags(), $message);
77
    }
78
79
    /**
80
     * @param Tag[] $tags
81
     * @param Tag $tag
82
     * @param string $expected
83
     * @param string $message
84
     * @dataProvider removeTagProvider
85
     */
86
    public function testRemoveTag($tags, $tag, $expected, $message)
87
    {
88
        $this->post->setTags($tags);
89
        $this->post->removeTag($tag);
90
        $this->assertEquals($expected, $this->post->getTags(), $message);
91
    }
92
93
    public function postConstructor()
94
    {
95
        return [
96
            [
97
                null,
98
                'A Post',
99
                'Lorem ipsum',
100
                new Author(
101
                    'Davis',
102
                    '[email protected]',
103
                    'Some string',
104
                    null,
105
                    new DateTime()
106
                ),
107
                [],
108
                null,
109
                Post::class,
110
                'no tags, no publish date',
111
            ],
112
            [
113
                Uuid::uuid4()->toString(),
114
                'A Post',
115
                'Lorem ipsum',
116
                new Author(
117
                    'Davis',
118
                    '[email protected]',
119
                    'Some string',
120
                    null,
121
                    new DateTime()
122
                ),
123
                [new Tag('tag1', null), new Tag('tag2', null)],
124
                null,
125
                Post::class,
126
                'have tags, unpublished',
127
            ],
128
            [
129
                null,
130
                'A Post',
131
                'Lorem ipsum',
132
                new Author(
133
                    'Davis',
134
                    '[email protected]',
135
                    'Some string',
136
                    null,
137
                    new DateTime()
138
                ),
139
                [],
140
                new DateTime(),
141
                Post::class,
142
                'no tags, published',
143
            ],
144
            [
145
                '',
146
                'A Post',
147
                'Lorem ipsum',
148
                new Author(
149
                    'Davis',
150
                    '[email protected]',
151
                    'Some string',
152
                    null,
153
                    new DateTime()
154
                ),
155
                [new Tag('tag1', null), new Tag('tag2', null)],
156
                new DateTime(),
157
                Post::class,
158
                'tags, published (most common scenario)',
159
            ]
160
        ];
161
    }
162
163
    public function publishDateProvider()
164
    {
165
        $date1 = new DateTime();
166
167
        return [
168
            [null, null, 'Null Test'],
169
            [$date1, $date1, 'Positive Test']
170
        ];
171
    }
172
173
    public function addTagProvider()
174
    {
175
        $tag1 = new Tag('tag 1', null);
176
        $tag2 = new Tag('tag 2', Uuid::uuid4()->toString());
177
178
        return [
179
            [[], $tag1, [$tag1], 'positive test, on null, first tag added'],
180
            [[$tag1], $tag2, [$tag1, $tag2], 'positive test, adding a new tag to existing tag vector'],
181
            [[$tag1, $tag2], $tag1, [$tag1, $tag2], 'negative test, tags should not be duplicated']
182
        ];
183
    }
184
185
    public function removeTagProvider()
186
    {
187
        $tag1 = new Tag('tag 1', '');
188
        $tag2 = new Tag('tag 2', null);
189
190
        return [
191
            [[$tag1], $tag1, [], 'positive test, all tags removed'],
192
            [[$tag1, $tag2], $tag1, [$tag2], 'positive test, removing one tag'],
193
            [[$tag1], $tag2, [$tag1], 'negative test, removing non-existent tag']
194
        ];
195
    }
196
}
197