[3.1.0] When flush fails, fail SimpleTest
[htmlpurifier.git] / tests / index.php
blobf9a3ab3e3a3b31013ee87ed0e347adcde3a9498c
1 <?php
3 /** @file
4 * Unit tester
5 *
6 * The heart and soul of HTML Purifier's correctness; anything and everything
7 * is tested here! Arguments are specified like --arg=opt, allowed arguments
8 * are:
9 * - flush, whether or not to flush definition caches before running
10 * - standalone, whether or not to test the standalone version
11 * - file (f), a single file to test
12 * - xml, whether or not to output XML
13 * - dry, whether or not to do a dry run
15 * @warning File setup does not exactly match with autoloader; make sure that
16 * non-test classes (i.e. classes that are not retrieved using
17 * $test_files) do not have underscores in their names.
20 define('HTMLPurifierTest', 1);
21 define('HTMLPURIFIER_SCHEMA_STRICT', true); // validate schemas
22 chdir(dirname(__FILE__));
24 require 'common.php';
26 $AC = array(); // parameters
27 $AC['flush'] = false;
28 $AC['standalone'] = false;
29 $AC['file'] = '';
30 $AC['xml'] = false;
31 $AC['dry'] = false;
32 $AC['php'] = 'php';
34 // Convenience parameters for running quicker tests; ideally all tests
35 // should be performed.
36 $AC['disable-phpt'] = false;
37 $AC['only-phpt'] = false;
39 $aliases = array(
40 'f' => 'file',
43 // It's important that this does not call the autoloader. Not a problem
44 // with a function, but could be if we put this in a class.
45 htmlpurifier_parse_args($AC, $aliases);
47 // Disable PHPT tests if they're not enabled
48 if (!$GLOBALS['HTMLPurifierTest']['PHPT']) $AC['disable-phpt'] = true;
50 if (!SimpleReporter::inCli()) {
51 // Undo any dangerous parameters
52 $AC['php'] = $php;
55 if ($AC['disable-phpt'] && $AC['only-phpt']) {
56 echo "Cannot disable and allow only PHPT tests!\n";
57 exit(1);
60 // Shell-script code is executed
62 if ($AC['xml']) {
63 if (!SimpleReporter::inCli()) header('Content-Type: text/xml;charset=UTF-8');
64 $reporter = new XmlReporter();
65 } elseif (SimpleReporter::inCli()) {
66 $reporter = new TextReporter();
67 } else {
68 $reporter = new HTMLPurifier_SimpleTest_Reporter('UTF-8', $AC);
71 if ($AC['flush']) {
72 htmlpurifier_flush($AC['php'], $reporter);
75 // initialize and load HTML Purifier
76 // use ?standalone to load the alterative standalone stub
77 if ($AC['standalone']) {
78 require '../library/HTMLPurifier.standalone.php';
79 } else {
80 require '../library/HTMLPurifier.path.php';
81 require 'HTMLPurifier.includes.php';
82 require '../library/HTMLPurifier.autoload.php';
84 require 'HTMLPurifier/Harness.php';
86 // Now, userland code begins to be executed
88 // setup special DefinitionCacheFactory decorator
89 $factory =& HTMLPurifier_DefinitionCacheFactory::instance();
90 $factory->addDecorator('Memory'); // since we deal with a lot of config objects
92 if (!$AC['disable-phpt']) {
93 $phpt = PHPT_Registry::getInstance();
94 $phpt->php = $AC['php'];
97 // load tests
99 $test_files = array();
100 $test_dirs = array();
101 $test_dirs_exclude = array();
102 $vtest_dirs = array();
103 $phpt_dirs = array();
105 require 'test_files.php'; // populates $test_files array
107 $FS = new FSTools();
109 // handle test dirs
110 foreach ($test_dirs as $dir) {
111 $raw_files = $FS->globr($dir, '*Test.php');
112 foreach ($raw_files as $file) {
113 $file = str_replace('\\', '/', $file);
114 if (isset($test_dirs_exclude[$file])) continue;
115 $test_files[] = $file;
119 // handle vtest dirs
120 foreach ($vtest_dirs as $dir) {
121 $raw_files = $FS->globr($dir, '*.vtest');
122 foreach ($raw_files as $file) {
123 $test_files[] = str_replace('\\', '/', $file);
127 // handle phpt files
128 foreach ($phpt_dirs as $dir) {
129 $phpt_files = $FS->globr($dir, '*.phpt');
130 foreach ($phpt_files as $file) {
131 $test_files[] = str_replace('\\', '/', $file);
135 array_unique($test_files);
136 sort($test_files); // for the SELECT
137 $GLOBALS['HTMLPurifierTest']['Files'] = $test_files; // for the reporter
138 $test_file_lookup = array_flip($test_files);
140 // determine test file
141 if ($AC['file']) {
142 if (!isset($test_file_lookup[$AC['file']])) {
143 echo "Invalid file passed\n";
144 exit;
148 // we can't use addTestFile because SimpleTest chokes on E_STRICT warnings
149 if ($AC['file']) {
151 $test = new TestSuite($AC['file']);
152 htmlpurifier_add_test($test, $AC['file']);
154 } else {
156 $standalone = '';
157 if ($AC['standalone']) $standalone = ' (standalone)';
158 $test = new TestSuite('All HTML Purifier tests on PHP ' . PHP_VERSION . $standalone);
159 foreach ($test_files as $test_file) {
160 htmlpurifier_add_test($test, $test_file);
165 if ($AC['dry']) $reporter->makeDry();
167 $test->run($reporter);