How to set $_SERVER[' '] variables when running phpunit tests through Jenkins How to set $_SERVER[' '] variables when running phpunit tests through Jenkins jenkins jenkins

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