Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
436 views
in Technique[技术] by (71.8m points)

php - Pass variable to PhpUnit

I developed a set of test cases using phpunit used for testing both development site and the production site. The only difference is the domain name. How can I pass the domain from command line to phpunit test cases so that I do not need to modify every file?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In your phpunit-bootstrap.php you can do something like this:

$site = getenv('DOMAIN');

and use

<php>
  <env name="DOMAIN" value="http://production.com"/>
</php>

in your phpunit.xml file as shown in the documentation.

The downside of this approach is that you need to have two different xml files.


Other options are using a custom wrapper script around phpunit like @DavidHarkness showed which tends to work out well

or, if you don't want to run those tests in an automated way (or use both approaches) to do something like:

$site = getenv('DOMAIN');
if(!$site) {
    echo "Enter Domain: ";
    $site = fgets(STDIN);
}

and have the runner check the environment and if nothing is there ask you for the domain.


Env or define or anything else

The same goes for pretty much every way that php can take in input from the outside world.

<php>
  <define name="DOMAIN" value="http://production.com"/>
</php>

also works in case you are using a constant anyways, for example.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...