2
2
3
3
namespace Illuminate \Tests \Integration \Database ;
4
4
5
+ use Exception ;
5
6
use Illuminate \Database \Eloquent \Model ;
6
7
use Illuminate \Database \Eloquent \Prunable ;
7
8
use Illuminate \Database \Eloquent \SoftDeletes ;
8
9
use Illuminate \Database \Events \ModelsPruned ;
9
10
use Illuminate \Database \Schema \Blueprint ;
10
11
use Illuminate \Support \Facades \Event ;
12
+ use Illuminate \Support \Facades \Exceptions ;
11
13
use Illuminate \Support \Facades \Schema ;
12
14
use LogicException ;
13
15
@@ -20,6 +22,7 @@ protected function afterRefreshingDatabase()
20
22
'prunable_soft_delete_test_models ' ,
21
23
'prunable_test_model_missing_prunable_methods ' ,
22
24
'prunable_with_custom_prune_method_test_models ' ,
25
+ 'prunable_with_exceptions ' ,
23
26
])->each (function ($ table ) {
24
27
Schema::create ($ table , function (Blueprint $ table ) {
25
28
$ table ->increments ('id ' );
@@ -97,6 +100,27 @@ public function testPruneWithCustomPruneMethod()
97
100
98
101
Event::assertDispatched (ModelsPruned::class, 1 );
99
102
}
103
+
104
+ public function testPruneWithExceptionAtOneOfModels ()
105
+ {
106
+ Event::fake ();
107
+ Exceptions::fake ();
108
+
109
+ collect (range (1 , 5000 ))->map (function ($ id ) {
110
+ return ['name ' => 'foo ' ];
111
+ })->chunk (200 )->each (function ($ chunk ) {
112
+ PrunableWithException::insert ($ chunk ->all ());
113
+ });
114
+
115
+ $ count = (new PrunableWithException )->pruneAll ();
116
+
117
+ $ this ->assertEquals (999 , $ count );
118
+
119
+ Event::assertDispatched (ModelsPruned::class, 1 );
120
+ Event::assertDispatched (fn (ModelsPruned $ event ) => $ event ->count === 999 );
121
+ Exceptions::assertReportedCount (1 );
122
+ Exceptions::assertReported (fn (Exception $ exception ) => $ exception ->getMessage () === 'foo bar ' );
123
+ }
100
124
}
101
125
102
126
class PrunableTestModel extends Model
@@ -136,6 +160,23 @@ public function prune()
136
160
}
137
161
}
138
162
163
+ class PrunableWithException extends Model
164
+ {
165
+ use Prunable;
166
+
167
+ public function prunable ()
168
+ {
169
+ return $ this ->where ('id ' , '<= ' , 1000 );
170
+ }
171
+
172
+ public function prune ()
173
+ {
174
+ if ($ this ->id === 500 ) {
175
+ throw new Exception ('foo bar ' );
176
+ }
177
+ }
178
+ }
179
+
139
180
class PrunableTestModelMissingPrunableMethod extends Model
140
181
{
141
182
use Prunable;
0 commit comments