PHPUnit – Passing Environment Variable To Your Application

I’m using environment variables for my configurations so that it will be easy to switch from development to production or to testing mode without changing any codes. As far as I’m using Zend Framework with the tip that I used for PHPunit, I don’t have any problems at all. But when I tried the same thing in Kohana Framework, there goes the problem and then there goes the simple solution.

Short Answer

I really like telling stories this and that but you may get bored so here is the quick and dirty answer for those who know what I’m talking about.

APPLICATION_ENV=testing phpunit

Where APPLICATION_ENV is the environment and testing is the value. That is common to most linux shell commands, I don’t know with Windows. For those who want to read the whole story, read along.

And for those who are done, go back to work and fix that code!

Environment Variables

In most of my Zend Framework applications, environment variables are used to tell the application what mode is it. It can be either on production mode, development, testing or staging. It is done on these components:

  • .htaccess file
  • index.php file – entry point
  • Configuration files

In .htaccess file, there is an additional directive that sets the environment variable APPLICATION_ENV to something like production or development, etc.

# Set environment
SetEnv APPLICATION_ENV "development"

It is then received in index.php with something like this:

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

And then the rest are handled by the application using the configuration files and Zend_Application.

// Create application, bootstrap, and run
$application = new Zend_Application(
    APPLICATION_PATH . '/configs/application.ini'


That was pretty basic for any Zend Framework based application. I have no problem with that setup and I don’t even have problem when using the command line for running PHPUnit for unit tests. It is because I followed the way some people do with PHPUnit in Zend Framework by using a TestHelper.php for running test cases. That TestHelper.php sets the APPLICATION_ENV constant that will tell the application that it is in testing mode.

PHPUnit, Kohana and Environment Variables

I tried to do the same thing as Zend Framework with Kohana. I set the .htaccess directive, receive it at index.php and let the bootstrap and the whole application know what mode we are. That was flawless! But when I tried it on the command line using PHPunit, whoops! It was in production mode.

It is because the environment variable was not present in the command line. Perhaps, getenv() returns null or something.

Because I was fond of slack-building – (the art of compiling a software to be installable natively in Slackware), I have noticed that one can pass a parameter to a script (slackbuild script) to configure it. I though it was the environment thing I was looking for. And it was indeed correct!

We simply add an environment variable to a script / command like this:

APPLICATION_ENV=testing phpunit

What we do above is set the environment that will be used by PHPUnit command which in turn becomes available to our Kohana application. So how do I actually use it in Kohana? Here it is:

application/bootstrap.php near init

 * Set the production status by environment
$production_status = true;

if (in_array(getenv('APPLICATION_ENV'), array('development', 'testing')))
	$production_status = false;

define('IN_PRODUCTION', $production_status);

// Other configuration code
// ...

// Initialization
	'base_url' 		=> '/',
	'index_file' 	=> FALSE,
	'profile'  		=> ! IN_PRODUCTION,
	'caching'    	=> IN_PRODUCTION

Leave a reply

Your email address will not be published. Required fields are marked *