1 |
efrain |
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 |
* Run a test scenario generator feature file.
|
|
|
19 |
*
|
|
|
20 |
* @package tool_generator
|
|
|
21 |
* @copyright 2023 Ferran Recio <ferran@moodle.com>
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
if (isset($_SERVER['REMOTE_ADDR'])) {
|
|
|
26 |
die(); // No access from web!
|
|
|
27 |
}
|
|
|
28 |
|
|
|
29 |
define('CLI_SCRIPT', true);
|
|
|
30 |
|
|
|
31 |
require_once(__DIR__ . '/../../../../config.php');
|
|
|
32 |
require_once(__DIR__ . '/../../../../lib/clilib.php');
|
|
|
33 |
require_once(__DIR__ . '/../../../../lib/behat/classes/behat_config_manager.php');
|
|
|
34 |
require_once(__DIR__ . '/../../../../lib/testing/lib.php');
|
|
|
35 |
|
|
|
36 |
ini_set('display_errors', '1');
|
|
|
37 |
ini_set('log_errors', '1');
|
|
|
38 |
|
|
|
39 |
list($options, $unrecognised) = cli_get_params(
|
|
|
40 |
[
|
|
|
41 |
'help' => false,
|
|
|
42 |
'feature' => '',
|
|
|
43 |
'disable-composer' => false,
|
|
|
44 |
'composer-upgrade' => true,
|
|
|
45 |
'composer-self-update' => true,
|
|
|
46 |
],
|
|
|
47 |
[
|
|
|
48 |
'h' => 'help',
|
|
|
49 |
'f' => 'feature',
|
|
|
50 |
]
|
|
|
51 |
);
|
|
|
52 |
|
|
|
53 |
// Checking run.php CLI script usage.
|
|
|
54 |
$help = "
|
|
|
55 |
Run a feature file into the current Moodle instance. The feature file can only
|
|
|
56 |
contains scenarios with core_data_generator steps. It is not yet compatible
|
|
|
57 |
with scenario outlines. All scenarios will be executed at once, event background
|
|
|
58 |
steps.
|
|
|
59 |
|
|
|
60 |
Usage:
|
|
|
61 |
php runtestscenario.php [--feature=\"value\"] [--help]
|
|
|
62 |
[--no-composer-self-update] [--no-composer-upgrade]
|
|
|
63 |
[--disable-composer]
|
|
|
64 |
|
|
|
65 |
Options:
|
|
|
66 |
-f, --feature Execute specified feature file (Absolute path of feature file).
|
|
|
67 |
|
|
|
68 |
--no-composer-self-update
|
|
|
69 |
Prevent upgrade of the composer utility using its self-update command
|
|
|
70 |
|
|
|
71 |
--no-composer-upgrade
|
|
|
72 |
Prevent update development dependencies using composer
|
|
|
73 |
|
|
|
74 |
--disable-composer
|
|
|
75 |
A shortcut to disable composer self-update and dependency update
|
|
|
76 |
Note: Installation of composer and/or dependencies will still happen as required
|
|
|
77 |
|
|
|
78 |
-h, --help Print out this help
|
|
|
79 |
|
|
|
80 |
Example from Moodle root directory:
|
|
|
81 |
\$ php admin/tool/generator/cli/runtestscenario.php --feature=/path/to/some/testing/scenario.feature
|
|
|
82 |
";
|
|
|
83 |
|
|
|
84 |
if (!empty($options['help'])) {
|
|
|
85 |
echo $help;
|
|
|
86 |
exit(0);
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
// The command will install composer if not present. Usually composer libraries are
|
|
|
90 |
// installed when behat or phpunit are installed, but we do not want to force users
|
|
|
91 |
// to create all phpunit or behat databases tables just to run a test scenario locally.
|
|
|
92 |
if (!file_exists($CFG->dirroot . '/vendor/autoload.php')) {
|
|
|
93 |
// Force OPcache reset if used, we do not want any stale caches
|
|
|
94 |
// when preparing test environment.
|
|
|
95 |
if (function_exists('opcache_reset')) {
|
|
|
96 |
opcache_reset();
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
if ($options['disable-composer']) {
|
|
|
100 |
// Disable self-update and upgrade easily.
|
|
|
101 |
// Note: Installation will still occur regardless of this setting.
|
|
|
102 |
$options['composer-self-update'] = false;
|
|
|
103 |
$options['composer-upgrade'] = false;
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
// Install and update composer and dependencies as required.
|
|
|
107 |
testing_update_composer_dependencies($options['composer-self-update'], $options['composer-upgrade']);
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
if (empty($options['feature'])) {
|
|
|
111 |
echo "Missing feature file path.\n";
|
|
|
112 |
exit(0);
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
$featurefile = $options['feature'];
|
|
|
116 |
if (!file_exists($featurefile)) {
|
|
|
117 |
echo "Feature file not found.\n";
|
|
|
118 |
exit(0);
|
|
|
119 |
}
|
|
|
120 |
|
11 |
efrain |
121 |
// Switch to admin user account.
|
|
|
122 |
\core\session\manager::set_user(get_admin());
|
|
|
123 |
|
1 |
efrain |
124 |
$runner = new tool_generator\local\testscenario\runner();
|
|
|
125 |
|
|
|
126 |
try {
|
|
|
127 |
$runner->init();
|
|
|
128 |
} catch (Exception $e) {
|
|
|
129 |
echo "Something is wrong with the behat setup.\n";
|
|
|
130 |
echo " Please,try running \"php admin/tool/behat/cli/init.php\" from your Moodle root directory.\n";
|
|
|
131 |
exit(0);
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
$content = file_get_contents($featurefile);
|
|
|
135 |
|
|
|
136 |
if (empty($content)) {
|
|
|
137 |
echo "The feature file is empty.\n";
|
|
|
138 |
exit(0);
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
try {
|
|
|
142 |
$parsedfeature = $runner->parse_feature($content);
|
|
|
143 |
} catch (\Exception $error) {
|
|
|
144 |
echo "Error parsing feature file: {$error->getMessage()}\n";
|
|
|
145 |
echo "Use the web version of the tool to see the parsing details:\n";
|
|
|
146 |
echo " Site administration -> development -> Create testing scenarios\n";
|
|
|
147 |
exit(0);
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
if (!$parsedfeature->is_valid()) {
|
|
|
151 |
echo "The file is not valid: {$parsedfeature->get_general_error()}\n";
|
|
|
152 |
echo "Use the web version of the tool to see the details:\n";
|
|
|
153 |
echo " Site administration -> development -> Create testing scenarios\n";
|
|
|
154 |
exit(0);
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
$total = 0;
|
|
|
158 |
$success = 0;
|
|
|
159 |
|
|
|
160 |
foreach ($parsedfeature->get_all_steps() as $step) {
|
|
|
161 |
if ($step->execute()) {
|
|
|
162 |
echo "\nOK: {$step->get_text()}\n";
|
|
|
163 |
echo "{$step->get_arguments_string()}\n";
|
|
|
164 |
$success++;
|
|
|
165 |
} else {
|
|
|
166 |
echo "\nFAIL: {$step->get_text()}\n";
|
|
|
167 |
echo "{$step->get_arguments_string()}\n";
|
|
|
168 |
echo "{$step->get_error()}\n";
|
|
|
169 |
}
|
|
|
170 |
$total++;
|
|
|
171 |
}
|
|
|
172 |
|
|
|
173 |
echo "\n{$success}/{$total} steps executed successfully.\n";
|
|
|
174 |
|
|
|
175 |
if ($success < $total) {
|
|
|
176 |
echo "\nSome steps failed.\n";
|
|
|
177 |
exit(1);
|
|
|
178 |
} else {
|
|
|
179 |
echo "\nAll steps executed successfully.\n";
|
|
|
180 |
exit(0);
|
|
|
181 |
}
|