Proyectos de Subversion Moodle

Rev

Rev 1 | | 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 - 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
 
1441 ariadna 33
// It makes no sense to use BEHAT_CLI for this script (you cannot initialise PHPunit starting from
34
// the Behat environment), so in case user has set tne environment variable, disable it.
35
putenv('BEHAT_CLI=0');
36
 
1 efrain 37
require_once(__DIR__.'/../../../../lib/clilib.php');
38
require_once(__DIR__.'/../../../../lib/phpunit/bootstraplib.php');
39
require_once(__DIR__.'/../../../../lib/testing/lib.php');
40
 
41
// Now get cli options.
42
list($options, $unrecognized) = cli_get_params(
43
    [
44
        'drop'                  => false,
45
        'install'               => false,
46
        'buildconfig'           => false,
47
        'buildcomponentconfigs' => false,
48
        'diag'                  => false,
49
        'run'                   => false,
50
        'help'                  => false,
51
    ],
52
    [
53
        'h' => 'help',
54
    ]
55
);
56
 
57
// Basic check to see if phpunit is installed.
58
if (!file_exists(__DIR__.'/../../../../vendor/phpunit/phpunit/composer.json') ||
59
        !file_exists(__DIR__.'/../../../../vendor/bin/phpunit') ||
60
        !file_exists(__DIR__.'/../../../../vendor/autoload.php')) {
61
    phpunit_bootstrap_error(PHPUNIT_EXITCODE_PHPUNITMISSING);
62
}
63
 
64
if ($options['install'] || $options['drop']) {
65
    define('CACHE_DISABLE_ALL', true);
66
}
67
 
68
if ($options['run']) {
69
    unset($options);
70
    unset($unrecognized);
71
 
72
    foreach ($_SERVER['argv'] as $k => $v) {
73
        if (strpos($v, '--run') === 0) {
74
            unset($_SERVER['argv'][$k]);
75
            $_SERVER['argc'] = $_SERVER['argc'] - 1;
76
        }
77
    }
78
    $_SERVER['argv'] = array_values($_SERVER['argv']);
79
    require(__DIR__ . '/../../../../vendor/bin/phpunit');
80
    exit(0);
81
}
82
 
83
define('PHPUNIT_UTIL', true);
84
 
85
require(__DIR__.'/../../../../vendor/autoload.php');
86
require(__DIR__ . '/../../../../lib/phpunit/bootstrap.php');
87
 
88
// From now on this is a regular moodle CLI_SCRIPT.
89
 
90
require_once($CFG->libdir.'/adminlib.php');
91
require_once($CFG->libdir.'/upgradelib.php');
92
require_once($CFG->libdir.'/clilib.php');
93
require_once($CFG->libdir.'/installlib.php');
94
 
95
if ($unrecognized) {
96
    $unrecognized = implode("\n  ", $unrecognized);
97
    cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
98
}
99
 
100
$diag = $options['diag'];
101
$drop = $options['drop'];
102
$install = $options['install'];
103
$buildconfig = $options['buildconfig'];
104
$buildcomponentconfigs = $options['buildcomponentconfigs'];
105
 
106
if ($options['help'] || (!$drop && !$install && !$buildconfig && !$buildcomponentconfigs && !$diag)) {
107
    $help = "Various PHPUnit utility functions
108
 
109
Options:
110
--drop         Drop database and dataroot
111
--install      Install database
112
--diag         Diagnose installation and return error code only
113
--run          Execute PHPUnit tests (alternative for standard phpunit binary)
114
--buildconfig  Build /phpunit.xml from /phpunit.xml.dist that runs all tests
115
--buildcomponentconfigs
116
               Build distributed phpunit.xml files for each component
117
 
118
-h, --help     Print out this help
119
 
120
Example:
121
\$ php ".testing_cli_argument_path('/admin/tool/phpunit/cli/util.php')." --install
122
";
123
    echo $help;
124
    exit(0);
125
}
126
 
127
if ($diag) {
128
    list($errorcode, $message) = phpunit_util::testing_ready_problem();
129
    if ($errorcode) {
130
        phpunit_bootstrap_error($errorcode, $message);
131
    }
132
    exit(0);
133
 
134
} else if ($buildconfig) {
135
    if (phpunit_util::build_config_file()) {
136
        exit(0);
137
    } else {
138
        phpunit_bootstrap_error(
139
            PHPUNIT_EXITCODE_CONFIGWARNING,
140
            'Can not create main /phpunit.xml configuration file, verify dirroot permissions'
141
        );
142
    }
143
 
144
} else if ($buildcomponentconfigs) {
145
    phpunit_util::build_component_config_files();
146
    exit(0);
147
 
148
} else if ($drop) {
149
    // Make sure tests do not run in parallel.
150
    test_lock::acquire('phpunit');
151
    phpunit_util::drop_site(true);
152
    // Note: we must stop here because $CFG is messed up and we can not reinstall, sorry.
153
    exit(0);
154
 
155
} else if ($install) {
156
    phpunit_util::install_site();
157
    exit(0);
158
}