【问题标题】:Setting up testing in Laravel project using Hyn/multi-tenant使用 Hyn/多租户在 Laravel 项目中设置测试
【发布时间】:2020-11-22 03:30:49
【问题描述】:

我正在使用Hyn/multi-tenant 5.6 包来支持多租户应用程序。 我的应用程序运行良好,我可以创建租户并且它们运行正常。

问题来自于测试。我想为我的测试目的设置一个现有租户。但不知何故,应用程序无法在测试期间创建正确的路由。 在 CreateApplication.php 中,我设置了用于测试的租户。其中有website_id = 1

trait CreatesApplication
{
    /**
     * Creates the application.
     *
     * @return \Illuminate\Foundation\Application
     */
    public function createApplication()
    {
        $app = require __DIR__.'/../bootstrap/app.php';

        $app->make(Kernel::class)->bootstrap();

        return $app;
    }

    public function setUp() : void{

        parent::setUp();

        $hostname = Hostname::find(1);
        $website = Website::findOrFail($hostname->website_id);
        $tenancy = app(Environment::class);
        $tenancy->hostname($hostname);

        Artisan::call('optimize:clear');
        Artisan::call('tenancy:migrate:fresh', ['--website_id' => 1]);
        Artisan::call('tenancy:db:seed',['--class' => 'TenantSeeder','--website_id' => 1]);

    }

}

我通过谷歌搜索发现了一些类似的问题,但他们的解决方案都不适合我: https://github.com/tenancy/multi-tenant/issues/584

我所有的路线都在我的tenants.php 文件中并且正在运行。 我正在使用最新版本的 PHPUNIT。

我得到的错误是:

Symfony\Component\Routing\Exception\RouteNotFoundException : Route [new_anmeldung_single] not defined.

【问题讨论】:

    标签: laravel testing phpunit multi-tenant laravel-7


    【解决方案1】:

    在我写这个问题时,我尝试了更多选项,添加和删除了一些代码(我上周至少做了 10 小时),令我惊讶的是我找到了解决方案。

    我只是忘了做:

    $tenancy->tenant($website); // switches the tenant and reconfigures the app (new RouteProvider(app()))->boot();//reloads the routes

    因此,如果您想为测试设置现有租户,则需要将以下代码添加到您的 setUp() 方法中。如果您想创建一个新租户,可以按照问题中发布的问题中的示例进行操作。

    public function setUp() : void{
    
            parent::setUp();
    
    
            $hostname = Hostname::find(1);
            $website = Website::findOrFail($hostname->website_id);
            $tenancy = app(Environment::class);
            $tenancy->hostname($hostname);
            $tenancy->tenant($website); // switches the tenant and reconfigures the app
    
            (new RouteProvider(app()))->boot();
    
            Artisan::call('tenancy:migrate:fresh', ['--website_id' => 1]);
            Artisan::call('tenancy:db:seed',['--class' => 'TenantSeeder','--website_id' => 1]);
    
        }
    

    遗憾的是这部分没有记录,所以我希望有类似问题的人能找到这篇文章。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-06
      • 1970-01-01
      • 1970-01-01
      • 2021-07-04
      • 2020-06-18
      • 1970-01-01
      • 2023-01-25
      • 1970-01-01
      相关资源
      最近更新 更多