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

58 lines
2.4 KiB
PHP

<?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'
&& $body['params']['RetargetingLists'][0]['Rules'][0]['Arguments'][0]['ExternalId'] === 58034825;
});
});
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->addCampaign('C', '2026-07-25', 385000000))->toThrow(RuntimeException::class);
});
it('sends the weekly spend limit in micros when adding a campaign', function () {
Http::fake([
'*/json/v5/campaigns' => Http::response(['result' => ['AddResults' => [['Id' => 42]]]], 200),
]);
$client = new YandexDirectClient('https://api-sandbox.direct.yandex.com', 'TESTTOKEN');
$id = $client->addCampaign('Лидерра кампания #5', '2026-07-25', 385000000);
expect($id)->toBe(42);
Http::assertSent(function ($req) {
$body = $req->data();
$strategy = $body['params']['Campaigns'][0]['TextCampaign']['BiddingStrategy'];
return $body['method'] === 'add'
&& $body['params']['Campaigns'][0]['StartDate'] === '2026-07-25'
&& $strategy['Search']['BiddingStrategyType'] === 'SERVING_OFF'
&& $strategy['Network']['BiddingStrategyType'] === 'HIGHEST_POSITION'
&& $strategy['Network']['NetworkHighestPosition']['WeeklySpendLimit'] === 385000000;
});
});