How does phpunit solve the api interface that requires user login for testing?

problem description

the framework built by our own company is not Laravel
phpunit to do automated testing and test the api interface. I log in using the login method in the setUp method.
then create a new method to test the api interface that requires the user to log in. For example, check the user"s own article
and report the following error

Cannot modify header information - headers already sent by (output started at /Users/pengpn/php/wwwroot/yesaway/ims/vendor/phpunit/phpunit/src/Util/Printer.php:109)

setCookie sets the cookie of user login in the login method

the environmental background of the problems and what methods you have tried

the framework built by our own company is not Laravel
. Some API methods need to determine whether there is user login information, and return user error if there is no user login information. So when I do the phpunit test, I want to test these interfaces, so I need to do the so-called simulated user login.
tried to use . / vendor/bin/phpunit-- stderr added-- stderr did not report that error, but setCookie did not set cookie

related codes

/ / Please paste the code text below (do not replace the code with pictures)


class ItineraryServiceTest extends ApiTestCase
{
    public function setUp()
    {
        $params = [
            "type" => "email",
            "email" => "312924860@qq.com",
        ];
        $this->userLogin($params);
    }
    public function testGetItineraryList()
    {
        $itineraryService = new ItineraryService();
        $list = $itineraryService->getItineraryList();
        $this->assertArrayHasKey("current",$list["data"]);

    }
}

setting cookie method of Login

public function setLoginCookieCache($userInfo, $lifeTime = 2592000)
    {
        $sessionId = md5($userInfo["user_id"] . "xxx_pc_user");

        $cacheKey = "user_" . md5($userInfo["user_id"]);

        $this->C["cache"]->set($cacheKey, $userInfo, $lifeTime);

        setcookie("xxx_pc_user", json_encode(array("key" => $sessionId, "info" => $userInfo)), time() + (int)$lifeTime, "/", $this->host);
        
    }

what result do you expect? What is the error message actually seen?

Cannot modify header information - headers already sent by (output started at /Users/pengpn/php/wwwroot/yesaway/ims/vendor/phpunit/phpunit/src/Util/Printer.php:109)
Jun.24,2022
Menu