Skip to content

Commit

Permalink
MDL-32323 experimental web UI for execution of PHPUnit tests
Browse files Browse the repository at this point in the history
It is not localised intentionally because only developers should use it…
  • Loading branch information
skodak committed Apr 10, 2012
1 parent 4b17369 commit 7b0ff21
Show file tree
Hide file tree
Showing 10 changed files with 417 additions and 131 deletions.
34 changes: 18 additions & 16 deletions admin/tool/phpunit/cli/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,24 @@
CLIDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
UTIL="$CLIDIR/util.php"

echo "Building phpunit.xml and initialising test database..."
echo "Initialising test database and creating phpunit.xml configuration..."

php $UTIL --buildconfig
RESULT=$?
if [ $RESULT -gt 0 ] ; then
exit $RESULT
fi

php $UTIL --drop
RESULT=$?
if [ $RESULT -gt 0 ] ; then
exit $RESULT
DIGERROR=`php $UTIL --diag`
DIAG=$?
if [ $DIAG -eq 132 ] ; then
php $UTIL --install
else
if [ $DIAG -eq 133 ] ; then
php $UTIL --drop
RESULT=$?
if [ $RESULT -gt 0 ] ; then
exit $RESULT
fi
php $UTIL --install
else
echo $DIGERROR
exit $DIAG
fi
fi

php $UTIL --install
RESULT=$?
if [ $RESULT -gt 0 ] ; then
exit $RESULT
fi
php $UTIL --buildconfig
31 changes: 24 additions & 7 deletions admin/tool/phpunit/cli/util.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
* Exit codes:
* 0 - success
* 1 - general error
* 130 - coding error
* 130 - missing PHPUnit error
* 131 - configuration problem
* 132 - install new test database
* 133 - drop existing data before installing
* 134 - can not create main phpunit.xml
*
* @package tool_phpunit
* @copyright 2012 Petr Skoda {@link http://skodak.org}
Expand All @@ -31,10 +33,11 @@

define('PHPUNIT_UTIL', true);

require_once(__DIR__ . '/../../../../lib/phpunit/bootstraplib.php');

// verify PHPUnit installation
if (!@include_once('PHPUnit/Autoload.php')) {
fwrite(STDERR, "Can not load PHPUnit PEAR library, is it installed?\n");
exit(1);
phpunit_bootstrap_error(130);
}

require(__DIR__ . '/../../../../lib/phpunit/bootstrap.php');
Expand All @@ -51,6 +54,7 @@
'drop' => false,
'install' => false,
'buildconfig' => false,
'diag' => false,
'help' => false,
),
array(
Expand All @@ -63,31 +67,44 @@
cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
}

$diag = $options['diag'];
$drop = $options['drop'];
$install = $options['install'];
$buildconfig = $options['buildconfig'];

if ($options['help'] or (!$drop and !$install and !$buildconfig)) {
if ($options['help'] or (!$drop and !$install and !$buildconfig and !$diag)) {
$help = "Various PHPUnit utility functions
Options:
--drop Drop database and dataroot
--install Install database
--buildconfig Build /phpunit.xml from /phpunit.xml.dist that includes suites for all plugins and core
--diag Diagnose installation and return error code only
-h, --help Print out this help
Example:
\$/usr/bin/php lib/phpunit/tool.php
";
echo $help;
die;
exit(0);
}

if ($buildconfig) {
phpunit_util::build_config_file();
if ($diag) {
list($errorcode, $message) = phpunit_util::testing_ready_problem();
if ($errorcode) {
phpunit_bootstrap_error($errorcode, $message);
}
exit(0);

} else if ($buildconfig) {
if (phpunit_util::build_config_file()) {
exit(0);
} else {
phpunit_bootstrap_error(134);
}


} else if ($drop) {
phpunit_util::drop_site();
// note: we must stop here because $CFG is messed up and we can not reinstall, sorry
Expand Down
2 changes: 2 additions & 0 deletions admin/tool/phpunit/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@
defined('MOODLE_INTERNAL') || die;

$ADMIN->add('development', new admin_externalpage('toolphpunit', get_string('pluginname', 'tool_phpunit'), "$CFG->wwwroot/$CFG->admin/tool/phpunit/index.php"));
$ADMIN->add('development', new admin_externalpage('toolphpunitwebrunner', get_string('pluginname', 'tool_phpunit'), "$CFG->wwwroot/$CFG->admin/tool/phpunit/webrunner.php",
'moodle/site:config', true));
196 changes: 196 additions & 0 deletions admin/tool/phpunit/webrunner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* PHPUnit shell execution wrapper
*
* @package tool_phpunit
* @copyright 2012 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

define('NO_OUTPUT_BUFFERING', true);

require(dirname(__FILE__) . '/../../../config.php');
require_once($CFG->libdir.'/adminlib.php');

$path = optional_param('path', null, PARAM_PATH);
$execute = optional_param('execute', 0, PARAM_BOOL);


navigation_node::override_active_url(new moodle_url('/admin/tool/phpunit/index.php'));
admin_externalpage_setup('toolphpunitwebrunner');

if (!debugging('', DEBUG_DEVELOPER)) {
error('Not available on production sites, sorry.');
}

set_time_limit(60*30);

$oldcwd = getcwd();
$code = 0;

if (!isset($CFG->phpunit_dataroot) or !isset($CFG->phpunit_prefix)) {
tool_phpunit_problem('Missing $CFG->phpunit_dataroot or $CFG->phpunit_prefix, can not execute tests.');
}
if (!file_exists($CFG->phpunit_dataroot)) {
mkdir($CFG->phpunit_dataroot, 02777, true);
}
if (!is_writable($CFG->phpunit_dataroot)) {
tool_phpunit_problem('$CFG->phpunit_dataroot in not writable, can not execute tests.');
}
$output = null;
exec('phpunit --version', $output, $code);
if ($code != 0) {
tool_phpunit_problem('Can not execute \'phpunit\' script.');
}
$output = null;
exec('php --version', $output, $code);
if ($code != 0) {
tool_phpunit_problem('Can not execute \'php\' binary.');
}

if ($execute) {

require_sesskey();

chdir($CFG->dirroot);
$output = null;
exec("php $CFG->admin/tool/phpunit/cli/util.php --diag", $output, $code);
if ($code == 0) {
// everything is ready

} else if ($code == 132) {
tool_phpunit_header();
echo $OUTPUT->box_start('generalbox');
echo '<pre>';
echo "Initialising test database:\n\n";
chdir($CFG->dirroot);
ignore_user_abort(true);
passthru("php $CFG->admin/tool/phpunit/cli/util.php --buildconfig", $code);
passthru("php $CFG->admin/tool/phpunit/cli/util.php --install", $code);
chdir($oldcwd);
echo '</pre>';
echo $OUTPUT->box_end();
if ($code != 0) {
tool_phpunit_problem('Can not initialize database');
}
$CFG->debug = 0; // no pesky redirect warning, we really want to redirect
redirect(new moodle_url($PAGE->url, array('execute'=>1, 'path'=>$path, 'sesskey'=>sesskey())), 'Reloading page');
echo $OUTPUT->footer();
die();

} else if ($code == 133) {
tool_phpunit_header();
ignore_user_abort(true);
echo $OUTPUT->box_start('generalbox');
echo '<pre>';
echo "Reinitialising test database:\n\n";
chdir($CFG->dirroot);
ignore_user_abort(true);
passthru("php $CFG->admin/tool/phpunit/cli/util.php --drop", $code);
passthru("php $CFG->admin/tool/phpunit/cli/util.php --buildconfig", $code);
passthru("php $CFG->admin/tool/phpunit/cli/util.php --install", $code);
chdir($oldcwd);
echo '</pre>';
echo $OUTPUT->box_end();
if ($code != 0) {
tool_phpunit_problem('Can not initialize database');
}
$CFG->debug = 0; // no pesky redirect warning, we really want to redirect
redirect(new moodle_url($PAGE->url, array('execute'=>1, 'path'=>$path, 'sesskey'=>sesskey())), 'Reloading page');
die();

} else {
tool_phpunit_header();
echo $OUTPUT->box_start('generalbox');
echo '<pre>';
echo "Error: $code\n\n";
echo implode("\n", $output);
echo '</pre>';
echo $OUTPUT->box_end();
tool_phpunit_problem('Can not execute tests');
die();
}

tool_phpunit_header();
echo $OUTPUT->box_start('generalbox');
echo '<pre>';

chdir($CFG->dirroot);
// use the dataroot file
$configdir = "$CFG->phpunit_dataroot/phpunit/webrunner.xml";
if (!file_exists($configdir)) {
passthru("php $CFG->admin/tool/phpunit/cli/util.php --buildconfig", $code);
if ($code != 0) {
tool_phpunit_problem('Can not create configuration file');
}
}
$configdir = escapeshellarg($configdir);
// cleanup the path - this is tricky because we can not use quotes for escaping
$path = escapeshellcmd($path);
$path = str_replace('\*', '*', $path);
passthru("phpunit -c $configdir $path", $code);
chdir($oldcwd);

echo '</pre>';
echo $OUTPUT->box_end();

} else {
tool_phpunit_header();
}

echo $OUTPUT->box_start('generalbox boxwidthwide boxaligncenter');
echo '<form method="get" action="webrunner.php">';
echo '<fieldset class="invisiblefieldset">';
echo '<label for="path">Test only</label> ';
echo '<input type="text" id="path" name="path" value="'.s($path).'" size="40" />';
echo '</p>';
echo '<input type="submit" value="Run" />';
echo '<input type="hidden" name="execute" value="1" />';
echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
echo '</fieldset>';
echo '</form>';
echo $OUTPUT->box_end();
echo $OUTPUT->footer();
die;



//========================================

/**
* Print headers and warning
*/
function tool_phpunit_header() {
global $OUTPUT;
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('pluginname', 'tool_phpunit'));
echo $OUTPUT->box('EXPERIMENTAL: it is recommended to execute PHPUnit tests and init scripts only from command line.', array('generalbox'));
}

/**
* Called when PHPUnit can not execute.
* @param string $message
* @return void
*/
function tool_phpunit_problem($message) {
global $OUTPUT, $PAGE;
if (!$PAGE->headerprinted) {
tool_phpunit_header();
}
notice($message, new moodle_url('/admin/tool/phpunit/'));
}
Loading

0 comments on commit 7b0ff21

Please sign in to comment.