Laravel phpunit 没有得到正确的网址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22967227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:20:14  来源:igfitidea点击:

Laravel phpunit not getting right url

phplaravellaravel-5laravel-4phpunit

提问by Benubird

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://localhostinstead of the value of app.url. What do I need to do to get phpunit to call the right path?

我已将 app.url 配置值更改为正确的 url ( http://testing.local) 以进行本地测试,但是当我运行 phpunit 测试并尝试 call() 时,它正在尝试查询http://localhost而不是 app.url 的值。我需要做什么才能让 phpunit 调用正确的路径?

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

我知道它实际上并没有调用 url,只是像它一样处理它,但我似乎无法让它实际工作。它可能与 testing.local 直接链接到/public而不是有关/吗?

回答by KorbenDallas

To change testing url of PHPUnit:

更改 PHPUnit 的测试 url:

Go to /tests/TestCase.phpin your Laravel project.

转到/tests/TestCase.php您的 Laravel 项目中。

Change next line to url you need:

将下一行更改为您需要的网址:

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

Done.

完毕。

回答by Moppo

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

如果要为测试静态指定根 URL,请将其添加到 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 $baseUrlmethod doesn't work anymore.

相反,如果您想在测试期间动态更改 URL,从 Laravel 5.4 开始,该$baseUrl方法不再起作用。

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

此外,尝试以动态方式设置 url\Config:set('app.url', 'http://testing.local')也不起作用,因为 Laravel 似乎缓存了根 url

You can set dynamically a custom URL with:

您可以使用以下方法动态设置自定义 URL:

\URL::forceRootUrl('http://testing.local');

回答by Mkk

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

如上所述,您可以Config::get('app.url')在 Laravel 代码的任何地方使用,包括您的单元测试。

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

注意:建议在您的 .env 文件中设置此值,以便可以针对每个环境进行专门设置​​。

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

使用 config 和 .env 变量时,请记住使用以下命令清除它们的缓存:

php artisan cache:clear
php artisan config:cache

回答by ndberg

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

我认为最好的方法是在 phpunit.xml 配置文件中设置 test app url:

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

回答by Yevgeniy Afanasyev

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

Laravel 有几个获取 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

在 phpunit 的情况下,测试正在从配置中设置数据

config('app.url')

and config is getting data from .envrecord APP_URL.

并且 config 正在从.envrecord获取数据APP_URL

So you have a few options

所以你有几个选择

1)

1)

to set APP_URLin .envfile or in phpunit.xmlfile - in both cases, a the value is only used in unit tests.

到组APP_URL.env文件中或在phpunit.xml文件-在两种情况下,该值仅在单元测试中使用。

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

但是对于所有单元测试,您将只有一个 URL。

2)

2)

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

APP_URL在运行时设置,但是,您需要在运行之前setUp()进行设置Tests\TestCase

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

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

因为所有的初始化都隐藏在setUp你不能使用的函数中 config()->set('app.url',...。之前parent::setUp();为时过早,之后 - 则为时已晚。

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

但是每个单元测试文件只有一个 URL。

3)

3)

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

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

你可以在setUp()函数后的任何地方设置它,它会覆盖.envconfig()

No 'BUTs'.

没有“但是”。

回答by JSweeney

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.

据我所知,Laravel 应该在您运行 PHPUnit 时使用“测试”环境变量。但是,我遇到了与您相同的问题。为了让它工作,我手动设置了应用程序 url。

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

Hope this helps anyone who comes across the same issue.

希望这可以帮助遇到相同问题的任何人。