diff --git a/CHANGELOG.md b/CHANGELOG.md index 2959619..d601079 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,5 +2,8 @@ All Notable changes to `laravel-model-cleanup` will be documented in this file +### 1.1.0 - 2016-08-10 +- Added `ModelWasCleanedUpEvent` + ### 1.0.0 - 2016-03-18 - Initial release diff --git a/README.md b/README.md index 4b88d51..4e70e55 100644 --- a/README.md +++ b/README.md @@ -119,6 +119,11 @@ protected function schedule(Schedule $schedule) } ``` +## Events + +After the model has been cleaned `Spatie\ModelCleanup\ModelWasCleanedUp` will be fired (even if there were no records deleted). +It has two public properties: `modelClass` and `numberOfDeletedRecords`. + ## Changelog Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. diff --git a/composer.json b/composer.json index f9b2674..0518841 100644 --- a/composer.json +++ b/composer.json @@ -29,7 +29,8 @@ }, "require-dev": { "phpunit/phpunit": "^5.0", - "orchestra/testbench":"^3.2" + "orchestra/testbench":"^3.2", + "mockery/mockery": "^0.9.5" }, "autoload": { "psr-4": { diff --git a/src/CleanUpModelsCommand.php b/src/CleanUpModelsCommand.php index d48ee69..e43e865 100644 --- a/src/CleanUpModelsCommand.php +++ b/src/CleanUpModelsCommand.php @@ -60,16 +60,13 @@ protected function getModelsThatShouldBeCleanedUp() : Collection protected function cleanUp(Collection $cleanableModels) { - $cleanableModels->each(function (string $class) { + $cleanableModels->each(function (string $modelClass) { - $numberOfDeletedRecords = $class::cleanUp($class::query())->delete(); + $numberOfDeletedRecords = $modelClass::cleanUp($modelClass::query())->delete(); - if($numberOfDeletedRecords) - { - event(new ModelCleanedEvent($class, $numberOfDeletedRecords)); - } + event(new ModelWasCleanedUp($modelClass, $numberOfDeletedRecords)); - $this->info("Deleted {$numberOfDeletedRecords} record(s) from {$class}."); + $this->info("Deleted {$numberOfDeletedRecords} record(s) from {$modelClass}."); }); } @@ -115,5 +112,4 @@ protected function getFullyQualifiedClassNameFromFile(string $path) : string }) ->first(); } - } diff --git a/src/ModelCleanedEvent.php b/src/ModelCleanedEvent.php deleted file mode 100644 index 93452a3..0000000 --- a/src/ModelCleanedEvent.php +++ /dev/null @@ -1,34 +0,0 @@ -modelName = $modelName; - $this->numberOfDeletedRecords = $numberOfDeletedRecords; - } -} \ No newline at end of file diff --git a/src/ModelWasCleanedUp.php b/src/ModelWasCleanedUp.php new file mode 100644 index 0000000..537396d --- /dev/null +++ b/src/ModelWasCleanedUp.php @@ -0,0 +1,27 @@ +modelClass = $modelClass; + + $this->numberOfDeletedRecords = $numberOfDeletedRecords; + } +} diff --git a/tests/DatabaseCleanupTest.php b/tests/DatabaseCleanupTest.php index 69040db..e77d710 100644 --- a/tests/DatabaseCleanupTest.php +++ b/tests/DatabaseCleanupTest.php @@ -2,11 +2,12 @@ namespace Spatie\ModelCleanup\Test; +use Spatie\ModelCleanup\ModelWasCleanedUp; use Spatie\ModelCleanup\Test\Models\CleanableItem; use Spatie\ModelCleanup\Test\Models\UncleanableItem; use Illuminate\Contracts\Console\Kernel; -class ModelCleanupTest extends TestCase +class DatabaseCleanupTest extends TestCase { /** @test */ public function it_can_delete_expired_records_from_a_database() @@ -31,8 +32,6 @@ public function it_can_cleanup_the_models_specified_in_the_config_file() $this->app->make(Kernel::class)->call('clean:models'); - $this->expectsEvents(ModelCleanedEvent::class); - $this->assertCount(10, CleanableItem::all()); } @@ -45,8 +44,6 @@ public function it_can_cleanup_the_directories_specified_in_the_config_file() $this->app->make(Kernel::class)->call('clean:models'); - $this->expectsEvents(ModelCleanedEvent::class); - $this->assertCount(10, CleanableItem::all()); } @@ -59,11 +56,51 @@ public function it_leaves_models_without_the_GetCleanUp_trait_untouched() $this->app->make(Kernel::class)->call('clean:models'); - $this->doesntExpectEvents(ModelCleanedEvent::class); - $this->assertCount(10, UncleanableItem::all()); } + /** @test */ + public function it_will_fire_off_an_event_when_a_model_has_been_cleaned() + { + $this->assertCount(20, CleanableItem::all()); + + $this->app['config']->set('laravel-model-cleanup', + [ + 'models' => [CleanableItem::class], + 'directories' => [], + + ]); + + $this->app->make(Kernel::class)->call('clean:models'); + + $firedEvent = $this->getFiredEvent(ModelWasCleanedUp::class); + + $this->assertInstanceOf(ModelWasCleanedUp::class, $firedEvent); + + $this->assertSame(10, $firedEvent->numberOfDeletedRecords); + } + + /** @test */ + public function it_will_fire_off_an_event_when_a_model_has_been_cleaned_even_if_no_records_were_deleted() + { + CleanableItem::truncate(); + + $this->app['config']->set('laravel-model-cleanup', + [ + 'models' => [CleanableItem::class], + 'directories' => [], + + ]); + + $this->app->make(Kernel::class)->call('clean:models'); + + $firedEvent = $this->getFiredEvent(ModelWasCleanedUp::class); + + $this->assertInstanceOf(ModelWasCleanedUp::class, $firedEvent); + + $this->assertSame(0, $firedEvent->numberOfDeletedRecords); + } + protected function setConfigThatCleansUpDirectory() { $this->app['config']->set('laravel-model-cleanup', diff --git a/tests/TestCase.php b/tests/TestCase.php index adc4cb6..5b639ea 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -7,13 +7,22 @@ use Spatie\ModelCleanup\ModelCleanupServiceProvider; use Spatie\ModelCleanup\Test\Models\CleanableItem; use Spatie\ModelCleanup\Test\Models\UncleanableItem; +use Event; abstract class TestCase extends \Orchestra\Testbench\TestCase { + /** @var array */ + protected $firedEvents = []; + public function setUp() { parent::setUp(); + $this->setUpDatabase($this->app); + + Event::listen('*', function ($event) { + $this->firedEvents[] = $event; + }); } protected function getPackageProviders($app) @@ -70,4 +79,13 @@ protected function createDatabaseRecords() ]); } } + + public function getFiredEvent($eventClassName) + { + return collect($this->firedEvents) + ->filter(function ($event) use ($eventClassName) { + return $event instanceof $eventClassName; + }) + ->first(); + } }