Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在测试期间替换配置参数的最佳实践是什么?_Php_Unit Testing_Laravel_Phpunit - Fatal编程技术网

Php 在测试期间替换配置参数的最佳实践是什么?

Php 在测试期间替换配置参数的最佳实践是什么?,php,unit-testing,laravel,phpunit,Php,Unit Testing,Laravel,Phpunit,我正在测试一种方法,该方法在将社会保险号保存到数据库之前使用公钥对其进行加密。看起来是这样的: public function setSsnAttribute($value) { // Load the public key $public = file_get_contents(Config::get('certificates.public')); // Attempt to encrypt the social security number using the

我正在测试一种方法,该方法在将社会保险号保存到数据库之前使用公钥对其进行加密。看起来是这样的:

public function setSsnAttribute($value)
{
    // Load the public key
    $public = file_get_contents(Config::get('certificates.public'));

    // Attempt to encrypt the social security number using the public key
    if (!openssl_public_encrypt($value, $crypted, $public))
    {
        throw new Exception('Could not encrypt data. Nothing was stored.');
    }

    // The value of $crypted returned by openssl_public_encrypt contains
    // binary characters. Rather than storing the data in a BLOB, I'm
    // electing to use base64 encoding to convert it to a string that is
    // suitable for storage in the database.
    $crypted = base64_encode($crypted);

    $this->attributes['ssn'] = $crypted;
}
问题在于
Config::get('certificates.public')
调用。我希望确保在加密步骤失败时引发适当的异常。
Config::get('certificates.public')
中的值返回配置文件中定义的公共证书的路径。我的想法是,测试异常的最简单方法是为公共证书提供错误路径

我可以在配置文件中定义一个附加参数。我在想一些类似于
certificates.test.public.bad
的东西会返回
/dev/null
或者类似的东西


在单元测试期间指定备用配置参数的最佳实践是什么?在
setSsnAttribute
方法中加载证书的路径对我来说似乎是可疑的。是否有一种更易于测试的方法来加载配置参数?

在回到Laravel文档之后,我意识到我可以通过在单元测试中对参数调用
config::set()
来覆盖任何需要的配置参数。例如:

/**
 * @expectedException Exception
 */
public function testSsnDecryptionFailureThrowsException()
{
    // Replace the private certificate with
    Config::set('certificates.private', '/dev/null');

    $application = FactoryMuff::create('Lease317\RentalApplication');

    // I must access the attribute in order to trigger the decryption
    $application->ssn;
}
这正如预期的那样工作,现在我在模型上有100%的代码覆盖率