Files
portal/app/tests/Feature/ClientTg/RunTelegramCampaignTransportTest.php
T
Дмитрий 146f0a7a35 feat телеграм-робот: джоб запуска умеет опросный канал
При transport=poll портал кладёт задание и выходит, робот заберёт его сам.
Процессный путь оставлен рабочим и остаётся умолчанием.
Полный набор тестов телеграма: 219 из 219 зелёные.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-07-31 09:07:39 +03:00

87 lines
3.3 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
declare(strict_types=1);
use App\Jobs\ClientTg\RunTelegramCampaignJob;
use App\Models\ClientTg\Campaign;
use App\Models\ClientTg\RobotJob;
use App\Models\Tenant;
use App\Services\ClientTg\RobotResult;
use App\Services\ClientTg\TelegramAudienceService;
use App\Services\ClientTg\TelegramRobotRunner;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
uses(RefreshDatabase::class);
function tgTransportCampaign(int $tenantId): Campaign
{
return Campaign::query()->create([
'tenant_id' => $tenantId,
'status' => Campaign::STATUS_QUEUED,
'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',
'created_by' => 1,
]);
}
/**
* Раннер подменяем как в RunCampaignJobTest — через andReturnUsing с записью вызовов.
* shouldNotReceive тут не годится: Mockery пытается сочинить возвращаемое значение
* типа RobotResult, а тот объявлен final. Считаем вызовы руками — доказательство
* то же самое, а зависимости от внутренностей Mockery нет.
*
* @param list<array<string, mixed>> $calls
*/
function tgTransportRunner(array &$calls): void
{
$mock = Mockery::mock(TelegramRobotRunner::class);
$mock->shouldReceive('run')->andReturnUsing(function (array $params) use (&$calls): RobotResult {
$calls[] = $params;
return RobotResult::failed('стенд', 'test');
});
app()->instance(TelegramRobotRunner::class, $mock);
}
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);
$this->campaign = tgTransportCampaign($this->tenant->id);
});
it('при опросном канале ставит задание роботу и не запускает процесс', function () {
config()->set('client_tg.robot.transport', 'poll');
$calls = [];
tgTransportRunner($calls);
(new RunTelegramCampaignJob($this->campaign->id, $this->tenant->id))->handle(
app(TelegramAudienceService::class),
app(TelegramRobotRunner::class),
);
expect($calls)->toBeEmpty();
expect(RobotJob::where('campaign_id', $this->campaign->id)->count())->toBe(1);
expect(Campaign::find($this->campaign->id)->status)->toBe(Campaign::STATUS_RUNNING);
});
it('при процессном канале по-прежнему запускает робота сам', function () {
config()->set('client_tg.robot.transport', 'process');
$calls = [];
tgTransportRunner($calls);
(new RunTelegramCampaignJob($this->campaign->id, $this->tenant->id))->handle(
app(TelegramAudienceService::class),
app(TelegramRobotRunner::class),
);
expect($calls)->toHaveCount(1);
expect(RobotJob::where('campaign_id', $this->campaign->id)->count())->toBe(0);
});