Files
portal/app/tests/Feature/Notifications/NewLeadsDigestJobTest.php
T

87 lines
3.2 KiB
PHP

<?php
declare(strict_types=1);
use App\Jobs\SendNewLeadsDigestJob;
use App\Mail\NewLeadNotification;
use App\Mail\NewLeadsDigestMail;
use App\Models\Deal;
use App\Models\Tenant;
use App\Models\User;
use App\Services\NotificationService;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Mail;
uses(DatabaseTransactions::class);
beforeEach(function () {
Mail::fake();
});
function digestUser(Tenant $tenant, string $email, bool $emailOn): User
{
return User::factory()->create([
'tenant_id' => $tenant->id,
'email' => $email,
'notification_preferences' => [
'new_lead' => ['email' => $emailOn, 'inapp' => true],
],
]);
}
it('шлёт одно письмо-сводку с N сделками подписанному пользователю', function () {
$tenant = Tenant::factory()->create();
digestUser($tenant, 'digest-on@example.test', true);
DB::statement('SET app.current_tenant_id = '.$tenant->id);
Deal::factory()->for($tenant)->create(['received_at' => now()->subMinutes(5), 'is_test' => false]);
Deal::factory()->for($tenant)->create(['received_at' => now()->subMinutes(10), 'is_test' => false]);
Deal::factory()->for($tenant)->create(['received_at' => now()->subMinutes(15), 'is_test' => false]);
(new SendNewLeadsDigestJob)->handle(app(NotificationService::class));
Mail::assertSent(
NewLeadsDigestMail::class,
fn (NewLeadsDigestMail $m) => $m->hasTo('digest-on@example.test') && $m->deals->count() === 3,
);
});
it('не шлёт сводку пользователю с выключенным email', function () {
$tenant = Tenant::factory()->create();
digestUser($tenant, 'digest-off@example.test', false);
DB::statement('SET app.current_tenant_id = '.$tenant->id);
Deal::factory()->for($tenant)->create(['received_at' => now()->subMinutes(5), 'is_test' => false]);
(new SendNewLeadsDigestJob)->handle(app(NotificationService::class));
Mail::assertNotSent(
NewLeadsDigestMail::class,
fn (NewLeadsDigestMail $m) => $m->hasTo('digest-off@example.test'),
);
});
it('не шлёт сводку, если за окно нет новых сделок', function () {
$tenant = Tenant::factory()->create();
digestUser($tenant, 'digest-old@example.test', true);
DB::statement('SET app.current_tenant_id = '.$tenant->id);
Deal::factory()->for($tenant)->create(['received_at' => now()->subMinutes(45), 'is_test' => false]);
(new SendNewLeadsDigestJob)->handle(app(NotificationService::class));
Mail::assertNotSent(
NewLeadsDigestMail::class,
fn (NewLeadsDigestMail $m) => $m->hasTo('digest-old@example.test'),
);
});
it('notifyNewLead больше не шлёт пер-лид письмо', function () {
$tenant = Tenant::factory()->create();
digestUser($tenant, 'perlead@example.test', true);
DB::statement('SET app.current_tenant_id = '.$tenant->id);
$deal = Deal::factory()->for($tenant)->create(['received_at' => now(), 'is_test' => false]);
app(NotificationService::class)->notifyNewLead($tenant, $deal);
Mail::assertNotSent(NewLeadNotification::class);
});