Page 1 of 1

[Solved] How to handle Include/Path in an application

Posted: Tue Aug 16, 2005 10:05 pm
by stryderjzw
Hi,

I came across a problem with includes and I was hoping to hear what other people did.

I saw this structure from nielsene.

Code: Select all

tests/All.php
tests/acceptance-tests/All.php
                                /<moduleName>/All.php
                                                        /<long multi-page process test>
tests/unit-tests/All.php
                     /classes/All.php
                                /<class hierarchy>/All.php
                                                           /<assorted tests cases>
                     /web/All.php
                             /<moduleName/All.php
                                                   /<ModuleFunctionality>/All.php
                                                                                    /<little test cases>
tests/include/test_setup.inc
                 /<Custom Subclasses of Unit/Web TestCases>
                 /sql/<preload scripts>
tests/var   (holding space for temp files, etc)
I can see that All.php in acceptance-tests and unit-tests folders probably run all the tests in their own respective folders.

The All.php under the root tests directory, I assume, will run both unit-tests and accpetance tests. I wrote this page for my own project, as below:

Code: Select all

...
$this->addTestFile('unit-tests/All.php');
$this->addTestFile('acceptance-test/All.php');
...
The All.php under the root tests directory did not work. It keeps giving me include errors.

I believe I heard there was include & path related "issues" with php. Well, not really issues, but runs differently than Java (which makes sense to me). The following sitepoint thread talks a little bit about what I'm trying to figure out.
http://www.sitepoint.com/forums/showthr ... clude+path

Anyways... My questions are:

1. How do you guys handle the path "issue"? use DEFINE? Are absolute paths always used? or can we achieve this with relative paths?

2. What do you put in a test_setup.inc file? are there constants in there that define paths?


I hope I am clear and someone understands... but I am not too sure how to explain this exactly.

Any help is appreciated. Thanks!

Edit: Maybe code samples will help, as well as any thoughts you might have.

Posted: Tue Aug 16, 2005 10:15 pm
by nielsene
Here's how I made it all work:

First here's a sample All.php file (this is the root one):

Code: Select all

<?php 
  /**
   * Run all tests.
   */

  /** Configures the testing environments (includes, defines) */
require_once('test_setup.inc');

/** Group Test containing all Tests (Unit and Acceptance)
 * @package Tests
 */
class AllTest extends GroupTest {
  function AllTest() {
    $this->GroupTest('All Tests -- Unit and Acceptance');
    $this->addTestFile(COMPINABOX.'tests/unit-tests/All.php');
    $this->addTestFile(COMPINABOX.'tests/acceptence-tests/All.php');
  }
}

if (! defined('RUNNER')) {
  define('RUNNER',TRUE);
  ini_set('max_execution_time',300);
  $test = &new AllTest();
  $test->run(new HTMLReporter());
 }
?>
Here's my test_setup.inc most of which is custom to my app....

Code: Select all

<?php 
  /**
   * Configuration of test environment.
   *
   * This is the "master" include for all tests.  It sets the contants
   * to reach the test library, the application code and helper defines
   * for navigating the test hierarchy.  In addition it includes the common
   * simpletest files as well as the CompInaBox custome testing subclasses.
   */

  /* Path Defines */


/** Define path to simpletest framework   */
define('SIMPLE_TEST','/usr/local/CIB/simpletest/');

/** Define base path of CompInaBox application   */
define('COMPINABOX','/usr/local/CIB/main-dev/compinabox/');

/**@@+ Configure the test sandbox for database tests */
define('TEST_DB_HOST','localhost');
define('TEST_DB_NAME','test_cib_central');
define('TEST_DB_OWNER','XXXXX');
define('TEST_DB_OWNER_PASS','XXXX');
define('TEST_DB_CIB_UNAUTH','test_cib_unauth_user');
define('TEST_DB_CIB_AUTH','test_cib_auth_user');
define('TEST_DB_CIB_ADMIN','test_cib_admin_user');
define('TEST_SCHEMA_FILE',COMPINABOX.'tests/include/sql/testing_base_schema.sql');
/**#@-*/



/** Set the CIB paths */
require_once(COMPINABOX.'include/paths.inc');
/** Load the CompInaBox config file for path to testing install */
require_once(COMPINABOX.'include/conf/tools/CompInaBox_config.inc');

/** Elevate base url to a constant for use in class fucntions   */
define('CIB_URL',$CIB_BASE_URL);
/** Elevate install directory to constats for use in returnTo web-tests */
define('CIB_DIR',"/$CIB_WEB_INSTALL_DIR");


  /** Define base path of tests of CompInaBox class's   */
define ('CLASS_TEST',COMPINABOX.'tests/unit-tests/classes/');
  /** Define base path of tests of CompInaBox web pages   */
define ('WEB_UNIT',COMPINABOX.'tests/unit-tests/web-pages/');

/* Simple Test Includes */

/** Load the SimpleTest test case framework */
require_once(SIMPLE_TEST.'unit_tester.php');

/** Load the SimpleTest web test case framework */
require_once(SIMPLE_TEST.'web_tester.php');

/** Load the SimpleTest test reporting tools */
require_once(SIMPLE_TEST.'reporter.php');

/** Load the mock generator */
require_once(SIMPLE_TEST.'mock_objects.php');

/** Load the database helper test cases */
require_once('DatabaseTestCases.inc');
/** Load the web test case with authentication */
require_once('WebAuthTestCase.inc');

?>
Then in my .htaccess of the the root of the test tree (symlinked in to the webroot, seperately from the application tree)

Code: Select all

php_value include_path "/usr/local/CIB/main-dev/compinabox/tests/include"
(test_setup.inc lives in the tests/include)

All of the All.php's require_once(test_setup.inc)

Afterwords I always use full paths, using the constants setup in test_setup.inc.

I'm sure there are better ways, but this is working pretty well for me so far.

Re: How to handle Include/Path in an application

Posted: Tue Aug 16, 2005 10:33 pm
by nielsene
stryderjzw wrote:Hi,

1. How do you guys handle the path "issue"? use DEFINE? Are absolute paths always used? or can we achieve this with relative paths?

2. What do you put in a test_setup.inc file? are there constants in there that define paths?
I just wanted to address these questions directly, in case my "answer by example" left some loose ends.

1. In my application code I did it with some normal variables at the script level because I like being able to interpolate the variables into the strings instead of having to concatenate the constant. In my test code I've switched to using the constant, not for any real reason, but now that I've used it for a while I'm considering changing the application code to also use constants.

In my application code my "header files" (just bunches of includes for different modules of the application) look likeL

Code: Select all

include "database/QueryResult.inc";
include "database/DB.inc";
include "database/CompDB.inc";
include "database/CIB_DB.inc";
include "database/StatsDB.inc";
include "phrasebook/PhraseBook.inc";
include "registry/Registry.inc";
include "printed-formatting/ListFormatter.inc";
include "printed-formatting/CompetitorListFormatter.inc";
include "printed-formatting/CompetitorAffilListFormatter.inc";
// ...
but the application also has a somewhat long include_path setup so I can reach the root of the classes directly.

2. My test_setup.inc, as you can see from the example, sets up the path constants, some database parameters for my sandboxed testing database, includes my custom testing subclasses and all the "normal" simpletest includes needed.

Posted: Fri Aug 19, 2005 12:18 am
by stryderjzw
Thanks! This totally solved my problem. Thanks again.