tags:

views:

25

answers:

3

I am currently doing unit testing, and use folders to organize my test cases. All cases pertaining to managing of user accounts, for example, go under \tests\accounts. Over time, there are more test cases, and I begin to seperate the cases by types, such as \tests\accounts\create, \tests\account\update and etc.

However, one annoying problem is I have to specify the path to a set of common includes. I have to use includes like this:

include_once ("../../../../autoload.php");
include_once ("../../../../init.php");

A test case in tests\accounts\ would require change to the include (one less directory level down). Is there anyway to have them somehow locating my two common includes? I understand I could set include paths within my PHP's configurations, or use server environment variables, but I would like to avoid such solutions as they make the application less portable and coupled with another layer which the programmer can't control (some web-host doesn't allow configuration of PHP's configuration settings, for example)

+3  A: 

Hi, you could try using "include_path" in php.ini or use the function set_include_path in php http://php.net/manual/en/function.set-include-path.php

set_include_path('/home/www/...');

or

set_include_path('../../../');

also, you make a constant in a global file, then

define('DIR_PATH', '../../');
include_once(DIR_PATH . 'file');

because the "set_include_path" isnt so efficient.

Hope I helped

WEBProject
I will have to do this for every of my unit test case file, and because the files are placed at different directory levels, the relative path will also vary from file to file
Extrakun
+2  A: 

why not just defining a variable

$includePath = "/unittests/include/";

and then

include_once ($includePath."something.php");
nico
can I ask why the downvote please?
nico
I will have to do this for every of my unit test case file
Extrakun
@Extrakun: well, it's easier then writing all the `../../../`... If you do not want to change the include path in your PHP configuration I don't see any other option. You can have all your unit test spawning from the same page though, so the variable would have to be defined only once.
nico
also, if you use an absolute path, it will always be the same, so you wouldn't haveto change it every time.
nico
beside defining a variable you have to include a file somehow, which contains this variable definition :)
Col. Shrapnel
@Col. Shrapnel: yes, either that or you define the variable in each file. In both cases if you change the path you'll have to change it in every file (I mean, if you change the path of the included file)... The other solution would be to have a test.php file that handles ALL of the unit tests by including different php files so that the variable is only defined once.
nico
A: 

because the files are placed at different directory levels, the relative path will also vary from file to file

that's why absolute path always considered better.

include $_SERVER['DOCUMENT_ROOT']."/../config/init.php";

is enough most of time

Col. Shrapnel