通过 Jenkins 运行 phpunit 测试时如何设置 $_SERVER[''] 变量

2022-01-25 00:00:00 jenkins php phpunit ant

我正在尝试为无法进行大量代码更改的应用程序编写单元测试.代码库中几乎所有的 .php 文件都使用了一些 $_SERVER[''] 变量,例如

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

require_once $_SERVER['DOCUMENT_ROOT'] . '/mainApi.php';

所以现在当我必须编写和运行 PHPUnit 测试用例时,我必须以某种方式设置这些变量.目前我是在用户环境中设置这些变量,然后在做

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

$_SERVER['DOCUMENT_ROOT'] = getenv('DOCUMENT_ROOT');
require_once $_SERVER['DOCUMENT_ROOT'] . '/mainApi.php';

像这样获取服务器变量可以正常工作.我通过命令行以 $ phpunit test.php 运行我的测试.

Getting the server variables like this is working fine. I run my tests through commandline as $ phpunit test.php.

问题1:是否可以在通过命令行运行 phpunit 测试时设置 $_SERVER 变量?

Ques1: Is it possible to set the $_SERVER variables while running the phpunit tests through commandline?

我还必须通过 Jenkins 运行这些单元测试,我无法通过 ANT/build 文件设置这些服务器变量.

I also have to run these unit tests through Jenkins and I am not able to set these server variable through ANT/build file.

问题2:是否可以通过 Jenkins 中的 ant 构建文件或在通过 Jenkins 执行 phpunit 测试之前运行任何 shell 脚本来设置这些变量?

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?

我尝试通过 shell 脚本导出服务器变量

I tried exporting the server variable through a shell script

    export DOCUMENT_ROOT=/server/path-to-root-dir

并在 Jenkins 的 build.xml 中调用该脚本

and calling that script in the build.xml in Jenkins

<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,但 PHPUnit 本身必须支持它.我看不出有任何方法可以通过命令行做到这一点.但是,如果您将当前的解决方法放入 bootstrap.php 中,则不必在每个测试中都这样做.

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.

对于 #2,<exec> 允许您使用嵌套的 <env> 元素设置环境变量.我在 Jenkins 中使用它.

For #2, <exec> allows you to set environment variables using nested <env> elements. I use this in Jenkins.

<exec executable="phpunit" ...>
    <env key="DOCUMENT_ROOT" value="/var/www/php"/>
</exec>

更新:您通常创建 bootstrap.php 来设置将源目录添加到包含路径并根据需要初始化测试环境.这个文件不是由 PHPUnit 提供的——不像 phpunit.xml.

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.

我将它放在与 phpunit.xml 相同的目录中,但那是因为我对每个项目都有一个单独的文件.它通常位于保存测试的目录中.这允许您从命令行运行 phpunit 而无需告诉它如何找到这些配置文件.否则你必须使用 --bootstrap 和/或 --configuration 来指向它们.

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.

这是我构建一个典型项目的方式:

Here is how I structure a typical project:

<project-root>/
    build.xml
    src/
        MyClass.php
    test/
        MyClassTest.php
        phpunit.xml
        bootstrap.php

相关文章