Files
portal/app/tests/Feature/ClientTg/ResubmitResultApplierTest.php
T

94 lines
3.4 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
use App\Models\ClientTg\Campaign;
use App\Models\Tenant;
use App\Services\ClientTg\RobotResult;
use App\Services\ClientTg\TelegramResubmitResultApplier;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
uses(RefreshDatabase::class);
function resubmitApplierCampaign(int $tenantId, ?string $mtsId): Campaign
{
return Campaign::query()->create([
'tenant_id' => $tenantId,
'status' => Campaign::STATUS_RUNNING,
'ad_text' => 'Приходите к нам за услугой',
'ad_link' => 'https://example.test/promo',
'ord_category' => 'Размещение рекламы',
'budget_cap_rub' => '500.00',
'audience_kind' => Campaign::AUDIENCE_LIST,
'planned_count' => 2,
'estimated_cost_rub' => '315.00',
'mts_campaign_id' => $mtsId,
'created_by' => 1,
]);
}
beforeEach(function () {
config()->set('client_tg.sandbox', true);
$this->tenant = Tenant::factory()->create(['balance_rub' => '1000.00']);
DB::statement('SET LOCAL app.current_tenant_id = '.$this->tenant->id);
});
it('успешная пересдача в бою уводит кампанию на пере-модерацию и чистит причину отказа', function () {
$campaign = resubmitApplierCampaign($this->tenant->id, '777');
$campaign->status_reason = 'нет лицензии';
$campaign->save();
app(TelegramResubmitResultApplier::class)->apply(
$this->tenant->id,
$campaign,
RobotResult::fromRobotJson(['ok' => true, 'resubmitted' => true]),
false,
);
$fresh = Campaign::find($campaign->id);
expect($fresh->status)->toBe(Campaign::STATUS_MODERATING);
expect($fresh->status_reason)->toBeNull();
});
it('отказ при наличии номера кабинета уводит на ручную сверку БЕЗ возврата денег', function () {
$campaign = resubmitApplierCampaign($this->tenant->id, '777');
app(TelegramResubmitResultApplier::class)->apply(
$this->tenant->id,
$campaign,
RobotResult::fromRobotJson(['ok' => false, 'reason' => 'редактор не открылся']),
false,
);
$fresh = Campaign::find($campaign->id);
expect($fresh->status)->toBe(Campaign::STATUS_NEEDS_REVIEW);
expect($fresh->status_reason)->toBe('редактор не открылся');
});
it('песочница без отправки оставляет кампанию готовым черновиком', function () {
$campaign = resubmitApplierCampaign($this->tenant->id, '777');
app(TelegramResubmitResultApplier::class)->apply(
$this->tenant->id,
$campaign,
RobotResult::fromRobotJson(['ok' => true, 'resubmitted' => false]),
true,
);
expect(Campaign::find($campaign->id)->status)->toBe(Campaign::STATUS_DRAFT_READY);
});
it('бой без отправки — аномалия: уводим на ручную сверку, деньги держим', function () {
$campaign = resubmitApplierCampaign($this->tenant->id, '777');
app(TelegramResubmitResultApplier::class)->apply(
$this->tenant->id,
$campaign,
RobotResult::fromRobotJson(['ok' => true, 'resubmitted' => false]),
false,
);
expect(Campaign::find($campaign->id)->status)->toBe(Campaign::STATUS_NEEDS_REVIEW);
});