From 929d93223471e4c9dce5a53f9b71d33c27daf2bc Mon Sep 17 00:00:00 2001 From: "Edward Z. Yang" Date: Sat, 16 Feb 2008 05:40:59 +0000 Subject: [PATCH] [3.1.0] Implement a few phpt, fix some autoload bugs - Make our autoload handler polite, ensuring that any __autoload() functions get added - Modify phpt calling code so that each phpt files gets its own test-case (this lets us run one phpt file at a time) - Implement phpt for loading, which test varying loading methods of HTML Purifier - Add --disable-phpt and --only-phpt flags - More descriptive veto messages, also fix test count git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1552 48356398-32a2-884e-a903-53898d9a118a --- library/HTMLPurifier.autoload.php | 4 +++ .../PHPT/loading/auto-includes.phpt | 11 ++++++++ .../PHPT/loading/auto-with-autoload.phpt | 25 +++++++++++++++++++ .../PHPT/loading/auto-with-spl-autoload.phpt | 25 +++++++++++++++++++ tests/HTMLPurifier/PHPT/loading/auto.phpt | 10 ++++++++ .../PHPT/{Smoketest.phpt => stub.phpt} | 0 tests/PHPT/Reporter/SimpleTest.php | 13 ++++++---- tests/common.php | 16 ++++++------ tests/index.php | 17 +++++++++++-- tests/test_files.php | 17 ++++++++++++- 10 files changed, 123 insertions(+), 15 deletions(-) create mode 100644 tests/HTMLPurifier/PHPT/loading/auto-includes.phpt create mode 100644 tests/HTMLPurifier/PHPT/loading/auto-with-autoload.phpt create mode 100644 tests/HTMLPurifier/PHPT/loading/auto-with-spl-autoload.phpt create mode 100644 tests/HTMLPurifier/PHPT/loading/auto.phpt rename tests/HTMLPurifier/PHPT/{Smoketest.phpt => stub.phpt} (100%) diff --git a/library/HTMLPurifier.autoload.php b/library/HTMLPurifier.autoload.php index 8a6b90c7..568463ff 100644 --- a/library/HTMLPurifier.autoload.php +++ b/library/HTMLPurifier.autoload.php @@ -2,6 +2,10 @@ if (function_exists('spl_autoload_register')) { spl_autoload_register(array('HTMLPurifier_Bootstrap', 'autoload')); + if (function_exists('__autoload')) { + // be polite and ensure that userland autoload gets retained + spl_autoload_register('__autoload'); + } } elseif (!function_exists('__autoload')) { function __autoload($class) {return HTMLPurifier_Bootstrap::autoload($class);} } diff --git a/tests/HTMLPurifier/PHPT/loading/auto-includes.phpt b/tests/HTMLPurifier/PHPT/loading/auto-includes.phpt new file mode 100644 index 00000000..707acc32 --- /dev/null +++ b/tests/HTMLPurifier/PHPT/loading/auto-includes.phpt @@ -0,0 +1,11 @@ +--TEST-- +HTMLPurifier.auto.php and HTMLPurifier.includes.php loading test +--FILE-- +purify('Salsa!'); +--EXPECT-- +Salsa! diff --git a/tests/HTMLPurifier/PHPT/loading/auto-with-autoload.phpt b/tests/HTMLPurifier/PHPT/loading/auto-with-autoload.phpt new file mode 100644 index 00000000..3645d367 --- /dev/null +++ b/tests/HTMLPurifier/PHPT/loading/auto-with-autoload.phpt @@ -0,0 +1,25 @@ +--TEST-- +HTMLPurifier.auto.php using spl_autoload_register with __autoload() already defined loading test +--SKIPIF-- +purify('Salsa!') . PHP_EOL; + +// purposely invoke older autoload +$bar = new Bar(); + +--EXPECT-- +Salsa! +Autoloading Bar... diff --git a/tests/HTMLPurifier/PHPT/loading/auto-with-spl-autoload.phpt b/tests/HTMLPurifier/PHPT/loading/auto-with-spl-autoload.phpt new file mode 100644 index 00000000..664e09b4 --- /dev/null +++ b/tests/HTMLPurifier/PHPT/loading/auto-with-spl-autoload.phpt @@ -0,0 +1,25 @@ +--TEST-- +HTMLPurifier.auto.php using spl_autoload_register with userland spl_autoload registration loading test +--SKIPIF-- +purify('Salsa!') . PHP_EOL; + +// purposely invoke older autoload +$bar = new Bar(); + +--EXPECT-- +Salsa! +Autoloading Bar... diff --git a/tests/HTMLPurifier/PHPT/loading/auto.phpt b/tests/HTMLPurifier/PHPT/loading/auto.phpt new file mode 100644 index 00000000..e342f6ad --- /dev/null +++ b/tests/HTMLPurifier/PHPT/loading/auto.phpt @@ -0,0 +1,10 @@ +--TEST-- +HTMLPurifier.auto.php loading test +--FILE-- +purify('Salsa!'); +--EXPECT-- +Salsa! diff --git a/tests/HTMLPurifier/PHPT/Smoketest.phpt b/tests/HTMLPurifier/PHPT/stub.phpt similarity index 100% rename from tests/HTMLPurifier/PHPT/Smoketest.phpt rename to tests/HTMLPurifier/PHPT/stub.phpt diff --git a/tests/PHPT/Reporter/SimpleTest.php b/tests/PHPT/Reporter/SimpleTest.php index 0d206e46..86488b57 100644 --- a/tests/PHPT/Reporter/SimpleTest.php +++ b/tests/PHPT/Reporter/SimpleTest.php @@ -14,33 +14,36 @@ class PHPT_Reporter_SimpleTest implements PHPT_Reporter $this->reporter = $reporter; } + // TODO: Figure out what the proper calls should be, since we've given + // each Suite its own UnitTestCase controller + /** * Called when the Reporter is started from a PHPT_Suite * @todo Figure out if Suites can be named */ public function onSuiteStart(PHPT_Suite $suite) { - $this->reporter->paintGroupStart('PHPT Suite', $suite->count()); + //$this->reporter->paintGroupStart('PHPT Suite', $suite->count()); } /** * Called when the Reporter is finished in a PHPT_Suite */ public function onSuiteEnd(PHPT_Suite $suite) { - $this->reporter->paintGroupEnd('PHPT Suite'); + //$this->reporter->paintGroupEnd('PHPT Suite'); } /** * Called when a Case is started */ public function onCaseStart(PHPT_Case $case) { - $this->reporter->paintCaseStart($case->name); + //$this->reporter->paintCaseStart($case->name); } /** * Called when a Case ends */ public function onCaseEnd(PHPT_Case $case) { - $this->reporter->paintCaseEnd($case->name); + //$this->reporter->paintCaseEnd($case->name); } /** @@ -54,7 +57,7 @@ class PHPT_Reporter_SimpleTest implements PHPT_Reporter * Called when a PHPT_Case_VetoException is thrown during a Case's run() */ public function onCaseSkip(PHPT_Case $case, PHPT_Case_VetoException $veto) { - $this->reporter->paintSkip("{$case->name} in {$case->filename}"); + $this->reporter->paintSkip($veto->getMessage() . ' [' . $case->filename .']'); } /** diff --git a/tests/common.php b/tests/common.php index f52ff6c3..3265aff6 100644 --- a/tests/common.php +++ b/tests/common.php @@ -120,12 +120,14 @@ function htmlpurifier_args(&$AC, $aliases, $o, $v) { * Adds a test-class; depending on the file's extension this may involve * a regular UnitTestCase or a special PHPT test */ -function htmlpurifier_add_test($test, $test_file) { - $info = pathinfo($test_file); - if (!isset($info['extension']) || $info['extension'] == 'phpt') { - $test->addTestCase(new PHPT_Controller_SimpleTest($test_file)); - } else { - require_once $test_file; - $test->addTestClass(path2class($test_file)); +function htmlpurifier_add_test($test, $test_file, $only_phpt = false) { + switch (strrchr($test_file, ".")) { + case '.phpt': + return $test->addTestCase(new PHPT_Controller_SimpleTest($test_file)); + case '.php': + require_once $test_file; + return $test->addTestClass(path2class($test_file)); + default: + trigger_error("$test_file is an invalid file for testing", E_USER_ERROR); } } \ No newline at end of file diff --git a/tests/index.php b/tests/index.php index bfd96a16..0591d939 100755 --- a/tests/index.php +++ b/tests/index.php @@ -26,6 +26,12 @@ $AC['file'] = ''; $AC['xml'] = false; $AC['dry'] = false; $AC['php'] = 'php'; + +// Convenience parameters for running quicker tests; ideally all tests +// should be performed. +$AC['disable-phpt'] = false; +$AC['only-phpt'] = false; + $aliases = array( 'f' => 'file', ); @@ -36,8 +42,15 @@ if (!SimpleReporter::inCli()) { $AC['php'] = 'php'; } -$phpt = PHPT_Registry::getInstance(); -$phpt->php = $AC['php']; +if ($AC['disable-phpt'] && $AC['only-phpt']) { + echo "Cannot disable and allow only PHPT tests!\n"; + exit(1); +} + +if (!$AC['disable-phpt']) { + $phpt = PHPT_Registry::getInstance(); + $phpt->php = $AC['php']; +} // clean out cache if necessary if ($AC['flush']) shell_exec($AC['php'] . ' ../maintenance/flush-definition-cache.php'); diff --git a/tests/test_files.php b/tests/test_files.php index aaa1e929..d8d0faf8 100644 --- a/tests/test_files.php +++ b/tests/test_files.php @@ -4,6 +4,8 @@ if (!defined('HTMLPurifierTest')) exit; // define callable test files (sorted alphabetically) +if (!$AC['only-phpt']) { + // HTML Purifier main library $test_files[] = 'HTMLPurifier/AttrCollectionsTest.php'; @@ -141,6 +143,19 @@ $test_files[] = 'ConfigSchema/StringHashReverseAdapterTest.php'; $test_files[] = 'ConfigSchema/StringHashParserTest.php'; $test_files[] = 'ConfigSchema/StringHashTest.php'; +} // end if ($AC['only-phpt']) + // PHPT tests -$test_files[] = 'HTMLPurifier/PHPT'; +if (!$AC['disable-phpt']) { + $phpt_dirs = array(); + $phpt_dirs[] = 'HTMLPurifier/PHPT'; + + foreach ($phpt_dirs as $dir) { + $FS = new FSTools(); + $phpt_files = $FS->globr($dir, '*.phpt'); + foreach ($phpt_files as $file) { + $test_files[] = str_replace('\\', '/', $file); + } + } +}