-
-
Notifications
You must be signed in to change notification settings - Fork 840
/
inttests.php
158 lines (128 loc) · 3.46 KB
/
inttests.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
/**
* Integration Test Library for DokuWiki
*
* Simulates a full DokuWiki HTTP Request and allows
* runtime inspection.
*/
// helper for recursive copy()
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;
}
rcopy($newdestdir, $source.'/'.$entry);
}
$dh->close();
}
}
// prepare temporary directories
define('DOKU_INC', dirname(dirname(__FILE__)).'/');
define('TMP_DIR', '/tmp/dwinttests-'.microtime(true));
define('DOKU_CONF', TMP_DIR.'/inttests.conf/');
define('DOKU_PLUGIN', TMP_DIR.'/plugins/');
define('DOKU_TMP_DATA', TMP_DIR.'/inttests.data/');
// create temp directories
mkdir(TMP_DIR);
mkdir(DOKU_PLUGIN);
// populate default dirs
rcopy(TMP_DIR, dirname(__FILE__).'/inttests.conf');
rcopy(TMP_DIR, dirname(__FILE__).'/inttests.data');
// cleanup dir after exit
register_shutdown_function(function() {
// TODO delete recursive tmp dir
});
// TODO disable all non-default plugins in config
// TODO if plugin test, enable plugin
// TODO load plugin descriptor and enable dependent plugins
// TODO set global variables, phpunit will restore them for every test (test that)
// load dw
require_once(DOKU_INC.'inc/init.php');
// output buffering
$output_buffer = '';
function ob_start_callback($buffer) {
global $output_buffer;
$output_buffer .= $buffer;
}
// Helper class to execute a fake request
class TestRequest {
var $server_vars = array(
'REMOTE_ADDR' => '127.0.0.1',
);
var $get_vars = array();
var $post_vars = array();
function __construct($page = '') {
$this->setPage($page);
}
function setServerVar($varName, $varValue) {
$this->sevrer_vars[$varName] = $varValue;
}
function setGetVar($varName, $varValue) {
$this->get_vars[$varName] = $varValue;
}
function setPostVar($varName, $varValue) {
$this->post_vars[$varName] = $varValue;
}
function setPage($pageName) {
$this->setGetVar('id', $pageName);
}
function addLocalConf($text) {
$this->conf_local[] = $text;
}
function hook($hook, $step, $function) {
global $EVENT_HANDLER;
$null = null;
$EVENT_HANDLER->register_hook($hook, $step, $null, $function);
}
function execute() {
global $output_buffer;
$output_buffer = '';
// fake php environment
foreach ($this->server_vars as $key => $value) {
$_SERVER[$key] = $value;
}
$_REQUEST = array();
$_GET = array();
foreach ($this->get_vars as $key => $value) {
$_GET[$key] = $value;
$_REQUEST[$key] = $value;
}
$_POST = array();
foreach ($this->post_vars as $key => $value) {
$_POST[$key] = $value;
$_REQUEST[$key] = $value;
}
// 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 {
var $content;
var $headers;
function __construct($content, $headers) {
$this->content = $content;
$this->headers = $headers;
}
function getContent() {
return $this->content;
}
function getHeaders() {
return $this->headers;
}
// TODO provide findById, findBy... (https://github.com/cosmocode/dokuwiki-plugin-scrape/blob/master/phpQuery-onefile.php)
}