2
2
3
3
namespace Tests ;
4
4
5
+ use Conner \Tagging \Contracts \TaggableContract ;
5
6
use Illuminate \Database \Eloquent \Model as Eloquent ;
6
7
use Conner \Tagging \Taggable ;
8
+ use Illuminate \Foundation \Testing \WithFaker ;
9
+ use Illuminate \Support \Collection ;
7
10
8
11
class CommonUsageTest extends TestCase
9
12
{
13
+ use WithFaker;
14
+
10
15
public function setUp (): void
11
16
{
12
17
parent ::setUp ();
18
+ $ this ->setUpFaker ();
13
19
14
20
\Schema::create ('books ' , function ($ table ) {
15
21
$ table ->increments ('id ' );
@@ -29,7 +35,7 @@ public function tearDown(): void
29
35
30
36
public function test_tag_call ()
31
37
{
32
- $ stub = Stub:: create ([ ' name ' => 123 ] );
38
+ $ stub = $ this -> book ( );
33
39
34
40
$ stub ->tag ('test123 ' );
35
41
$ stub ->tag ('456 ' );
@@ -40,7 +46,7 @@ public function test_tag_call()
40
46
41
47
public function test_untag_call ()
42
48
{
43
- $ stub = Stub:: create ([ ' name ' => ' Stub ' ] );
49
+ $ stub = $ this -> book ( );
44
50
45
51
$ stub ->tag ('one ' );
46
52
$ stub ->tag ('two ' );
@@ -56,8 +62,7 @@ public function test_untag_call()
56
62
57
63
public function test_retag ()
58
64
{
59
- /** @var Stub $stub */
60
- $ stub = Stub::create (['name ' =>123 ]);
65
+ $ stub = $ this ->book ();
61
66
62
67
$ stub ->tag ('first ' );
63
68
$ stub ->tag ('second ' );
@@ -69,7 +74,7 @@ public function test_retag()
69
74
70
75
public function test_tag_names_attribute ()
71
76
{
72
- $ stub = Stub:: create ([ ' name ' => 123 , 'tag_names ' =>'foo, bar ' ]);
77
+ $ stub = $ this -> book ([ 'tag_names ' =>'foo, bar ' ]);
73
78
74
79
$ stub ->save ();
75
80
@@ -78,8 +83,7 @@ public function test_tag_names_attribute()
78
83
79
84
public function test_the_tagged_property ()
80
85
{
81
- /** @var Stub $stub */
82
- $ stub = Stub::create (['name ' =>123 ]);
86
+ $ stub = $ this ->book ();
83
87
84
88
$ stub ->tag ('first ' );
85
89
$ stub ->tag ('second ' );
@@ -93,8 +97,7 @@ public function test_the_tagged_property()
93
97
94
98
public function test_calling_tagNames_as_a_property ()
95
99
{
96
- /** @var Stub $stub */
97
- $ stub = Stub::create (['name ' =>123 ]);
100
+ $ stub = $ this ->book ();
98
101
99
102
$ stub ->tag ('first ' );
100
103
$ stub ->tag ('second ' );
@@ -105,8 +108,7 @@ public function test_calling_tagNames_as_a_property()
105
108
106
109
public function test_get_tags ()
107
110
{
108
- /** @var Stub $stub */
109
- $ stub = Stub::create (['name ' =>123 ]);
111
+ $ stub = $ this ->book ();
110
112
111
113
$ stub ->tag ('first ' );
112
114
$ stub ->tag ('second ' );
@@ -116,7 +118,7 @@ public function test_get_tags()
116
118
117
119
public function test_setting_tag_names_array ()
118
120
{
119
- $ stub = new Stub ();
121
+ $ stub = new Book ();
120
122
$ stub ->name = 'test ' ;
121
123
$ stub ->tag_names = ['foo ' , 'bar ' ];
122
124
$ stub ->save ();
@@ -129,21 +131,79 @@ public function test_setting_tag_names_array()
129
131
130
132
public function test_tagging_with_empty_tags ()
131
133
{
132
- /** @var Stub $stub */
133
- $ stub = Stub::create (['name ' =>123 ]);
134
+ $ stub = $ this ->book ();
134
135
135
136
$ tagName = "Japan, Asia, Economy, , , , , " ;
136
137
137
138
$ stub ->retag ($ tagName );
138
139
139
140
$ this ->assertEquals (['Japan ' , 'Asia ' , 'Economy ' ], $ stub ->tag_names );
140
141
}
142
+
143
+ function test_withAllTags ()
144
+ {
145
+ $ one = $ this ->book ();
146
+ $ two = $ this ->book ();
147
+ $ three = $ this ->book ();
148
+
149
+ $ one ->tag (['one ' ]);
150
+ $ two ->tag (['b ' , 'two ' ]);
151
+ $ three ->tag (['one ' , 'two ' ]);
152
+
153
+ $ list = Book::withAllTags (['one ' , 'two ' ])->get ();
154
+
155
+ $ this ->assertCount (1 , $ list );
156
+ $ this ->assertEquals ($ three ->id , $ list [0 ]->id );
157
+ }
158
+
159
+ function test_withAnyTags ()
160
+ {
161
+ $ one = $ this ->book ();
162
+ $ two = $ this ->book ();
163
+ $ three = $ this ->book ();
164
+
165
+ $ one ->tag (['one ' ]);
166
+ $ two ->tag (['b ' , 'two ' ]);
167
+
168
+ /** @var Collection $list */
169
+ $ list = Book::withAnyTag (['one ' , 'two ' ])->get ();
170
+
171
+ $ this ->assertCount (2 , $ list );
172
+ $ indexed = $ list ->keyBy ('id ' );
173
+ $ this ->assertNotEmpty ($ indexed [$ one ->id ]);
174
+ $ this ->assertNotEmpty ($ indexed [$ two ->id ]);
175
+ }
176
+
177
+ function test_withoutTags ()
178
+ {
179
+ $ one = $ this ->book ();
180
+ $ two = $ this ->book ();
181
+ $ three = $ this ->book ();
182
+
183
+ $ one ->tag (['one ' ]);
184
+ $ two ->tag (['b ' , 'two ' ]);
185
+
186
+ /** @var Collection $list */
187
+ $ list = Book::withoutTags (['one ' , 'two ' ])->get ();
188
+
189
+ $ this ->assertCount (1 , $ list );
190
+ $ this ->assertEquals ($ three ->id , $ list [0 ]->id );
191
+ }
192
+
193
+
194
+ private function book ($ attributes = []): Book
195
+ {
196
+ $ attributes = array_merge (['name ' =>$ this ->faker ->name ], $ attributes );
197
+
198
+ return Book::create ($ attributes );
199
+ }
141
200
}
142
201
202
+
143
203
/**
144
204
* @property string name
145
205
*/
146
- class Stub extends Eloquent
206
+ class Book extends Eloquent implements TaggableContract
147
207
{
148
208
use Taggable;
149
209
0 commit comments