diff options
author | Andreas Gohr <gohr@cosmocode.de> | 2012-04-17 15:38:03 +0200 |
---|---|---|
committer | Andreas Gohr <gohr@cosmocode.de> | 2012-04-17 15:38:03 +0200 |
commit | e048653b52aad13b5964e1626192ffee2211870b (patch) | |
tree | d5aaa7168f1496aa8262920ad2320a5d3668b6a5 /_testing/core | |
parent | c45ce44912e3e54ecdf0a19af2d7df1960aa2962 (diff) | |
download | rpg-e048653b52aad13b5964e1626192ffee2211870b.tar.gz rpg-e048653b52aad13b5964e1626192ffee2211870b.tar.bz2 |
moved functions and classes out of bootstrap
There's still more that I'd like to moved out. bootstrap should not
contain any logic but only call the appropriate functions for setup the
test environment.
Diffstat (limited to '_testing/core')
-rw-r--r-- | _testing/core/DokuWikiTest.php | 52 | ||||
-rw-r--r-- | _testing/core/TestRequest.php | 67 | ||||
-rw-r--r-- | _testing/core/TestUtils.php | 52 |
3 files changed, 171 insertions, 0 deletions
diff --git a/_testing/core/DokuWikiTest.php b/_testing/core/DokuWikiTest.php new file mode 100644 index 000000000..8ae261a52 --- /dev/null +++ b/_testing/core/DokuWikiTest.php @@ -0,0 +1,52 @@ +<?php +/** + * Helper class to provide basic functionality for tests + */ +abstract class DokuWikiTest extends PHPUnit_Framework_TestCase { + /** + * Reset the DokuWiki environment before each test run + * + * Makes sure loaded config, language and plugins are correct + */ + function setUp() { + // reload config + global $conf, $config_cascade; + $conf = array(); + foreach (array('default','local','protected') as $config_group) { + if (empty($config_cascade['main'][$config_group])) continue; + foreach ($config_cascade['main'][$config_group] as $config_file) { + if (@file_exists($config_file)) { + include($config_file); + } + } + } + + // reload license config + global $license; + $license = array(); + + // load the license file(s) + foreach (array('default','local') as $config_group) { + if (empty($config_cascade['license'][$config_group])) continue; + foreach ($config_cascade['license'][$config_group] as $config_file) { + if(@file_exists($config_file)){ + include($config_file); + } + } + } + + // make real paths and check them + init_paths(); + init_files(); + + // reset loaded plugins + global $plugin_controller_class, $plugin_controller; + $plugin_controller = new $plugin_controller_class(); + global $EVENT_HANDLER; + $EVENT_HANDLER = new Doku_Event_Handler(); + + // reload language + $local = $conf['lang']; + trigger_event('INIT_LANG_LOAD', $local, 'init_lang', true); + } +} diff --git a/_testing/core/TestRequest.php b/_testing/core/TestRequest.php new file mode 100644 index 000000000..f77c494c3 --- /dev/null +++ b/_testing/core/TestRequest.php @@ -0,0 +1,67 @@ +<?php +/** + * Simulates a full DokuWiki HTTP Request and allows + * runtime inspection. + */ + +/** + * Helper class to execute a fake request + */ +class TestRequest { + + /** + * Executes the request + * + * @return TestResponse response + */ + function execute() { + global $output_buffer; + $output_buffer = ''; + + // now execute dokuwiki and grep the output + header_remove(); + ob_start('ob_start_callback'); + include(DOKU_INC.'doku.php'); + ob_end_flush(); + + // it's done, return the page result + return new TestResponse( + $output_buffer, + headers_list() + ); + } +} + +/** + * holds a copy of all produced outputs of a TestRequest + */ +class TestResponse { + protected $content; + protected $headers; + protected $pq = null; + + function __construct($content, $headers) { + $this->content = $content; + $this->headers = $headers; + } + + function getContent() { + return $this->content; + } + + function getHeaders() { + return $this->headers; + } + + /** + * Query the response for a JQuery compatible CSS selector + * + * @link https://code.google.com/p/phpquery/wiki/Selectors + * @param string selector + * @returns object a PHPQuery object + */ + function queryHTML($selector){ + if(is_null($pq)) $pq = phpQuery::newDocument($this->content); + return pq($selector); + } +} diff --git a/_testing/core/TestUtils.php b/_testing/core/TestUtils.php new file mode 100644 index 000000000..4fd56e85d --- /dev/null +++ b/_testing/core/TestUtils.php @@ -0,0 +1,52 @@ +<?php + +class TestUtils { + + /** + * helper for recursive copy() + */ + static function rcopy($destdir, $source) { + if (!is_dir($source)) { + copy($source, $destdir.'/'.basename($source)); + } else { + $newdestdir = $destdir.'/'.basename($source); + mkdir($newdestdir); + + $dh = dir($source); + while (false !== ($entry = $dh->read())) { + if ($entry == '.' || $entry == '..') { + continue; + } + TestUtils::rcopy($newdestdir, $source.'/'.$entry); + } + $dh->close(); + } + } + + /** + * helper for recursive rmdir()/unlink() + */ + static function rdelete($target) { + if (!is_dir($target)) { + unlink($target); + } else { + $dh = dir($target); + while (false !== ($entry = $dh->read())) { + if ($entry == '.' || $entry == '..') { + continue; + } + TestUtils::rdelete("$target/$entry"); + } + $dh->close(); + rmdir($target); + } + } + + // helper to append text to a file + static function fappend($file, $text) { + $fh = fopen($file, 'a'); + fwrite($fh, $text); + fclose($fh); + } + +} |