tags:

views:

302

answers:

3

We set a global in our prepend file used to form the path for our require_once calls. For example:

require_once($GLOBALS['root'].'/library/particleboard/JsonUtil.php');

Problem is, when I run PHPUnit's skeleton test builder, the prepend file is not run, so the global is never set. When I run

cd /company/trunk/queue/process; phpunit --skeleton-test QueueProcessView

PHPUnit tries to resolve a require_once in QueueProcessView, but since the $GLOBALS['root'] is never set, I get a fatal error when including the required file.

For example, to PHPUnit, what should be

require_once(/code/trunk/library/particleboard/JsonUtil.php)

is resolved as

require_once(/library/particleboard/JsonUtil.php)

Notice the missing root.

Does anyone know if the skeleton-test code has some way to call PHP file before it is run? In this I could set my GLOBAL['root'] in this file.

Any other creative solutions would be appreciated.

A: 

the superglobal is called $_GLOBALS not $GLOBALS

Rufinus
Wrong. It's $GLOBALS.http://www.php.net/manual/en/reserved.variables.globals.php
Vili
A: 

Ok, I figured it out. I simply edited the PHPUnit/Util/Test.php file to include my reference. Not the most elegant solution, but since the rest of the framework allows you to call a bootstrap file, I can live with this one hacked utility.

ministerOfPower
A: 

In the same way as you can bootstrap a regular test suite, you can also do much the same for the skeleton generation

phpunit --bootstrap prepend.php --skeleton-test QueueProcessView

I tested this with three files:

test.php:

<?php   
require_once($GLOBALS['root'].'/confirmedToRun.php');
class test
{
   function doStuff()
   {
   }
}

prepend.php:

<?php   
$root = "/tmp/";

confirmedToRun.php:

<?php   
echo __FILE__;

Running phpunit --bootstrap prepend.php --skeleton-test test to generate the skeleton class, testTest.php - also runs the confirmedToRun.php file.

phpunit --bootstrap prepend.php --skeleton-test test
PHPUnit 3.4.2 by Sebastian Bergmann.

/tmp/confirmedToRun.php
Wrote skeleton for "testTest" to "/home/me/tmp/testTest.php".
Alister Bulman
Thanks Alister! That's perfect.
ministerOfPower