6

I'm testing a Zend Framework application using PHPUnit and Jenkins. I need to override the APPLICATION_ENV environment variable which is access using PHP's getenv in the PHPUnit bootstrap.php file:

<?php

// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'testing'));

... use APPLICATION_ENV to pick database, configuration, etc. ...

I have two environments: testing (for local machines) and testing-ci (for Jenkins machine). How can I set the variable to testing-ci when it runs in Jenkins? Is there any way to set it in build.xml for Ant or Phing?

David Harkness
  • 35,992
  • 10
  • 112
  • 134
dextervip
  • 4,999
  • 16
  • 65
  • 93

2 Answers2

7

Step 1: Add the environment variables to Jenkins.

Open either the global or project-specific configuration page depending on your needs and scan down for the Environment variables section. Check the checkbox and use the Add button to add key/value pairs.

These will be passed by Jenkins to your Ant build script.

Step 2: Load them into Ant.

Near the top of your Ant build.xml script, load all environment variables with an env prefix so they don't interfere with other properties.

<property environment="env"/>

Now all imported variables will be available using the env prefix, e.g. ${env.HOME}.

Step 3: Pass them to PHPUnit.

Assuming you're using the <exec> task to run PHPUnit, you can pass each needed variable to it using the <env> child element.

<exec taskname="test" executable="phpunit">
    <env key="APPLICATION_ENV" value="${env.APPLICATION_ENV}"/>
    ...
</exec>

Note: You might want to try just the first step to see if Ant will pass the environment variables along to executed child processes, but I think the other two steps are good for making it clear what is required to other developers.

David Harkness
  • 35,992
  • 10
  • 112
  • 134
0

OK.

Here's what you do...

First, create a new file called bootstrap.php.

Next, in boostrap.php, put the following code:

if (!empty($argv) && 
    ($key = array_search('--environment', $argv)) !== FALSE)
{
    $env = $argv[$key + 1];
    putenv('APPLICATION_ENV=' . $env);
}

Load the bootstrap.php into your testsuite or (even better) phpunit.xml.

Finally, via your CI build config, or via the console or wherever, execute your unit tests like phpunit UnitTest.php --environment dev.

You're good to go.

Theodore R. Smith
  • 21,848
  • 12
  • 65
  • 91
  • Sorry I don't know if you understood me but I already have bootstrap.php and phpunit.xml, nowdays It works locally on my machine database but when goes to jenkins it's another database password. I need a solution to change the env variable in jenkins configuration or in the build.xml so that won't break my local testing and will work on jenkins. – dextervip Feb 04 '12 at 01:57
  • Oh I'm sorry. I'm editing my post now with an updated solution. – Theodore R. Smith Feb 04 '12 at 03:59
  • It would be a solution however in my case I'm running a suite test case configured by xml (phpunit --configuration ./tests/phpunit.xml). PHPUnit doesn't accept receive --environment but anyway thanks :) – dextervip Feb 06 '12 at 23:53
  • Yes it does. Man. Do some research before you dismiss my answer. http://www.phpunit.de/manual/3.6/en/appendixes.configuration.html See the PHP INI settings. You'd want . – Theodore R. Smith Feb 07 '12 at 03:56
  • I know it, I was using it before but this solution needed 2 config xml (phpunit.xml and phpunit-ci.xml) it works but I need to keep two xml files otherwise it will apply the environment to my local machine and jenkins. – dextervip Feb 07 '12 at 12:16