简体   繁体   中英

How can I provide credentials to PHPUnit tests

I have an API classes library, but testing it requires API key. 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? Does PHPUnit allow to pass options via command line?

You could set an environment variable before running the tests, then retrieve it in your tests using getenv . For example:

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

Then in your PHP files:

$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'); in setup(). Here's the link for the page in the manual for PhpUnit and for PHP .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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