Skip to content

Commit

Permalink
[10.x] Dispatch model pruning started and ended events (#47669)
Browse files Browse the repository at this point in the history
* fire start/end events

* test events

* style ci

* formatting

* fix method

---------

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
ziadoz and taylorotwell authored Jul 8, 2023
1 parent 33aa4a9 commit b83abb4
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Illuminate/Database/Console/PruneCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Illuminate\Database\Eloquent\MassPrunable;
use Illuminate\Database\Eloquent\Prunable;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Events\ModelPruningFinished;
use Illuminate\Database\Events\ModelPruningStarting;
use Illuminate\Database\Events\ModelsPruned;
use Illuminate\Support\Str;
use InvalidArgumentException;
Expand Down Expand Up @@ -70,10 +72,14 @@ public function handle(Dispatcher $events)
$this->components->twoColumnDetail($event->model, "{$event->count} records");
});

$events->dispatch(new ModelPruningStarting($models->all()));

$models->each(function ($model) {
$this->pruneModel($model);
});

$events->dispatch(new ModelPruningFinished($models->all()));

$events->forget(ModelsPruned::class);
}

Expand Down
24 changes: 24 additions & 0 deletions src/Illuminate/Database/Events/ModelPruningFinished.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Illuminate\Database\Events;

class ModelPruningFinished
{
/**
* The class names of the models that were pruned.
*
* @var array<class-string>
*/
public $models;

/**
* Create a new event instance.
*
* @param array<class-string> $models
* @return void
*/
public function __construct($models)
{
$this->models = $models;
}
}
24 changes: 24 additions & 0 deletions src/Illuminate/Database/Events/ModelPruningStarting.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Illuminate\Database\Events;

class ModelPruningStarting
{
/**
* The class names of the models that will be pruned.
*
* @var array<class-string>
*/
public $models;

/**
* Create a new event instance.
*
* @param array<class-string> $models
* @return void
*/
public function __construct($models)
{
$this->models = $models;
}
}
27 changes: 27 additions & 0 deletions tests/Database/PruneCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Tests\Database;

use Closure;
use Illuminate\Container\Container;
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
use Illuminate\Database\Capsule\Manager as DB;
Expand All @@ -10,8 +11,11 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Prunable;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Events\ModelPruningFinished;
use Illuminate\Database\Events\ModelPruningStarting;
use Illuminate\Database\Events\ModelsPruned;
use Illuminate\Events\Dispatcher;
use Mockery as m;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
Expand Down Expand Up @@ -192,6 +196,27 @@ public function testTheCommandMayBePretendedOnSoftDeletedModel()
$this->assertEquals(4, PrunableTestSoftDeletedModelWithPrunableRecords::withTrashed()->count());
}

public function testTheCommandDispatchesEvents()
{
$dispatcher = m::mock(DispatcherContract::class);

$dispatcher->shouldReceive('dispatch')->once()->withArgs(function ($event) {
return get_class($event) === ModelPruningStarting::class &&
$event->models === [PrunableTestModelWithPrunableRecords::class];
});
$dispatcher->shouldReceive('listen')->once()->with(ModelsPruned::class, m::type(Closure::class));
$dispatcher->shouldReceive('dispatch')->twice()->with(m::type(ModelsPruned::class));
$dispatcher->shouldReceive('dispatch')->once()->withArgs(function ($event) {
return get_class($event) === ModelPruningFinished::class &&
$event->models === [PrunableTestModelWithPrunableRecords::class];
});
$dispatcher->shouldReceive('forget')->once()->with(ModelsPruned::class);

Container::getInstance()->singleton(DispatcherContract::class, fn () => $dispatcher);

$this->artisan(['--model' => PrunableTestModelWithPrunableRecords::class]);
}

protected function artisan($arguments)
{
$input = new ArrayInput($arguments);
Expand All @@ -209,6 +234,8 @@ protected function tearDown(): void
parent::tearDown();

Container::setInstance(null);

m::close();
}
}

Expand Down

0 comments on commit b83abb4

Please sign in to comment.