MDL-32323 improve mysql test reset performance
[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) {
219d1a4e
PS
36 case 0:
37 // this is not an error, just print information and exit
38 break;
7b0ff213
PS
39 case 1:
40 $text = 'Error: '.$text;
41 break;
42 case 130:
43 $text = 'Can not load PHPUnit PEAR library, is it installed?';
44 case 131:
45 $text = 'Configuration problem: '.$text;
46 break;
47 case 132:
48 $text = 'Database was not initialised to run unit tests, please use "php admin/tool/phpunit/cli/util.php --install"';
49 break;
50 case 133:
51 $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"';
52 break;
53 case 134:
6e2cff2d 54 $text = 'Can not create main configuration file, please verify dirroot permissions.';
7b0ff213
PS
55 break;
56 default:
57 $text = empty($text) ? '' : ': '.$text;
58 $text = 'Unknown error '.$errorcode.$text;
59 break;
60 }
2fde5392 61 if (defined('PHPUNIT_UTIL') and PHPUNIT_UTIL) {
7b0ff213
PS
62 // do not write to error stream because we need the error message in PHP exec from web ui
63 echo($text."\n");
64 } else {
65 fwrite(STDERR, $text."\n");
66 }
67 exit($errorcode);
68}
69
70/**
71 * Mark empty dataroot to be used for testing.
72 * @param string $dataroot The dataroot directory
73 * @return void
74 */
75function phpunit_bootstrap_initdataroot($dataroot) {
76 global $CFG;
77 umask(0);
78 if (!file_exists("$dataroot/phpunittestdir.txt")) {
79 file_put_contents("$dataroot/phpunittestdir.txt", 'Contents of this directory are used during tests only, do not delete this file!');
80 }
6e2cff2d 81 phpunit_boostrap_fix_file_permissions("$dataroot/phpunittestdir.txt");
7b0ff213
PS
82 if (!file_exists("$CFG->phpunit_dataroot/phpunit")) {
83 mkdir("$CFG->phpunit_dataroot/phpunit", $CFG->directorypermissions);
84 }
85}
6e2cff2d
PS
86
87/**
88 * Try to change permissions to $CFG->dirroot or $CFG->dataroot if possible
89 * @param string $file
90 * @return bool success
91 */
92function phpunit_boostrap_fix_file_permissions($file) {
93 global $CFG;
94
95 $permissions = fileperms($file);
96 if ($permissions & $CFG->filepermissions != $CFG->filepermissions) {
97 $permissions = $permissions | $CFG->filepermissions;
98 return chmod($file, $permissions);
99 }
100
101 return true;
102}