Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

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