Issue #27 - CLI implemented. Added full test coverage to CLI classes.

This commit is contained in:
Daniel Thee Roperto
2016-09-16 17:26:48 +10:00
parent 2cfda63922
commit f569157368
16 changed files with 1429 additions and 15 deletions

View File

@@ -0,0 +1,61 @@
<?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/>.
use auth_outage\cli\clibase;
defined('MOODLE_INTERNAL') || die();
/**
* Tests performed on CLIs.
*
* @package auth_outage
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
* @copyright Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class cli_testcase extends advanced_testcase {
public function setUp() {
$this->resetAfterTest(true);
$this->set_parameters([]);
parent::setUp();
}
/**
* Mocks the command line parameters.
* @param array $options Options to use as parameters.
*/
protected function set_parameters(array $options) {
array_unshift($options, 'cli.php');
$_SERVER['argv'] = $options;
$_SERVER['argc'] = count($options);
}
/**
* Executes the CLI.
* @param clibase $cli CLI to execute.
* @return string The output text.
*/
protected function execute(clibase $cli) {
ob_start();
try {
$cli->execute();
$text = ob_get_contents();
return $text;
} finally {
ob_end_clean();
}
}
}

270
tests/cli/create_test.php Normal file
View File

@@ -0,0 +1,270 @@
<?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/>.
use auth_outage\cli\cliexception;
use auth_outage\cli\create;
use auth_outage\models\outage;
use auth_outage\outagedb;
defined('MOODLE_INTERNAL') || die();
require_once('cli_testcase.php');
/**
* Tests performed on CLI create class.
*
* @package auth_outage
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
* @copyright Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @SuppressWarnings("public")
*/
class create_test extends cli_testcase {
public function test_noarguments() {
$cli = new create();
$this->setExpectedException(cliexception::class);
$this->execute($cli);
}
public function test_invalidargumentparam() {
$this->set_parameters(['--aninvalidparameter']);
$this->setExpectedException(cliexception::class);
new create();
}
public function test_invalidargumentgiven() {
$this->setExpectedException(cliexception::class);
new create(['anotherinvalidparameter']);
}
public function test_invalidparam_notanumber() {
$cli = new create(['start' => 'some day']);
$cli->set_defaults([
'warn' => 50,
'start' => 200,
'duration' => 300,
'title' => 'Default Title',
'description' => 'Default Description',
]);
$this->setExpectedException(cliexception::class);
$this->execute($cli);
}
public function test_invalidparam_negative() {
$cli = new create(['start' => -1]);
$cli->set_defaults([
'warn' => 50,
'start' => 200,
'duration' => 300,
'title' => 'Default Title',
'description' => 'Default Description',
]);
$this->setExpectedException(cliexception::class);
$this->execute($cli);
}
public function test_invalidparam_emptystring() {
$cli = new create(['start' => 0, 'title' => '']);
$cli->set_defaults([
'warn' => 50,
'start' => 200,
'duration' => 300,
'title' => 'Default Title',
'description' => 'Default Description',
]);
$this->setExpectedException(cliexception::class);
$this->execute($cli);
}
public function test_invalidparam_notastring() {
$cli = new create(['start' => 0, 'title' => true]);
$cli->set_defaults([
'warn' => 50,
'start' => 200,
'duration' => 300,
'title' => 'Default Title',
'description' => 'Default Description',
]);
$this->setExpectedException(cliexception::class);
$this->execute($cli);
}
public function test_setreferencetime_invalid() {
$cli = new create(['start' => 0]);
$this->setExpectedException(InvalidArgumentException::class);
$cli->set_referencetime(-1);
}
public function test_help() {
$this->set_parameters(['--help']);
$cli = new create();
$output = $this->execute($cli);
self::assertContains('Creates', $output);
self::assertContains('--help', $output);
}
public function test_options() {
$cli = new create();
$options = $cli->generateoptions();
foreach (array_keys($options) as $k) {
self::assertTrue(is_string($k));
}
$shorts = $cli->generateshortcuts();
foreach ($shorts as $s) {
self::assertArrayHasKey($s, $options);
}
}
public function test_create_withoptions() {
$this->set_parameters([
'--warn=10',
'--start=0',
'--duration=30',
'--title=A Title',
'--description=A Description',
]);
$now = time();
$cli = new create();
$cli->set_referencetime($now);
$text = $this->execute($cli);
self::assertContains('created', $text);
// Check creted outage.
list(, $id) = explode(':', $text);
$id = (int)$id;
$outage = outagedb::get_by_id($id);
self::assertSame($now, $outage->starttime);
self::assertSame(10 * 60, $outage->get_warning_duration());
self::assertSame(30 * 60, $outage->get_duration());
self::assertNull($outage->finished);
self::assertSame('A Title', $outage->title);
self::assertSame('A Description', $outage->description);
}
public function test_create_onlyid() {
$this->set_parameters([
'--onlyid',
'--warn=10',
'--start=0',
'--duration=30',
'--title=Title',
'--description=Description',
]);
$now = time();
$cli = new create();
$cli->set_referencetime($now);
$id = $this->execute($cli);
// Check if the id contains is only a number (parameter onlyid).
$id = trim($id);
self::assertTrue(is_number($id));
$id = (int)$id;
// Check creted outage.
$outage = outagedb::get_by_id($id);
self::assertSame($now, $outage->starttime);
self::assertSame($outage->starttime - (10 * 60), $outage->warntime);
self::assertSame($outage->starttime + (30 * 60), $outage->stoptime);
self::assertNull($outage->finished);
self::assertSame('Title', $outage->title);
self::assertSame('Description', $outage->description);
}
public function test_create_withdefaults() {
$this->set_parameters([
'--warn=100',
'--start=50',
]);
$now = time();
$cli = new create();
$cli->set_referencetime($now);
$cli->set_defaults([
'warn' => 50,
'start' => 200,
'duration' => 300,
'title' => 'Default Title',
'description' => 'Default Description',
]);
$text = $this->execute($cli);
self::assertContains('created', $text);
// Check creted outage.
list(, $id) = explode(':', $text);
$id = (int)$id;
$outage = outagedb::get_by_id($id);
self::assertSame($now + (50 * 60), $outage->starttime, 'Wrong starttime.');
self::assertSame($outage->starttime - (100 * 60), $outage->warntime, 'Wrong warntime.');
self::assertSame($outage->starttime + (300 * 60), $outage->stoptime, 'Wrong stoptime.');
self::assertNull($outage->finished);
self::assertSame('Default Title', $outage->title);
self::assertSame('Default Description', $outage->description);
}
public function test_create_withclone() {
$this->setAdminUser();
$now = time();
// Create the outage to clone.
$original = new outage([
'warntime' => $now - 120,
'starttime' => $now,
'stoptime' => $now + 120,
'title' => 'Title',
'description' => 'Description',
]);
$id = outagedb::save($original);
// Clone it using CLI.
$this->set_parameters([
'--onlyid',
'--start=60',
'--clone=' . $id,
]);
$cli = new create();
$cli->set_referencetime($now);
$id = trim($this->execute($cli));
// Check cloned data.
$cloned = outagedb::get_by_id((int)$id);
self::assertSame($now + (60 * 60), $cloned->starttime);
self::assertSame($original->get_warning_duration(), $cloned->get_warning_duration());
self::assertSame($original->get_duration(), $cloned->get_duration());
self::assertSame($original->title, $cloned->title);
self::assertSame($original->description, $cloned->description);
}
public function test_create_withclone_invalid() {
$this->setExpectedException(cliexception::class);
$this->set_parameters([
'--start=60',
'--clone=-1',
]);
$cli = new create();
$this->execute($cli);
}
public function test_create_withblock() {
// Not an extensive test in the blocking API, cliwaitforit tests should cover them deeper.
$this->set_parameters([
'--block',
'--warn=10',
'--start=0',
'--duration=30',
'--title=Title',
'--description=Description',
]);
$now = time();
$cli = new create();
$cli->set_referencetime($now);
$text = $this->execute($cli);
self::assertContains('created', $text);
self::assertContains('started', $text);
}
}

64
tests/cli/finish_test.php Normal file
View File

@@ -0,0 +1,64 @@
<?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/>.
use auth_outage\cli\cliexception;
use auth_outage\cli\finish;
defined('MOODLE_INTERNAL') || die();
require_once('cli_testcase.php');
/**
* Tests performed on CLI finish class.
*
* @package auth_outage
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
* @copyright Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class finish_test extends cli_testcase {
public function test_constructor() {
$cli = new finish();
self::assertNotNull($cli);
}
public function test_options() {
$cli = new finish();
$options = $cli->generateoptions();
foreach (array_keys($options) as $k) {
self::assertTrue(is_string($k));
}
$shorts = $cli->generateshortcuts();
foreach ($shorts as $s) {
self::assertArrayHasKey($s, $options);
}
}
public function test_help() {
$this->set_parameters(['--help']);
$cli = new finish();
$text = $this->execute($cli);
self::assertContains('Finishes', $text);
self::assertContains('--help', $text);
}
public function test_noarguments() {
$cli = new finish();
$this->setExpectedException(cliexception::class);
$this->execute($cli);
}
}

View File

@@ -0,0 +1,171 @@
<?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/>.
use auth_outage\cli\cliexception;
use auth_outage\cli\waitforit;
use auth_outage\models\outage;
use auth_outage\outagedb;
defined('MOODLE_INTERNAL') || die();
require_once('cli_testcase.php');
/**
* Tests performed on CLI waitforit class.
*
* @package auth_outage
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
* @copyright Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @SuppressWarnings("public")
*/
class waitforit_test extends cli_testcase {
public function test_constructor() {
$cli = new waitforit();
self::assertNotNull($cli);
}
public function test_generateoptions() {
$cli = new waitforit();
$options = $cli->generateoptions();
foreach (array_keys($options) as $k) {
self::assertTrue(is_string($k));
}
}
public function test_generateshortcuts() {
$cli = new waitforit();
$options = $cli->generateoptions();
$shorts = $cli->generateshortcuts();
foreach ($shorts as $s) {
self::assertArrayHasKey($s, $options);
}
}
public function test_help() {
$this->set_parameters(['--help']);
$cli = new waitforit();
$text = $this->execute($cli);
self::assertContains('Waits', $text);
self::assertContains('--help', $text);
}
public function test_bothparams() {
$this->set_parameters(['--outageid=1', '--active']);
$cli = new waitforit();
$this->setExpectedException(cliexception::class);
$cli->execute();
}
public function test_invalidoutageid() {
$this->set_parameters(['-id=-1']);
$cli = new waitforit();
$this->setExpectedException(cliexception::class);
$this->execute($cli);
}
public function test_outagenotfound() {
$this->set_parameters(['-a']);
$cli = new waitforit();
$this->setExpectedException(cliexception::class);
$this->execute($cli);
}
public function test_endedoutage() {
$this->setAdminUser();
$now = time();
$id = outagedb::save(new outage([
'warntime' => $now - 200,
'starttime' => $now - 100,
'stoptime' => $now - 50,
'title' => 'Title',
'description' => 'Description',
]));
$this->set_parameters(['-id=' . $id]);
$cli = new waitforit();
$cli->set_referencetime($now);
$this->setExpectedException(cliexception::class);
$this->execute($cli);
}
public function test_activeverbose() {
$this->setAdminUser();
$now = time();
outagedb::save(new outage([
'warntime' => $now - 10,
'starttime' => $now + 1,
'stoptime' => $now + 10,
'title' => 'Title',
'description' => 'Description',
]));
$this->set_parameters(['-v', '--active']);
$cli = new waitforit();
$cli->set_referencetime($now);
$output = $this->execute($cli);
self::assertContains('Verbose mode', $output);
self::assertContains('starting in 1 sec', $output);
self::assertContains('started', $output);
}
public function test_countdown() {
$this->setAdminUser();
$now = time();
outagedb::save(new outage([
'warntime' => $now,
'starttime' => $now + 45,
'stoptime' => $now + (60 * 60),
'title' => 'Title',
'description' => 'Description',
]));
$this->set_parameters(['-v', '--active', '--sleep=30']);
$cli = new waitforit();
$cli->set_referencetime($now);
$cli->set_sleepcallback(function ($sleep) use (&$now) {
$now += $sleep;
return $now;
});
$output = $this->execute($cli);
self::assertContains("starting in 45", $output);
self::assertContains("sleep 30 second", $output);
self::assertContains("starting in 15", $output);
self::assertContains("sleep 15 second", $output);
self::assertContains("started!", $output);
}
public function test_outagechanged() {
$this->setAdminUser();
$now = time();
$id = outagedb::save(new outage([
'warntime' => $now,
'starttime' => $now + (2 * 60 * 60),
'stoptime' => $now + (60 * 60),
'title' => 'Title',
'description' => 'Description',
]));
$this->set_parameters(['-v', '--active', '--sleep=30']);
$cli = new waitforit();
$cli->set_referencetime($now);
$cli->set_sleepcallback(function () use ($id) {
// Change outage when not expected to.
$outage = outagedb::get_by_id($id);
$outage->title = 'New title!';
outagedb::save($outage);
// Pretend it is time to start, but it should get an error instead.
return $outage->starttime;
});
$this->setExpectedException(cliexception::class);
$this->execute($cli);
}
}

31
tests/phpunit.xml Normal file
View File

@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../lib/phpunit/phpunit.xsd"
bootstrap="../../../lib/phpunit/bootstrap.php"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
backupGlobals="false"
backupStaticAttributes="false"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
printerClass="Hint_ResultPrinter"
testSuiteLoaderClass="phpunit_autoloader"
>
<testsuites>
<testsuite name="auth_outage_testsuite">
<directory suffix="_test.php">./</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="false">
<directory suffix=".php">../classes</directory>
</whitelist>
</filter>
</phpunit>