How to set $_SERVER variables when running phpunit tests through Jenkins

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';

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

  • Django error on makemessages “should be run from the Django Git tree or your project or app tree”
  • Working with Eclipse .classpath and project files in Git?
  • How to lookup the latest git commit hash from an ant build script
  • How can I output from java code to the Jenkins console?
  • How can I get the latest commit ID of the currently checked out branch in git, using ANT, without having an active git installation?
  • ANT checkout task fails
    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.

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

    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: 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?

    I tried exporting the server variable through a shell script

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

    and calling that script in the build.xml in Jenkins

    <export name="setEnv" description="set server var">
        <exec executable="sh">
           <arg value = "" />

    but its not working. Is there any setting that I can do for this?

  • Trying to enable Github post commit hook with jenkins
  • how to make jenkins use an already checked out codebase?
  • Schedule Fortnightly jobs on jenkins
  • Run docker after building own image
  • Jenkins path to git Windows master / Linux slave
  • Git Laravel - what should I do if I change files in the vendor directory?
  • One Solution collect form web for “How to set $_SERVER variables when running phpunit tests through Jenkins”

    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.

    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"/>

    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.

    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:

    Git Baby is a git and github fan, let's start git clone.