Testing with PHPUnit
This instructions are based on my setup using netbeans, I believe that will not change the final result.
To view the steps to install phpunit support in netbeans you can follow:
http://netbeans.org/kb/docs/php/phpunit.html
To setup your netbeans to use phpunit, you can also follow the instructions on this link:
http://blogs.sun.com/netbeansphp/entry/recent_improvements_in_phpunit_support
Creating a test:
Select a class
Right click on it
Select Tools > Create PHP Unit test
If this is the first time you do it a window will popup asking you to Select a directory for 'project' test files:
On Test Sources: Click on browse
At the project root level create a new folder called tests
Click Ok
A message will indicate that PHPUnit classes should be available on Global Include Path, if you follow any of the first 2 links this is already done, otherwise add the path now.
Now you should be able to see 2 directories under your project
Source files
Test files
Previous versions of Tequila
If you are using older versions of tequila just modify your main file to load c_square conditionally:
In main.php
if(!isset($config['unit_test'])) include($tequilapath."c_square.php");
Add that setting in config.php
$config['unit_test'] = true;
Adding configuration, bootstrap and _autoloading
Tequila source files need to be loaded so all functionality is available, you will need to download or create 3 files to achieve this
bootstrap.php - Load Tequila resources
configuration.xml - PHPUnit options, here we set the bootstrapping file
test_loader.php - Replaces c_square class loading *
* In the last test I made global vars were lost with any 'backupGlobals' setting, causing the library to fail to load classes
Download the attached .zip file and open it on the directory you created before (tests)
Setting the configuration and bootstrap files
Right click on the folder 'Test files' (Project > Test Files)
Select Properties
The tab PHPUnit is selected by default
Select Use Bootstrap
Browse to the file you downloaded (in the project_root/tests/bootstrap.php)
Select Use XML Configuratiom
Browse to the file you downloaded (in the project_root/tests/configuration.xml)
Click OK
Optionally you can create the files with generate and the replace the content.
Content of files:
bootstrap.php
<?php
/**
* @author itzco
*/
// 1 Add project files
ini_set("include_path",ini_get("include_path").PATH_SEPARATOR. "..");
include("includes/config.php");
// 2 Set tequila path properly (For relative paths, we are one level deeper)
$tequilapath ="../$tequilapath";
ini_set("include_path",ini_get("include_path").PATH_SEPARATOR. $tequilapath);
// This should display a path otherwise is incorrect
//echo "\n 1. New Tequila path $tequilapath ::" . realpath($tequilapath);
// 3 Set __autoloader function (c_square not used)
include('test_loader.php');
define('CACHE_FILE_NAME', '../temp/cache.inc.php');
define('TEQUILA_PATH',$tequilapath);
// 4 Let know main.php that we're testing (stop loading c_square.php)
$config['unit_test'] = true;
// 5 Include main.php
include("main.php");
?>
configuration.xml
<?xml version="1.0"?>
<phpunit bootstrap='bootstrap.php' colors="false" backupGlobals="true" />
test_loader.php
<?php
/**
* The class used by tequila c_square caches values at a global level, this values are currently lost in phpunit
* (Have tried both backupGlobals="true"/"false")
* When c_square works this function can be removed:
* as well as the code in main:22
* if(!isset($config['unit_test'])) include($tequilapath."c_square.php");
* Change to :
* include($tequilapath."c_square.php");
*
* @global string $locatordirectory
* @param string> $classname
* @return void
*/
function __autoload($classname) {
global $locatordirectory;
require_once(CACHE_FILE_NAME);
if(isset($cache[$classname])) {
if(file_exists($cache[$classname])) {
$locatordirectory = false;
require_once($cache[$classname]);
return;
}
}
// Be sure this matches the var definition in c_square (If u changed it)
$filenameTemplates = array("@CLASS@.php", "@CLASS@.class.php", "@CLASS@.inc", "@CLASS@.inc.php", "class.@CLASS@.inc.php", "class.@CLASS@.php", "i@CLASS@.php", "@CLASS@.interface.php");
$possiblefilenames = str_replace("@CLASS@", $classname, $filenameTemplates);
if ($locatordirectory){
$file = findfile($possiblefilenames,$locatordirectory);
$locatordirectory = false;
} else {
$file = findfile($possiblefilenames);
}
// ICD: FIX FOR 1 SOURCE ONLY ::This work inverse as c_square (loading order)
if (!$file){
$file = findfile($possiblefilenames,TEQUILA_PATH);
}
if($file) {
// This function does not update the cache file! You should run the application normally
if (!class_exists($classname))
{
require_once($file); // include the found file
return;
}
}
if (!class_exists($classname))
{
die("\n CANNOT FIND $classname class doesn't exist \n");
}
}
function findFile($file) {
// Normalizing the first parameter
if(!is_array($file)) {
$file = array($file);
}
// Checking for the second, the "hidden" parameter
// Used for recursion-reasons
if(func_num_args() > 1) {
$startDir = func_get_arg(1);
}
else {
$startDir = dirname(__FILE__);
}
$subDirectories = array();
$dirIt = new DirectoryIterator($startDir);
foreach($dirIt as $dirItem) {
if($dirItem->isDot()) {
continue;
}
if($dirItem->isDir()) {
$subDirectories[] = $dirItem->getPathname();
continue;
}
if(in_array($dirItem->getFilename(), $file)) {
return $dirItem->getPathname();
}
}
foreach($subDirectories as $directory) {
$result = findfile($file, $directory);
if($result) {
return $result;
}
}
return false;
}
?>
This files are also available in tequila mercurial repository /appbeta