如何在通过 Jenkins 运行 phpunit 测试时设置 $_SERVER[' '] 变量 [英] How to set $_SERVER[' '] variables when running phpunit tests through Jenkins

查看:27
本文介绍了如何在通过 Jenkins 运行 phpunit 测试时设置 $_SERVER[' '] 变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为无法进行大量代码更改的应用程序编写单元测试.代码库中几乎所有的 .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.

Ques2: 是否可以通过 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, 允许您使用嵌套的 元素设置环境变量.我在詹金斯使用这个.

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

这篇关于如何在通过 Jenkins 运行 phpunit 测试时设置 $_SERVER[' '] 变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆