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

87 lines
3.3 KiB
PHP
Raw Normal View History

<?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);
});