76 lines
2.6 KiB
PHP
76 lines
2.6 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
use App\Models\SalesAdAudienceFirm;
|
||
|
|
use App\Models\SalesAdAudienceWarmingEpisode;
|
||
|
|
use App\Services\Sales\WarmingEpisodeRecorder;
|
||
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||
|
|
use Tests\Concerns\SharesSupplierPdo;
|
||
|
|
|
||
|
|
uses(DatabaseTransactions::class, SharesSupplierPdo::class);
|
||
|
|
|
||
|
|
function episodeFirm(): SalesAdAudienceFirm
|
||
|
|
{
|
||
|
|
return SalesAdAudienceFirm::create(['firm_name' => 'Тест', 'warmup_started_at' => now()]);
|
||
|
|
}
|
||
|
|
|
||
|
|
it('open заводит открытый эпизод канала', function () {
|
||
|
|
$firm = episodeFirm();
|
||
|
|
|
||
|
|
(new WarmingEpisodeRecorder)->open($firm->id, 'yandex');
|
||
|
|
|
||
|
|
$ep = SalesAdAudienceWarmingEpisode::where('firm_id', $firm->id)->where('channel', 'yandex')->sole();
|
||
|
|
expect($ep->ended_at)->toBeNull();
|
||
|
|
});
|
||
|
|
|
||
|
|
it('повторный open уже открытого эпизода не плодит второй', function () {
|
||
|
|
$firm = episodeFirm();
|
||
|
|
$rec = new WarmingEpisodeRecorder;
|
||
|
|
|
||
|
|
$rec->open($firm->id, 'yandex');
|
||
|
|
$rec->open($firm->id, 'yandex');
|
||
|
|
|
||
|
|
expect(SalesAdAudienceWarmingEpisode::where('firm_id', $firm->id)->where('channel', 'yandex')->count())->toBe(1);
|
||
|
|
});
|
||
|
|
|
||
|
|
it('close проставляет ended_at открытому эпизоду', function () {
|
||
|
|
$firm = episodeFirm();
|
||
|
|
$rec = new WarmingEpisodeRecorder;
|
||
|
|
|
||
|
|
$rec->open($firm->id, 'yandex');
|
||
|
|
$rec->close($firm->id, 'yandex');
|
||
|
|
|
||
|
|
$ep = SalesAdAudienceWarmingEpisode::where('firm_id', $firm->id)->where('channel', 'yandex')->sole();
|
||
|
|
expect($ep->ended_at)->not->toBeNull();
|
||
|
|
});
|
||
|
|
|
||
|
|
it('повторный open после close заводит второй эпизод (грели дважды)', function () {
|
||
|
|
$firm = episodeFirm();
|
||
|
|
$rec = new WarmingEpisodeRecorder;
|
||
|
|
|
||
|
|
$rec->open($firm->id, 'yandex');
|
||
|
|
$rec->close($firm->id, 'yandex');
|
||
|
|
$rec->open($firm->id, 'yandex');
|
||
|
|
|
||
|
|
expect(SalesAdAudienceWarmingEpisode::where('firm_id', $firm->id)->where('channel', 'yandex')->count())->toBe(2);
|
||
|
|
});
|
||
|
|
|
||
|
|
it('record пишет мгновенный эпизод СМС (started_at = ended_at)', function () {
|
||
|
|
$firm = episodeFirm();
|
||
|
|
|
||
|
|
(new WarmingEpisodeRecorder)->record($firm->id, 'sms');
|
||
|
|
|
||
|
|
$ep = SalesAdAudienceWarmingEpisode::where('firm_id', $firm->id)->where('channel', 'sms')->sole();
|
||
|
|
expect($ep->ended_at)->not->toBeNull()
|
||
|
|
->and($ep->started_at->timestamp)->toBe($ep->ended_at->timestamp);
|
||
|
|
});
|
||
|
|
|
||
|
|
it('close без открытого эпизода — тихий no-op', function () {
|
||
|
|
$firm = episodeFirm();
|
||
|
|
|
||
|
|
(new WarmingEpisodeRecorder)->close($firm->id, 'yandex');
|
||
|
|
|
||
|
|
expect(SalesAdAudienceWarmingEpisode::where('firm_id', $firm->id)->count())->toBe(0);
|
||
|
|
});
|