Various improvements to test script command line options, i.e. --type
[htmlpurifier/rdancer.git] / tests / index.php
blobad043ee335daba2d79a975e0fcb807b523e8a303
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
14 * - type, the type of tests to run, can be 'htmlpurifier', 'configdoc',
15 * 'fstools', 'htmlt', 'vtest' or 'phpt'
17 * If you're interested in running the test-cases, mosey over to
18 * ../test-settings.sample.php, copy the file to test-settings.php and follow
19 * the enclosed instructions.
21 * @warning File setup does not exactly match with autoloader; make sure that
22 * non-test classes (i.e. classes that are not retrieved using
23 * $test_files) do not have underscores in their names.
26 define('HTMLPurifierTest', 1);
27 define('HTMLPURIFIER_SCHEMA_STRICT', true); // validate schemas
28 chdir(dirname(__FILE__));
30 $php = 'php'; // for safety
31 ini_set('memory_limit', '64M');
33 require 'common.php';
35 $AC = array(); // parameters
36 $AC['flush'] = false;
37 $AC['standalone'] = false;
38 $AC['file'] = '';
39 $AC['xml'] = false;
40 $AC['dry'] = false;
41 $AC['php'] = $php;
43 $AC['type'] = '';
44 $AC['disable-phpt'] = false;
45 $AC['only-phpt'] = false; // alias for --type=phpt
47 $aliases = array(
48 'f' => 'file',
51 // It's important that this does not call the autoloader. Not a problem
52 // with a function, but could be if we put this in a class.
53 htmlpurifier_parse_args($AC, $aliases);
55 // Disable PHPT tests if they're not enabled
56 if (!$GLOBALS['HTMLPurifierTest']['PHPT']) {
57 $AC['disable-phpt'] = true;
58 } elseif (!$AC['type'] && $AC['only-phpt']) {
59 // backwards-compat
60 $AC['type'] = 'phpt';
63 if (!SimpleReporter::inCli()) {
64 // Undo any dangerous parameters
65 $AC['php'] = $php;
68 // Shell-script code is executed
70 if ($AC['xml']) {
71 if (!SimpleReporter::inCli()) header('Content-Type: text/xml;charset=UTF-8');
72 $reporter = new XmlReporter();
73 } elseif (SimpleReporter::inCli()) {
74 $reporter = new TextReporter();
75 } else {
76 $reporter = new HTMLPurifier_SimpleTest_Reporter('UTF-8', $AC);
79 if ($AC['flush']) {
80 htmlpurifier_flush($AC['php'], $reporter);
83 // initialize and load HTML Purifier
84 // use ?standalone to load the alterative standalone stub
85 if ($AC['standalone']) {
86 require '../library/HTMLPurifier.standalone.php';
87 } else {
88 require '../library/HTMLPurifier.path.php';
89 require 'HTMLPurifier.includes.php';
91 require '../library/HTMLPurifier.autoload.php';
92 require 'HTMLPurifier/Harness.php';
94 // Now, userland code begins to be executed
96 // setup special DefinitionCacheFactory decorator
97 $factory = HTMLPurifier_DefinitionCacheFactory::instance();
98 $factory->addDecorator('Memory'); // since we deal with a lot of config objects
100 if (!$AC['disable-phpt']) {
101 $phpt = PHPT_Registry::getInstance();
102 $phpt->php = $AC['php'];
105 // load tests
106 require 'test_files.php';
108 $FS = new FSTools();
110 // handle test dirs
111 foreach ($test_dirs as $dir) {
112 $raw_files = $FS->globr($dir, '*Test.php');
113 foreach ($raw_files as $file) {
114 $file = str_replace('\\', '/', $file);
115 if (isset($test_dirs_exclude[$file])) continue;
116 $test_files[] = $file;
120 // handle vtest dirs
121 foreach ($vtest_dirs as $dir) {
122 $raw_files = $FS->globr($dir, '*.vtest');
123 foreach ($raw_files as $file) {
124 $test_files[] = str_replace('\\', '/', $file);
128 // handle phpt files
129 foreach ($phpt_dirs as $dir) {
130 $phpt_files = $FS->globr($dir, '*.phpt');
131 foreach ($phpt_files as $file) {
132 $test_files[] = str_replace('\\', '/', $file);
136 // handle htmlt dirs
137 foreach ($htmlt_dirs as $dir) {
138 $htmlt_files = $FS->globr($dir, '*.htmlt');
139 foreach ($htmlt_files as $file) {
140 $test_files[] = str_replace('\\', '/', $file);
144 array_unique($test_files);
145 sort($test_files); // for the SELECT
146 $GLOBALS['HTMLPurifierTest']['Files'] = $test_files; // for the reporter
147 $test_file_lookup = array_flip($test_files);
149 // determine test file
150 if ($AC['file']) {
151 if (!isset($test_file_lookup[$AC['file']])) {
152 echo "Invalid file passed\n";
153 exit;
157 if ($AC['file']) {
159 $test = new TestSuite($AC['file']);
160 htmlpurifier_add_test($test, $AC['file']);
162 } else {
164 $standalone = '';
165 if ($AC['standalone']) $standalone = ' (standalone)';
166 $test = new TestSuite('All HTML Purifier tests on PHP ' . PHP_VERSION . $standalone);
167 foreach ($test_files as $test_file) {
168 htmlpurifier_add_test($test, $test_file);
173 if ($AC['dry']) $reporter->makeDry();
175 $test->run($reporter);