Files
portal/app/tests/Unit/Autopodbor/Agent/RealCompetitorAgentTest.php
T

61 lines
3.3 KiB
PHP

<?php
use App\Services\Autopodbor\Agent\Dto\StudyCompetitorRequest;
use App\Services\Autopodbor\Agent\FakeCompetitorAgent;
use App\Services\Autopodbor\Agent\Fetch\DirectoryCard;
use App\Services\Autopodbor\Agent\Fetch\FetchedSite;
use App\Services\Autopodbor\Agent\RealCompetitorAgent;
use Tests\Unit\Autopodbor\Agent\FakeFetcher;
it('studyCompetitor: настоящий номер из кода+2ГИС наверху, подменный внизу, сайт отдельной строкой', function () {
$fetcher = new FakeFetcher(
sites: [
'https://k.ru' => new FetchedSite(
url: 'https://k.ru',
rawHtml: '<a href="tel:+73912920000">x</a><script src="//cdn.callibri.ru/x.js"></script><span>8(391)111-22-33</span>',
visiblePhones: ['8(391)111-22-33'],
),
],
cards: [
'https://2gis.ru/firm/1' => [
new DirectoryCard(number: '8(391)292-00-00', office: 'Единая справочная', url: 'https://2gis.ru/firm/1', source: '2ГИС'),
],
],
);
$agent = new RealCompetitorAgent($fetcher, new FakeCompetitorAgent);
$res = $agent->studyCompetitor(new StudyCompetitorRequest(
competitor: ['name' => 'КрасЛомбард', 'site_url' => 'k.ru', 'directory_urls' => ['https://2gis.ru/firm/1']],
regionCode: 391,
));
$calls = array_values(array_filter($res->sources, fn ($s) => $s['signal_type'] === 'call'));
$sites = array_values(array_filter($res->sources, fn ($s) => $s['signal_type'] === 'site'));
// настоящий 292-00-00 (код+2ГИС = 2 подтверждения) выше подменного 111-22-33
expect($calls[0]['identifier'])->toBe('73912920000');
expect($calls[0]['phone_kind'])->toBe('real');
expect(end($calls)['phone_kind'])->toBe('substitute');
// сайт конкурента — отдельной строкой
expect(collect($sites)->pluck('identifier'))->toContain('k.ru');
});
it('сводит филиалы из разных справочников: больше подтверждений — выше', function () {
$fetcher = new FakeFetcher(
sites: ['https://k.ru' => new FetchedSite(url: 'https://k.ru', rawHtml: '<a href="tel:+73912920000">x</a>')],
cards: [
'https://2gis.ru/firm/1' => [new DirectoryCard(number: '8(391)292-00-00', office: 'Справочная', url: 'https://2gis.ru/firm/1', source: '2ГИС')],
'https://yandex.ru/maps/2' => [new DirectoryCard(number: '8(391)281-70-70', office: 'Кр. рабочий, 61', url: 'https://yandex.ru/maps/2', source: 'Яндекс.Карты')],
],
);
$agent = new RealCompetitorAgent($fetcher, new FakeCompetitorAgent);
$res = $agent->studyCompetitor(new StudyCompetitorRequest(
competitor: ['name' => 'КрасЛомбард', 'site_url' => 'k.ru', 'directory_urls' => ['https://2gis.ru/firm/1', 'https://yandex.ru/maps/2']],
regionCode: 391,
));
$calls = array_values(array_filter($res->sources, fn ($s) => $s['signal_type'] === 'call'));
// 292-00-00: код + 2ГИС = 2 подтверждения → выше, чем 281-70-70 (только Яндекс = 1)
expect($calls[0]['identifier'])->toBe('73912920000');
expect(collect($calls)->pluck('identifier'))->toContain('73912817070');
});