<?php

namespace Tests;

use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Illuminate\Routing\Middleware\ThrottleRequests;

use App\Models\User;

abstract class TestCase extends BaseTestCase
{
    use CreatesApplication;
    use ObjectsTrait;

    protected function setUp(): void
    {
        parent::setUp();
        $this->withoutMiddleware(
            ThrottleRequests::class
        );
    }

    protected function signIn($user = null)
    {
        if (!$user instanceof User) {
            $user = User::factory()->create();
        }

        $this->actingAs($user);
        $this->assertEquals($user->id, auth()->user()->id);
        $user->disableEditing();

        return $this;
    }

    protected function signInAdmin()
    {
        $user = User::find(1);
        //$user->addRole('admin');
        $user->disableEditing();
        return $this->signIn($user);
    }

    protected function enableEditing($enable = true)
    {
        if (auth()->check()) {
            $user = auth()->user();
            $user->editing = $enable;
            $user->save();
        }
    }

    protected function disableEditing()
    {
        $this->enableEditing(false);
    }
}
