Files
portal/app/tests/Unit/Advertising/YandexDirectClientTest.php
T

38 lines
1.7 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
use App\Services\Advertising\YandexDirectClient;
use Illuminate\Support\Facades\Http;
use Tests\TestCase;
uses(TestCase::class);
it('adds a retargeting list referencing an audience segment', function () {
Http::fake([
'*/json/v5/retargetinglists' => Http::response(['result' => ['AddResults' => [['Id' => 777]]]], 200),
]);
$client = new YandexDirectClient('https://api-sandbox.direct.yandex.com', 'TESTTOKEN');
$id = $client->addRetargetingList('Лидерра кампания #5', 58034825);
expect($id)->toBe(777);
Http::assertSent(function ($req) {
$body = $req->data();
return str_ends_with($req->url(), '/json/v5/retargetinglists')
&& $req->hasHeader('Authorization', 'Bearer TESTTOKEN')
&& $body['method'] === 'add'
&& $body['params']['RetargetingLists'][0]['Type'] === 'AUDIENCE'
&& $body['params']['RetargetingLists'][0]['Rules'][0]['Operator'] === 'ALL'
// Сегмент Аудиторий указывается с приставкой «20» — см. addRetargetingList().
&& $body['params']['RetargetingLists'][0]['Rules'][0]['Arguments'][0]['ExternalId'] === 2058034825;
});
});
it('surfaces an API error as RuntimeException', function () {
Http::fake(['*/json/v5/campaigns' => Http::response(['error' => ['error_string' => 'Нет доступа к API', 'error_code' => 53]], 200)]);
$client = new YandexDirectClient('https://api-sandbox.direct.yandex.com', 'T');
expect(fn () => $client->addCpmBannerCampaign('C', '2026-08-01', '2026-08-14', 72000000, 90000000, 3, 14))->toThrow(RuntimeException::class);
});