<?php

namespace Tests\Unit\Modules\Segments\Services;

use App\Classes\Modules\Segments\DataTransferObjects\SegmentObject;
use App\Classes\Modules\Segments\Services\UpdatesSegment;
use App\Models\Segment;
use PHPUnit\Framework\Attributes\Group;
use Tests\TestCase;

#[Group('unit')]
#[Group('segments')]
#[Group('services')]
#[Group('ok_to_run')]
class UpdatesSegmentTest extends TestCase
{
    // use DatabaseTransactions;

    /**
     * Test it can update a segment.
     *
     * @return void
     * @throws \App\Classes\Exceptions\MalformedRequestException
     */
    public function test_it_can_update_a_segment()
    {
        // A R R A N G E
        $segment = new Segment();
        $segment->name = 'test_it_can_update_a_segment_ORIGINAL';
        $segment->save();

        $newName = 'test_it_can_update_a_segment_UPDATED';
        $object = new SegmentObject($newName);
        $service = new UpdatesSegment();

        // A C T
        $result = $service->execute($segment, $object);

        // A S S E R T
        $this->assertInstanceOf(Segment::class, $result);
        $this->assertEquals($newName, $result->name);
        $this->assertEquals($segment->id, $result->id);

        $this->assertDatabaseHas('segments', [
            'id' => $segment->id,
            'name' => $newName,
        ]);
    }
}
