在 phpunit class 中使用来自 config.php 的数据
using data from config.php in phpunit class
我正在尝试编写一些测试,其中我使用来自 config.php
文件的数据。这些文件位于同一文件夹中。
我知道我不能包含 class 中的文件,但我也不知道在我的 phpunit 测试 class.
中使用这些敏感数据的正确方法是什么
class 看起来像这样:
<?php
require dirname(__FILE__).'/../../src/vendor/autoload.php';
$config = require "config.php";
use GuzzleHttp\Client;
echo $config['base_url']; //HERE I GET THE DATA
class ApiAdTest extends PHPUnit_Framework_TestCase
{--
public function testApiAd_postAd()
{
$client = new Client(['base_uri' => --DATA FROM CONFIG.PHP--]);
$response = $client->post(--DATA FROM CONFIG.PHP--, ['form_params' => [
'name' => 'bellow content - guzzle testing',
'description' => 'guzzle testing ad - demo'
]]);
$code = $response->getStatusCode();
$data = json_decode($response->getBody());
$adId = $data->id;
$this->assertEquals($code, 200);
return $adId;
}
在我的测试 class 中使用来自 config.php 的数据的正确方法是什么??
更新:
config.php内容:
<?php
return array(
'base_url' => 'http://10.0.0.0/',
'path' => 'api/ad/',
);
如果您使用的是 Composer,我喜欢使用一个名为 PHP Dotenv 的包来设置配置。
它使用项目根目录中的 .env
文件,并允许您将自己的设置添加到 PHP 的 $_ENV
数组中。然后,您可以在任何您想使用它们的地方使用 getenv()
将它们拉出来。
然后您可以将 .env
添加到您的 .gitignore
文件,这样您就不会不小心将其提交到源代码管理。
作为本机 PHP 解决方案,我建议使用 composer 的自动加载器来加载额外的配置文件。为此,您可以在 composer.json
文件中定义一个 files
数组,如下所示:
{
"autoload": {
"files": ["config.php"]
}
}
然后在你的配置文件中,我会创建一个全局函数来访问配置变量:
<?php
function config($item) {
$config = [
// Config variables here
];
return $config[$item];
}
在 运行 composer dumpautoload
之后,您可以在您的应用程序中全局访问 config()
以获取配置项。
(您可能希望根据您的要求调整此解决方案,我更喜欢使用我之前的回答)
注意:如果您使用源代码控制,请不要提交您的 config.php 文件,因为这存在安全风险
你可以在测试用例的setup方法中加载config.php文件并将值赋值给class属性,然后你可以在测试方法中使用如下:
<?php
require dirname(__FILE__).'/../../src/vendor/autoload.php';
use GuzzleHttp\Client;
class ApiAdTest extends PHPUnit_Framework_TestCase
{
protected $config;
protected function setUp()
{
$this->config = require("config.php");
}
public function testApiAd_postAd()
{
$client = new Client(['base_uri' =>$this->config['base_url'] ] );
$response = $client->post($this->config['path'], ['form_params' => [
'name' => 'bellow content - guzzle testing',
'description' => 'guzzle testing ad - demo'
]]);
$code = $response->getStatusCode();
$data = json_decode($response->getBody());
$adId = $data->id;
$this->assertEquals($code, 200);
return $adId;
}
}
希望对您有所帮助
我正在尝试编写一些测试,其中我使用来自 config.php
文件的数据。这些文件位于同一文件夹中。
我知道我不能包含 class 中的文件,但我也不知道在我的 phpunit 测试 class.
class 看起来像这样:
<?php
require dirname(__FILE__).'/../../src/vendor/autoload.php';
$config = require "config.php";
use GuzzleHttp\Client;
echo $config['base_url']; //HERE I GET THE DATA
class ApiAdTest extends PHPUnit_Framework_TestCase
{--
public function testApiAd_postAd()
{
$client = new Client(['base_uri' => --DATA FROM CONFIG.PHP--]);
$response = $client->post(--DATA FROM CONFIG.PHP--, ['form_params' => [
'name' => 'bellow content - guzzle testing',
'description' => 'guzzle testing ad - demo'
]]);
$code = $response->getStatusCode();
$data = json_decode($response->getBody());
$adId = $data->id;
$this->assertEquals($code, 200);
return $adId;
}
在我的测试 class 中使用来自 config.php 的数据的正确方法是什么??
更新: config.php内容:
<?php
return array(
'base_url' => 'http://10.0.0.0/',
'path' => 'api/ad/',
);
如果您使用的是 Composer,我喜欢使用一个名为 PHP Dotenv 的包来设置配置。
它使用项目根目录中的 .env
文件,并允许您将自己的设置添加到 PHP 的 $_ENV
数组中。然后,您可以在任何您想使用它们的地方使用 getenv()
将它们拉出来。
然后您可以将 .env
添加到您的 .gitignore
文件,这样您就不会不小心将其提交到源代码管理。
作为本机 PHP 解决方案,我建议使用 composer 的自动加载器来加载额外的配置文件。为此,您可以在 composer.json
文件中定义一个 files
数组,如下所示:
{
"autoload": {
"files": ["config.php"]
}
}
然后在你的配置文件中,我会创建一个全局函数来访问配置变量:
<?php
function config($item) {
$config = [
// Config variables here
];
return $config[$item];
}
在 运行 composer dumpautoload
之后,您可以在您的应用程序中全局访问 config()
以获取配置项。
(您可能希望根据您的要求调整此解决方案,我更喜欢使用我之前的回答)
注意:如果您使用源代码控制,请不要提交您的 config.php 文件,因为这存在安全风险
你可以在测试用例的setup方法中加载config.php文件并将值赋值给class属性,然后你可以在测试方法中使用如下:
<?php
require dirname(__FILE__).'/../../src/vendor/autoload.php';
use GuzzleHttp\Client;
class ApiAdTest extends PHPUnit_Framework_TestCase
{
protected $config;
protected function setUp()
{
$this->config = require("config.php");
}
public function testApiAd_postAd()
{
$client = new Client(['base_uri' =>$this->config['base_url'] ] );
$response = $client->post($this->config['path'], ['form_params' => [
'name' => 'bellow content - guzzle testing',
'description' => 'guzzle testing ad - demo'
]]);
$code = $response->getStatusCode();
$data = json_decode($response->getBody());
$adId = $data->id;
$this->assertEquals($code, 200);
return $adId;
}
}
希望对您有所帮助