<?php

declare(strict_types=1);

namespace HyperfTest\Cases\Unit\Model;

use App\Model\Role;
use PHPUnit\Framework\TestCase;

/**
 * @internal
 * @coversNothing
 */
class RoleTest extends TestCase
{
    protected function runInCoroutine(callable $callback): void
    {
        if (\Swoole\Coroutine::getCid() > 0) {
            $callback();
            return;
        }

        $exception = null;
        \Swoole\Coroutine\run(static function () use ($callback, &$exception): void {
            try {
                $callback();
            } catch (\Throwable $e) {
                $exception = $e;
            }
        });
        if ($exception) {
            throw $exception;
        }
    }

    public function test_role_has_correct_fillable(): void
    {
        $role = new Role();
        $this->assertEqualsCanonicalizing(['name', 'label', 'description'], $role->getFillable());
    }

    public function test_role_table_name(): void
    {
        $role = new Role();
        $this->assertSame('roles', $role->getTable());
    }

    public function test_administrator_role_exists(): void
    {
        $this->runInCoroutine(function (): void {
            $role = Role::query()->where('name', 'administrator')->first();
            $this->assertNotNull($role);
            $this->assertSame('超级管理员', $role->label);
        });
    }

    public function test_developer_role_exists(): void
    {
        $this->runInCoroutine(function (): void {
            $role = Role::query()->where('name', 'developer')->first();
            $this->assertNotNull($role);
            $this->assertSame('开发者', $role->label);
        });
    }

    public function test_accessor_role_exists(): void
    {
        $this->runInCoroutine(function (): void {
            $role = Role::query()->where('name', 'accessor')->first();
            $this->assertNotNull($role);
            $this->assertSame('数据访问者', $role->label);
        });
    }
}
