BaseURL in PHPUnit tests

How can i configure a base URL for all CakePHP 5 unit tests?

Currently, a test like this makes a request to “http://localhost/pages/home”, but I would like to change it to “http://abc.localhost/pages/home”.

    public function testDisplay()
    {
        Configure::write('debug', true);
        $this->get('/pages/home');
        $this->assertResponseOk();
        $this->assertResponseContains('Impressum');
        $this->assertResponseContains('<html>');
    }

Not in a redundant way, but in a central place.

I found the following lines in the file /tests/bootstrap.php, but it didn’t work.

if (empty($_SERVER['HTTP_HOST']) && !Configure::read('App.fullBaseUrl')) {
    Configure::write('App.fullBaseUrl', 'http://abc.localhost');
}

In my config/bootstrap.php, I have

if (PHP_SAPI === 'cli') {
    require CONFIG . 'bootstrap_cli.php';
    Configure::load('app_cli');
}

and then in my config/app_cli.php I have

'App' => [
	'fullBaseUrl' => '<what I want the fullBaseUrl to be when running from the command line>',
],

Note that this affects both unit tests and commands run through bin/cake. If you want to affect just one or the other, there are ways to accomplish that. Also note that I read that “what I want” part from the environment, so it’s easy to change it from one server to another if necessary.

1 Like