summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorChristian Fowler <spider@viovio.com>2005-10-12 15:13:51 +0000
committerChristian Fowler <spider@viovio.com>2005-10-12 15:13:51 +0000
commit32506efbcaba41d85c0f41197b92eea224058323 (patch)
tree1775b0f1ccd10ff4c2e924814d104985fca80644 /test
parent7ccbe895a45e54147b1fa67c4c47d84be9c6da2e (diff)
downloadkernel-32506efbcaba41d85c0f41197b92eea224058323.tar.gz
kernel-32506efbcaba41d85c0f41197b92eea224058323.tar.bz2
kernel-32506efbcaba41d85c0f41197b92eea224058323.zip
merge recent changes to HEAD
Diffstat (limited to 'test')
-rw-r--r--test/TestBitCache.php35
-rw-r--r--test/TestBitDatabase.phpbin3080 -> 4506 bytes
-rw-r--r--test/TestBitPreferences.php53
-rw-r--r--test/TestBitPreferencesCache.php65
-rw-r--r--test/TestBitPreferencesCacheDatabase.php89
-rw-r--r--test/TestBitPreferencesDatabase.php103
-rw-r--r--test/TestBitSmartyFilter.php83
-rw-r--r--test/index.php359
8 files changed, 273 insertions, 514 deletions
diff --git a/test/TestBitCache.php b/test/TestBitCache.php
index a20ce1e..cdfe860 100644
--- a/test/TestBitCache.php
+++ b/test/TestBitCache.php
@@ -1,60 +1,61 @@
<?php
require_once('bit_setup_inc.php');
-class TestBitCache extends Test {
+class TestBitCache extends UnitTestCase {
var $test;
var $name ="TestBitCache";
function TestBitCache()
{
- $this->test = new BitCache();
- Assert::equals(is_object($this->test), 'Error during initialisation');
- if (!is_object($this->test)) {
- $this = NULL;
- return;
- }
+ // global $gBitCache;
+ $this->test = new BitCache();
+ ## This test can not be performed in the constructor in simpleTest
+ # $this->assertTrue(is_object($this->test), 'Error during initialisation');
+ if (!is_object($this->test)) {
+ $this = NULL;
+ return;
+ }
}
function testGetNonexistentCachedItem()
{
- Assert::equals($this->test->getCached("TestBitCache"), NULL);
+ $this->assertNull($this->test->getCached("TestBitCache"));
}
function testIsNonexistentItemCached()
{
- Assert::equals($this->test->isCached("TestBitCache"), false);
+ $this->assertFalse($this->test->isCached("TestBitCache"));
}
function testRemoveNonexistentCachedItem()
{
$this->test->removeCached("TestBitCache");
- Assert::equals($this->test->isCached("TestBitCache"), false);
+ $this->assertFalse($this->test->isCached("TestBitCache"));
}
function testSetCachedItem()
{
$this->test->setCached("TestBitCache", "123");
- Assert::equals($this->test->isCached("TestBitCache"), true);
+ $this->assertTrue($this->test->isCached("TestBitCache"));
}
function testGetCachedItem()
{
- Assert::equals($this->test->getCached("TestBitCache"), "123");
+ $this->assertEqual($this->test->getCached("TestBitCache"), "123");
}
function testIsItemCached()
{
- Assert::equals($this->test->isCached("TestBitCache"), true);
+ $this->assertTrue($this->test->isCached("TestBitCache"));
}
function testRemoveCachedItem()
{
$this->test->removeCached("TestBitCache");
- Assert::equals($this->test->isCached("TestBitCache") ||
- $this->test->getCached("TestBitCache") != NULL, false);
+ $this->assertFalse($this->test->isCached("TestBitCache") ||
+ $this->test->getCached("TestBitCache") != NULL);
}
}
-?>
-
+?> \ No newline at end of file
diff --git a/test/TestBitDatabase.php b/test/TestBitDatabase.php
index d55520f..f0d9c04 100644
--- a/test/TestBitDatabase.php
+++ b/test/TestBitDatabase.php
Binary files differ
diff --git a/test/TestBitPreferences.php b/test/TestBitPreferences.php
index e95d7e4..1692b57 100644
--- a/test/TestBitPreferences.php
+++ b/test/TestBitPreferences.php
@@ -1,44 +1,77 @@
<?php
-require_once('bit_setup_inc.php');
-require_once(KERNEL_PKG_PATH.'BitPreferences.php');
+require_once(KERNEL_PKG_PATH . 'BitPreferences.php');
-class TestBitPreferences extends Test {
+class TestBitPreferences extends UnitTestCase {
var $test;
function TestBitPreferences()
{
- $this->test = new BitPreferences("TestBitPreferences", NULL, NULL);
- Assert::equalsTrue($this->test != NULL, 'Error during initialisation');
+ // no general initialization
}
+ function setUp ()
+ {
+ global $gBitDb, $gCache;
+ $tmpDB = $gBitDb;
+ $tmpCache = $gCache;
+ $gBitDb = NULL;
+ $gCache = NULL;
+ $this->test = new BitPreferences("TestBitPreferences");
+ $this->test->mDebug = false;
+ $gBitDb = $tmpDB;
+ $gCache = $tmpCache;
+ // $this->assertNotNull($this->test),
+ // 'Error during initialisation');
+ // This check is probably not needed - it only adds to the passes
+ // and if it is not true it does not save anything anyway.
+ }
+
+
+ function tearDown ()
+ {
+ $this->test = NULL;
+ }
+
function testGetNonexistentItem()
{
- Assert::equals($this->test->getPreference("test"), NULL);
+ $this->assertNull($this->test->getPreference("test"));
}
function testSetNonexistentItem()
{
$this->test->setPreference("test", "123");
- Assert::equals($this->test->getPreference("test"), "123");
+ $this->assertEqual($this->test->getPreference("test"), "123", "");
}
function testSetDefaultItem()
{
+ $this->test->setPreference("test", "123");
$this->test->setDefaultPreference("test", "456");
- Assert::equals($this->test->getPreference("test"), "123");
+ $this->assertEqual($this->test->getPreference("test"), "123");
}
function testSetAsDefaultItem()
{
+ $this->test->setPreference("test", "123");
+ $this->test->setDefaultPreference("test", "456");
$this->test->setPreference("test", "456");
- Assert::equals($this->test->getPreference("test"), "456");
+ $this->assertEqual($this->test->getPreference("test"), "456");
+ }
+
+ function testReadDefaultItem()
+ {
+ $this->test->setPreference("test", "123");
+ $this->test->setDefaultPreference("test", "456");
+ $this->test->setPreference("test", NULL);
+ $this->assertEqual($this->test->getPreference("test"), "456");
}
function testResetItem()
{
+ $this->test->setPreference("test", "123");
$this->test->setPreference("test", NULL);
- Assert::equals($this->test->getPreference("test"), "456");
+ $this->assertNull($this->test->getPreference("test"));
}
}
?>
diff --git a/test/TestBitPreferencesCache.php b/test/TestBitPreferencesCache.php
index eaa62f6..0895c82 100644
--- a/test/TestBitPreferencesCache.php
+++ b/test/TestBitPreferencesCache.php
@@ -1,50 +1,91 @@
<?php
-require_once('bit_setup_inc.php');
require_once(KERNEL_PKG_PATH.'BitPreferences.php');
+require_once(KERNEL_PKG_PATH.'test/TestBitPreferences.php');
+# class TestBitPreferencesCache extends UnitTestCase {
-class TestBitPreferencesCache extends Test {
+class TestBitPreferencesCache extends TestBitPreferences {
+
+ function setUp ()
+ {
+ global $gBitDb, $gCache;
+ $tmpDB = $gBitDb;
+ $tmpCache = $gCache;
+ $gBitDb = NULL;
+ $gCache = NULL;
+ $this->test = new BitPreferences("TestBitPreferencesCache");
+ $this->test->mDebug = false;
+ $gBitDb = $tmpDB;
+ $gCache = $tmpCache;
+ // $this->assertNotNull($this->test),
+ // 'Error during initialisation');
+ // This check is probably not needed - it only adds to the passes
+ // and if it is not true it does not save anything anyway.
+ }
+
+ function tearDown ()
+ {
+ $this->test = NULL;
+ }
+
+
+ /*
+
+ We are using inheritance instead
var $name = "TestBitPreferencesCache";
function initBitPreferences()
{
- $test = new BitPreferences($this->name, new BitCache(), NULL);
+ global $gBitDb;
+ $tmpDB = $gBitDb;
+ $gBitDb = NULL;
+ $test = new BitPreferences($this->name);
+ $gBitDb = $tmpDB;
return $test;
}
function TestBitPreferencesCache()
{
+ #global $gCache;
+ #if (!is_object($gCache)) {
+ # print "We have LANDED <br />\n";
+ # $this = NULL;
+ # return;
+ #}
$test = $this->initBitPreferences();
- Assert::equalsTrue($test != NULL, 'Error during initialisation');
+ // This check is probably not needed - it only adds to the passes
+ // and if it is not true it does not save anything anyway.
+
+ // Assert::equals($test != NULL, 'Error during initialisation');
}
function testGetNonexistentItem()
{
$test = $this->initBitPreferences();
- Assert::equals($test->getPreference("test"), NULL);
+ $this->assertNull($test->getPreference("test"));
}
function testSetNonexistentItem()
{
$test = $this->initBitPreferences();
$test->setPreference("test", "123");
- Assert::equals($test->getPreference("test"), "123");
+ $this->assertEqual($test->getPreference("test"), "123");
}
-
+
function testSetDefaultItem()
{
$test = $this->initBitPreferences();
$test->setDefaultPreference("test", "456");
- Assert::equals($test->getPreference("test"), "123");
+ $this->assertEqual($test->getPreference("test"), "123");
}
-
+
function testSetAsDefaultItem()
{
$test = $this->initBitPreferences();
$test->setDefaultPreference("test", "456");
$test->setPreference("test", "456");
- Assert::equals($test->getPreference("test"), "456");
+ $this->assertEqual($test->getPreference("test"), "456");
}
function testResetItem()
@@ -52,7 +93,9 @@ class TestBitPreferencesCache extends Test {
$test = $this->initBitPreferences();
$test->setDefaultPreference("test", "456");
$test->setPreference("test", NULL);
- Assert::equals($test->getPreference("test"), "456");
+ $this->assertEqual($test->getPreference("test"), "456");
}
+ */
+
}
?>
diff --git a/test/TestBitPreferencesCacheDatabase.php b/test/TestBitPreferencesCacheDatabase.php
index a197cf1..9d949a4 100644
--- a/test/TestBitPreferencesCacheDatabase.php
+++ b/test/TestBitPreferencesCacheDatabase.php
@@ -1,75 +1,92 @@
<?php
-require_once('bit_setup_inc.php');
require_once(KERNEL_PKG_PATH.'BitPreferences.php');
+#require_once(KERNEL_PKG_PATH.'test/TestBitPreferences.php');
-class TestBitPreferencesCacheDatabase extends Test {
+class TestBitPreferencesCacheDatabase extends UnitTestCase {
+ //class TestBitPreferencesCacheDatabase extends TestBitPreferences {
var $name = "TestBitPreferencesCacheDatabase";
-
- function initBitPreferences()
+ var $test;
+
+ function TestBitPreferencesCacheDatabase()
{
- $test = new BitPreferences($this->name, new BitCache(), new BitDb());
- return $test;
}
-
- function TestBitPreferencesCacheDatabase()
+
+ function setUp ()
{
global $gBitDb, $gBitCache;
$gBitCache = new BitCache();
if (!is_object($gBitDb) || !is_object($gBitCache)) {
- $this = NULL;
- return;
+ $this = NULL;
+ return;
}
$name = "`".$this->name."`";
if (!$gBitDb->tableExists($name)) {
- $tables = array(
- $name => "
+ $tables = array(
+ $name => "
`name` C(50) PRIMARY,
`value` C(255)
");
- $gBitDb->createTables($tables);
- }
- $test = $this->initBitPreferences();
- Assert::equalsTrue($test != NULL, 'Error during initialisation');
+ $gBitDb->createTables($tables);
+ }
+ $this->test = new BitPreferences($this->name);
+ // This test can not be performed in the constructor in simpleTest
+ $this->assertTrue($this->test != NULL, 'Error during initialisation');
}
-
+
+
+ function tearDown ()
+ {
+ global $gBitDb;
+ $name = "`".$this->name."`";
+ $tables = array ($name);
+ $gBitDb->dropTables($tables);
+ $this->test = NULL;
+ }
+
+
+ // Tests duplicated from TestBitPreferences
+ // Could not inherit them like in TestBitPreferencesCache for some reason.
function testGetNonexistentItem()
{
- $test = $this->initBitPreferences();
- Assert::equals($test->getPreference("test"), NULL);
+ $this->assertNull($this->test->getPreference("test"));
}
function testSetNonexistentItem()
{
- $test = $this->initBitPreferences();
- $test->setPreference("test", "123");
- Assert::equals($test->getPreference("test"), "123");
+ $this->test->setPreference("test", "123");
+ $this->assertEqual($this->test->getPreference("test"), "123", "");
}
function testSetDefaultItem()
{
- $test = $this->initBitPreferences();
- //$test->mDebug = true;
- $test->setDefaultPreference("test", "456");
- Assert::equals($test->getPreference("test"), "123");
+ $this->test->setPreference("test", "123");
+ $this->test->setDefaultPreference("test", "456");
+ $this->assertEqual($this->test->getPreference("test"), "123");
}
function testSetAsDefaultItem()
{
- $test = $this->initBitPreferences();
- //$test->mDebug = true;
- $test->setDefaultPreference("test", "456");
- $test->setPreference("test", "456");
- Assert::equals($test->getPreference("test"), "456");
+ $this->test->setPreference("test", "123");
+ $this->test->setDefaultPreference("test", "456");
+ $this->test->setPreference("test", "456");
+ $this->assertEqual($this->test->getPreference("test"), "456");
}
+ function testReadDefaultItem()
+ {
+ $this->test->setPreference("test", "123");
+ $this->test->setDefaultPreference("test", "456");
+ $this->test->setPreference("test", NULL);
+ $this->assertEqual($this->test->getPreference("test"), "456");
+ }
+
function testResetItem()
{
- $test = $this->initBitPreferences();
- $test->setDefaultPreference("test", "456");
- $test->setPreference("test", NULL);
- Assert::equals($test->getPreference("test"), "456");
+ $this->test->setPreference("test", "123");
+ $this->test->setPreference("test", NULL);
+ $this->assertNull($this->test->getPreference("test"));
}
}
-?>
+?> \ No newline at end of file
diff --git a/test/TestBitPreferencesDatabase.php b/test/TestBitPreferencesDatabase.php
index 88dca1e..879403e 100644
--- a/test/TestBitPreferencesDatabase.php
+++ b/test/TestBitPreferencesDatabase.php
@@ -1,78 +1,93 @@
<?php
-require_once('bit_setup_inc.php');
-require_once(KERNEL_PKG_PATH.'BitPreferences.php');
-
-class TestBitPreferencesDatabase extends Test {
+require_once(KERNEL_PKG_PATH . 'BitPreferences.php');
+# require_once(KERNEL_PKG_PATH . 'test/TestBitPreferences.php');
+
+class TestBitPreferencesDatabase extends UnitTestCase {
+ // class TestBitPreferencesDatabase extends TestBitPreferences {
+
var $name = "TestBitPreferencesDatabase";
+ var $test;
- function initBitPreferences()
+ function TestBitPreferencesDatabase()
{
- $test = new BitPreferences($this->name, NULL, new BitDb());
- return $test;
+ // No general initialization
}
-
- function TestBitPreferencesDatabase()
+
+ function setUp ()
{
global $gBitDb;
if (!is_object($gBitDb)) {
- $this = NULL;
- return;
+ $this = NULL;
+ return;
}
- $name = "`".$this->name."`";
+ $name = "`" . $this->name . "`";
if (!$gBitDb->tableExists($name)) {
- $tables = array(
- $name => "
+ $tables = array($name => "
`name` C(50) PRIMARY,
`value` C(255)
");
- $gBitDb->createTables($tables);
- }
- $test = $this->initBitPreferences();
- Assert::equalsTrue($test != NULL, 'Error during initialisation');
-
+ }
+ global $gCache;
+ $tmpCache = $gCache;
+ $gCache = NULL;
+ $this->test = new BitPreferences($this->name);
+ $gCache = $tmpCache;
}
-
+
+
+ function tearDown ()
+ {
+ global $gBitDb;
+ $name = "`".$this->name."`";
+ $tables = array ($name);
+ $gBitDb->dropTables($tables);
+ $this->test = NULL;
+ }
+
+
+
+ // Tests duplicated from TestBitPreferences
+ // Could not inherit them like in TestBitPreferencesCache for some reason.
function testGetNonexistentItem()
{
- $test = $this->initBitPreferences();
- Assert::equals($test->getPreference("test"), NULL);
+ $this->assertNull($this->test->getPreference("test"));
}
function testSetNonexistentItem()
{
- $test = $this->initBitPreferences();
- //$test->mDebug = true;
- $test->setPreference("test", "123");
- Assert::equals($test->getPreference("test"), "123");
+ $this->test->setPreference("test", "123");
+ $this->assertEqual($this->test->getPreference("test"), "123", "");
}
-
+
function testSetDefaultItem()
{
- $test = $this->initBitPreferences();
- //$test->mDebug = true;
- $test->setDefaultPreference("test", "456");
- Assert::equals($test->getPreference("test"), "123");
+ $this->test->setPreference("test", "123");
+ $this->test->setDefaultPreference("test", "456");
+ $this->assertEqual($this->test->getPreference("test"), "123");
}
function testSetAsDefaultItem()
{
- $test = $this->initBitPreferences();
- //$test->mDebug = true;
- $test->setDefaultPreference("test", "456");
- $test->setPreference("test", "456");
- Assert::equals($test->getPreference("test"), "456");
+ $this->test->setPreference("test", "123");
+ $this->test->setDefaultPreference("test", "456");
+ $this->test->setPreference("test", "456");
+ $this->assertEqual($this->test->getPreference("test"), "456");
}
+ function testReadDefaultItem()
+ {
+ $this->test->setPreference("test", "123");
+ $this->test->setDefaultPreference("test", "456");
+ $this->test->setPreference("test", NULL);
+ $this->assertEqual($this->test->getPreference("test"), "456");
+ }
+
function testResetItem()
{
- global $gBitDb;
- $tables = array("`".$this->name."`");
- $test = $this->initBitPreferences();
- $test->setDefaultPreference("test", "456");
- $test->setPreference("test", NULL);
- Assert::equals($test->getPreference("test"), "456");
- $gBitDb->dropTables($tables);
+ $this->test->setPreference("test", "123");
+ $this->test->setPreference("test", NULL);
+ $this->assertNull($this->test->getPreference("test"));
}
}
-?>
+?> \ No newline at end of file
diff --git a/test/TestBitSmartyFilter.php b/test/TestBitSmartyFilter.php
index 0bd5ef4..e2402a0 100644
--- a/test/TestBitSmartyFilter.php
+++ b/test/TestBitSmartyFilter.php
@@ -1,23 +1,22 @@
<?php
-require_once('bit_setup_inc.php');
-// require_once(KERNEL_PKG_PATH.'BitPreferences.php');
-
-
+// For future use - we should convert the input output tester
+// to simple test extension.
class InputOutputTester {
}
-class TestBitSmartyFilter extends Test {
+
+class TestBitSmartyFilter extends UnitTestCase {
var $filterTestDir;
var $smartyDir;
-
+
function TestBitSmartyFilter ()
{
// directory that contains test directories
- $this->filterTestDir = 'smarty_filter_tests';
+ $this->filterTestDir = KERNEL_PKG_PATH . '/test/smarty_filter_tests';
$this->smartyDir = KERNEL_PKG_PATH . 'smarty_bit';
}
@@ -25,36 +24,36 @@ class TestBitSmartyFilter extends Test {
{
global $gBitLanguage;
$gBitLanguage->mLanguage = 'sv';
-
- Assert::assert(is_dir("$this->filterTestDir"), // Quite fatal
- "$this->filterTestDir is not a directory");
-
+
+ $this->assertTrue (is_dir("$this->filterTestDir"), // Quite fatal
+ "$this->filterTestDir is not a directory");
+
// echo "$this->filterTestDir<br />";
-
+
$filterTestDirHandle = opendir($this->filterTestDir);
while (false !== ($filterTestCase = readdir($filterTestDirHandle))) {
$filterTestCaseDir = $this->filterTestDir . '/' . $filterTestCase;
- Assert::assert(is_dir ($filterTestCaseDir),
- "$filterTestCaseDir is not a directory");
-
+ $this->assertTrue(is_dir ($filterTestCaseDir),
+ "$filterTestCaseDir is not a directory");
+
// echo "$filterTestCaseDir<br />";
if (preg_match('/(.+)filter\.(.+)/', $filterTestCase, $matches)) {
// echo "$matches[0]<br />";
-
+
$filterType = $matches[1];
$filterBase = $matches[2];
-
+
$smartyFile =
$this->smartyDir . '/' . $filterType .'filter.' . $filterBase . '.php';
$filterName = 'smarty_' . $filterType . 'filter_' .$filterBase;
-
+
// echo "$smartyFile<br />";
// echo "$filterName<br />";
-
- Assert::equalsTrue(file_exists ($smartyFile),
- "Smarty filter $smartyFile is missing");
+
+ $this->assertTrue(file_exists ($smartyFile),
+ "Smarty filter $smartyFile is missing");
include_once($smartyFile);
-
+
// echo "$filterTestCaseDir<br />";
$filterTestCaseDirHandle = opendir("$filterTestCaseDir");
while (false != ($inputFile = readdir($filterTestCaseDirHandle))) {
@@ -63,34 +62,38 @@ class TestBitSmartyFilter extends Test {
$outputFile = $filterTestCaseDir . '/' . $baseName . '.output';
$errorFile = $filterTestCaseDir . '/' . $baseName . '.error';
$inputFile = $filterTestCaseDir . '/' . $inputFile;
-
-
+
+
// echo "$inputFile<br />";
// echo "$outputFile<br />";
// echo "$errorFile<br />";
-
- Assert::equalsTrue(file_exists ($inputFile),
+
+ $this->assertTrue(file_exists ($inputFile),
+ (file_exists ($inputFile) ?
+ "" :
"Input file <strong>$inputFile</strong> " .
- "is missing");
-
+ "is missing"));
+
// remove error file if there is no error
-
+
if (file_exists($errorFile)) {
@unlink ($errorFile);
}
-
+
$input = file_get_contents($inputFile);
// $filterOutput = call_user_func ($filterName, $input, &$smarty);
$filterOutput = call_user_func_array ($filterName,
array($input, &$gBitSmarty));
-
+
if (!file_exists($outputFile)) {
// Output file does not exist - Create error file
// echo "OUTPUT does not Exists<br />";
- Assert::equalsTrue(file_exists ($outputFile),
+ $this->assertTrue(file_exists ($outputFile),
+ (file_exists ($outputFile) ?
+ "" :
"Output file <strong>$outputFile</strong> " .
"is missing, " .
- "<strong>$errorFile</strong> created.");
+ "<strong>$errorFile</strong> created."));
// Error handling missing when writing to file,
// final code should be encapsulated in a function.
@@ -105,10 +108,16 @@ class TestBitSmartyFilter extends Test {
// echo "OUTPUT Exists<br />";
$output = file_get_contents($outputFile);
$compareResult = strcmp ($output, $filterOutput);
- Assert::equals(0, $compareResult,
- "<strong>$inputFile</strong> did not match " .
- "output, incorrect data stored in " .
- "<strong>$errorFile</strong>");
+ // print "$output <br />\n";
+ // print "$filterOutput <br />\n";
+ // print "$compareResult <br \>\n";
+
+ $this->assertTrue (0 == $compareResult,
+ (0 == $compareResult ?
+ "" :
+ "<strong>$inputFile</strong> did not match " .
+ "output, incorrect data stored in " .
+ "<strong>$errorFile</strong>"));
if (0 != $compareResult) {
$outHandle = fopen ($errorFile, 'wb');
fwrite ($outHandle, $filterOutput, strlen ($filterOutput));
diff --git a/test/index.php b/test/index.php
deleted file mode 100644
index cd36aa4..0000000
--- a/test/index.php
+++ /dev/null
@@ -1,359 +0,0 @@
-<?php
-if (!defined('PHP_UNIT_INCLUDED'))
-{
-
- define('PHP_UNIT_INCLUDED', true);
-
-function errorHandler($errno, $errstr, $errfile, $errline)
-{
- global $ERROR_FOUND, $ERROR_CRITICAL;
- switch($errno)
- {
- case E_USER_ERROR:
- echo "Fatal error: $errstr<br>";
- echo "Skipping remaining tests for this unit...";
- $ERROR_FOUND = true;
- $ERROR_CRITICAL = true;
- break;
- case E_USER_WARNING:
- echo $errstr . '<br>';
- $ERROR_FOUND = true;
- break;
- case E_WARNING:
- echo "PHP WARNING on line <b>$errline</b> of file <b>$errfile</b>:";
- echo "$errstr<br>";
- break;
- case E_NOTICE:
- echo "PHP NOTICE on line <b>$errline</b> of file <b>$errfile</b>:";
- echo "$errstr<br>";
- break;
- case E_ERROR:
- echo "PHP ERROR on line <b>$errline</b> of file <b>$errfile</b>";
- echo "<br><b>Aborting...</b>";
- exit -1;
- break;
- default:
- echo "PHP Unkown error $errno: $errstr<br>\n";
- break;
- }
-}
-
-class Assert
-{
- function assert($bool, $message = '')
- {
- if (!$bool)
- {
- if ($message == '')
- {
- $message = "Assertion failed.";
- }
- trigger_error($message, E_USER_ERROR);
- }
- }
-
- function equals($value1, $value2, $message = '')
- {
- if ($value1 != $value2)
- {
- if ($message == '')
- {
- $message = "Assertion failed: <b>'$value1' != '$value2'</b>";
- }
- trigger_error($message, E_USER_WARNING);
- }
- }
-
- function equalsTrue($bool, $message = '')
- {
- Assert::equals($bool, true, $message);
- }
-
- function equalsFalse($bool, $message = '')
- {
- Assert::equals($bool, false, $message);
- }
-}
-
-class Test
-{
- /***
- * Call all methods starting with 'test'. $class is the name of the class.
- * Although not strictly necessary, it makes the output look better
- * (properly capitalized).
- ***/
- function run($class)
- {
- global $ERROR_FOUND, $ERROR_CRITICAL, $TESTS_COMPLETED,
- $TESTS_SKIPPED, $TESTS_TOTAL; // Yikes!
- if (strlen($class) > 4 && strtolower(substr($class, -4)) == 'test')
- {
- $class = substr($class, 0, strlen($class) - 4);
- }
- echo " <tr>\n";
- echo " <th colspan=2><b>$class</b></th>\n";
- echo " </tr>\n";
- $ERROR_CRITICAL = false;
- $flipCss = false;
- $methods = get_class_methods($this);
- foreach ($methods as $method) {
- // Don't continue running tests if something really bad happened.
- // That is, if Assert::assert evaluated to true.
- if ($ERROR_CRITICAL) {
- break;
- }
- // Run a test if this method applies
- if (strlen($method) > 4 && substr($method, 0, 4) == 'test') {
- $TESTS_TOTAL++;
- $ERROR_FOUND = false;
- $css = $flipCss ? 'dark' : 'light';
- $flipCss = !$flipCss;
- echo " <tr>\n";
- echo " <td class=$css width=50 nowrap valign=top>";
- echo substr($method, 4), "</td>\n";
- echo " <td class=$css valign=top>";
- $this->$method();
- if (!$ERROR_FOUND) {
- $TESTS_COMPLETED++;
- echo "<b>OK</b>";
- }
- echo "</td>\n";
- echo " </tr>\n";
- }
- }
- }
-}
-
-if (isset($GET['directory'])) {
- $directory = dirname($GET['directory']);
-} elseif (isset($_SERVER['PATH_TRANSLATED'])) {
- $directory = dirname($_SERVER['PATH_TRANSLATED']);
-} elseif (isset($_SERVER['SCRIPT_FILENAME'])) {
- $directory = dirname($_SERVER['SCRIPT_FILENAME']);
-}
-
-$dirValid = ($directory != '' && is_dir($directory));
-
-if ($dirValid)
-{
- $title = ': Results';
-}
-else
-{
- $title = ': Configuration';
-}
-?>
-<html>
- <head>
- <title>PHP Unit Tester<?php echo $title ?></title>
- <style>
- body {
- background-color: rgb(95%, 95%, 100%);
- font-family: Verdana, Arial, Helvetica, sans-serif;
- margin-left: 10%;
- margin-right: 10%;
- }
-
- h1, h2 {
- text-align: center;
- }
-
- th {
- background-color: rgb(0%, 0%, 0%);
- color: white;
- font-size: 15px;
- font-weight: bold;
- }
-
- td {
- padding: 2px 10px 2px 10px;
- font-size: 13px;
- }
-
- table {
- border: 1px solid black;
- }
-
- td.dark {
- background-color: rgb(80%, 80%, 90%);
- }
-
- td.light {
- background-color: rgb(90%, 90%, 100%);
- }
-
- a:link {
- text-decoration: none;
- color: rgb(0%, 0%, 60%);
- font-weight: bold;
- }
-
- a:hover {
- text-decoration: underline;
- color: rgb(0%, 0%, 70%);
- }
-
- a:visited {
- color: rgb(0%, 0%, 80%);
- }
- </style>
- </head>
-
- <body>
- <h1>PHP Unit Tester<?php echo $title ?></h1>
-<?php
-if ($dirValid)
-{
- $errorHandler = set_error_handler("errorHandler");
- error_reporting (E_ALL);
- echo " <table align=center border=0 cellpadding=0 cellspacing=0 ";
- echo "width=100%>\n";
- $TESTS_TOTAL = 0;
- $TESTS_COMPLETED = 0;
- $handle = opendir($directory);
- while (($file = readdir($handle)) !== false)
- {
- if (strlen($file) > 5 && substr($file, -3) == 'php' && $file != 'bit_setup_inc.php')
- {
- include_once($directory . '/' . $file);
- $class = substr($file, 0, strlen($file) - 4);
- if (class_exists($class))
- {
- $test = new $class;
- if (is_subclass_of($test, 'Test'))
- {
- $test->run($class);
- }
- }
- }
- }
- closedir($handle);
- set_error_handler("errorHandler");
- echo " </table>\n";
- echo " <p>Successfully executed <b>$TESTS_COMPLETED</b> of ";
- echo "<b>$TESTS_TOTAL</b> tests</p>\n";
-}
-else
-{
-?>
- <h2>Set the Test Directory</h2>
- <form>
- <b>Directory</b>: <input type=text name="directory" value="<?php echo $directory ?>"
- size=30> <input type=submit value="Run Tests">
- </form>
- <p>
- <b>Tip</b>: After setting the correct directory and clicking <b>Run Tests</b>, save
- a bookmark to the resulting page. Opening that bookmark will immediately re-run all
- tests.
- </p>
-
-<?php
-} // !$dirValid
-?>
- <h2>About the PHP Unit Tester</h2>
- <p>
- This small application can be used to automatically test PHP units. Although
- (very!) distantly related to <a href="http://www.junit.org">jUnit</a>, it is by no
- means as advanced as that particular package, nor is it intended to be. By
- the way, if you're not into Object-Oriented Programming, haven't heard of
- <i>Extreme Programming</i>, or the term <i>Refactoring</i> means nothing to you,
- this application is probably not for you.
- </p>
- <p>
- How does it work? Well, first put this file somewhere on your development
- server. Then open it in your favorite browser (through the server), type in the
- local path to the test units (e.g. <b>/data/www/tests</b>), and press
- <b>Run Tests</b>. That's all!
- </p>
- <p>
- Once a valid directory has been set, this application reads all PHP-files
- (<b>*.php</b>) in that directory, and runs the tests in them. Thus, to add a
- test to your suite, simply put it in mentioned directory.
- </p>
- <p>
- Every test must be implemented in a class with the same name as the file it
- is in. Thus, class <b>FileTest</b> should be in <b>FileTest.php</b>. Also,
- the class must be a subclass of class <b>Test</b>. There's no need to include
- this class, as this application takes care of that.
- </p>
- <p>
- A basic test-class looks as follows (note, class <b>File</b> doesn't actually exist):
- </p>
- <pre><code> require_once('File.php'); // This is the class we're testing
-
- class FileTest extends Test
- {
- var $file;
-
- function FileTest()
- {
- unset($this->file);
- $this->file = new File();
- }
-
- function testCreate()
- {
- Assert::assert(isset($this->file), "File object could not be instantiated");
- }
-
- function testOpen()
- {
- $this->file->open('test.dat');
- Assert::equalsTrue($this->file->isOpened(), "File couldn't be opened");
- }
-
- function testReadChar()
- {
- $char = '0';
- $char = $this->file->readChar();
- Assert::equals('a', $char);
- }
-
- function testClose()
- {
- $this->file->close();
- Assert::equalsFalse($this->file->isOpened(), "File couldn't be closed");
- }
- }</code></pre>
- <p>
- In short, the rules are as follows:
- </p>
- <ul>
- <li>There's no need to call the parent constructor; it doesn't even exist.</li>
- <li>A method that executes a test should have a name starting with <b>test</b>.</li>
- <li>There should be no method with the name <b>run</b>. This is the <i>only</i>
- method defined in class <b>Test</b>, and it isn't meant to be overridden.</li>
- <li>Test methods are called in the order they are defined in the class. This allows
- you - for example - to set up some variable in one test-method that can be
- reused in a second.</li>
- <li><b>Assert:assert</b> expects a boolean and an optional message string. If the
- boolean evaluates to <b>false</b>, the test for that particular unit is
- stopped, and the application continues with the next unit (if it exists).</li>
- <li><b>Assert::equals</b> expects two values and an optional message. If the two
- values aren't equal an error is printed, but all other tests for the unit will
- still be executed.</li>
- <li><b>Assert::equalsTrue</b> expects a value and an optional message. Its behavior
- is like <b>Assert::equals</b>, with one of the values set to <b>true</b>.</li>
- <li><b>Assert::equalsFalse</b> is like <b>Assert::equalsTrue</b>, with <b>false</b>
- instead of <b>true</b>. (What a surprise...)
- </ul>
- <p>
- As mentioned earlier, it's all very simple. At the same time I find it very useful.
- If you want the application to do more, you are free to modify this code for your
- own personal needs.
- </p>
- <p>
- Final note: the code for this application is by no means representative for the
- code I normally write! I hacked this thing together in about half an hour, so there
- there you go. (Normally, I would <i>never</i> use global variables! I swear!)
- </p>
- <p>
- Vincent Oostindi&euml;,
- <a href="mailto:vincent@sunlight.tmfweb.nl">vincent@sunlight.tmfweb.nl</a>,
- March 2002.
- </p>
- </body>
-</html>
-<?php
-} // !defined('PHP_UNIT_INCLUDED')
-?>