php单元测试时,如何在RouteServiceProvider map()方法中获取子域



在Laravel中运行phpunit测试时,有没有办法获得子域?

更具体的方法是:

app/Providers/RouteServiceProvider.php

/**
* Define the routes for the application.
*
* @return void
*/
public function map()
{
$this->mapApiRoutes();
$this->mapWebRoutes();
// inside here get subdomain while running a phpunit test??

所以,如果我做一些像这样的虚拟测试:

/** @test */
public function user_can_see_welcome_page()
{
$response = $this->call('GET', 'https://subdomain.domain.com');
$response->assertStatus(200);
}

我想在RouteServiceProvider 的map((方法中获取subdomain

为了更改域,您可以将其添加到phpunit.xml以全局设置:

<php>
<env name="APP_URL" value="https://subdomain.domain.com"/>
</php>

但是在评论中讨论你的问题,这就是你问题的实际解决方案:

多个路由文件:

您可以在自己的映射方法和路由文件中拆分每个子域,并在RouteServiceProvider中注册它们。

对于每个子域:

protected function mapSubDomainRoutes()
{
Route::group([
'middleware' => 'web',
'domain' => 'subdomain.domain.com',
'namespace' => $this->namespace,
], function () {
require base_path('routes/subdomain.domain.php');
});
}

单一路由文件:

或者,如果你把所有东西都放在一个路由文件中,你可以把路由包装在一个组中:

Route::group(['domain' => ['subdomain.domain.com']], function () {
// domain specific routes
});

最新更新