I am trying to write unit tests for an application where a lot of code changes is not possible. Almost all the .php files in the code base uses some $_SERVER[''] variables like
我正在尝试为无法进行大量代码更改的应用程序编写单元测试。几乎所有代码库中的.php文件都使用了一些$ _SERVER ['']变量
require_once $_SERVER['DOCUMENT_ROOT'] . '/mainApi.php';
So now when I have to write and run PHPUnit test cases I have to somehow set these variables. At present I am setting these variables in the user environment and then doing
所以现在当我必须编写并运行PHPUnit测试用例时,我必须以某种方式设置这些变量。目前我在用户环境中设置这些变量,然后进行
$_SERVER['DOCUMENT_ROOT'] = getenv('DOCUMENT_ROOT');
require_once $_SERVER['DOCUMENT_ROOT'] . '/mainApi.php';
Getting the server variables like this is working fine. I run my tests through commandline as $ phpunit test.php
.
获取这样的服务器变量工作正常。我通过命令行运行我的测试为$ phpunit test.php。
Ques1: Is it possible to set the $_SERVER variables while running the phpunit tests through commandline?
问题1:通过命令行运行phpunit测试时是否可以设置$ _SERVER变量?
I also have to run these unit tests through Jenkins and I am not able to set these server variable through ANT/build file.
我还必须通过Jenkins运行这些单元测试,我无法通过ANT / build文件设置这些服务器变量。
Ques2: Is it possible to set these variable through ant build file in Jenkins or by running any shell script before executing the phpunit tests through Jenkins?
问题2:是否可以通过Jenkins中的ant构建文件设置这些变量,或者在通过Jenkins执行phpunit测试之前运行任何shell脚本?
I tried exporting the server variable through a shell script
我尝试通过shell脚本导出服务器变量
export DOCUMENT_ROOT=/server/path-to-root-dir
and calling that script in the build.xml in Jenkins
并在Jenkins的build.xml中调用该脚本
<export name="setEnv" description="set server var">
<exec executable="sh">
<arg value = "sumit.sh" />
</exec>
</target>
but its not working. Is there any setting that I can do for this? Thanks!
但它不起作用。我可以为此做任何设置吗?谢谢!
1 个解决方案
#1
6
I'm not sure about #1, but PHPUnit itself would have to support it. I don't see any way to do that via the command line. However, if you put your current workaround into bootstrap.php
you don't have to do it in each test.
我不确定#1,但PHPUnit本身必须支持它。我没有看到通过命令行做任何方法。但是,如果将当前的解决方法放入bootstrap.php,则不必在每个测试中执行此操作。
For #2, <exec>
allows you to set environment variables using nested <env>
elements. I use this in Jenkins.
对于#2,
<exec executable="phpunit" ...>
<env key="DOCUMENT_ROOT" value="/var/www/php"/>
</exec>
Update: You typically create bootstrap.php
to setup add the source directory to the include path and initialize the test environment however you need. This file isn't supplied by PHPUnit--unlike phpunit.xml
.
更新:您通常创建bootstrap.php以设置将源目录添加到包含路径并根据需要初始化测试环境。 PHPUnit不提供此文件 - 与phpunit.xml不同。
I place it in the same directory as phpunit.xml
, but that's because I have a separate file for each project. It goes in the directory that holds your tests typically. This allows you to run phpunit
from the command-line without telling it how to find those configuration files. Otherwise you have to use --bootstrap
and/or --configuration
to point to them.
我把它放在与phpunit.xml相同的目录中,但那是因为我为每个项目都有一个单独的文件。它通常位于保存测试的目录中。这允许您从命令行运行phpunit,而不告诉它如何查找这些配置文件。否则,您必须使用--bootstrap和/或--configuration指向它们。
Here is how I structure a typical project:
以下是我构建典型项目的方法:
<project-root>/
build.xml
src/
MyClass.php
test/
MyClassTest.php
phpunit.xml
bootstrap.php
#1
6
I'm not sure about #1, but PHPUnit itself would have to support it. I don't see any way to do that via the command line. However, if you put your current workaround into bootstrap.php
you don't have to do it in each test.
我不确定#1,但PHPUnit本身必须支持它。我没有看到通过命令行做任何方法。但是,如果将当前的解决方法放入bootstrap.php,则不必在每个测试中执行此操作。
For #2, <exec>
allows you to set environment variables using nested <env>
elements. I use this in Jenkins.
对于#2,
<exec executable="phpunit" ...>
<env key="DOCUMENT_ROOT" value="/var/www/php"/>
</exec>
Update: You typically create bootstrap.php
to setup add the source directory to the include path and initialize the test environment however you need. This file isn't supplied by PHPUnit--unlike phpunit.xml
.
更新:您通常创建bootstrap.php以设置将源目录添加到包含路径并根据需要初始化测试环境。 PHPUnit不提供此文件 - 与phpunit.xml不同。
I place it in the same directory as phpunit.xml
, but that's because I have a separate file for each project. It goes in the directory that holds your tests typically. This allows you to run phpunit
from the command-line without telling it how to find those configuration files. Otherwise you have to use --bootstrap
and/or --configuration
to point to them.
我把它放在与phpunit.xml相同的目录中,但那是因为我为每个项目都有一个单独的文件。它通常位于保存测试的目录中。这允许您从命令行运行phpunit,而不告诉它如何查找这些配置文件。否则,您必须使用--bootstrap和/或--configuration指向它们。
Here is how I structure a typical project:
以下是我构建典型项目的方法:
<project-root>/
build.xml
src/
MyClass.php
test/
MyClassTest.php
phpunit.xml
bootstrap.php