<?php

namespace Tests;

use Illuminate\Foundation\Testing\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
    /**
     * Reset resolved auth guards before each simulated HTTP request that uses
     * a real bearer token, so that token revocation is correctly reflected
     * within the same test method without invalidating Sanctum::actingAs().
     */
    public function call($method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null)
    {
        // Only flush guard cache when the request carries a real bearer token
        // (i.e. not set up via Sanctum::actingAs which uses setUser directly).
        $hasBearer = collect($server)->keys()->contains(fn ($k) => strtolower($k) === 'http_authorization')
            || isset($this->defaultHeaders['Authorization'])
            || isset($this->serverVariables['HTTP_AUTHORIZATION']);

        if ($hasBearer && isset($this->app)) {
            $this->app['auth']->forgetGuards();
        }

        return parent::call($method, $uri, $parameters, $cookies, $files, $server, $content);
    }
}
