Home » Php » php – Laravel phpunit not getting right url

php – Laravel phpunit not getting right url

Posted by: admin July 12, 2020 Leave a comment


I’ve changed the app.url config value to the correct url (http://testing.local) for testing locally, however when I run my phpunit tests and try to call(), it is trying to query http://localhost instead of the value of app.url. What do I need to do to get phpunit to call the right path?

I know that it is not actually calling the url, just processing it as if it was, but I can’t seem to get it to actually work. Could it have something to do with testing.local directly linking to /public instead of /?

How to&Answers:

To change testing url of PHPUnit:

Go to /tests/TestCase.php in your Laravel project.

Change next line to url you need:

// The base URL to use while testing the application.
protected $baseUrl = 'http://newurl.com';



If you want to statically specify a root URL for your tests, add this to phpunit.xml:

<env name="APP_URL" value="http://testing.local"/>

Instead, if you want to change the URL dinamically during your tests, from Laravel 5.4 the $baseUrl method doesn’t work anymore.

Also, trying to set the url dinamically with \Config:set('app.url', 'http://testing.local') doesn’t work either, as it seems that Laravel caches the root url

You can set dynamically a custom URL with:



As stated above, you can use Config::get('app.url') anywhere in your laravel code, including your unit-test.

Note: it is recommended to set this value in your .env file so that it can be set specifically for each environment.

When working with config and .env variables, remember to clear the cache for these with the following commands:

php artisan cache:clear
php artisan config:cache


I think the best way is to set the test app url in phpunit.xml configuration file:

<env name="APP_URL" value="some-url.test"/>


Laravel has a few functions to get url: (1) url()->current() (2) request()->url()

Both of them are using the same source.

In case of phpunit, tests are setting data there from the config


and config is getting data from .env record APP_URL.

So you have a few options


to set APP_URL in .env file or in phpunit.xml file – in both cases, a the value is only used in unit tests.

BUT you would have only one URL for all your unit tests.


set APP_URL in runtime
but, you need to do it before function setUp() in Tests\TestCase

public function setUp(): void
         $_ENV['APP_URL'] = 'example.com';

Because all the initialisation is hidden in setUp function you CANNOT use
config()->set('app.url',.... Before parent::setUp(); would be too early, and after it – would be too late.

BUT you would have only one URL per unit test file.


request()->headers->set('HOST', 'example.com');

you can set it anywhere after setUp() function and it will overwrite .env and config()

No ‘BUTs’.


To the best of my understanding, Laravel is supposed to use the ‘testing’ environment variables when you run PHPUnit. However, I am having the same issue that you are. To get it to work I manually set the app url.

$test_url = Config::get('app.url');

Hope this helps anyone who comes across the same issue.