diff --git a/classes/outage.php b/classes/outage.php index 1520632..d167148 100644 --- a/classes/outage.php +++ b/classes/outage.php @@ -74,7 +74,7 @@ class outage /** * outage constructor. - * @param mixed $data An object, an array or null. + * @param object|array|null The data for the outage. */ public function __construct($data = null) { if (is_null($data)) { diff --git a/tests/outage_test.php b/tests/outage_test.php new file mode 100644 index 0000000..d955874 --- /dev/null +++ b/tests/outage_test.php @@ -0,0 +1,42 @@ +. + +/** + * Tests performed on outage class. + * + * @package auth_outage + * @author Daniel Thee Roperto + * @copyright Catalyst IT + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +use \auth_outage\outage; + +defined('MOODLE_INTERNAL') || die(); + + +class outage_test extends basic_testcase +{ + public function test_constructor() { + $outage = new outage(); + // Very important, this should never change. + self::assertNull($outage->id, 'New empty outage can never have an id set.'); + // Ensure all other fields are also null. + foreach ($outage as $k=>$v) { + self::assertNull($v); + } + } +} diff --git a/tests/outageutils_test.php b/tests/outageutils_test.php new file mode 100644 index 0000000..f56d117 --- /dev/null +++ b/tests/outageutils_test.php @@ -0,0 +1,74 @@ +. + +/** + * Tests performed on outageutils class. + * + * @package auth_outage + * @author Daniel Thee Roperto + * @copyright Catalyst IT + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +use \auth_outage\outageutils; + +defined('MOODLE_INTERNAL') || die(); + + +class outageutils_test extends basic_testcase +{ + public function test_data2object() { + // Using object data, no new fields, not strict. + $obj = new stdClass(); + $obj->foo = 'bar'; + $obj->number = 42; + $data = new stdClass(); + $data->foo = 'not bar'; + outageutils::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.'); + + // Using array data, with new fields, not strict. + $obj = new stdClass(); + $obj->foo = 'bar'; + $obj->number = 42; + $data = ['foo' => 'foobar', 'flag' => false]; + outageutils::data2object($data, $obj, false); + self::assertEquals(get_object_vars($obj), ['foo' => 'foobar', 'number' => 42], 'Invalid result.'); + + // Using object data, no new fields, strict. + $obj = new stdClass(); + $obj->foo = 'bar'; + $obj->number = 42; + $data = new stdClass(); + $data->foo = 'not bar'; + outageutils::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.'); + + // Using array data, with new fields, strict. + $obj = new stdClass(); + $obj->foo = 'bar'; + $obj->number = 42; + $data = ['foo' => 'foobar', 'flag' => false]; + try { + outageutils::data2object($data, $obj, true); + $this->fail('Exception was expected.'); + } + catch (InvalidArgumentException $e){ + } + } +}