Fixed insepections, refactored to reduce complexity (phpmd warning).

This commit is contained in:
Daniel Thee Roperto
2016-09-22 16:47:07 +10:00
parent b302d9dfa5
commit 5df01b342e
24 changed files with 366 additions and 181 deletions

View File

@@ -32,14 +32,20 @@ require_once(__DIR__.'/cli_testcase.php');
* @SuppressWarnings("public")
*/
class cli_test extends cli_testcase {
/**
* @expectedException auth_outage\local\cli\cli_exception
* @expectedExceptionCode 2
*/
public function test_invalidargumentparam() {
$this->set_parameters(['--aninvalidparameter']);
$this->setExpectedException(cli_exception::class);
new create();
}
/**
* @expectedException auth_outage\local\cli\cli_exception
* @expectedExceptionCode 2
*/
public function test_invalidargumentgiven() {
$this->setExpectedException(cli_exception::class);
new create(['anotherinvalidparameter']);
}
@@ -49,9 +55,12 @@ class cli_test extends cli_testcase {
$cli->set_referencetime(60 * 60 * 24 * 7);
}
/**
* @expectedException coding_exception
*/
public function test_setreferencetime_invalid() {
$cli = new create(['start' => 0]);
$this->setExpectedException(coding_exception::class);
$this->set_parameters(['--start=60']);
$cli = new create();
$cli->set_referencetime(-1);
}
@@ -63,8 +72,11 @@ class cli_test extends cli_testcase {
self::assertContains('--help', $output);
}
/**
* @expectedException auth_outage\local\cli\cli_exception
* @expectedExceptionCode 1
*/
public function test_exception() {
self::setExpectedException(cli_exception::class, '*ERROR* An CLI exception.', 5);
throw new cli_exception('An CLI exception.', 5);
throw new cli_exception('An CLI exception.');
}
}

View File

@@ -14,10 +14,10 @@
// 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\dml\outagedb;
use auth_outage\local\cli\cli_exception;
use auth_outage\local\cli\create;
use auth_outage\local\outage;
use auth_outage\local\outagedb;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__.'/cli_testcase.php');
@@ -32,12 +32,19 @@ require_once(__DIR__.'/cli_testcase.php');
* @SuppressWarnings("public") Allow this test to have as many tests as necessary.
*/
class create_test extends cli_testcase {
/**
* @expectedException auth_outage\local\cli\cli_exception
* @expectedExceptionCode 4
*/
public function test_noarguments() {
$cli = new create();
$this->setExpectedException(cli_exception::class);
$this->execute($cli);
}
/**
* @expectedException auth_outage\local\cli\cli_exception
* @expectedExceptionCode 3
*/
public function test_invalidparam_notanumber() {
$cli = new create(['start' => 'some day']);
$cli->set_defaults([
@@ -47,10 +54,13 @@ class create_test extends cli_testcase {
'title' => 'Default Title',
'description' => 'Default Description',
]);
$this->setExpectedException(cli_exception::class);
$this->execute($cli);
}
/**
* @expectedException auth_outage\local\cli\cli_exception
* @expectedExceptionCode 3
*/
public function test_invalidparam_negative() {
$cli = new create(['start' => -1]);
$cli->set_defaults([
@@ -60,10 +70,13 @@ class create_test extends cli_testcase {
'title' => 'Default Title',
'description' => 'Default Description',
]);
$this->setExpectedException(cli_exception::class);
$this->execute($cli);
}
/**
* @expectedException auth_outage\local\cli\cli_exception
* @expectedExceptionCode 3
*/
public function test_invalidparam_emptystring() {
$cli = new create(['start' => 0, 'title' => '']);
$cli->set_defaults([
@@ -73,10 +86,13 @@ class create_test extends cli_testcase {
'title' => 'Default Title',
'description' => 'Default Description',
]);
$this->setExpectedException(cli_exception::class);
$this->execute($cli);
}
/**
* @expectedException auth_outage\local\cli\cli_exception
* @expectedExceptionCode 3
*/
public function test_invalidparam_notastring() {
$cli = new create(['start' => 0, 'title' => true]);
$cli->set_defaults([
@@ -86,7 +102,6 @@ class create_test extends cli_testcase {
'title' => 'Default Title',
'description' => 'Default Description',
]);
$this->setExpectedException(cli_exception::class);
$this->execute($cli);
}
@@ -227,8 +242,11 @@ class create_test extends cli_testcase {
self::assertSame($original->description, $cloned->description);
}
/**
* @expectedException auth_outage\local\cli\cli_exception
* @expectedExceptionCode 3
*/
public function test_create_withclone_invalid() {
$this->setExpectedException(cli_exception::class);
$this->set_parameters([
'--start=60',
'--clone=-1',

View File

@@ -14,10 +14,10 @@
// 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\dml\outagedb;
use auth_outage\local\cli\cli_exception;
use auth_outage\local\cli\finish;
use auth_outage\local\outage;
use auth_outage\local\outagedb;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__.'/cli_testcase.php');
@@ -59,12 +59,19 @@ class finish_test extends cli_testcase {
self::assertContains('--help', $text);
}
/**
* @expectedException auth_outage\local\cli\cli_exception
* @expectedExceptionCode 4
*/
public function test_noarguments() {
$cli = new finish();
$this->setExpectedException(cli_exception::class);
$this->execute($cli);
}
/**
* @expectedException auth_outage\local\cli\cli_exception
* @expectedExceptionCode 5
*/
public function test_endedoutage() {
self::setAdminUser();
$now = time();
@@ -79,7 +86,6 @@ class finish_test extends cli_testcase {
$this->set_parameters(['-id='.$id]);
$cli = new finish();
$cli->set_referencetime($now);
$this->setExpectedException(cli_exception::class);
$this->execute($cli);
}
@@ -100,27 +106,36 @@ class finish_test extends cli_testcase {
$this->execute($cli);
}
/**
* @expectedException auth_outage\local\cli\cli_exception
* @expectedExceptionCode 6
*/
public function test_activenotfound() {
self::setAdminUser();
$this->set_parameters(['-a']);
$cli = new finish();
$this->setExpectedException(cli_exception::class);
$this->execute($cli);
}
/**
* @expectedException auth_outage\local\cli\cli_exception
* @expectedExceptionCode 3
*/
public function test_invalidid() {
self::setAdminUser();
$this->set_parameters(['-id=theid']);
$cli = new finish();
$this->setExpectedException(cli_exception::class);
$this->execute($cli);
}
/**
* @expectedException auth_outage\local\cli\cli_exception
* @expectedExceptionCode 6
*/
public function test_idnotfound() {
self::setAdminUser();
$this->set_parameters(['-id=99999']);
$cli = new finish();
$this->setExpectedException(cli_exception::class);
$this->execute($cli);
}
}

View File

@@ -14,10 +14,9 @@
// 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\local\cli\cli_exception;
use auth_outage\dml\outagedb;
use auth_outage\local\cli\waitforit;
use auth_outage\local\outage;
use auth_outage\local\outagedb;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__.'/cli_testcase.php');
@@ -63,27 +62,40 @@ class waitforit_test extends cli_testcase {
self::assertContains('--help', $text);
}
/**
* @expectedException auth_outage\local\cli\cli_exception
* @expectedExceptionCode 3
*/
public function test_bothparams() {
$this->set_parameters(['--outageid=1', '--active']);
$cli = new waitforit();
$this->setExpectedException(cli_exception::class);
$cli->execute();
}
/**
* @expectedException auth_outage\local\cli\cli_exception
* @expectedExceptionCode 3
*/
public function test_invalidoutageid() {
$this->set_parameters(['-id=-1']);
$cli = new waitforit();
$this->setExpectedException(cli_exception::class);
$this->execute($cli);
}
/**
* @expectedException auth_outage\local\cli\cli_exception
* @expectedExceptionCode 6
*/
public function test_outagenotfound() {
$this->set_parameters(['-a']);
$cli = new waitforit();
$this->setExpectedException(cli_exception::class);
$this->execute($cli);
}
/**
* @expectedException auth_outage\local\cli\cli_exception
* @expectedExceptionCode 5
*/
public function test_endedoutage() {
self::setAdminUser();
$now = time();
@@ -98,7 +110,6 @@ class waitforit_test extends cli_testcase {
$this->set_parameters(['-id='.$id]);
$cli = new waitforit();
$cli->set_referencetime($now);
$this->setExpectedException(cli_exception::class);
$this->execute($cli);
}
@@ -148,6 +159,10 @@ class waitforit_test extends cli_testcase {
self::assertContains("started!", $output);
}
/**
* @expectedException auth_outage\local\cli\cli_exception
* @expectedExceptionCode 7
*/
public function test_outagechanged() {
self::setAdminUser();
$now = time();
@@ -170,7 +185,6 @@ class waitforit_test extends cli_testcase {
// Pretend it is time to start, but it should get an error instead.
return $outage->starttime;
});
$this->setExpectedException(cli_exception::class);
$this->execute($cli);
}
}

View File

@@ -14,8 +14,8 @@
// 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\dml\outagedb;
use auth_outage\local\outage;
use auth_outage\local\outagedb;
defined('MOODLE_INTERNAL') || die();

View File

@@ -14,8 +14,8 @@
// 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\dml\outagedb;
use auth_outage\local\outage;
use auth_outage\local\outagedb;
defined('MOODLE_INTERNAL') || die();