Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6220085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:53:46+00:00 2026-05-24T07:53:46+00:00

I am trying to write unit tests for an application where a lot of

  • 0

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

$_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.

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 = "sumit.sh" />
    </exec> 
</target>

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T07:53:47+00:00Added an answer on May 24, 2026 at 7:53 am

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

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write unit tests for a bit of code involving Events.
I'm trying to write unit tests for an application that reports on Entries in
I want to write a few unit tests that do not make any changes
I am trying to write some unit tests for my code. In out project
I am trying to write a unit test for a client server application. To
I'm trying to write unit tests in MSTest and I've created two TestClasses. When
I'm trying to write a unit test that will loop through all action methods
I'm trying to write a unit test for a piece of code that generates
I'm trying to write unit tests to ensure correctness of various decorators I've written.
I'm trying to write some tests for an MVC application we're developing. We have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.