MDL-32323 experimental web UI for execution of PHPUnit tests
[moodle.git] / lib / phpunit / bootstraplib.php
CommitLineData
7b0ff213
PS
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * PHPUnit bootstrap function
19 *
20 * Note: these functions must be self contained and must not rely on any library or include
21 *
22 * @package core
23 * @category phpunit
24 * @copyright 2012 Petr Skoda {@link http://skodak.org}
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 */
27
28/**
29 * Print error and stop execution
30 * @param int $errorcode The exit error code
31 * @param string $text An error message to display
32 * @return void stops code execution with error code
33 */
34function phpunit_bootstrap_error($errorcode, $text = '') {
35 switch ($errorcode) {
36 case 1:
37 $text = 'Error: '.$text;
38 break;
39 case 130:
40 $text = 'Can not load PHPUnit PEAR library, is it installed?';
41 case 131:
42 $text = 'Configuration problem: '.$text;
43 break;
44 case 132:
45 $text = 'Database was not initialised to run unit tests, please use "php admin/tool/phpunit/cli/util.php --install"';
46 break;
47 case 133:
48 $text = 'Database was initialised for different version, please use "php admin/tool/phpunit/cli/util.php --drop; php admin/tool/phpunit/cli/util.php --install"';
49 break;
50 case 134:
51 $text = 'Can not create main configuration file, please verify dirroot permissions."';
52 break;
53 default:
54 $text = empty($text) ? '' : ': '.$text;
55 $text = 'Unknown error '.$errorcode.$text;
56 break;
57 }
58 if (PHPUNIT_UTIL) {
59 // do not write to error stream because we need the error message in PHP exec from web ui
60 echo($text."\n");
61 } else {
62 fwrite(STDERR, $text."\n");
63 }
64 exit($errorcode);
65}
66
67/**
68 * Mark empty dataroot to be used for testing.
69 * @param string $dataroot The dataroot directory
70 * @return void
71 */
72function phpunit_bootstrap_initdataroot($dataroot) {
73 global $CFG;
74 umask(0);
75 if (!file_exists("$dataroot/phpunittestdir.txt")) {
76 file_put_contents("$dataroot/phpunittestdir.txt", 'Contents of this directory are used during tests only, do not delete this file!');
77 }
78 if (fileperms("$dataroot/phpunittestdir.txt") & $CFG->filepermissions != $CFG->filepermissions) {
79 chmod("$dataroot/phpunittestdir.txt", $CFG->filepermissions);
80 }
81 if (!file_exists("$CFG->phpunit_dataroot/phpunit")) {
82 mkdir("$CFG->phpunit_dataroot/phpunit", $CFG->directorypermissions);
83 }
84}