6
6
use Illuminate \Contracts \Events \Dispatcher as DispatcherContract ;
7
7
use Illuminate \Database \Capsule \Manager as DB ;
8
8
use Illuminate \Database \Console \PruneCommand ;
9
- use Illuminate \Database \Eloquent \MassPrunable ;
10
- use Illuminate \Database \Eloquent \Model ;
11
- use Illuminate \Database \Eloquent \Prunable ;
12
- use Illuminate \Database \Eloquent \SoftDeletes ;
13
9
use Illuminate \Database \Events \ModelPruningFinished ;
14
10
use Illuminate \Database \Events \ModelPruningStarting ;
15
11
use Illuminate \Database \Events \ModelsPruned ;
@@ -26,7 +22,15 @@ protected function setUp(): void
26
22
{
27
23
parent ::setUp ();
28
24
29
- Application::setInstance ($ container = new Application );
25
+ Application::setInstance ($ container = new Application (__DIR__ .'/Pruning ' ));
26
+
27
+ Closure::bind (
28
+ fn () => $ this ->namespace = 'Illuminate \\Tests \\Database \\Pruning \\' ,
29
+ $ container ,
30
+ Application::class,
31
+ )();
32
+
33
+ $ container ->useAppPath (__DIR__ .'/Pruning ' );
30
34
31
35
$ container ->singleton (DispatcherContract::class, function () {
32
36
return new Dispatcher ();
@@ -37,12 +41,12 @@ protected function setUp(): void
37
41
38
42
public function testPrunableModelWithPrunableRecords ()
39
43
{
40
- $ output = $ this ->artisan (['--model ' => PrunableTestModelWithPrunableRecords::class]);
44
+ $ output = $ this ->artisan (['--model ' => Pruning \ Models \ PrunableTestModelWithPrunableRecords::class]);
41
45
42
46
$ output = $ output ->fetch ();
43
47
44
48
$ this ->assertStringContainsString (
45
- 'Illuminate\Tests\Database\PrunableTestModelWithPrunableRecords ' ,
49
+ 'Illuminate\Tests\Database\Pruning\Models\ PrunableTestModelWithPrunableRecords ' ,
46
50
$ output ,
47
51
);
48
52
@@ -52,7 +56,7 @@ public function testPrunableModelWithPrunableRecords()
52
56
);
53
57
54
58
$ this ->assertStringContainsString (
55
- 'Illuminate\Tests\Database\PrunableTestModelWithPrunableRecords ' ,
59
+ 'Illuminate\Tests\Database\Pruning\Models\ PrunableTestModelWithPrunableRecords ' ,
56
60
$ output ,
57
61
);
58
62
@@ -64,10 +68,10 @@ public function testPrunableModelWithPrunableRecords()
64
68
65
69
public function testPrunableTestModelWithoutPrunableRecords ()
66
70
{
67
- $ output = $ this ->artisan (['--model ' => PrunableTestModelWithoutPrunableRecords::class]);
71
+ $ output = $ this ->artisan (['--model ' => Pruning \ Models \ PrunableTestModelWithoutPrunableRecords::class]);
68
72
69
73
$ this ->assertStringContainsString (
70
- 'No prunable [Illuminate\Tests\Database\PrunableTestModelWithoutPrunableRecords] records found. ' ,
74
+ 'No prunable [Illuminate\Tests\Database\Pruning\Models\ PrunableTestModelWithoutPrunableRecords] records found. ' ,
71
75
$ output ->fetch ()
72
76
);
73
77
}
@@ -92,12 +96,12 @@ public function testPrunableSoftDeletedModelWithPrunableRecords()
92
96
['value ' => 4 , 'deleted_at ' => '2021-12-02 00:00:00 ' ],
93
97
]);
94
98
95
- $ output = $ this ->artisan (['--model ' => PrunableTestSoftDeletedModelWithPrunableRecords::class]);
99
+ $ output = $ this ->artisan (['--model ' => Pruning \ Models \ PrunableTestSoftDeletedModelWithPrunableRecords::class]);
96
100
97
101
$ output = $ output ->fetch ();
98
102
99
103
$ this ->assertStringContainsString (
100
- 'Illuminate\Tests\Database\PrunableTestSoftDeletedModelWithPrunableRecords ' ,
104
+ 'Illuminate\Tests\Database\Pruning\Models\ PrunableTestSoftDeletedModelWithPrunableRecords ' ,
101
105
$ output ,
102
106
);
103
107
@@ -106,29 +110,51 @@ public function testPrunableSoftDeletedModelWithPrunableRecords()
106
110
$ output ,
107
111
);
108
112
109
- $ this ->assertEquals (2 , PrunableTestSoftDeletedModelWithPrunableRecords::withTrashed ()->count ());
113
+ $ this ->assertEquals (2 , Pruning \ Models \ PrunableTestSoftDeletedModelWithPrunableRecords::withTrashed ()->count ());
110
114
}
111
115
112
116
public function testNonPrunableTest ()
113
117
{
114
- $ output = $ this ->artisan (['--model ' => NonPrunableTestModel::class]);
118
+ $ output = $ this ->artisan (['--model ' => Pruning \ Models \ NonPrunableTestModel::class]);
115
119
116
120
$ this ->assertStringContainsString (
117
- 'No prunable [Illuminate\Tests\Database\NonPrunableTestModel] records found. ' ,
121
+ 'No prunable [Illuminate\Tests\Database\Pruning\Models\ NonPrunableTestModel] records found. ' ,
118
122
$ output ->fetch (),
119
123
);
120
124
}
121
125
122
126
public function testNonPrunableTestWithATrait ()
123
127
{
124
- $ output = $ this ->artisan (['--model ' => NonPrunableTrait::class]);
128
+ $ output = $ this ->artisan (['--model ' => Pruning \ Models \ NonPrunableTrait::class]);
125
129
126
130
$ this ->assertStringContainsString (
127
131
'No prunable models found. ' ,
128
132
$ output ->fetch (),
129
133
);
130
134
}
131
135
136
+ public function testNonModelFilesAreIgnoredTest ()
137
+ {
138
+ $ output = $ this ->artisan (['--path ' => 'Models ' ]);
139
+
140
+ $ output = $ output ->fetch ();
141
+
142
+ $ this ->assertStringNotContainsString (
143
+ 'No prunable [Illuminate\Tests\Database\Pruning\Models\AbstractPrunableModel] records found. ' ,
144
+ $ output ,
145
+ );
146
+
147
+ $ this ->assertStringNotContainsString (
148
+ 'No prunable [Illuminate\Tests\Database\Pruning\Models\SomeClass] records found. ' ,
149
+ $ output ,
150
+ );
151
+
152
+ $ this ->assertStringNotContainsString (
153
+ 'No prunable [Illuminate\Tests\Database\Pruning\Models\SomeEnum] records found. ' ,
154
+ $ output ,
155
+ );
156
+ }
157
+
132
158
public function testTheCommandMayBePretended ()
133
159
{
134
160
$ db = new DB ;
@@ -151,16 +177,16 @@ public function testTheCommandMayBePretended()
151
177
]);
152
178
153
179
$ output = $ this ->artisan ([
154
- '--model ' => PrunableTestModelWithPrunableRecords::class,
180
+ '--model ' => Pruning \ Models \ PrunableTestModelWithPrunableRecords::class,
155
181
'--pretend ' => true ,
156
182
]);
157
183
158
184
$ this ->assertStringContainsString (
159
- '3 [Illuminate\Tests\Database\PrunableTestModelWithPrunableRecords] records will be pruned. ' ,
185
+ '3 [Illuminate\Tests\Database\Pruning\Models\ PrunableTestModelWithPrunableRecords] records will be pruned. ' ,
160
186
$ output ->fetch (),
161
187
);
162
188
163
- $ this ->assertEquals (5 , PrunableTestModelWithPrunableRecords::count ());
189
+ $ this ->assertEquals (5 , Pruning \ Models \ PrunableTestModelWithPrunableRecords::count ());
164
190
}
165
191
166
192
public function testTheCommandMayBePretendedOnSoftDeletedModel ()
@@ -184,16 +210,16 @@ public function testTheCommandMayBePretendedOnSoftDeletedModel()
184
210
]);
185
211
186
212
$ output = $ this ->artisan ([
187
- '--model ' => PrunableTestSoftDeletedModelWithPrunableRecords::class,
213
+ '--model ' => Pruning \ Models \ PrunableTestSoftDeletedModelWithPrunableRecords::class,
188
214
'--pretend ' => true ,
189
215
]);
190
216
191
217
$ this ->assertStringContainsString (
192
- '2 [Illuminate\Tests\Database\PrunableTestSoftDeletedModelWithPrunableRecords] records will be pruned. ' ,
218
+ '2 [Illuminate\Tests\Database\Pruning\Models\ PrunableTestSoftDeletedModelWithPrunableRecords] records will be pruned. ' ,
193
219
$ output ->fetch (),
194
220
);
195
221
196
- $ this ->assertEquals (4 , PrunableTestSoftDeletedModelWithPrunableRecords::withTrashed ()->count ());
222
+ $ this ->assertEquals (4 , Pruning \ Models \ PrunableTestSoftDeletedModelWithPrunableRecords::withTrashed ()->count ());
197
223
}
198
224
199
225
public function testTheCommandDispatchesEvents ()
@@ -202,19 +228,19 @@ public function testTheCommandDispatchesEvents()
202
228
203
229
$ dispatcher ->shouldReceive ('dispatch ' )->once ()->withArgs (function ($ event ) {
204
230
return get_class ($ event ) === ModelPruningStarting::class &&
205
- $ event ->models === [PrunableTestModelWithPrunableRecords::class];
231
+ $ event ->models === [Pruning \ Models \ PrunableTestModelWithPrunableRecords::class];
206
232
});
207
233
$ dispatcher ->shouldReceive ('listen ' )->once ()->with (ModelsPruned::class, m::type (Closure::class));
208
234
$ dispatcher ->shouldReceive ('dispatch ' )->twice ()->with (m::type (ModelsPruned::class));
209
235
$ dispatcher ->shouldReceive ('dispatch ' )->once ()->withArgs (function ($ event ) {
210
236
return get_class ($ event ) === ModelPruningFinished::class &&
211
- $ event ->models === [PrunableTestModelWithPrunableRecords::class];
237
+ $ event ->models === [Pruning \ Models \ PrunableTestModelWithPrunableRecords::class];
212
238
});
213
239
$ dispatcher ->shouldReceive ('forget ' )->once ()->with (ModelsPruned::class);
214
240
215
241
Application::getInstance ()->instance (DispatcherContract::class, $ dispatcher );
216
242
217
- $ this ->artisan (['--model ' => PrunableTestModelWithPrunableRecords::class]);
243
+ $ this ->artisan (['--model ' => Pruning \ Models \ PrunableTestModelWithPrunableRecords::class]);
218
244
}
219
245
220
246
protected function artisan ($ arguments )
@@ -238,57 +264,3 @@ protected function tearDown(): void
238
264
m::close ();
239
265
}
240
266
}
241
-
242
- class PrunableTestModelWithPrunableRecords extends Model
243
- {
244
- use MassPrunable;
245
-
246
- protected $ table = 'prunables ' ;
247
- protected $ connection = 'default ' ;
248
-
249
- public function pruneAll ()
250
- {
251
- event (new ModelsPruned (static ::class, 10 ));
252
- event (new ModelsPruned (static ::class, 20 ));
253
-
254
- return 20 ;
255
- }
256
-
257
- public function prunable ()
258
- {
259
- return static ::where ('value ' , '>= ' , 3 );
260
- }
261
- }
262
-
263
- class PrunableTestSoftDeletedModelWithPrunableRecords extends Model
264
- {
265
- use MassPrunable, SoftDeletes;
266
-
267
- protected $ table = 'prunables ' ;
268
- protected $ connection = 'default ' ;
269
-
270
- public function prunable ()
271
- {
272
- return static ::where ('value ' , '>= ' , 3 );
273
- }
274
- }
275
-
276
- class PrunableTestModelWithoutPrunableRecords extends Model
277
- {
278
- use Prunable;
279
-
280
- public function pruneAll ()
281
- {
282
- return 0 ;
283
- }
284
- }
285
-
286
- class NonPrunableTestModel extends Model
287
- {
288
- // ..
289
- }
290
-
291
- trait NonPrunableTrait
292
- {
293
- use Prunable;
294
- }
0 commit comments