Added full phpunit coverage for classes in: cli and event

This commit is contained in:
Daniel Thee Roperto
2016-09-19 20:05:21 +10:00
parent 2959719c58
commit edbbc2dd83
15 changed files with 265 additions and 40 deletions

View File

@@ -19,10 +19,6 @@ namespace auth_outage\cli;
use core\session\manager;
use InvalidArgumentException;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir . '/clilib.php');
/**
* Outage CLI base class.
*
@@ -48,6 +44,9 @@ abstract class clibase {
* @throws cliexception
*/
public function __construct(array $options = null) {
global $CFG;
require_once($CFG->libdir . '/clilib.php');
$this->becomeadmin();
if (is_null($options)) {

View File

@@ -27,4 +27,13 @@ use Exception;
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class cliexception extends Exception {
/**
* cliexception constructor.
* @param string $message An explanation of the exception.
* @param int $code Exit code to be used.
* @param Exception|null $previous Another exception as reference.
*/
public function __construct($message, $code = 1, Exception $previous = null) {
parent::__construct('*ERROR* ' . $message, $code, $previous = null);
}
}

View File

@@ -19,8 +19,6 @@ namespace auth_outage\cli;
use auth_outage\models\outage;
use auth_outage\outagedb;
defined('MOODLE_INTERNAL') || die();
/**
* Outage CLI to create outage.
*

View File

@@ -19,8 +19,6 @@ namespace auth_outage\cli;
use auth_outage\models\outage;
use auth_outage\outagedb;
defined('MOODLE_INTERNAL') || die();
/**
* Outage CLI to finish an outage.
*

View File

@@ -19,8 +19,6 @@ namespace auth_outage\cli;
use auth_outage\models\outage;
use auth_outage\outagedb;
defined('MOODLE_INTERNAL') || die();
/**
* Outage CLI to wait for an outage to start.
*

View File

@@ -17,8 +17,7 @@
namespace auth_outage\event;
use core\event\base;
defined('MOODLE_INTERNAL') || die();
use moodle_url;
/**
* The auth_outage outage created class.
@@ -51,9 +50,9 @@ class outage_created extends base {
/**
* Returns relevant URL, override in subclasses.
* @return \moodle_url
* @return moodle_url
*/
public function get_url() {
return new \moodle_url('/auth/outage/list.php#auth_outage_id_' . $this->other['id']);
return new moodle_url('/auth/outage/list.php#auth_outage_id_' . $this->other['id']);
}
}

View File

@@ -18,8 +18,6 @@ namespace auth_outage\event;
use core\event\base;
defined('MOODLE_INTERNAL') || die();
/**
* The auth_outage outage deleted class.
*

View File

@@ -18,8 +18,6 @@ namespace auth_outage\event;
use core\event\base;
defined('MOODLE_INTERNAL') || die();
/**
* The auth_outage outage updated class.
*

View File

@@ -16,9 +16,7 @@
namespace auth_outage;
if (!defined('MOODLE_INTERNAL')) {
die('Direct access to this script is forbidden.'); // It must be included from a Moodle page.
}
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/calendar/lib.php');
@@ -259,7 +257,7 @@ class outagedb {
if (is_null($time)) {
$time = time();
}
if (!is_int($time)) {
if (!is_int($time) && ($time <= 0)) {
throw new InvalidArgumentException('$time must be an int or null.');
}

70
tests/cli/cli_test.php Normal file
View File

@@ -0,0 +1,70 @@
<?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;
defined('MOODLE_INTERNAL') || die();
require_once('cli_testcase.php');
/**
* Tests performed on CLI base and exception 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
* @covers \auth_outage\cli\clibase
* @covers \auth_outage\cli\cliexception
* @SuppressWarnings("public")
*/
class cli_test extends cli_testcase {
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_setreferencetime() {
$cli = new create(['start' => 0]);
$cli->set_referencetime(1);
$cli->set_referencetime(60 * 60 * 24 * 7);
}
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(['-h']);
$cli = new create();
$output = $this->execute($cli);
self::assertContains('-h', $output);
self::assertContains('--help', $output);
}
public function test_exception() {
self::setExpectedException(cliexception::class, '*ERROR* An CLI exception.', 5);
throw new cliexception('An CLI exception.', 5);
}
}

View File

@@ -29,7 +29,7 @@ require_once('cli_testcase.php');
* @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")
* @SuppressWarnings("public") Allow this test to have as many tests as necessary.
*/
class create_test extends cli_testcase {
public function test_noarguments() {
@@ -38,17 +38,6 @@ class create_test extends cli_testcase {
$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([
@@ -101,12 +90,6 @@ class create_test extends cli_testcase {
$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();

View File

@@ -16,6 +16,8 @@
use auth_outage\cli\cliexception;
use auth_outage\cli\finish;
use auth_outage\models\outage;
use auth_outage\outagedb;
defined('MOODLE_INTERNAL') || die();
require_once('cli_testcase.php');
@@ -27,6 +29,7 @@ require_once('cli_testcase.php');
* @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
* @covers \auth_outage\cli\finish
*/
class finish_test extends cli_testcase {
public function test_constructor() {
@@ -61,4 +64,61 @@ class finish_test extends cli_testcase {
$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 finish();
$cli->set_referencetime($now);
$this->setExpectedException(cliexception::class);
$this->execute($cli);
}
public function test_finish() {
$this->setAdminUser();
$now = time();
$id = outagedb::save(new outage([
'warntime' => $now - 200,
'starttime' => $now - 100,
'stoptime' => $now + 100,
'title' => 'Title',
'description' => 'Description',
]));
$this->set_parameters(['-id=' . $id]);
$cli = new finish();
$cli->set_referencetime($now);
$this->execute($cli);
}
public function test_activenotfound() {
$this->setAdminUser();
$this->set_parameters(['-a']);
$cli = new finish();
$this->setExpectedException(cliexception::class);
$this->execute($cli);
}
public function test_invalidid() {
$this->setAdminUser();
$this->set_parameters(['-id=theid']);
$cli = new finish();
$this->setExpectedException(cliexception::class);
$this->execute($cli);
}
public function test_idnotfound() {
$this->setAdminUser();
$this->set_parameters(['-id=99999']);
$cli = new finish();
$this->setExpectedException(cliexception::class);
$this->execute($cli);
}
}

View File

@@ -29,6 +29,7 @@ require_once('cli_testcase.php');
* @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
* @covers \auth_outage\cli\waitforit
* @SuppressWarnings("public")
*/
class waitforit_test extends cli_testcase {

115
tests/events_test.php Normal file
View File

@@ -0,0 +1,115 @@
<?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\models\outage;
use auth_outage\outagedb;
defined('MOODLE_INTERNAL') || die();
/**
* Tests performed on outage 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
* @covers \auth_outage\event\outage_created
* @covers \auth_outage\event\outage_updated
* @covers \auth_outage\event\outage_deleted
*/
class events_test extends advanced_testcase {
public function test_save() {
global $DB;
$this->setAdminUser();
$this->resetAfterTest(false);
// Save new outage.
$now = time();
$id = outagedb::save(new outage([
'warntime' => $now - 60,
'starttime' => 60,
'stoptime' => 120,
'title' => 'Title',
'description' => 'Description',
]));
// Check existance.
$event = $DB->get_record_select(
'event',
"(eventtype = 'auth_outage' AND instance = :outageid)",
['outageid' => $id],
'id',
IGNORE_MISSING
);
self::assertNotFalse($event);
// Another test will use it.
return [$id, $event->id];
}
/**
* @param array $ids
* @depends test_save
*/
public function test_update($ids) {
global $DB;
$this->setAdminUser();
$this->resetAfterTest(false);
list($idoutage, $idevent) = $ids;
$outage = outagedb::get_by_id($idoutage);
$outage->starttime += 10;
outagedb::save($outage);
// Should still exist.
$event = $DB->get_record_select(
'event',
"(eventtype = 'auth_outage' AND instance = :idoutage)",
['idoutage' => $idoutage],
'id',
IGNORE_MISSING
);
self::assertNotFalse($event);
self::assertSame($idevent, $event->id);
return $ids;
}
/**
* @param array $ids
* @depends test_update
*/
public function test_delete($ids) {
global $DB;
$this->setAdminUser();
$this->resetAfterTest(true);
list($idoutage, $idevent) = $ids;
outagedb::delete($idoutage);
// Should not exist.
$event = $DB->get_record_select(
'event',
"(eventtype = 'auth_outage' AND instance = :idoutage) OR (id = :idevent)",
['idoutage' => $idoutage, 'idevent' => $idevent],
'id',
IGNORE_MISSING
);
self::assertFalse($event);
}
}

View File

@@ -25,6 +25,7 @@ defined('MOODLE_INTERNAL') || die();
* @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
* @covers \auth_outage\models\outage
*/
class outage_test extends basic_testcase {
public function test_constructor() {