5
5
use Illuminate \Database \Eloquent \Model as Eloquent ;
6
6
use Conner \Tagging \Model \Tag ;
7
7
use Conner \Tagging \Model \TagGroup ;
8
+ use Illuminate \Foundation \Testing \WithFaker ;
8
9
9
10
class TagGroupTest extends TestCase
10
11
{
12
+ use WithFaker;
13
+
11
14
public function setUp (): void
12
15
{
13
16
parent ::setUp ();
17
+ $ this ->setUpFaker ();
14
18
15
19
Eloquent::unguard ();
16
20
}
@@ -24,73 +28,87 @@ public function test_create_group()
24
28
25
29
public function test_tag_group_tags_list ()
26
30
{
27
- $ tag_group = $ this ->createTagGroup ('MyTagGroup ' );
31
+ $ tagGroup = $ this ->createTagGroup ('MyTagGroup ' );
28
32
29
33
$ tag = $ this ->createTag ();
30
34
31
- $ tag ->setGroup ($ tag_group ->name );
35
+ $ tag ->setGroup ($ tagGroup ->name );
32
36
33
- $ this ->assertEquals (1 , $ tag_group ->tags ()->count ());
37
+ $ this ->assertEquals (1 , $ tagGroup ->tags ()->count ());
38
+ $ this ->assertEquals ($ tagGroup ->id , $ tag ->tag_group_id );
34
39
}
35
40
36
41
public function test_add_group_to_tag ()
37
42
{
38
- $ tag_group = $ this ->createTagGroup ('MyTagGroup ' );
43
+ $ tagGroup = $ this ->createTagGroup ('MyTagGroup ' );
39
44
40
45
$ tag = $ this ->createTag ();
41
46
42
- $ tag ->setGroup ($ tag_group ->name );
47
+ $ tag ->setGroup ($ tagGroup ->name );
43
48
44
- $ this ->assertCount (1 , Tag::inGroup ($ tag_group ->name )->get ());
49
+ $ this ->assertCount (1 , Tag::inGroup ($ tagGroup ->name )->get ());
45
50
46
51
$ this ->assertTrue ($ tag ->isInGroup ('MyTagGroup ' ));
47
52
}
48
53
49
54
public function test_delete_group_from_tag ()
50
55
{
51
- $ tag_group = $ this ->createTagGroup ('MyTagGroup ' );
56
+ $ tagGroup = $ this ->createTagGroup ('MyTagGroup ' );
52
57
53
58
$ tag = $ this ->createTag ();
54
59
55
- $ tag ->setGroup ($ tag_group ->name );
60
+ $ tag ->setGroup ($ tagGroup ->name );
56
61
57
- $ this ->assertCount (1 , Tag::inGroup ($ tag_group ->name )->get ());
62
+ $ this ->assertCount (1 , Tag::inGroup ($ tagGroup ->name )->get ());
63
+ $ this ->assertEquals ($ tagGroup ->id , $ tag ->tag_group_id );
58
64
59
- $ tag ->removeGroup ($ tag_group -> name );
65
+ $ tag ->removeGroup ();
60
66
61
- $ this ->assertCount (0 , Tag::inGroup ($ tag_group ->name )->get ());
67
+ $ this ->assertCount (0 , Tag::inGroup ($ tagGroup ->name )->get ());
62
68
63
69
$ this ->assertFalse ($ tag ->isInGroup ('MyTagGroup ' ));
64
70
}
65
71
72
+ public function test_removeGroup_with_no_group ()
73
+ {
74
+ $ tag = $ this ->createTag ();
75
+
76
+ $ tag ->removeGroup ();
77
+
78
+ $ this ->assertTrue (true ); // no exceptions thrown
79
+ }
80
+
66
81
public function test_delete_group_tag ()
67
82
{
68
- $ tag_group = $ this ->createTagGroup ('MyTagGroup ' );
83
+ $ tagGroup = $ this ->createTagGroup ('MyTagGroup ' );
69
84
70
85
$ tag = $ this ->createTag ();
71
86
72
- $ tag ->setGroup ($ tag_group ->name );
87
+ $ tag ->setGroup ($ tagGroup ->name );
73
88
74
- $ tag_group ->delete ();
89
+ $ tagGroup ->delete ();
75
90
76
91
// unless you refresh the tag, it will still think there is a relationship
77
92
$ tag = $ tag ->fresh ();
78
93
79
- $ this ->assertFalse ($ tag_group ->exists );
94
+ $ this ->assertFalse ($ tagGroup ->exists );
80
95
81
96
$ this ->assertNull ($ tag ->group , 'The group should not exist on the tag after it is deleted ' );
82
97
83
98
$ this ->assertFalse ($ tag ->isInGroup ('MyTagGroup ' ), 'The tag should not belong to a deleted group ' );
84
99
}
85
100
86
- private function createTagGroup ($ group_name = ' MyTagGroup ' )
101
+ private function createTagGroup ($ name = null ): TagGroup
87
102
{
103
+ if (is_null ($ name )) {
104
+ $ name = $ this ->faker ->name ;
105
+ }
88
106
return TagGroup::create ([
89
- 'name ' => $ group_name
107
+ 'name ' => $ name
90
108
]);
91
109
}
92
110
93
- private function createTag ($ name = 'Test Tag ' )
111
+ private function createTag ($ name = 'Test Tag ' ): Tag
94
112
{
95
113
$ tag = new Tag ();
96
114
$ tag ->name = $ name ;
0 commit comments