79 lines
2.5 KiB
PHP
79 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit\Event;
|
|
|
|
use App\Domains\Event\Models\EventDate;
|
|
use App\Domains\Event\Resources\EventResource;
|
|
use App\Domains\Event\Services\EventDateGroupingService;
|
|
use App\Domains\Tenant\Models\Tenant;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
use Tests\TestCase;
|
|
|
|
class EventDateGroupingServiceTest extends TestCase
|
|
{
|
|
public function test_it_groups_chains_under_the_final_destination_and_orders_by_active_date(): void
|
|
{
|
|
$source13 = $this->date(1, '2026-10-13', 5);
|
|
$source22 = $this->date(2, '2026-10-22', 3);
|
|
$middle24 = $this->date(3, '2026-10-24', 5);
|
|
$active25 = $this->date(4, '2026-10-25');
|
|
$destination30 = $this->date(5, '2026-10-30');
|
|
|
|
$grouped = (new EventDateGroupingService)->group(new Collection([
|
|
$source13,
|
|
$source22,
|
|
$middle24,
|
|
$active25,
|
|
$destination30,
|
|
]));
|
|
|
|
$this->assertSame([4, 5], $grouped->map->getKey()->all());
|
|
$this->assertSame(
|
|
[1, 2, 3],
|
|
$grouped->last()->getRelation('adminRescheduledDates')->modelKeys(),
|
|
);
|
|
$this->assertSame(
|
|
[5, 5, 5],
|
|
$grouped->last()->getRelation('adminRescheduledDates')
|
|
->pluck('rescheduled_to_event_date_id')
|
|
->all(),
|
|
);
|
|
|
|
$tenant = new Tenant(['codigo' => 'acme']);
|
|
$tenant->id = 1;
|
|
$tenant->setRelation('eventDates', new Collection([
|
|
$source13,
|
|
$source22,
|
|
$middle24,
|
|
$active25,
|
|
$destination30,
|
|
]));
|
|
$tenant->setRelation('socialMedia', new Collection);
|
|
$payload = EventResource::make($tenant)->response()->getData(true)['data'];
|
|
|
|
$this->assertCount(2, $payload['dates']);
|
|
$this->assertSame(4, $payload['dates'][0]['id']);
|
|
$this->assertSame([1, 2, 3], array_column($payload['dates'][1]['rescheduled_dates'], 'id'));
|
|
$this->assertSame(
|
|
[5, 5, 5],
|
|
array_column(
|
|
$payload['dates'][1]['rescheduled_dates'],
|
|
'rescheduled_to_event_date_id',
|
|
),
|
|
);
|
|
}
|
|
|
|
private function date(int $id, string $date, ?int $destinationId = null): EventDate
|
|
{
|
|
$eventDate = new EventDate([
|
|
'date' => $date,
|
|
'time_start' => '00:00:00',
|
|
'time_end' => '23:59:00',
|
|
'rescheduled_to_event_date_id' => $destinationId,
|
|
]);
|
|
$eventDate->id = $id;
|
|
|
|
return $eventDate;
|
|
}
|
|
}
|