source: moodle/trunk/fuentes/admin/tool/behat/cli/init.php @ 1331

Last change on this file since 1331 was 1331, checked in by jrpelegrina, 3 years ago

Updated to moodle 3.0.3

File size: 4.2 KB
Line 
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 * CLI script to set up all the behat test environment.
19 *
20 * @package    tool_behat
21 * @copyright  2013 David Monllaó
22 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25if (isset($_SERVER['REMOTE_ADDR'])) {
26    die(); // No access from web!
27}
28
29// Force OPcache reset if used, we do not want any stale caches
30// when preparing test environment.
31if (function_exists('opcache_reset')) {
32    opcache_reset();
33}
34
35// Is not really necessary but adding it as is a CLI_SCRIPT.
36define('CLI_SCRIPT', true);
37define('CACHE_DISABLE_ALL', true);
38
39// Basic functions.
40require_once(__DIR__ . '/../../../../lib/clilib.php');
41require_once(__DIR__ . '/../../../../lib/behat/lib.php');
42
43list($options, $unrecognized) = cli_get_params(
44    array(
45        'parallel' => 0,
46        'maxruns'  => false,
47        'help'     => false,
48        'fromrun'  => 1,
49        'torun'    => 0,
50    ),
51    array(
52        'j' => 'parallel',
53        'm' => 'maxruns',
54        'h' => 'help',
55    )
56);
57
58// Checking run.php CLI script usage.
59$help = "
60Behat utilities to initialise behat tests
61
62Usage:
63  php init.php [--parallel=value [--maxruns=value] [--fromrun=value --torun=value]] [--help]
64
65Options:
66-j, --parallel Number of parallel behat run to initialise
67-m, --maxruns  Max parallel processes to be executed at one time.
68--fromrun      Execute run starting from (Used for parallel runs on different vms)
69--torun        Execute run till (Used for parallel runs on different vms)
70
71-h, --help     Print out this help
72
73Example from Moodle root directory:
74\$ php admin/tool/behat/cli/init.php --parallel=2
75
76More info in http://docs.moodle.org/dev/Acceptance_testing#Running_tests
77";
78
79if (!empty($options['help'])) {
80    echo $help;
81    exit(0);
82}
83
84// Check which util file to call.
85$utilfile = 'util_single_run.php';
86$paralleloption = "";
87// If parallel run then use utilparallel.
88if ($options['parallel'] && $options['parallel'] > 1) {
89    $utilfile = 'util.php';
90    $paralleloption = "";
91    foreach ($options as $option => $value) {
92        if ($value) {
93            $paralleloption .= " --$option=\"$value\"";
94        }
95    }
96}
97
98// Changing the cwd to admin/tool/behat/cli.
99$cwd = getcwd();
100$output = null;
101
102// If behat dependencies not downloaded then do it first, else symfony/process can't be used.
103testing_update_composer_dependencies();
104
105// Check whether the behat test environment needs to be updated.
106chdir(__DIR__);
107exec("php $utilfile --diag $paralleloption", $output, $code);
108
109if ($code == 0) {
110    echo "Behat test environment already installed\n";
111
112} else if ($code == BEHAT_EXITCODE_INSTALL) {
113    // Behat and dependencies are installed and we need to install the test site.
114    chdir(__DIR__);
115    passthru("php $utilfile --install $paralleloption", $code);
116    if ($code != 0) {
117        chdir($cwd);
118        exit($code);
119    }
120
121} else if ($code == BEHAT_EXITCODE_REINSTALL) {
122    // Test site data is outdated.
123    chdir(__DIR__);
124    passthru("php $utilfile --drop $paralleloption", $code);
125    if ($code != 0) {
126        chdir($cwd);
127        exit($code);
128    }
129
130    chdir(__DIR__);
131    passthru("php $utilfile --install $paralleloption", $code);
132    if ($code != 0) {
133        chdir($cwd);
134        exit($code);
135    }
136
137} else {
138    // Generic error, we just output it.
139    echo implode("\n", $output)."\n";
140    chdir($cwd);
141    exit($code);
142}
143
144// Enable editing mode according to config.php vars.
145chdir(__DIR__);
146passthru("php $utilfile --enable $paralleloption", $code);
147if ($code != 0) {
148    echo "Error enabling site" . PHP_EOL;
149    chdir($cwd);
150    exit($code);
151}
152
153exit(0);
Note: See TracBrowser for help on using the repository browser.