alebatistella/duskapiconf

使用简单的API更改每个Dusk测试的Laravel配置

v1.2.5 2023-08-14 18:25 UTC

This package is auto-updated.

Last update: 2024-09-14 21:06:02 UTC


README

一个Laravel模块,允许您在Dusk测试中执行实时配置更改。由Manyapp DuskApiConf仓库分支而来。

问题

目前,在Dusk测试期间定义您的Laravel应用程序配置的唯一方法是在专门的.env.dusk.local文件中设置相关变量。此文件在应用程序启动期间被复制并读取,因此在Dusk测试中无法更改。

这种行为可能会引起问题,因为许多开发人员需要在特定的测试中更改配置以查看应用程序是否做出相应反应。

此处所述,没有简单的解决方案来解决这个问题。

解决方案

此模块提供了一种简单的方法,可以在Dusk测试的运行时更改应用程序的配置。

它是通过提供一个隐藏的API路由来实现的,该路由可以将配置注册到一个临时文件中,该文件随后在Dusk测试的后续请求中被读取。

安装

composer require alebatistella/duskapiconf --dev

您需要将特性添加到您的DustTestCase.php中,如下所示:

<?php

use Laravel\Dusk\TestCase as BaseTestCase;
use AleBatistella\DuskApiConfig\Traits\UsesDuskApiConfig;

abstract class DuskTestCase extends BaseTestCase {
    use UsesDuskApiConfig;

    // ...
}

使用方法

要使用它,请直接在您的Dusk测试中使用以下定义的方法。

/** @test */
public function test_should_use_dusk_for_something ()
{
    // Get a config variable
    // Here, $appName will be "Laravel" on a fresh install
    $appName = $this->getConfig('app.name');

    // Change a config variable
    $this->setConfig('app.name', 'Laravel is fantastic');

    // Here, $appName will be "Laravel is fantastic"
    $appName = $this->getConfig('app.name');

    // Your tests with assertions
    // ...

    // You can reset all config variables set before.
    // This is not mandatory: you can keep the variables set for the next test
    // if you want (even though it is not a good practice).
    $this->resetConfig();
}

发布配置文件

php artisan vendor:publish --provider="AleBatistella\DuskApiConf\DuskApiConfServiceProvider"

使用此命令,您将在config文件夹中创建一个新的duskapiconf.php文件。然后,您可以修改存储磁盘和临时文件的名称,包括有关使用环境的配置。

许可证

MIT。