Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added optional days argument to clean command #497

Merged
merged 6 commits into from
Apr 15, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/CleanActivitylogCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
class CleanActivitylogCommand extends Command
{
protected $signature = 'activitylog:clean
{log? : (optional) The log name that will be cleaned.}';
{log? : (optional) The log name that will be cleaned.}
{--days= : (optional) Records older than this number of days will be cleaned.}';

protected $description = 'Clean up old records from the activity log.';

Expand All @@ -19,7 +20,7 @@ public function handle()

$log = $this->argument('log');

$maxAgeInDays = config('activitylog.delete_records_older_than_days');
$maxAgeInDays = $this->option('days') ?? config('activitylog.delete_records_older_than_days');

$cutOffDate = Carbon::now()->subDays($maxAgeInDays)->format('Y-m-d H:i:s');

Expand Down
21 changes: 21 additions & 0 deletions tests/CleanActivitylogCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,25 @@ public function it_can_clean_the_activity_log()

$this->assertCount(0, Activity::where('created_at', '<', $cutOffDate)->get());
}

/** @test */
public function it_can_accept_days_as_option_to_override_config_setting()
{
collect(range(1, 60))->each(function (int $index) {
Activity::create([
'description' => "item {$index}",
'created_at' => Carbon::now()->subDays($index)->startOfDay(),
]);
});

$this->assertCount(60, Activity::all());

Artisan::call('activitylog:clean', ['--days' => 7]);

$this->assertCount(7, Activity::all());

$cutOffDate = Carbon::now()->subDays(7)->format('Y-m-d H:i:s');

$this->assertCount(0, Activity::where('created_at', '<', $cutOffDate)->get());
}
}