-
Notifications
You must be signed in to change notification settings - Fork 11k
/
PruneCommandTest.php
244 lines (194 loc) · 7.14 KB
/
PruneCommandTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<?php
namespace Illuminate\Tests\Database;
use Illuminate\Container\Container;
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
use Illuminate\Database\Capsule\Manager as DB;
use Illuminate\Database\Console\PruneCommand;
use Illuminate\Database\Eloquent\MassPrunable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Prunable;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Events\ModelsPruned;
use Illuminate\Events\Dispatcher;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
class PruneCommandTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
Container::setInstance($container = new Container);
$container->singleton(DispatcherContract::class, function () {
return new Dispatcher();
});
$container->alias(DispatcherContract::class, 'events');
}
public function testPrunableModelWithPrunableRecords()
{
$output = $this->artisan(['--model' => PrunableTestModelWithPrunableRecords::class]);
$this->assertEquals(<<<'EOF'
10 [Illuminate\Tests\Database\PrunableTestModelWithPrunableRecords] records have been pruned.
20 [Illuminate\Tests\Database\PrunableTestModelWithPrunableRecords] records have been pruned.
EOF, str_replace("\r", '', $output->fetch()));
}
public function testPrunableTestModelWithoutPrunableRecords()
{
$output = $this->artisan(['--model' => PrunableTestModelWithoutPrunableRecords::class]);
$this->assertEquals(<<<'EOF'
No prunable [Illuminate\Tests\Database\PrunableTestModelWithoutPrunableRecords] records found.
EOF, str_replace("\r", '', $output->fetch()));
}
public function testPrunableSoftDeletedModelWithPrunableRecords()
{
$db = new DB;
$db->addConnection([
'driver' => 'sqlite',
'database' => ':memory:',
]);
$db->bootEloquent();
$db->setAsGlobal();
DB::connection('default')->getSchemaBuilder()->create('prunables', function ($table) {
$table->string('value')->nullable();
$table->datetime('deleted_at')->nullable();
});
DB::connection('default')->table('prunables')->insert([
['value' => 1, 'deleted_at' => null],
['value' => 2, 'deleted_at' => '2021-12-01 00:00:00'],
['value' => 3, 'deleted_at' => null],
['value' => 4, 'deleted_at' => '2021-12-02 00:00:00'],
]);
$output = $this->artisan(['--model' => PrunableTestSoftDeletedModelWithPrunableRecords::class]);
$this->assertEquals(<<<'EOF'
2 [Illuminate\Tests\Database\PrunableTestSoftDeletedModelWithPrunableRecords] records have been pruned.
EOF, str_replace("\r", '', $output->fetch()));
$this->assertEquals(2, PrunableTestSoftDeletedModelWithPrunableRecords::withTrashed()->count());
}
public function testNonPrunableTest()
{
$output = $this->artisan(['--model' => NonPrunableTestModel::class]);
$this->assertEquals(<<<'EOF'
No prunable [Illuminate\Tests\Database\NonPrunableTestModel] records found.
EOF, str_replace("\r", '', $output->fetch()));
}
public function testNonPrunableTestWithATrait()
{
$output = $this->artisan(['--model' => NonPrunableTrait::class]);
$this->assertEquals(<<<'EOF'
No prunable models found.
EOF, str_replace("\r", '', $output->fetch()));
}
public function testTheCommandMayBePretended()
{
$db = new DB;
$db->addConnection([
'driver' => 'sqlite',
'database' => ':memory:',
]);
$db->bootEloquent();
$db->setAsGlobal();
DB::connection('default')->getSchemaBuilder()->create('prunables', function ($table) {
$table->string('name')->nullable();
$table->string('value')->nullable();
});
DB::connection('default')->table('prunables')->insert([
['name' => 'zain', 'value' => 1],
['name' => 'patrice', 'value' => 2],
['name' => 'amelia', 'value' => 3],
['name' => 'stuart', 'value' => 4],
['name' => 'bello', 'value' => 5],
]);
$output = $this->artisan([
'--model' => PrunableTestModelWithPrunableRecords::class,
'--pretend' => true,
]);
$this->assertEquals(<<<'EOF'
3 [Illuminate\Tests\Database\PrunableTestModelWithPrunableRecords] records will be pruned.
EOF, str_replace("\r", '', $output->fetch()));
$this->assertEquals(5, PrunableTestModelWithPrunableRecords::count());
}
public function testTheCommandMayBePretendedOnSoftDeletedModel()
{
$db = new DB;
$db->addConnection([
'driver' => 'sqlite',
'database' => ':memory:',
]);
$db->bootEloquent();
$db->setAsGlobal();
DB::connection('default')->getSchemaBuilder()->create('prunables', function ($table) {
$table->string('value')->nullable();
$table->datetime('deleted_at')->nullable();
});
DB::connection('default')->table('prunables')->insert([
['value' => 1, 'deleted_at' => null],
['value' => 2, 'deleted_at' => '2021-12-01 00:00:00'],
['value' => 3, 'deleted_at' => null],
['value' => 4, 'deleted_at' => '2021-12-02 00:00:00'],
]);
$output = $this->artisan([
'--model' => PrunableTestSoftDeletedModelWithPrunableRecords::class,
'--pretend' => true,
]);
$this->assertEquals(<<<'EOF'
2 [Illuminate\Tests\Database\PrunableTestSoftDeletedModelWithPrunableRecords] records will be pruned.
EOF, str_replace("\r", '', $output->fetch()));
$this->assertEquals(4, PrunableTestSoftDeletedModelWithPrunableRecords::withTrashed()->count());
}
protected function artisan($arguments)
{
$input = new ArrayInput($arguments);
$output = new BufferedOutput;
tap(new PruneCommand())
->setLaravel(Container::getInstance())
->run($input, $output);
return $output;
}
public function tearDown(): void
{
parent::tearDown();
Container::setInstance(null);
}
}
class PrunableTestModelWithPrunableRecords extends Model
{
use MassPrunable;
protected $table = 'prunables';
protected $connection = 'default';
public function pruneAll()
{
event(new ModelsPruned(static::class, 10));
event(new ModelsPruned(static::class, 20));
return 20;
}
public function prunable()
{
return static::where('value', '>=', 3);
}
}
class PrunableTestSoftDeletedModelWithPrunableRecords extends Model
{
use MassPrunable, SoftDeletes;
protected $table = 'prunables';
protected $connection = 'default';
public function prunable()
{
return static::where('value', '>=', 3);
}
}
class PrunableTestModelWithoutPrunableRecords extends Model
{
use Prunable;
public function pruneAll()
{
return 0;
}
}
class NonPrunableTestModel extends Model
{
// ..
}
trait NonPrunableTrait
{
use Prunable;
}