简体   繁体   English

如何为PHPUnit测试提供凭据

[英]How can I provide credentials to PHPUnit tests

I have an API classes library, but testing it requires API key. 我有一个API类库,但是测试它需要API密钥。 Of course the key is not public and there is my question. 当然,密钥不是公开的,这是我的问题。 How can I provide credentials to PHPUnit configuration, so everyone could run the tests? 如何为PHPUnit配置提供凭据,以便每个人都可以运行测试? Does PHPUnit allow to pass options via command line? PHPUnit是否允许通过命令行传递选项?

You could set an environment variable before running the tests, then retrieve it in your tests using getenv . 您可以在运行测试之前设置环境变量,然后使用getenv在测试中检索它。 For example: 例如:

#!/bin/bash
API_KEY=secretapikey
phpunit <parameters>

Then in your PHP files: 然后在您的PHP文件中:

$apiKey = getenv('API_KEY');

Might have a look at the '-d' option to PhpUnit it's meant to set php.ini option but you can read that with a simple $setting = ini_get('my_setting'); 可以看看PhpUnit的'-d'选项,它是用来设置php.ini选项的,但是您可以使用简单的$ setting = ini_get('my_setting');来阅读它。 in setup(). 在setup()中。 Here's the link for the page in the manual for PhpUnit and for PHP . 这是PhpUnitPHP 手册页面的链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM