Merge branch 'MDL-49840-master' of git://github.com/andrewnicols/moodle
[moodle.git] / admin / tool / behat / cli / init.php
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/>.
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  */
25 if (isset($_SERVER['REMOTE_ADDR'])) {
26     die(); // No access from web!
27 }
29 // Force OPcache reset if used, we do not want any stale caches
30 // when preparing test environment.
31 if (function_exists('opcache_reset')) {
32     opcache_reset();
33 }
35 // Is not really necessary but adding it as is a CLI_SCRIPT.
36 define('CLI_SCRIPT', true);
37 define('CACHE_DISABLE_ALL', true);
39 // Basic functions.
40 require_once(__DIR__ . '/../../../../lib/clilib.php');
41 require_once(__DIR__ . '/../../../../lib/behat/lib.php');
43 list($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 );
58 // Checking run.php CLI script usage.
59 $help = "
60 Behat utilities to initialise behat tests
62 Usage:
63   php init.php [--parallel=value [--maxruns=value] [--fromrun=value --torun=value]] [--help]
65 Options:
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)
71 -h, --help     Print out this help
73 Example from Moodle root directory:
74 \$ php admin/tool/behat/cli/init.php --parallel=2
76 More info in http://docs.moodle.org/dev/Acceptance_testing#Running_tests
77 ";
79 if (!empty($options['help'])) {
80     echo $help;
81     exit(0);
82 }
84 // Check which util file to call.
85 $utilfile = 'util_single_run.php';
86 $paralleloption = "";
87 // If parallel run then use utilparallel.
88 if ($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 }
98 // Changing the cwd to admin/tool/behat/cli.
99 $cwd = getcwd();
100 $output = null;
102 // If behat dependencies not downloaded then do it first, else symfony/process can't be used.
103 testing_update_composer_dependencies();
105 // Check whether the behat test environment needs to be updated.
106 chdir(__DIR__);
107 exec("php $utilfile --diag $paralleloption", $output, $code);
109 if ($code == 0) {
110     echo "Behat test environment already installed\n";
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     }
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     }
130     chdir(__DIR__);
131     passthru("php $utilfile --install $paralleloption", $code);
132     if ($code != 0) {
133         chdir($cwd);
134         exit($code);
135     }
137 } else {
138     // Generic error, we just output it.
139     echo implode("\n", $output)."\n";
140     chdir($cwd);
141     exit($code);
144 // Enable editing mode according to config.php vars.
145 chdir(__DIR__);
146 passthru("php $utilfile --enable $paralleloption", $code);
147 if ($code != 0) {
148     echo "Error enabling site" . PHP_EOL;
149     chdir($cwd);
150     exit($code);
153 exit(0);