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

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() {