manyapp/duskapiconf

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

v0.0.3 2019-07-05 14:16 UTC

This package is auto-updated.

Last update: 2024-09-06 01:56:56 UTC


README

Manyapp DuskApiConf

一个Laravel模块,用于从Dusk测试中执行实时配置更改

问题

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

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

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

解决方案

此模块提供了一种在Dusk测试运行时轻松更改应用程序配置的方法。

在我们的博客文章中查看它是如何工作的。

安装

使用以下命令安装模块:

composer require manyapp/duskapiconf --dev

然后,您需要修改您的DustTestCase.php文件以添加三个方法。或者,您可以将以下方法添加到您选择的Trait中,并在Dusk测试中使用该Trait。

/**
* Set live config option
*
* @param string $key 
* @param mixed $value
* @return void
*/
public function setConfig($key, $value)
{
    $encoded = base64_encode(json_encode($value));
    $query = "?key=".$key.'&value='.$encoded;
    $this->browse(function($browser) use ($query) {
        $data = $browser->visit('/duskapiconf/set'.$query)->element('.content')->getAttribute('innerHTML');
        $data = trim($data);
        if ($data !== 'ok') {
            $this->assertTrue(false);
        }
    });
}


/**
* Get a current configuration item
*
* @param string $key
* @return mixed
*/
public function getConfig($key)
{
    $query = "?key=".$key;
    $result = null;
    $this->browse(function($browser) use ($query, &$result) {
        $data = $browser->visit('/duskapiconf/get'.$query)->element('.content')->getAttribute('innerHTML');
        $result = json_decode(base64_decode($data), true);
    });
    return $result;
}

/**
* Reset the configuration to its initial status
*
* @return void
*/
public function resetConfig()
{
    $this->browse(function($browser) {
        $browser->visit('/duskapiconf/reset');
    });
}

用法

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

/** @test */
public function your_dusk_test()
{
    // 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.
    $this->resetConfig();
}

更改配置临时文件的存储位置

输入以下命令:

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

修改存储磁盘和临时文件的名称。

贡献

对于任何错误或功能请求,请使用Github。

对于任何其他反馈,请在此博客文章联系我们上发表评论。

许可证

MIT。