Namespace changes, outagedb changed to used static methods instead on singleton, utils renamed to outagelib (better name when using the 'use' directive). Issue #1

This commit is contained in:
Daniel Thee Roperto
2016-09-02 11:55:17 +10:00
parent 5f4ea51fcc
commit 2cf9afb5c5
13 changed files with 83 additions and 119 deletions

View File

@@ -23,30 +23,29 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use \auth_outage\outage;
use \auth_outage\outageutils;
use \auth_outage\outagedb;
use \auth_outage\outageform;
use auth_outage\models\outage;
use auth_outage\outagedb;
use auth_outage\outagelib;
require_once('../../config.php');
require_once($CFG->libdir . '/adminlib.php');
require_once($CFG->libdir . '/formslib.php');
outageutils::pagesetup();
outagelib::pagesetup();
$mform = new outageform();
$mform = new \auth_outage\forms\outage\edit();
if ($mform->is_cancelled()) {
redirect('/auth/outage/list.php');
} else if ($fromform = $mform->get_data()) {
$fromform = outageutils::parseformdata($fromform);
$fromform = outagelib::parseformdata($fromform);
$outage = new outage($fromform);
$id = outagedb::get()->save($outage);
$id = outagedb::save($outage);
redirect('/auth/outage/list.php#auth_outage_id=' . $id);
}
$id = required_param('id', PARAM_INT);
$outage = outagedb::get()->getbyid($id);
$outage = outagedb::getbyid($id);
if ($outage == null) {
throw new invalid_parameter_exception('Outage #' . $id . ' not found.');
}

View File

@@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace auth_outage;
namespace auth_outage\forms\outage;
if (!defined('MOODLE_INTERNAL')) {
die('Direct access to this script is forbidden.'); // It must be included from a Moodle page.
@@ -30,7 +30,7 @@ require_once($CFG->libdir . '/formslib.php');
* @copyright Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class outagedeleteform extends \moodleform {
class delete extends \moodleform {
/**
* {@inheritDoc}
* @see moodleform::definition()

View File

@@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace auth_outage;
namespace auth_outage\forms\outage;
if (!defined('MOODLE_INTERNAL')) {
die('Direct access to this script is forbidden.'); // It must be included from a Moodle page.
@@ -30,7 +30,7 @@ require_once($CFG->libdir . '/formslib.php');
* @copyright Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class outageform extends \moodleform {
class edit extends \moodleform {
/**
* {@inheritDoc}
* @see moodleform::definition()

View File

@@ -23,7 +23,9 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace auth_outage;
namespace auth_outage\models;
use auth_outage\outagelib;
class outage
{
@@ -82,7 +84,7 @@ class outage
}
if (is_object($data) || is_array($data)) {
outageutils::data2object($data, $this);
outagelib::data2object($data, $this);
return;
}

View File

@@ -25,43 +25,12 @@
namespace auth_outage;
use auth_outage\models\outage;
final class outagedb
{
/**
* @var outagedb Singleton reference created on first use.
*/
private static $singleton = null;
/**
* Returns the singleton instance.
*
* @return outagedb The singleton object.
*/
public static function get() {
if (is_null(self::$singleton)) {
self::$singleton = new outagedb();
}
return self::$singleton;
}
/**
* Private clone method to prevent cloning singleton.
*
* @return void
*/
private function __clone() {
}
/**
* Private unserialize method to prevent unserializing singleton.
*
* @return void
*/
private function __wakeup() {
}
/**
* Private constructor (singleton), use outagedb::get() instead.
* Private constructor, use static methods instead.
*/
private function __construct() {
}
@@ -69,7 +38,7 @@ final class outagedb
/**
* Gets all outage entries.
*/
public function getall() {
public static function getall() {
global $DB;
$outages = [];
@@ -83,7 +52,11 @@ final class outagedb
return $outages;
}
public function getbyid($id) {
/**
* @param $id int Outage id to get.
* @return outage|null Returns the outage or null if not found.
*/
public static function getbyid($id) {
global $DB;
if (!is_int($id)) {
@@ -107,7 +80,7 @@ final class outagedb
* @param outage $outage Outage to save.
* @return int Outage ID.
*/
public function save(outage $outage) {
public static function save(outage $outage) {
global $DB, $USER;
// Do not change the original object.
@@ -139,7 +112,7 @@ final class outagedb
* @param $id outage Outage ID to delete
* @throws InvalidArgumentException If ID is not valid.
*/
public function delete($id) {
public static function delete($id) {
global $DB;
if (!is_int($id)) {

View File

@@ -28,7 +28,7 @@ if (!defined('MOODLE_INTERNAL')) {
* @copyright Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class outageutils
class outagelib
{
/**
* Initializes admin pages for outage.
@@ -43,7 +43,7 @@ class outageutils
}
/**
* Loads data from an object or array into another object.
* Loads data from an object or array into another object. It ensures no new fields are created in the $obj.
*
* @param $data mixed An object or array.
* @param $obj object Destination object to write the properties.
@@ -77,7 +77,13 @@ class outageutils
}
}
public static function parseformdata($data) {
/**
* Parses data from the form ensuring it is valid for an outage object.
*
* @param $data stdClass The input data.
* @return stdClass The parsed data.
*/
public static function parseformdata(\stdClass $data) {
if ($data->description['format'] != '1') {
throw new \InvalidArgumentException('Not implemented for format ' . $data->description['format']);
}

View File

@@ -23,24 +23,23 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use \auth_outage\outage;
use \auth_outage\outageutils;
use \auth_outage\outagedb;
use \auth_outage\outageform;
use auth_outage\models\outage;
use auth_outage\outagedb;
use auth_outage\outagelib;
require_once('../../config.php');
require_once($CFG->libdir . '/adminlib.php');
require_once($CFG->libdir . '/formslib.php');
outageutils::pagesetup();
outagelib::pagesetup();
$mform = new outageform();
$mform = new \auth_outage\forms\outage\edit();
if ($mform->is_cancelled()) {
redirect('/auth/outage/list.php');
} else if ($fromform = $mform->get_data()) {
$fromform = outageutils::parseformdata($fromform);
$fromform = outagelib::parseformdata($fromform);
$outage = new outage($fromform);
$id = outagedb::get()->save($outage);
$id = outagedb::save($outage);
redirect('/auth/outage/list.php#auth_outage_id=' . $id);
}

View File

@@ -23,18 +23,16 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use \auth_outage\outage;
use \auth_outage\outageutils;
use \auth_outage\outagedb;
use auth_outage\outagedb;
use auth_outage\outagelib;
require_once('../../config.php');
require_once($CFG->libdir . '/adminlib.php');
$renderer = outageutils::pagesetup();
$renderer = outagelib::pagesetup();
echo $OUTPUT->header();
// TODO Add paging or limiting past entries displayed.
echo $renderer->renderoutagelist(outagedb::get()->getall());
echo $renderer->renderoutagelist(outagedb::getall());
echo $OUTPUT->footer();

View File

@@ -23,27 +23,26 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use \auth_outage\outage;
use \auth_outage\outageutils;
use \auth_outage\outagedb;
use \auth_outage\outagedeleteform;
use auth_outage\models\outage;
use auth_outage\outagedb;
use auth_outage\outagelib;
require_once('../../config.php');
require_once($CFG->libdir . '/adminlib.php');
require_once($CFG->libdir . '/formslib.php');
$renderer = outageutils::pagesetup();
$renderer = outagelib::pagesetup();
$mform = new outagedeleteform();
$mform = new \auth_outage\forms\outage\delete();
if ($mform->is_cancelled()) {
redirect('/auth/outage/list.php');
} else if ($fromform = $mform->get_data()) {
outagedb::get()->delete($fromform->id);
outagedb::delete($fromform->id);
redirect('/auth/outage/list.php');
}
$id = required_param('id', PARAM_INT);
$outage = outagedb::get()->getbyid($id);
$outage = outagedb::getbyid($id);
if ($outage == null) {
throw new invalid_parameter_exception('Outage #' . $id . ' not found.');
}

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\outage;
use \auth_outage\outageform;
use auth_outage\models\outage;
use auth_outage\models\outageform;
if (!defined('MOODLE_INTERNAL')) {
die('Direct access to this script is forbidden.'); // It must be included from a Moodle page.

View File

@@ -23,7 +23,7 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use \auth_outage\outage;
use auth_outage\models\outage;
defined('MOODLE_INTERNAL') || die();

View File

@@ -23,33 +23,25 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use \auth_outage\outage;
use \auth_outage\outagedb;
use auth_outage\models\outage;
use auth_outage\outagedb;
defined('MOODLE_INTERNAL') || die();
class outagedb_test extends advanced_testcase
{
/**
* Make sure the db context is a singleton.
*/
public function test_singleton() {
self::assertSame(outagedb::get(), outagedb::get(), 'Must always get same instance.');
}
/**
* Make sure we can save and update.
*/
public function test_save() {
$this->resetAfterTest(true);
$db = outagedb::get();
// Save new outage.
$id = $db->save($this->createoutage(1));
$id = outagedb::save($this->createoutage(1));
// Update it.
$outage = $this->createoutage(2);
$outage->id = $id;
$db->save($outage);
outagedb::save($outage);
}
/**
@@ -57,16 +49,15 @@ class outagedb_test extends advanced_testcase
*/
public function test_getbyid() {
$this->resetAfterTest(true);
$db = outagedb::get();
// Create something.
$id = $db->save($this->createoutage(1));
$id = outagedb::save($this->createoutage(1));
// Get should work.
$outage = $db->getbyid($id);
$outage = outagedb::getbyid($id);
self::assertNotNull($outage);
// Delete it.
$db->delete($id);
outagedb::delete($id);
// Get should be null.
$outage = $db->getbyid($id);
$outage = outagedb::getbyid($id);
self::assertNull($outage);
}
@@ -75,13 +66,12 @@ class outagedb_test extends advanced_testcase
*/
public function test_delete() {
$this->resetAfterTest(true);
$db = outagedb::get();
// Create something.
$id = $db->save($this->createoutage(1));
$id = outagedb::save($this->createoutage(1));
// Delete it.
$db->delete($id);
outagedb::delete($id);
// Should not exist anymore.
self::assertNull($db->getbyid($id));
self::assertNull(outagedb::getbyid($id));
}
/**
@@ -89,17 +79,16 @@ class outagedb_test extends advanced_testcase
*/
public function test_getall() {
$this->resetAfterTest(true);
$db = outagedb::get();
$amount = 10;
// Should start empty.
$outages = $db->getall();
$outages = outagedb::getall();
self::assertSame([], $outages);
// Create some stuff outages.
for ($i = 0; $i < $amount; $i++) {
$db->save($this->createoutage($i));
outagedb::save($this->createoutage($i));
}
// Count entries created.
self::assertSame($amount, count($db->getall()));
self::assertSame($amount, count(outagedb::getall()));
}
/**
@@ -108,20 +97,19 @@ class outagedb_test extends advanced_testcase
public function test_basiccrud() {
return;
$this->resetAfterTest(true);
$db = outagedb::get();
// Create some outages.
$outages = [];
for ($i = 1; $i <= 10; $i++) {
$outage = $this->createoutage($i);
$id = $db->save($outage);
$id = outagedb::save($outage);
$outages[$id] = $outage;
}
// With all created outages.
foreach ($outages as $id => $outage) {
// Get it.
$inserted = $db->getbyid($id);
$inserted = outagedb::getbyid($id);
self::assertNotNull($inserted);
// Check its data.
foreach (['starttime', 'stoptime', 'warningduration', 'title', 'description'] as $field) {
@@ -134,14 +122,14 @@ class outagedb_test extends advanced_testcase
self::assertNotNull($inserted->modifiedby);
// Change it.
$inserted->title = 'Title ID' . $id;
$db->save($inserted);
outagedb::save($inserted);
// Get it again and check data.
$updated = $db->getbyid($id);
$updated = outagedb::getbyid($id);
self::assertSame('Title ID' . $id, $updated->title);
self::assertSame($inserted->description, $updated->description);
// Delete it.
$db->delete($id);
$deleted = $db->getbyid($id);
outagedb::delete($id);
$deleted = outagedb::getbyid($id);
self::assertNull($deleted);
}
}

View File

@@ -23,12 +23,12 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use \auth_outage\outageutils;
use auth_outage\outagelib;
defined('MOODLE_INTERNAL') || die();
class outageutils_test extends basic_testcase
class outagelib_test extends basic_testcase
{
public function test_data2object() {
// Using object data, no new fields, not strict.
@@ -37,7 +37,7 @@ class outageutils_test extends basic_testcase
$obj->number = 42;
$data = new stdClass();
$data->foo = 'not bar';
outageutils::data2object($data, $obj, false);
outagelib::data2object($data, $obj, false);
self::assertEquals(get_object_vars($obj), ['foo' => 'not bar', 'number' => 42], 'Invalid result.');
self::assertEquals(get_object_vars($data), ['foo' => 'not bar'], 'Data should not change.');
@@ -46,7 +46,7 @@ class outageutils_test extends basic_testcase
$obj->foo = 'bar';
$obj->number = 42;
$data = ['foo' => 'foobar', 'flag' => false];
outageutils::data2object($data, $obj, false);
outagelib::data2object($data, $obj, false);
self::assertEquals(get_object_vars($obj), ['foo' => 'foobar', 'number' => 42], 'Invalid result.');
// Using object data, no new fields, strict.
@@ -55,7 +55,7 @@ class outageutils_test extends basic_testcase
$obj->number = 42;
$data = new stdClass();
$data->foo = 'not bar';
outageutils::data2object($data, $obj, true);
outagelib::data2object($data, $obj, true);
self::assertEquals(get_object_vars($obj), ['foo' => 'not bar', 'number' => 42], 'Invalid result.');
self::assertEquals(get_object_vars($data), ['foo' => 'not bar'], 'Data should not change.');
@@ -65,7 +65,7 @@ class outageutils_test extends basic_testcase
$obj->number = 42;
$data = ['foo' => 'foobar', 'flag' => false];
try {
outageutils::data2object($data, $obj, true);
outagelib::data2object($data, $obj, true);
$this->fail('Exception was expected.');
}
catch (InvalidArgumentException $e){