Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel phpunit not getting right url

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 /?

like image 464
Benubird Avatar asked Apr 09 '14 15:04

Benubird


2 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';

Done.

like image 110
KorbenDallas Avatar answered Sep 21 '22 16:09

KorbenDallas


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:

\URL::forceRootUrl('http://testing.local');
like image 24
Moppo Avatar answered Sep 22 '22 16:09

Moppo