<?php

namespace goodboyalex\php_components_pack\tests\classes;

use goodboyalex\php_components_pack\classes\ActionState;
use goodboyalex\php_components_pack\enums\MessageType;
use goodboyalex\php_components_pack\models\ActionStateMessageModel;
use PHPUnit\Framework\TestCase;

class ActionStateTest extends TestCase
{
    public function testActionState (): void
    {
        $this->PrepareForTest();
        $actionState = new ActionState('123');

        $this->assertEquals('123', $actionState->Value);

        $actionState->AddError("Non critical error");
        $actionState->AddCritical("Critical error");
        $actionState->AddWarning("Its a warning");

        $this->assertTrue($actionState->HasErrors());
        $this->assertTrue($actionState->HasErrors(true));
        $this->assertTrue($actionState->HasWarnings());
        $this->assertFalse($actionState->IsSuccess());

        $messages = $actionState->GetMessages(ActionState::GET_STRING_ERROR_ONLY());
        $this->assertEquals(2, $messages->Count());

        $this->assertEquals("Non critical error",
            $actionState->GetStringMessages(fn (ActionStateMessageModel $message)
                => $message->MessageType
                == MessageType::Error
                && !$message->IsCritical));

        $actionState2 = new ActionState('321');

        $actionState2->AddState($actionState);

        $this->assertEquals(3, $actionState2->Count(ActionState::GET_STRING_ALL()));
        $this->assertEquals('123', $actionState2->Value);
    }

    private function PrepareForTest (): void
    {
        require_once __DIR__ . '/../../sources/classes/ActionState.php';
        require_once __DIR__ . '/../../sources/traits/ActionState/ActionStateAddTrait.php';
        require_once __DIR__ . '/../../sources/traits/ActionState/ActionStateGetTrait.php';
        require_once __DIR__ . '/../../sources/traits/ActionState/ActionStateHasTrait.php';
        require_once __DIR__ . '/../../sources/traits/ActionState/ActionStateStaticTrait.php';
        require_once __DIR__ . '/../../sources/enums/MessageType.php';
    }
}