1

need your help. I try to testing my login page with feature test but i got this error, even thats run normaly when code is executed. but went i test the code i got error like this.

ErrorException: Cannot modify header information - headers already sent by (output started at D:\project\e-services-disdukcapil\vendor\phpunit\phpunit\src\Util\Printer.php:104)

that's my code :

class AuthAdminTest extends FeatureTestCase
{

    protected $models;

    protected $seed = 'Tests\Support\Database\Seeds\AuthSeeder';
    protected $basePath = APPPATH . 'Database/';
    protected $namespace = 'App';

    public function setUp(): void
    {
        parent::setUp();
        $this->models = new UserAdminModel();
        // Extra code to run before each test
    }

    public function testAdminLoginView()
    {
        $res = $this->get(route_to('admin-login'));
        $this->assertTrue($res->isOK());
        $res->assertSee(lang('login'));
    }

and this is my login controller :

public function login()
    {
        // No need to show a login form if the user
        // is already logged in.
        if ($this->auth->check())
        {
            $redirectURL = session('redirect_url') ?? route_to('admin-dashboard');
            unset($_SESSION['redirect_url']);
            
            return redirect()->to($redirectURL);
        }
        // Set a return URL if none is specified
        if (session('redirect_url') == site_url('logout'))
        {
            $_SESSION['redirect_url'] = previous_url() ?? route_to('admin-dashboard');
        }

        if (previous_url() == site_url('logout'))
        {
            $_SESSION['redirect_url'] = route_to('admin-dashboard');
        }

        return view($this->config->views['admin-login'], ['config' => $this->config]);
    }
blanksix
  • 340
  • 1
  • 16
  • Does this answer your question? [How to fix "Headers already sent" error in PHP](https://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php) – ADyson May 09 '21 at 08:18
  • @ADyson, no i think the problem is in redirecting with RedirectException, but i have no idea how to fix it. – blanksix May 09 '21 at 15:02
  • The error tells you you've started some output before you tried to set the redirect header. That's why it's basically a duplicate. Did you check the code at `Printer.php:104` as mentioned in the error? That's where it starts outputting. You can't do that if you're then going to set response headers later on. It's not clear if any of the code you've shown above is from Printer.php, or which line is 104? – ADyson May 09 '21 at 19:06
  • 1
    That the problem, printer.php is part of phpunit, not mine, there is an print syntax that i think make the error, but i have no idea how to fix that. – blanksix May 09 '21 at 22:01
  • 1
    @blanksix did you ever manage to fix this? I've hit the same issue – AndyD Nov 03 '21 at 01:04

0 Answers0