1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* PHPUnit related utilities.
|
|
|
19 |
*
|
|
|
20 |
* Exit codes: {@see phpunit_bootstrap_error()}
|
|
|
21 |
*
|
|
|
22 |
* @package tool_phpunit
|
|
|
23 |
* @copyright 2012 Petr Skoda {@link http://skodak.org}
|
|
|
24 |
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
25 |
*/
|
|
|
26 |
|
|
|
27 |
if (isset($_SERVER['REMOTE_ADDR'])) {
|
|
|
28 |
die; // No access from web!
|
|
|
29 |
}
|
|
|
30 |
|
|
|
31 |
define('IGNORE_COMPONENT_CACHE', true);
|
|
|
32 |
|
|
|
33 |
require_once(__DIR__.'/../../../../lib/clilib.php');
|
|
|
34 |
require_once(__DIR__.'/../../../../lib/phpunit/bootstraplib.php');
|
|
|
35 |
require_once(__DIR__.'/../../../../lib/testing/lib.php');
|
|
|
36 |
|
|
|
37 |
// Now get cli options.
|
|
|
38 |
list($options, $unrecognized) = cli_get_params(
|
|
|
39 |
[
|
|
|
40 |
'drop' => false,
|
|
|
41 |
'install' => false,
|
|
|
42 |
'buildconfig' => false,
|
|
|
43 |
'buildcomponentconfigs' => false,
|
|
|
44 |
'diag' => false,
|
|
|
45 |
'run' => false,
|
|
|
46 |
'help' => false,
|
|
|
47 |
],
|
|
|
48 |
[
|
|
|
49 |
'h' => 'help',
|
|
|
50 |
]
|
|
|
51 |
);
|
|
|
52 |
|
|
|
53 |
// Basic check to see if phpunit is installed.
|
|
|
54 |
if (!file_exists(__DIR__.'/../../../../vendor/phpunit/phpunit/composer.json') ||
|
|
|
55 |
!file_exists(__DIR__.'/../../../../vendor/bin/phpunit') ||
|
|
|
56 |
!file_exists(__DIR__.'/../../../../vendor/autoload.php')) {
|
|
|
57 |
phpunit_bootstrap_error(PHPUNIT_EXITCODE_PHPUNITMISSING);
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
if ($options['install'] || $options['drop']) {
|
|
|
61 |
define('CACHE_DISABLE_ALL', true);
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
if ($options['run']) {
|
|
|
65 |
unset($options);
|
|
|
66 |
unset($unrecognized);
|
|
|
67 |
|
|
|
68 |
foreach ($_SERVER['argv'] as $k => $v) {
|
|
|
69 |
if (strpos($v, '--run') === 0) {
|
|
|
70 |
unset($_SERVER['argv'][$k]);
|
|
|
71 |
$_SERVER['argc'] = $_SERVER['argc'] - 1;
|
|
|
72 |
}
|
|
|
73 |
}
|
|
|
74 |
$_SERVER['argv'] = array_values($_SERVER['argv']);
|
|
|
75 |
require(__DIR__ . '/../../../../vendor/bin/phpunit');
|
|
|
76 |
exit(0);
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
define('PHPUNIT_UTIL', true);
|
|
|
80 |
|
|
|
81 |
require(__DIR__.'/../../../../vendor/autoload.php');
|
|
|
82 |
require(__DIR__ . '/../../../../lib/phpunit/bootstrap.php');
|
|
|
83 |
|
|
|
84 |
// From now on this is a regular moodle CLI_SCRIPT.
|
|
|
85 |
|
|
|
86 |
require_once($CFG->libdir.'/adminlib.php');
|
|
|
87 |
require_once($CFG->libdir.'/upgradelib.php');
|
|
|
88 |
require_once($CFG->libdir.'/clilib.php');
|
|
|
89 |
require_once($CFG->libdir.'/installlib.php');
|
|
|
90 |
|
|
|
91 |
if ($unrecognized) {
|
|
|
92 |
$unrecognized = implode("\n ", $unrecognized);
|
|
|
93 |
cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
$diag = $options['diag'];
|
|
|
97 |
$drop = $options['drop'];
|
|
|
98 |
$install = $options['install'];
|
|
|
99 |
$buildconfig = $options['buildconfig'];
|
|
|
100 |
$buildcomponentconfigs = $options['buildcomponentconfigs'];
|
|
|
101 |
|
|
|
102 |
if ($options['help'] || (!$drop && !$install && !$buildconfig && !$buildcomponentconfigs && !$diag)) {
|
|
|
103 |
$help = "Various PHPUnit utility functions
|
|
|
104 |
|
|
|
105 |
Options:
|
|
|
106 |
--drop Drop database and dataroot
|
|
|
107 |
--install Install database
|
|
|
108 |
--diag Diagnose installation and return error code only
|
|
|
109 |
--run Execute PHPUnit tests (alternative for standard phpunit binary)
|
|
|
110 |
--buildconfig Build /phpunit.xml from /phpunit.xml.dist that runs all tests
|
|
|
111 |
--buildcomponentconfigs
|
|
|
112 |
Build distributed phpunit.xml files for each component
|
|
|
113 |
|
|
|
114 |
-h, --help Print out this help
|
|
|
115 |
|
|
|
116 |
Example:
|
|
|
117 |
\$ php ".testing_cli_argument_path('/admin/tool/phpunit/cli/util.php')." --install
|
|
|
118 |
";
|
|
|
119 |
echo $help;
|
|
|
120 |
exit(0);
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
if ($diag) {
|
|
|
124 |
list($errorcode, $message) = phpunit_util::testing_ready_problem();
|
|
|
125 |
if ($errorcode) {
|
|
|
126 |
phpunit_bootstrap_error($errorcode, $message);
|
|
|
127 |
}
|
|
|
128 |
exit(0);
|
|
|
129 |
|
|
|
130 |
} else if ($buildconfig) {
|
|
|
131 |
if (phpunit_util::build_config_file()) {
|
|
|
132 |
exit(0);
|
|
|
133 |
} else {
|
|
|
134 |
phpunit_bootstrap_error(
|
|
|
135 |
PHPUNIT_EXITCODE_CONFIGWARNING,
|
|
|
136 |
'Can not create main /phpunit.xml configuration file, verify dirroot permissions'
|
|
|
137 |
);
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
} else if ($buildcomponentconfigs) {
|
|
|
141 |
phpunit_util::build_component_config_files();
|
|
|
142 |
exit(0);
|
|
|
143 |
|
|
|
144 |
} else if ($drop) {
|
|
|
145 |
// Make sure tests do not run in parallel.
|
|
|
146 |
test_lock::acquire('phpunit');
|
|
|
147 |
phpunit_util::drop_site(true);
|
|
|
148 |
// Note: we must stop here because $CFG is messed up and we can not reinstall, sorry.
|
|
|
149 |
exit(0);
|
|
|
150 |
|
|
|
151 |
} else if ($install) {
|
|
|
152 |
phpunit_util::install_site();
|
|
|
153 |
exit(0);
|
|
|
154 |
}
|