|
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\Post; |
|
13
|
|
|
use DavisPeixoto\BlogCore\Entity\Author; |
|
14
|
|
|
use DavisPeixoto\BlogCore\Entity\Tag; |
|
15
|
|
|
use PHPUnit_Framework_TestCase; |
|
16
|
|
|
use Ramsey\Uuid\Uuid; |
|
17
|
|
|
|
|
18
|
|
|
class TestPost extends PHPUnit_Framework_TestCase |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @Post $post |
|
22
|
|
|
*/ |
|
23
|
|
|
private $post; |
|
24
|
|
|
|
|
25
|
|
|
public function setUp() |
|
26
|
|
|
{ |
|
27
|
|
|
$this->post = new Post(Uuid::uuid4(), 'A Post', 'Lorem ipsum', new Author(Uuid::uuid4(), 'Davis', '[email protected]', 'Some string', new DateTime()), [], null); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param $uuid |
|
32
|
|
|
* @param $title |
|
33
|
|
|
* @param $body |
|
34
|
|
|
* @param $author |
|
35
|
|
|
* @param $tags |
|
36
|
|
|
* @param $publishDate |
|
37
|
|
|
* @param $expected |
|
38
|
|
|
* @param $message |
|
39
|
|
|
* @dataProvider postConstructor |
|
40
|
|
|
*/ |
|
41
|
|
|
public function testConstructor($uuid, $title, $body, $author, $tags, $publishDate, $expected, $message) |
|
42
|
|
|
{ |
|
43
|
|
|
$post = new Post($uuid, $title, $body, $author, $tags, $publishDate); |
|
44
|
|
|
$this->assertInstanceOf($expected, $post, $message); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param $date |
|
49
|
|
|
* @param $expected |
|
50
|
|
|
* @param $message |
|
51
|
|
|
* @dataProvider publishDateProvider |
|
52
|
|
|
*/ |
|
53
|
|
|
public function testPublishDates($date, $expected, $message) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->post->setPublishDate($date); |
|
56
|
|
|
$this->assertEquals($expected, $this->post->getPublishDate(), $message); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param $tags |
|
61
|
|
|
* @param $tag |
|
62
|
|
|
* @param $expected |
|
63
|
|
|
* @param $message |
|
64
|
|
|
* @dataProvider addTagProvider |
|
65
|
|
|
*/ |
|
66
|
|
|
public function testAddTag($tags, $tag, $expected, $message) |
|
67
|
|
|
{ |
|
68
|
|
|
$this->post->setTags($tags); |
|
69
|
|
|
$this->post->addTag($tag); |
|
70
|
|
|
$this->assertEquals($expected, $this->post->getTags(), $message); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param $tags |
|
75
|
|
|
* @param $tag |
|
76
|
|
|
* @param $expected |
|
77
|
|
|
* @param $message |
|
78
|
|
|
* @dataProvider removeTagProvider |
|
79
|
|
|
*/ |
|
80
|
|
|
public function testRemoveTag($tags, $tag, $expected, $message) |
|
81
|
|
|
{ |
|
82
|
|
|
$this->post->setTags($tags); |
|
83
|
|
|
$this->post->removeTag($tag); |
|
84
|
|
|
$this->assertEquals($expected, $this->post->getTags(), $message); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function postConstructor() |
|
88
|
|
|
{ |
|
89
|
|
|
return [ |
|
90
|
|
|
[Uuid::uuid4(), 'A Post', 'Lorem ipsum', new Author(Uuid::uuid4(), 'Davis', '[email protected]', 'Some string', new DateTime()), [], null, Post::class, 'no tags, no publish date'], |
|
91
|
|
|
[Uuid::uuid4(), 'A Post', 'Lorem ipsum', new Author(Uuid::uuid4(), 'Davis', '[email protected]', 'Some string', new DateTime()), [new Tag(Uuid::uuid4(),'tag1'), new Tag(Uuid::uuid4(),'tag2')], null, Post::class, 'have tags, unpublished'], |
|
|
|
|
|
|
92
|
|
|
[Uuid::uuid4(), 'A Post', 'Lorem ipsum', new Author(Uuid::uuid4(), 'Davis', '[email protected]', 'Some string', new DateTime()), [], new DateTime(), Post::class, 'no tags, published'], |
|
93
|
|
|
[Uuid::uuid4(), 'A Post', 'Lorem ipsum', new Author(Uuid::uuid4(), 'Davis', '[email protected]', 'Some string', new DateTime()), [new Tag(Uuid::uuid4(),'tag1'), new Tag(Uuid::uuid4(),'tag2')], new DateTime(), Post::class, 'tags, published (most common scenario)'] |
|
|
|
|
|
|
94
|
|
|
]; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function publishDateProvider() |
|
98
|
|
|
{ |
|
99
|
|
|
$date1 = new DateTime(); |
|
100
|
|
|
|
|
101
|
|
|
return [ |
|
102
|
|
|
[null, null, 'Null Test'], |
|
103
|
|
|
[$date1, $date1, 'Positive Test'] |
|
104
|
|
|
]; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
View Code Duplication |
public function addTagProvider() |
|
|
|
|
|
|
108
|
|
|
{ |
|
109
|
|
|
$tag1 = new Tag(Uuid::uuid4(), 'tag 1'); |
|
110
|
|
|
$tag2 = new Tag(Uuid::uuid4(), 'tag 2'); |
|
111
|
|
|
|
|
112
|
|
|
return [ |
|
113
|
|
|
[[], $tag1, [$tag1], 'positive test, on null, first tag added'], |
|
114
|
|
|
[[$tag1], $tag2, [$tag1, $tag2], 'positive test, adding a new tag to existing tag vector'], |
|
115
|
|
|
[[$tag1, $tag2], $tag1, [$tag1, $tag2], 'negative test, tags should not be duplicated'] |
|
116
|
|
|
]; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
View Code Duplication |
public function removeTagProvider() |
|
|
|
|
|
|
120
|
|
|
{ |
|
121
|
|
|
$tag1 = new Tag(Uuid::uuid4(), 'tag 1'); |
|
122
|
|
|
$tag2 = new Tag(Uuid::uuid4(), 'tag 2'); |
|
123
|
|
|
|
|
124
|
|
|
return [ |
|
125
|
|
|
[[$tag1], $tag1, [], 'positive test, all tags removed'], |
|
126
|
|
|
[[$tag1, $tag2], $tag1, [$tag2], 'positive test, removing one tag'], |
|
127
|
|
|
[[$tag1], $tag2, [$tag1], 'negative test, removing non-existent tag'] |
|
128
|
|
|
]; |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.