1 : <?php
2 :
3 : /**
4 : * XSLT Benchmarking
5 : * @link https://github.com/masicek/XSLT-Benchmarking
6 : * @author Viktor Mašíček <viktor@masicek.net>
7 : * @license "New" BSD License
8 : */
9 :
10 : namespace XSLTBenchmarking\Tools;
11 :
12 : define('DATA_TOOLS', __DIR__ . '/Data');
13 : define('TESTS_TOOLS', __DIR__ . '/Tests');
14 : define('ROOT_TOOLS', __DIR__ . '/../XSLTBenchmarking');
15 : define('LIBS_TOOLS', __DIR__ . '/../Libs');
16 :
17 : if (!defined('LIBS'))
18 : {
19 : define ('LIBS', LIBS_TOOLS);
20 : }
21 : if (!defined('ROOT'))
22 : {
23 : define ('ROOT', ROOT_TOOLS);
24 : }
25 : if (!defined('VERSION'))
26 : {
27 : define('VERSION', 'testing');
28 : }
29 :
30 :
31 : require_once LIBS . '/PhpOptions/PhpOptions.min.php';
32 : require_once LIBS . '/PhpPath/PhpPath.min.php';
33 : require_once ROOT . '/Printer.php';
34 :
35 : use PhpOptions\Option;
36 : use PhpOptions\Options;
37 : use PhpPath\P;
38 :
39 : /**
40 : * Runner of scripts for run tests and generating documentation
41 : *
42 : * @author Viktor Mašíček <viktor@masicek.net>
43 : */
44 : class Runner
45 : {
46 :
47 : /**
48 : * Command-line options
49 : *
50 : * @var PhpOptions\Options
51 : */
52 : private $options;
53 :
54 : /**
55 : * Flag of including PHPUnit tests
56 : *
57 : * @var bool
58 : */
59 : private static $testsIncludeDone = FALSE;
60 :
61 :
62 : // ---- RUNNING ----
63 :
64 :
65 : /**
66 : * Define expected options
67 : */
68 : public function __construct()
69 : {
70 : // set options + help
71 0 : $optionsList = array();
72 0 : $optionsList[] = Option::make('Help')->description('Show this help');
73 0 : $optionsList[] = Option::make('All')->description('Run script parts (Tests, Docs)');
74 0 : $optionsList[] = Option::make('Tests')->description('Run all tests');
75 0 : $optionsList[] = Option::make('Skipped on')->description('Turn on skipping of slow tests prepared for skipping');
76 0 : $optionsList[] = Option::make('Tests unit')->short()->long('tu')->description('Run unit tests');
77 0 : $optionsList[] = Option::make('Tests regression')->short()->long('tr')->description('Run regression tests');
78 0 : $optionsList[] = Option::make('Docs')->description('Run script for generating API documenation of XSLT Benchmarking');
79 :
80 0 : $options = new Options();
81 0 : $options->add($optionsList);
82 0 : $options->defaults('Help');
83 0 : $options->description(
84 0 : 'XSTL Benchmarking' . PHP_EOL .
85 0 : 'author: Viktor Masicek <viktor@masicek.net>' . PHP_EOL . PHP_EOL .
86 : 'Script for running tests and generating API documentation of XSLT Benchmarking.'
87 0 : );
88 :
89 0 : $this->options = $options;
90 :
91 0 : \XSLTBenchmarking\Printer::$mode = \XSLTBenchmarking\Printer::MODE_TEST;
92 0 : }
93 :
94 :
95 : /**
96 : * Run scripts by options defined in command-line
97 : *
98 : * @return void
99 : */
100 : public function run()
101 : {
102 0 : $options = $this->options;
103 :
104 : // help
105 0 : if ($options->get('Help'))
106 0 : {
107 0 : fwrite(STDOUT, $options->getHelp());
108 0 : return;
109 : }
110 :
111 0 : $all = $options->get('All');
112 :
113 : // tests
114 0 : if ($all || $options->get('Tests'))
115 0 : {
116 0 : $this->runTestsAll();
117 0 : }
118 : else
119 : {
120 0 : if ($options->get('Tests unit'))
121 0 : {
122 0 : $this->runTestsUnit();
123 0 : }
124 0 : if ($options->get('Tests regression'))
125 0 : {
126 0 : $this->runTestsRegression();
127 0 : }
128 : }
129 :
130 : // documentation
131 0 : if ($all || $options->get('Docs'))
132 0 : {
133 0 : $this->generateDocumentation();
134 0 : }
135 0 : }
136 :
137 :
138 : // ---- PARTS OF RUNNING ----
139 :
140 :
141 : /**
142 : * Generate dodumentation
143 : *
144 : * @return void
145 : */
146 : private function generateDocumentation()
147 : {
148 0 : $this->printHeader('Generate Documentation');
149 :
150 0 : $sources = array();
151 0 : $sources[] = P::m(ROOT_TOOLS, '.');
152 0 : $sources = '--source ' . implode(' --source ', $sources);
153 :
154 0 : $destination = P::m(DATA_TOOLS, '/Docs');
155 :
156 : $options = array(
157 0 : '--title "XSTL Benchmarking"',
158 0 : $sources,
159 0 : '--todo yes',
160 0 : '--destination ' . $destination,
161 0 : );
162 0 : $options = implode(' ', $options);
163 :
164 0 : $apigen = P::m(LIBS, '/Apigen/apigen.php');
165 0 : passthru('php ' . $apigen . ' ' . $options);
166 0 : }
167 :
168 :
169 : /**
170 : * Run tests
171 : *
172 : * @return void
173 : */
174 : private function runTestsAll()
175 : {
176 0 : $this->printHeader('Run tests');
177 :
178 0 : $this->testsInclude();
179 :
180 : // run tests
181 0 : $coverage = P::m(DATA_TOOLS, '/Coverage');
182 0 : $tests = P::m(TESTS_TOOLS);
183 0 : $this->setArguments('boot.php',
184 0 : '--coverage-html ' . $coverage . '
185 0 : ' . $tests
186 0 : );
187 0 : \PHPUnit_TextUI_Command::main(FALSE);
188 :
189 0 : $this->printInfo();
190 0 : }
191 :
192 :
193 : /**
194 : * Run unit tests
195 : *
196 : * @return void
197 : */
198 : private function runTestsUnit()
199 : {
200 0 : $this->printHeader('Run unit tests');
201 :
202 0 : $this->testsInclude();
203 :
204 : // run tests
205 0 : $coverage = P::m(DATA_TOOLS, '/Coverage');
206 0 : $tests = P::m(TESTS_TOOLS, '/Unit');
207 0 : $this->setArguments('boot.php',
208 0 : '--coverage-html ' . $coverage . '
209 0 : ' . $tests
210 0 : );
211 0 : \PHPUnit_TextUI_Command::main(FALSE);
212 :
213 0 : $this->printInfo();
214 0 : }
215 :
216 :
217 : /**
218 : * Run regression tests
219 : *
220 : * @return void
221 : */
222 : private function runTestsRegression()
223 : {
224 0 : $this->printHeader('Run regression tests');
225 :
226 0 : $this->testsInclude();
227 :
228 : // run tests
229 0 : $coverage = P::m(DATA_TOOLS, '/Coverage');
230 0 : $tests = P::m(TESTS_TOOLS, '/Regression');
231 0 : $this->setArguments('boot.php',
232 0 : '--coverage-html ' . $coverage . '
233 0 : ' . $tests
234 0 : );
235 0 : \PHPUnit_TextUI_Command::main(FALSE);
236 :
237 0 : $this->printInfo();
238 0 : }
239 :
240 :
241 : // ---- HELPS FUNCTIONS ----
242 :
243 :
244 : /**
245 : * Include PHPUnit for runnig tests.
246 : * Including is done one once.
247 : *
248 : * @return void
249 : */
250 : private function testsInclude()
251 : {
252 0 : if (!self::$testsIncludeDone)
253 0 : {
254 0 : define('TEST_SKIPPED', (bool)$this->options->get('Skipped on'));
255 :
256 0 : self::$testsIncludeDone = TRUE;
257 :
258 0 : $this->mockeryInclude();
259 :
260 : // set libs as include path
261 0 : $libs = P::m(LIBS, '/PHPUnit');
262 0 : set_include_path(get_include_path() . PATH_SEPARATOR . $libs);
263 0 : require_once P::m('PHPUnit/Autoload.php');
264 :
265 : // include my TestCase
266 0 : require_once P::m(TESTS_TOOLS, '/TestCase.php');
267 0 : }
268 0 : }
269 :
270 :
271 : /**
272 : * Include Mockery for tests
273 : *
274 : * @return void
275 : */
276 : private function mockeryInclude()
277 : {
278 0 : $libs = P::m(LIBS, '/Mockery');
279 0 : set_include_path(get_include_path() . PATH_SEPARATOR . $libs);
280 0 : require_once P::m('Mockery/Loader.php');
281 0 : $loader = new \Mockery\Loader;
282 0 : $loader->register();
283 0 : }
284 :
285 :
286 : /**
287 : * Print header of one runnig script
288 : *
289 : * @param string $header Text of printed header
290 : * @param bool $turnOff Turn of printing
291 : *
292 : * @return void
293 : */
294 : private function printHeader($header, $turnOff = TRUE)
295 : {
296 : if ($turnOff)
297 0 : {
298 0 : $header = $header . ':';
299 0 : $line = str_repeat('-', strlen($header));
300 0 : $this->printInfo($header);
301 0 : $this->printInfo($line);
302 0 : }
303 0 : }
304 :
305 :
306 : /**
307 : * Print information text
308 : *
309 : * @param string $info Text of printed info
310 : * @param bool $turnOff Turn of printing
311 : *
312 : * @return void
313 : */
314 : private function printInfo($info = '', $turnOff = TRUE)
315 : {
316 : if ($turnOff)
317 0 : {
318 0 : fwrite(STDOUT, "$info\n");
319 0 : }
320 0 : }
321 :
322 :
323 : /**
324 : * Simulate input command-line arguments
325 : *
326 : * @param string $arguments List of arguments
327 : *
328 : * @return void
329 : */
330 : private function setArguments($scriptName, $arguments)
331 : {
332 0 : $arguments = preg_replace('/(' . "\r\n|\t" . ')+/', ' ', $arguments);
333 0 : $arguments = trim($arguments);
334 0 : $argumentsNew = '';
335 0 : $inQuation = FALSE;
336 0 : for ($i = 0; $i < strlen($arguments); $i++)
337 : {
338 0 : $char = $arguments[$i];
339 0 : if ($char == '"' && !$inQuation)
340 0 : {
341 0 : $inQuation = TRUE;
342 0 : }
343 0 : elseif ($char == '"' && $inQuation)
344 : {
345 0 : $inQuation = FALSE;
346 0 : }
347 :
348 0 : if ($char == ' ' && $inQuation)
349 0 : {
350 0 : $argumentsNew .= '###SPACE###';
351 0 : }
352 0 : elseif ($char != '"')
353 : {
354 0 : $argumentsNew .= $char;
355 0 : }
356 0 : }
357 0 : $arguments = $argumentsNew;
358 0 : $arguments = preg_replace('/ +/', ' ', $arguments);
359 :
360 0 : $argv = explode(' ', trim($scriptName . ' ' . $arguments));
361 :
362 0 : for ($i = 0; $i < count($argv); $i++)
363 : {
364 0 : $argv[$i] = str_replace('###SPACE###', ' ', $argv[$i]);
365 0 : }
366 :
367 0 : $_SERVER['argv'] = $argv;
368 0 : }
369 :
370 :
371 : }
|