Skip to content

Commit b83abb4

Browse files
ziadoztaylorotwell
andauthored
[10.x] Dispatch model pruning started and ended events (#47669)
* fire start/end events * test events * style ci * formatting * fix method --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 33aa4a9 commit b83abb4

File tree

4 files changed

+81
-0
lines changed

4 files changed

+81
-0
lines changed

src/Illuminate/Database/Console/PruneCommand.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use Illuminate\Database\Eloquent\MassPrunable;
88
use Illuminate\Database\Eloquent\Prunable;
99
use Illuminate\Database\Eloquent\SoftDeletes;
10+
use Illuminate\Database\Events\ModelPruningFinished;
11+
use Illuminate\Database\Events\ModelPruningStarting;
1012
use Illuminate\Database\Events\ModelsPruned;
1113
use Illuminate\Support\Str;
1214
use InvalidArgumentException;
@@ -70,10 +72,14 @@ public function handle(Dispatcher $events)
7072
$this->components->twoColumnDetail($event->model, "{$event->count} records");
7173
});
7274

75+
$events->dispatch(new ModelPruningStarting($models->all()));
76+
7377
$models->each(function ($model) {
7478
$this->pruneModel($model);
7579
});
7680

81+
$events->dispatch(new ModelPruningFinished($models->all()));
82+
7783
$events->forget(ModelsPruned::class);
7884
}
7985

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Illuminate\Database\Events;
4+
5+
class ModelPruningFinished
6+
{
7+
/**
8+
* The class names of the models that were pruned.
9+
*
10+
* @var array<class-string>
11+
*/
12+
public $models;
13+
14+
/**
15+
* Create a new event instance.
16+
*
17+
* @param array<class-string> $models
18+
* @return void
19+
*/
20+
public function __construct($models)
21+
{
22+
$this->models = $models;
23+
}
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Illuminate\Database\Events;
4+
5+
class ModelPruningStarting
6+
{
7+
/**
8+
* The class names of the models that will be pruned.
9+
*
10+
* @var array<class-string>
11+
*/
12+
public $models;
13+
14+
/**
15+
* Create a new event instance.
16+
*
17+
* @param array<class-string> $models
18+
* @return void
19+
*/
20+
public function __construct($models)
21+
{
22+
$this->models = $models;
23+
}
24+
}

tests/Database/PruneCommandTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Illuminate\Tests\Database;
44

5+
use Closure;
56
use Illuminate\Container\Container;
67
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
78
use Illuminate\Database\Capsule\Manager as DB;
@@ -10,8 +11,11 @@
1011
use Illuminate\Database\Eloquent\Model;
1112
use Illuminate\Database\Eloquent\Prunable;
1213
use Illuminate\Database\Eloquent\SoftDeletes;
14+
use Illuminate\Database\Events\ModelPruningFinished;
15+
use Illuminate\Database\Events\ModelPruningStarting;
1316
use Illuminate\Database\Events\ModelsPruned;
1417
use Illuminate\Events\Dispatcher;
18+
use Mockery as m;
1519
use PHPUnit\Framework\TestCase;
1620
use Symfony\Component\Console\Input\ArrayInput;
1721
use Symfony\Component\Console\Output\BufferedOutput;
@@ -192,6 +196,27 @@ public function testTheCommandMayBePretendedOnSoftDeletedModel()
192196
$this->assertEquals(4, PrunableTestSoftDeletedModelWithPrunableRecords::withTrashed()->count());
193197
}
194198

199+
public function testTheCommandDispatchesEvents()
200+
{
201+
$dispatcher = m::mock(DispatcherContract::class);
202+
203+
$dispatcher->shouldReceive('dispatch')->once()->withArgs(function ($event) {
204+
return get_class($event) === ModelPruningStarting::class &&
205+
$event->models === [PrunableTestModelWithPrunableRecords::class];
206+
});
207+
$dispatcher->shouldReceive('listen')->once()->with(ModelsPruned::class, m::type(Closure::class));
208+
$dispatcher->shouldReceive('dispatch')->twice()->with(m::type(ModelsPruned::class));
209+
$dispatcher->shouldReceive('dispatch')->once()->withArgs(function ($event) {
210+
return get_class($event) === ModelPruningFinished::class &&
211+
$event->models === [PrunableTestModelWithPrunableRecords::class];
212+
});
213+
$dispatcher->shouldReceive('forget')->once()->with(ModelsPruned::class);
214+
215+
Container::getInstance()->singleton(DispatcherContract::class, fn () => $dispatcher);
216+
217+
$this->artisan(['--model' => PrunableTestModelWithPrunableRecords::class]);
218+
}
219+
195220
protected function artisan($arguments)
196221
{
197222
$input = new ArrayInput($arguments);
@@ -209,6 +234,8 @@ protected function tearDown(): void
209234
parent::tearDown();
210235

211236
Container::setInstance(null);
237+
238+
m::close();
212239
}
213240
}
214241

0 commit comments

Comments
 (0)