5
5
use Illuminate \Container \Container ;
6
6
use Illuminate \Contracts \Events \Dispatcher as DispatcherContract ;
7
7
use Illuminate \Database \Capsule \Manager as DB ;
8
- use Illuminate \Database \ConnectionResolverInterface ;
9
8
use Illuminate \Database \Console \PruneCommand ;
10
9
use Illuminate \Database \Eloquent \MassPrunable ;
11
10
use Illuminate \Database \Eloquent \Model ;
12
11
use Illuminate \Database \Eloquent \Prunable ;
12
+ use Illuminate \Database \Eloquent \SoftDeletes ;
13
13
use Illuminate \Database \Events \ModelsPruned ;
14
14
use Illuminate \Events \Dispatcher ;
15
- use Mockery as m ;
16
15
use PHPUnit \Framework \TestCase ;
17
16
use Symfony \Component \Console \Input \ArrayInput ;
18
17
use Symfony \Component \Console \Output \BufferedOutput ;
@@ -53,6 +52,36 @@ public function testPrunableTestModelWithoutPrunableRecords()
53
52
EOF, str_replace ("\r" , '' , $ output ->fetch ()));
54
53
}
55
54
55
+ public function testPrunableSoftDeletedModelWithPrunableRecords ()
56
+ {
57
+ $ db = new DB ;
58
+ $ db ->addConnection ([
59
+ 'driver ' => 'sqlite ' ,
60
+ 'database ' => ':memory: ' ,
61
+ ]);
62
+ $ db ->bootEloquent ();
63
+ $ db ->setAsGlobal ();
64
+ DB ::connection ('default ' )->getSchemaBuilder ()->create ('prunables ' , function ($ table ) {
65
+ $ table ->string ('value ' )->nullable ();
66
+ $ table ->datetime ('deleted_at ' )->nullable ();
67
+ });
68
+ DB ::connection ('default ' )->table ('prunables ' )->insert ([
69
+ ['value ' => 1 , 'deleted_at ' => null ],
70
+ ['value ' => 2 , 'deleted_at ' => '2021-12-01 00:00:00 ' ],
71
+ ['value ' => 3 , 'deleted_at ' => null ],
72
+ ['value ' => 4 , 'deleted_at ' => '2021-12-02 00:00:00 ' ],
73
+ ]);
74
+
75
+ $ output = $ this ->artisan (['--model ' => PrunableTestSoftDeletedModelWithPrunableRecords::class]);
76
+
77
+ $ this ->assertEquals (<<<'EOF'
78
+ 2 [Illuminate\Tests\Database\PrunableTestSoftDeletedModelWithPrunableRecords] records have been pruned.
79
+
80
+ EOF, str_replace ("\r" , '' , $ output ->fetch ()));
81
+
82
+ $ this ->assertEquals (2 , PrunableTestSoftDeletedModelWithPrunableRecords::withTrashed ()->count ());
83
+ }
84
+
56
85
public function testNonPrunableTest ()
57
86
{
58
87
$ output = $ this ->artisan (['--model ' => NonPrunableTestModel::class]);
@@ -70,6 +99,7 @@ public function testTheCommandMayBePretended()
70
99
'driver ' => 'sqlite ' ,
71
100
'database ' => ':memory: ' ,
72
101
]);
102
+ $ db ->bootEloquent ();
73
103
$ db ->setAsGlobal ();
74
104
DB ::connection ('default ' )->getSchemaBuilder ()->create ('prunables ' , function ($ table ) {
75
105
$ table ->string ('name ' )->nullable ();
@@ -82,8 +112,6 @@ public function testTheCommandMayBePretended()
82
112
['name ' => 'stuart ' , 'value ' => 4 ],
83
113
['name ' => 'bello ' , 'value ' => 5 ],
84
114
]);
85
- $ resolver = m::mock (ConnectionResolverInterface::class, ['connection ' => $ db ->getConnection ('default ' )]);
86
- PrunableTestModelWithPrunableRecords::setConnectionResolver ($ resolver );
87
115
88
116
$ output = $ this ->artisan ([
89
117
'--model ' => PrunableTestModelWithPrunableRecords::class,
@@ -98,6 +126,39 @@ public function testTheCommandMayBePretended()
98
126
$ this ->assertEquals (5 , PrunableTestModelWithPrunableRecords::count ());
99
127
}
100
128
129
+ public function testTheCommandMayBePretendedOnSoftDeletedModel ()
130
+ {
131
+ $ db = new DB ;
132
+ $ db ->addConnection ([
133
+ 'driver ' => 'sqlite ' ,
134
+ 'database ' => ':memory: ' ,
135
+ ]);
136
+ $ db ->bootEloquent ();
137
+ $ db ->setAsGlobal ();
138
+ DB ::connection ('default ' )->getSchemaBuilder ()->create ('prunables ' , function ($ table ) {
139
+ $ table ->string ('value ' )->nullable ();
140
+ $ table ->datetime ('deleted_at ' )->nullable ();
141
+ });
142
+ DB ::connection ('default ' )->table ('prunables ' )->insert ([
143
+ ['value ' => 1 , 'deleted_at ' => null ],
144
+ ['value ' => 2 , 'deleted_at ' => '2021-12-01 00:00:00 ' ],
145
+ ['value ' => 3 , 'deleted_at ' => null ],
146
+ ['value ' => 4 , 'deleted_at ' => '2021-12-02 00:00:00 ' ],
147
+ ]);
148
+
149
+ $ output = $ this ->artisan ([
150
+ '--model ' => PrunableTestSoftDeletedModelWithPrunableRecords::class,
151
+ '--pretend ' => true ,
152
+ ]);
153
+
154
+ $ this ->assertEquals (<<<'EOF'
155
+ 2 [Illuminate\Tests\Database\PrunableTestSoftDeletedModelWithPrunableRecords] records will be pruned.
156
+
157
+ EOF, str_replace ("\r" , '' , $ output ->fetch ()));
158
+
159
+ $ this ->assertEquals (4 , PrunableTestSoftDeletedModelWithPrunableRecords::withTrashed ()->count ());
160
+ }
161
+
101
162
protected function artisan ($ arguments )
102
163
{
103
164
$ input = new ArrayInput ($ arguments );
@@ -139,6 +200,19 @@ public function prunable()
139
200
}
140
201
}
141
202
203
+ class PrunableTestSoftDeletedModelWithPrunableRecords extends Model
204
+ {
205
+ use MassPrunable, SoftDeletes;
206
+
207
+ protected $ table = 'prunables ' ;
208
+ protected $ connection = 'default ' ;
209
+
210
+ public function prunable ()
211
+ {
212
+ return static ::where ('value ' , '>= ' , 3 );
213
+ }
214
+ }
215
+
142
216
class PrunableTestModelWithoutPrunableRecords extends Model
143
217
{
144
218
use Prunable;
0 commit comments