Files
portal/app/tests/Feature/VisitorsPruneTest.php
T

43 lines
1.4 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Tests\Concerns\SharesSupplierPdo;
uses(DatabaseTransactions::class);
uses(SharesSupplierPdo::class);
beforeEach(function () {
DB::table('site_events')->delete();
DB::table('site_visitors')->delete();
});
test('гости старше 180 дней удаляются, свежие остаются', function () {
$old = (string) Str::uuid();
$fresh = (string) Str::uuid();
DB::table('site_visitors')->insert([
['id' => $old, 'first_seen_at' => now()->subDays(200), 'last_seen_at' => now()->subDays(200)],
['id' => $fresh, 'first_seen_at' => now()->subDays(10), 'last_seen_at' => now()->subDays(10)],
]);
$this->artisan('visitors:prune')->assertExitCode(0);
expect(DB::table('site_visitors')->where('id', $old)->exists())->toBeFalse();
expect(DB::table('site_visitors')->where('id', $fresh)->exists())->toBeTrue();
});
test('срок хранения настраивается ключом --days', function () {
$id = (string) Str::uuid();
DB::table('site_visitors')->insert([
['id' => $id, 'first_seen_at' => now()->subDays(40), 'last_seen_at' => now()->subDays(40)],
]);
$this->artisan('visitors:prune', ['--days' => 30])->assertExitCode(0);
expect(DB::table('site_visitors')->where('id', $id)->exists())->toBeFalse();
});