<?php

use App\Database\Seeds\DatabaseSeeder;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\DatabaseTestTrait;
use CodeIgniter\Test\FeatureTestTrait;

/**
 * @internal
 */
final class AuthenticationFlowTest extends CIUnitTestCase
{
    use DatabaseTestTrait;
    use FeatureTestTrait;

    protected $migrate = true;
    protected $refresh = true;
    protected $seed = DatabaseSeeder::class;
    protected $DBGroup = 'tests';
    protected $namespace = ['CodeIgniter\\Shield', 'CodeIgniter\\Settings', 'App'];

    public function testLoginPageRenders(): void
    {
        $result = $this->get('/login');

        $result->assertOK();
    }

    public function testAdminCanLoginAndLogout(): void
    {
        $login = $this->post('/login', [
            'email' => 'admin@academia.local',
            'password' => 'Academia123!',
        ]);

        $login->assertRedirectTo('dashboard');
        $login->assertSessionHas('user');

        $logout = $this->withSession($_SESSION)->get('/logout');

        $logout->assertRedirectTo('login');
    }
}
