Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set laravel test to go to site name instead of localhost

I am trying to write some tests for an application. I have the server set up on MAMP going to dev.myappnamehere.com.

When I run a test (based off of Laracasts Integrated) it fails because it is looking for the route

 http://localhost/p/profile

But what it needs to go to is

 http://dev.myappnamehere/p/profile

How can I change that so it does not default to looking for the localhost and instead goes to the correct path?

I attempted to change this in the test but got nowhere and I was unable to located an answer through googling.

 <?php

 use Laracasts\Integrated\Extensions\Laravel as IntegrationTest;
 use Laracests\TestDummy\Factory as TestDummy;

 class ExampleTest extends TestCase {

/**
 * A basic functional test example.
 *
 * @return void
 */
public function testBasicExample()
{
    $this->visit('/')
    ->see('Login')
        ->type('[email protected]', 'email')
        ->type('password', 'password')
        ->press('Login')
        ->seePageIs('/p/profile');
  }

}
like image 935
Rockwell Rice Avatar asked Nov 05 '15 15:11

Rockwell Rice


People also ask

How do I change my localhost domain to Laravel?

You need to make sure XAMPP, WAMP etc is listening for traffic to that domain and then point your local hosts file at your server which is usually 127.0. 0.1 So for example using XAMPP you would add an addition to your httpd-vhosts. config file mapping the domain to the public Laravel folder.

What is unit test in Laravel?

Unit tests are tests that focus on a very small, isolated portion of your code. In fact, most unit tests probably focus on a single method. Tests within your "Unit" test directory do not boot your Laravel application and therefore are unable to access your application's database or other framework services.


4 Answers

Most likely you need to change a domain root in your existing request().

$domain = 'homestead.test';
request()->headers->set('HOST', $domain);

test

\Log::debug(request()->root());

http://homestead.test

like image 109
Yevgeniy Afanasyev Avatar answered Oct 31 '22 12:10

Yevgeniy Afanasyev


So shortly after I asked I stumbled on the answer. In

 LaravelTestCase.php 

there is a function called

  baseUrl() 

which sets the url it looks for. Once I changed that it looked in the correct spot. That file was part of the laracast testing that I loaded in.

like image 45
Rockwell Rice Avatar answered Oct 31 '22 13:10

Rockwell Rice


From Laravel 5.4 the $baseUrl method doesn't seem to work anymore

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

A way to set a custom root url is:

\URL::forceRootUrl('http://dev.myappnamehere');
like image 20
Moppo Avatar answered Oct 31 '22 12:10

Moppo


For Laravel 5, In the tests directory there should be a file called TestCase.php. In that file is a property $baseUrl. Update the value to your desired url. For example change

protected $baseUrl = 'http://localhost';

to

protected $baseUrl = 'http://dev.myappnamehere';
like image 29
Dallin Avatar answered Oct 31 '22 13:10

Dallin